Skip to content

Commit 92f0485

Browse files
Copilothotlong
andcommitted
Refactor runtime and examples to use ObjectKernel (MiniKernel)
Co-authored-by: hotlong <50353452+hotlong@users.noreply.github.com>
1 parent a49c488 commit 92f0485

8 files changed

Lines changed: 214 additions & 66 deletions

File tree

examples/custom-objectql-example.ts

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* custom configuration.
77
*/
88

9-
import { ObjectStackKernel, ObjectQLPlugin, ObjectQL } from '@objectstack/runtime';
9+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, ObjectQL } from '@objectstack/runtime';
1010
import { InMemoryDriver } from '@objectstack/driver-memory';
1111

1212
(async () => {
@@ -27,18 +27,22 @@ import { InMemoryDriver } from '@objectstack/driver-memory';
2727
});
2828

2929
// Create kernel with the custom ObjectQL instance
30-
const kernel = new ObjectStackKernel([
30+
const kernel = new ObjectKernel();
31+
32+
kernel
3133
// Register your custom ObjectQL instance
32-
new ObjectQLPlugin(customQL),
34+
.use(new ObjectQLPlugin(customQL))
3335

3436
// Add your driver
35-
new InMemoryDriver(),
37+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'));
3638

3739
// Add other plugins and app configs as needed
38-
]);
3940

40-
await kernel.start();
41+
await kernel.bootstrap();
4142

4243
console.log('✅ Kernel started with custom ObjectQL instance');
43-
console.log('ObjectQL instance:', kernel.ql);
44+
45+
// Access ObjectQL via service registry
46+
const objectql = kernel.getService('objectql');
47+
console.log('ObjectQL instance:', objectql);
4448
})();

examples/host/debug-registry.ts

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime';
2+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin } from '@objectstack/runtime';
33
import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
44
import { InMemoryDriver } from '@objectstack/driver-memory';
55

@@ -10,13 +10,14 @@ import TodoApp from '@objectstack/example-todo/objectstack.config';
1010
console.log('Apps:', [TodoApp.name]);
1111
console.log('Objects inside App:', TodoApp.objects?.map((o: any) => o.name));
1212

13-
const kernel = new ObjectStackKernel([
14-
new ObjectQLPlugin(),
15-
TodoApp,
16-
new InMemoryDriver()
17-
]);
13+
const kernel = new ObjectKernel();
1814

19-
await kernel.start();
15+
kernel
16+
.use(new ObjectQLPlugin())
17+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'))
18+
.use(new AppManifestPlugin(TodoApp));
19+
20+
await kernel.bootstrap();
2021

2122
console.log('--- Post Start ---');
2223

examples/host/src/index.ts

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ObjectStackKernel, ObjectQLPlugin, ObjectQL } from '@objectstack/runtime';
1+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin, ObjectQL } from '@objectstack/runtime';
22
import { InMemoryDriver } from '@objectstack/driver-memory';
33
import { HonoServerPlugin } from '@objectstack/plugin-hono-server';
44

@@ -9,32 +9,26 @@ import BiPluginManifest from '@objectstack/plugin-bi/objectstack.config';
99
(async () => {
1010
console.log('🚀 Booting Kernel...');
1111

12-
// Option 1: Use default ObjectQL via plugin (recommended)
13-
const kernel = new ObjectStackKernel([
14-
// Register ObjectQL engine explicitly via plugin
15-
new ObjectQLPlugin(),
16-
17-
// App manifests
18-
CrmApp,
19-
TodoApp,
20-
BiPluginManifest,
12+
// Use MiniKernel architecture
13+
const kernel = new ObjectKernel();
14+
15+
kernel
16+
// Register ObjectQL engine
17+
.use(new ObjectQLPlugin())
2118

2219
// Database driver
23-
new InMemoryDriver(),
20+
.use(new DriverPlugin(new InMemoryDriver(), 'memory'))
21+
22+
// App manifests
23+
.use(new AppManifestPlugin(CrmApp))
24+
.use(new AppManifestPlugin(TodoApp))
25+
.use(new AppManifestPlugin(BiPluginManifest))
2426

2527
// Load the Hono Server Plugin
26-
new HonoServerPlugin({
28+
.use(new HonoServerPlugin({
2729
port: 3004,
2830
staticRoot: './public'
29-
})
30-
]);
31-
32-
// Option 2: Use custom ObjectQL instance
33-
// const customQL = new ObjectQL({ env: 'production', customConfig: true });
34-
// const kernel = new ObjectStackKernel([
35-
// new ObjectQLPlugin(customQL),
36-
// ...other plugins
37-
// ]);
31+
}));
3832

39-
await kernel.start();
33+
await kernel.bootstrap();
4034
})();

examples/msw-react-crud/src/mocks/browser.ts

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,13 @@
55
* and the MSW Plugin which automatically exposes the API.
66
*/
77

8-
import { ObjectStackKernel, ObjectQLPlugin } from '@objectstack/runtime';
8+
import { ObjectKernel, ObjectQLPlugin, DriverPlugin, AppManifestPlugin } from '@objectstack/runtime';
99
import { InMemoryDriver } from '@objectstack/driver-memory';
1010
import { MSWPlugin } from '@objectstack/plugin-msw';
1111
// import appConfig from '../../objectstack.config';
1212
import todoConfig from '@objectstack/example-todo/objectstack.config';
1313

14-
let kernel: ObjectStackKernel | null = null;
14+
let kernel: ObjectKernel | null = null;
1515

1616
export async function startMockServer() {
1717
if (kernel) return;
@@ -20,28 +20,27 @@ export async function startMockServer() {
2020

2121
const driver = new InMemoryDriver();
2222

23-
// Define Seed Data using the Dataset Protocol
24-
// We use the data defined in the Todo App config
23+
// Create kernel with MiniKernel architecture
24+
kernel = new ObjectKernel();
2525

26-
kernel = new ObjectStackKernel([
27-
// Register ObjectQL engine explicitly
28-
new ObjectQLPlugin(),
26+
kernel
27+
// Register ObjectQL engine
28+
.use(new ObjectQLPlugin())
2929

30-
// Todo App Config (contains objects and data)
31-
todoConfig,
30+
// Register the driver
31+
.use(new DriverPlugin(driver, 'memory'))
3232

33-
// In-Memory Database (runs in browser)
34-
driver,
33+
// Load todo app config as a plugin
34+
.use(new AppManifestPlugin(todoConfig))
3535

3636
// MSW Plugin (intercepts network requests)
37-
new MSWPlugin({
37+
.use(new MSWPlugin({
3838
enableBrowser: true,
3939
baseUrl: '/api/v1',
4040
logRequests: true
41-
})
42-
]);
43-
44-
await kernel.start();
41+
}));
42+
43+
await kernel.bootstrap();
4544

4645
return kernel;
4746
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
import { Plugin, PluginContext } from './types.js';
2+
import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
3+
4+
/**
5+
* AppManifestPlugin
6+
*
7+
* Wraps an ObjectStack app manifest (objectstack.config.ts export)
8+
* as a Plugin so it can be loaded in the MiniKernel architecture.
9+
*
10+
* Handles:
11+
* - Registering the app/plugin in SchemaRegistry
12+
* - Registering all objects defined in the manifest
13+
* - Seeding data if provided
14+
*
15+
* Dependencies: ['com.objectstack.engine.objectql']
16+
*/
17+
export class AppManifestPlugin implements Plugin {
18+
name: string;
19+
version?: string;
20+
dependencies = ['com.objectstack.engine.objectql'];
21+
22+
private manifest: any;
23+
24+
constructor(manifest: any) {
25+
this.manifest = manifest;
26+
this.name = manifest.id || manifest.name || 'unnamed-app';
27+
this.version = manifest.version;
28+
}
29+
30+
async init(ctx: PluginContext) {
31+
ctx.logger.log(`[AppManifestPlugin] Loading App Manifest: ${this.manifest.id || this.manifest.name}`);
32+
33+
// Register the app/plugin in the schema registry
34+
SchemaRegistry.registerPlugin(this.manifest);
35+
36+
// Register all objects defined in the manifest
37+
if (this.manifest.objects) {
38+
for (const obj of this.manifest.objects) {
39+
SchemaRegistry.registerObject(obj);
40+
ctx.logger.log(`[AppManifestPlugin] Registered Object: ${obj.name}`);
41+
}
42+
}
43+
}
44+
45+
async start(ctx: PluginContext) {
46+
// Seed data if provided
47+
if (this.manifest.data && Array.isArray(this.manifest.data)) {
48+
ctx.logger.log(`[AppManifestPlugin] Seeding data for ${this.manifest.name || this.manifest.id}...`);
49+
50+
const objectql = ctx.getService<ObjectQL>('objectql');
51+
52+
for (const seed of this.manifest.data) {
53+
try {
54+
// Check if data already exists
55+
const existing = await objectql.find(seed.object, { top: 1 });
56+
if (existing.length === 0) {
57+
ctx.logger.log(`[AppManifestPlugin] Inserting ${seed.records.length} records into ${seed.object}`);
58+
for (const record of seed.records) {
59+
await objectql.insert(seed.object, record);
60+
}
61+
}
62+
} catch (e) {
63+
ctx.logger.warn(`[AppManifestPlugin] Failed to seed ${seed.object}`, e);
64+
}
65+
}
66+
}
67+
}
68+
}

packages/runtime/src/index.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
export { ObjectQL, SchemaRegistry } from '@objectstack/objectql';
33

44
// Export Kernels
5-
export { ObjectStackKernel } from './kernel.js';
65
export { ObjectKernel } from './mini-kernel.js';
6+
// @deprecated Use ObjectKernel instead
7+
export { ObjectStackKernel } from './kernel.js';
78

89
// Export Plugins
910
export { ObjectQLPlugin } from './objectql-plugin.js';
1011
export { DriverPlugin } from './driver-plugin.js';
12+
export { AppManifestPlugin } from './app-manifest-plugin.js';
1113

1214
// Export Protocol
1315
export { ObjectStackRuntimeProtocol } from './protocol.js';

0 commit comments

Comments
 (0)