Skip to content

Commit a83b756

Browse files
committed
重构 ObjectQL 引擎以支持插件系统,添加插件加载和注册逻辑,更新 SchemaRegistry,移除不再使用的注册器
1 parent a0fe90e commit a83b756

File tree

6 files changed

+101
-107
lines changed

6 files changed

+101
-107
lines changed

examples/objectql/src/engine.ts

Lines changed: 60 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,74 @@
1-
import { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec';
1+
import { DriverInterface, DriverOptions, QueryAST, ObjectStackManifest, ManifestSchema } from '@objectstack/spec';
2+
import { SchemaRegistry } from './registry';
3+
4+
/**
5+
* Host Context provided to plugins
6+
*/
7+
export interface PluginContext {
8+
ql: ObjectQL;
9+
logger: Console;
10+
// Extensible map for host-specific globals (like HTTP Router, etc.)
11+
[key: string]: any;
12+
}
213

314
/**
415
* ObjectQL Engine
5-
*
6-
* The core orchestration layer that sits between the API/UI and the Data Driver.
7-
* It handles:
8-
* 1. Request Validation (using Schemas)
9-
* 2. Security Enforcement (ACLs, Sharing Rules)
10-
* 3. Workflow Triggers
11-
* 4. Driver Delegation
1216
*/
1317
export class ObjectQL {
1418
private drivers = new Map<string, DriverInterface>();
1519
private defaultDriver: string | null = null;
20+
private plugins = new Map<string, any>();
21+
22+
// Host provided context additions (e.g. Server router)
23+
private hostContext: Record<string, any> = {};
1624

17-
constructor() {
25+
constructor(hostContext: Record<string, any> = {}) {
26+
this.hostContext = hostContext;
1827
console.log(`[ObjectQL] Engine Instance Created`);
1928
}
2029

30+
/**
31+
* Load and Register a Plugin
32+
*/
33+
async use(manifestPart: any, runtimePart?: any) {
34+
// 1. Validate / Register Manifest
35+
if (manifestPart) {
36+
// In a real scenario, we might strictly parse this using Zod
37+
// For now, simple ID check
38+
const id = manifestPart.id;
39+
if (!id) {
40+
console.warn('[ObjectQL] Plugin manifest missing ID', manifestPart);
41+
return;
42+
}
43+
44+
console.log(`[ObjectQL] Loading Plugin: ${id}`);
45+
SchemaRegistry.registerPlugin(manifestPart as ObjectStackManifest);
46+
47+
// Register contributions
48+
if (manifestPart.contributes?.kinds) {
49+
for (const kind of manifestPart.contributes.kinds) {
50+
SchemaRegistry.registerKind(kind);
51+
}
52+
}
53+
}
54+
55+
// 2. Execute Runtime
56+
if (runtimePart) {
57+
const pluginDef = (runtimePart as any).default || runtimePart;
58+
if (pluginDef.onEnable) {
59+
const context: PluginContext = {
60+
ql: this,
61+
logger: console,
62+
// Expose the driver registry helper explicitly if needed
63+
drivers: this, // Since `registerDriver` is on `this`, we can alias it or expose `this`
64+
...this.hostContext
65+
};
66+
67+
await pluginDef.onEnable(context);
68+
}
69+
}
70+
}
71+
2172
/**
2273
* Register a new storage driver
2374
*/

examples/objectql/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// Export core engine
22
export { ObjectQL } from './engine';
3+
export { SchemaRegistry } from './registry';
34

45
// Re-export common types from spec for convenience
56
export type { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec';

examples/server/src/index.ts

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,11 @@ import { DataEngine } from './kernel/engine';
88

99
// 1. Initialize Kernel
1010
const app = new Hono();
11-
const dataEngine = new DataEngine();
11+
const dataEngine = new DataEngine(); // Engine loads plugins internally now
1212

1313
app.use('*', logger());
1414
app.use('*', cors());
1515

16-
// 2. Load Plugins (CRM, Todo)
17-
// Initialization moved to async startup below
18-
loadPlugins(dataEngine).then(() => {
19-
console.log('[Server] Kernel Ready');
20-
}).catch(console.error);
21-
2216
// 3. Define Unified Routes
2317

2418
/**

examples/server/src/kernel/engine.ts

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,18 @@
11
import { ServiceObject } from '@objectstack/spec';
2-
import { SchemaRegistry } from './registry';
3-
import { ObjectQL } from '@objectstack/objectql';
2+
import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
3+
4+
// Import Packages/Plugins
5+
// @ts-ignore
6+
import CrmApp from '@objectstack/example-crm/objectstack.config';
7+
// @ts-ignore
8+
import TodoApp from '@objectstack/example-todo/objectstack.config';
9+
// @ts-ignore
10+
import BiPluginManifest from '@objectstack/plugin-bi/objectstack.config';
11+
import BiPluginRuntime from '@objectstack/plugin-bi';
12+
// @ts-ignore
13+
import DriverMemoryManifest from '@objectstack/plugin-driver-memory/objectstack.config';
14+
import DriverMemoryRuntime from '@objectstack/plugin-driver-memory';
15+
416

517
/**
618
* Server Data Engine Wrapper
@@ -14,16 +26,34 @@ export class DataEngine {
1426
public ql: ObjectQL;
1527

1628
constructor() {
17-
this.ql = new ObjectQL();
29+
// 1. Initialize Engine with Host Context (Simulated OS services)
30+
this.ql = new ObjectQL({
31+
os: { registerService: () => console.log('OS Service Registered') },
32+
app: { router: { get: () => {} } },
33+
storage: { set: () => {} },
34+
services: { register: () => {} },
35+
i18n: {}
36+
});
37+
38+
// 2. Load Plugins (Declarative)
39+
this.loadPlugins();
40+
41+
// 3. Start Engine
42+
this.ql.init().then(() => {
43+
this.seed();
44+
}).catch(console.error);
45+
}
1846

19-
// Driver is now registered via Plugin System (see context.drivers.register) in loader.ts
20-
// We just wait for init
47+
async loadPlugins() {
48+
// Apps
49+
await this.ql.use(CrmApp);
50+
await this.ql.use(TodoApp);
2151

22-
// 2. Start Engine
23-
this.ql.init().catch(console.error);
52+
// Plugins (Manifest + Runtime)
53+
await this.ql.use(BiPluginManifest, BiPluginRuntime);
2454

25-
// Seed after a short delay (mocking async startup)
26-
setTimeout(() => this.seed(), 500);
55+
// Drivers
56+
await this.ql.use(DriverMemoryManifest, DriverMemoryRuntime);
2757
}
2858

2959
async seed() {

examples/server/src/loader.ts

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)