@objectstack/runtime 0.4.2 → 0.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/src/kernel.ts DELETED
@@ -1,203 +0,0 @@
1
- import { ServiceObject } from '@objectstack/spec/data';
2
- import { SchemaRegistry, ObjectQL } from '@objectstack/objectql';
3
-
4
- /**
5
- * ObjectStack Kernel (Microkernel)
6
- *
7
- * The central container orchestrating the application lifecycle,
8
- * plugins, and the core ObjectQL engine.
9
- */
10
- export class ObjectStackKernel {
11
- public ql?: ObjectQL; // Will be set by ObjectQLPlugin or fallback initialization
12
- private plugins: any[];
13
-
14
- constructor(plugins: any[] = []) {
15
- this.plugins = plugins;
16
-
17
- // Check if any plugin provides ObjectQL via type: 'objectql'
18
- // This aligns with the manifest schema that supports objectql as a package type
19
- const hasObjectQLPlugin = plugins.some(p =>
20
- p && typeof p === 'object' && p.type === 'objectql'
21
- );
22
-
23
- if (!hasObjectQLPlugin) {
24
- // Backward compatibility: Initialize ObjectQL directly if no plugin provides it
25
- console.warn('[Kernel] No ObjectQL plugin found, using default initialization. Consider using ObjectQLPlugin.');
26
- this.ql = new ObjectQL({
27
- env: process.env.NODE_ENV || 'development'
28
- });
29
- }
30
- }
31
-
32
- /**
33
- * Ensure ObjectQL engine is initialized
34
- * @throws Error if ObjectQL is not available
35
- */
36
- private ensureObjectQL(): ObjectQL {
37
- if (!this.ql) {
38
- throw new Error('[Kernel] ObjectQL engine not initialized. Ensure ObjectQLPlugin is registered or kernel is properly initialized.');
39
- }
40
- return this.ql;
41
- }
42
-
43
- async start() {
44
- console.log('[Kernel] Starting...');
45
-
46
- // 0. Register Provided Plugins
47
- for (const p of this.plugins) {
48
- // Check if it is a Runtime Plugin (System Capability)
49
- if ('onStart' in p || 'install' in p) {
50
- console.log(`[Kernel] Loading Runtime Plugin: ${p.name}`);
51
- if (p.install) await p.install({ engine: this });
52
- continue;
53
- }
54
-
55
- // Otherwise treat as App Manifest
56
- console.log(`[Kernel] Loading App Manifest: ${p.id || p.name}`);
57
- SchemaRegistry.registerPlugin(p);
58
-
59
- // Register Objects from App/Plugin
60
- if (p.objects) {
61
- for (const obj of p.objects) {
62
- SchemaRegistry.registerObject(obj);
63
- console.log(`[Kernel] Registered Object: ${obj.name}`);
64
- }
65
- }
66
- }
67
-
68
- // 1. Load Drivers (Default to Memory if none provided in plugins)
69
- // TODO: Detect driver from plugins. For now, we still hard load memory driver if needed?
70
- // In strict mode, user should pass driver in plugins array (DriverManifest).
71
- // check if driver is registered
72
-
73
- // For Backwards Compat / Easy Dev, try dynamic import of memory driver if installed
74
- try {
75
- // @ts-ignore
76
- const { InMemoryDriver } = await import('@objectstack/driver-memory');
77
- const driver = new InMemoryDriver();
78
- this.ensureObjectQL().registerDriver(driver);
79
- } catch (e) {
80
- // Ignore if not present
81
- }
82
-
83
- // 2. Initialize Engine
84
- await this.ensureObjectQL().init();
85
-
86
-
87
- // 3. Seed Data
88
- await this.seed();
89
-
90
- // 4. Start Runtime Plugins
91
- for (const p of this.plugins) {
92
- if (('onStart' in p) && typeof p.onStart === 'function') {
93
- console.log(`[Kernel] Starting Plugin: ${p.name}`);
94
- await p.onStart({ engine: this });
95
- }
96
- }
97
- }
98
-
99
- async seed() {
100
- // If no driver registered yet, this might fail or wait.
101
- // In real world, we wait for 'ready' event.
102
- try {
103
- // Mock System Table
104
- try {
105
- // We don't have SystemStatus defined in schema usually, skipping for general engine
106
- // await this.ql.insert('SystemStatus', { status: 'OK', uptime: 0 });
107
- } catch {}
108
-
109
- // Iterate over all registered plugins/apps and check for 'data' property in manifest
110
-
111
- const plugins = SchemaRegistry.getRegisteredTypes(); // This returns types like 'plugin', 'app'
112
-
113
- // This is a bit hacky because we don't have a direct "getAllManifests" API exposed easily
114
- // We will iterate known apps for now, or improve Registry API later.
115
- // Actually, SchemaRegistry.listItems('app') returns the manifests!
116
-
117
- const apps = [...SchemaRegistry.listItems('app'), ...SchemaRegistry.listItems('plugin')];
118
-
119
- for (const appItem of apps) {
120
- const app = appItem as any; // Cast to access data prop safely
121
- if (app.data && Array.isArray(app.data)) {
122
- console.log(`[Kernel] Seeding data for ${app.name || app.id}...`);
123
- for (const seed of app.data) {
124
- try {
125
- // Check if data exists
126
- const existing = await this.ensureObjectQL().find(seed.object, { top: 1 });
127
- if (existing.length === 0) {
128
- console.log(`[Kernel] Inserting ${seed.records.length} records into ${seed.object}`);
129
- for (const record of seed.records) {
130
- await this.ensureObjectQL().insert(seed.object, record);
131
- }
132
- }
133
- } catch (e) {
134
- console.warn(`[Kernel] Failed to seed ${seed.object}`, e);
135
- }
136
- }
137
- }
138
- }
139
-
140
- } catch(e) {
141
- console.warn('Seed failed (driver might not be ready):', e);
142
- }
143
- }
144
-
145
- // Forward methods to ObjectQL
146
- async find(objectName: string, query: any) {
147
- this.ensureSchema(objectName);
148
- const results = await this.ensureObjectQL().find(objectName, { top: 100 });
149
- return { value: results, count: results.length };
150
- }
151
-
152
- async get(objectName: string, id: string) {
153
- this.ensureSchema(objectName);
154
- // Find One
155
- const results = await this.ensureObjectQL().find(objectName, { top: 1 }); // Mock implementation
156
- return results[0];
157
- }
158
-
159
- async create(objectName: string, data: any) {
160
- this.ensureSchema(objectName);
161
- return this.ensureObjectQL().insert(objectName, data);
162
- }
163
-
164
- async update(objectName: string, id: string, data: any) {
165
- this.ensureSchema(objectName);
166
- return this.ensureObjectQL().update(objectName, id, data);
167
- }
168
-
169
- async delete(objectName: string, id: string) {
170
- this.ensureSchema(objectName);
171
- return this.ensureObjectQL().delete(objectName, id);
172
- }
173
-
174
- // [New Methods for ObjectUI]
175
- getMetadata(objectName: string) {
176
- return this.ensureSchema(objectName);
177
- }
178
-
179
- getView(objectName: string, viewType: 'list' | 'form' = 'list') {
180
- const schema = this.ensureSchema(objectName);
181
-
182
- // Auto-Scaffold Default View
183
- if (viewType === 'list') {
184
- return {
185
- type: 'datagrid',
186
- title: `${schema.label || objectName} List`,
187
- columns: Object.keys(schema.fields || {}).map(key => ({
188
- field: key,
189
- label: schema.fields?.[key]?.label || key,
190
- width: 150
191
- })),
192
- actions: ['create', 'edit', 'delete']
193
- };
194
- }
195
- return null;
196
- }
197
-
198
- private ensureSchema(name: string): ServiceObject {
199
- const schema = SchemaRegistry.getObject(name);
200
- if (!schema) throw new Error(`Unknown object: ${name}`);
201
- return schema;
202
- }
203
- }
@@ -1,47 +0,0 @@
1
- import { ObjectQL } from '@objectstack/objectql';
2
- import { RuntimePlugin, RuntimeContext } from '@objectstack/types';
3
-
4
- /**
5
- * ObjectQL Engine Plugin
6
- *
7
- * Registers the ObjectQL engine instance with the kernel.
8
- * This allows users to provide their own ObjectQL implementation or configuration.
9
- *
10
- * Usage:
11
- * - new ObjectQLPlugin() - Creates new ObjectQL with default settings
12
- * - new ObjectQLPlugin(existingQL) - Uses existing ObjectQL instance
13
- * - new ObjectQLPlugin(undefined, { custom: 'context' }) - Creates new ObjectQL with custom context
14
- */
15
- export class ObjectQLPlugin implements RuntimePlugin {
16
- name = 'com.objectstack.engine.objectql';
17
- type = 'objectql' as const;
18
-
19
- private ql: ObjectQL;
20
-
21
- /**
22
- * @param ql - Existing ObjectQL instance to use (optional)
23
- * @param hostContext - Host context for new ObjectQL instance (ignored if ql is provided)
24
- */
25
- constructor(ql?: ObjectQL, hostContext?: Record<string, any>) {
26
- if (ql && hostContext) {
27
- console.warn('[ObjectQLPlugin] Both ql and hostContext provided. hostContext will be ignored.');
28
- }
29
-
30
- if (ql) {
31
- this.ql = ql;
32
- } else {
33
- this.ql = new ObjectQL(hostContext || {
34
- env: process.env.NODE_ENV || 'development'
35
- });
36
- }
37
- }
38
-
39
- /**
40
- * Install the ObjectQL engine into the kernel
41
- */
42
- async install(ctx: RuntimeContext) {
43
- // Attach the ObjectQL engine to the kernel
44
- ctx.engine.ql = this.ql;
45
- console.log('[ObjectQLPlugin] ObjectQL engine registered');
46
- }
47
- }
package/src/protocol.ts DELETED
@@ -1,129 +0,0 @@
1
- import { SchemaRegistry } from '@objectstack/objectql';
2
- import { ObjectStackKernel } from './kernel';
3
-
4
- export interface ApiRequest {
5
- params: Record<string, string>;
6
- query: Record<string, string | string[]>;
7
- body?: any;
8
- }
9
-
10
- export class ObjectStackRuntimeProtocol {
11
- private engine: ObjectStackKernel;
12
-
13
- constructor(engine: ObjectStackKernel) {
14
- this.engine = engine;
15
- }
16
-
17
- // 1. Discovery
18
- getDiscovery() {
19
- return {
20
- name: 'ObjectOS Server',
21
- version: '1.0.0',
22
- environment: process.env.NODE_ENV || 'development',
23
- routes: {
24
- discovery: '/api/v1',
25
- metadata: '/api/v1/meta',
26
- data: '/api/v1/data',
27
- auth: '/api/v1/auth',
28
- ui: '/api/v1/ui'
29
- },
30
- capabilities: {
31
- search: true,
32
- files: true
33
- }
34
- };
35
- }
36
-
37
- // 2. Metadata: List Types
38
- getMetaTypes() {
39
- const types = SchemaRegistry.getRegisteredTypes();
40
- return {
41
- data: types.map(type => ({
42
- type,
43
- href: `/api/v1/meta/${type}s`,
44
- count: SchemaRegistry.listItems(type).length
45
- }))
46
- };
47
- }
48
-
49
- // 3. Metadata: List Items by Type
50
- getMetaItems(typePlural: string) {
51
- // Simple Singularization Mapping
52
- const typeMap: Record<string, string> = {
53
- 'objects': 'object',
54
- 'apps': 'app',
55
- 'flows': 'flow',
56
- 'reports': 'report',
57
- 'plugins': 'plugin',
58
- 'kinds': 'kind'
59
- };
60
- const type = typeMap[typePlural] || typePlural;
61
- const items = SchemaRegistry.listItems(type);
62
-
63
- const summaries = items.map((item: any) => ({
64
- id: item.id,
65
- name: item.name,
66
- label: item.label,
67
- type: item.type,
68
- icon: item.icon,
69
- description: item.description,
70
- ...(type === 'object' ? { path: `/api/v1/data/${item.name}` } : {}),
71
- self: `/api/v1/meta/${typePlural}/${item.name || item.id}`
72
- }));
73
-
74
- return { data: summaries };
75
- }
76
-
77
- // 4. Metadata: Get Single Item
78
- getMetaItem(typePlural: string, name: string) {
79
- const typeMap: Record<string, string> = {
80
- 'objects': 'object',
81
- 'apps': 'app',
82
- 'flows': 'flow',
83
- 'reports': 'report',
84
- 'plugins': 'plugin',
85
- 'kinds': 'kind'
86
- };
87
- const type = typeMap[typePlural] || typePlural;
88
- const item = SchemaRegistry.getItem(type, name);
89
- if (!item) throw new Error(`Metadata not found: ${type}/${name}`);
90
- return item;
91
- }
92
-
93
- // 5. UI: View Definition
94
- getUiView(objectName: string, type: 'list' | 'form') {
95
- const view = this.engine.getView(objectName, type);
96
- if (!view) throw new Error('View not generated');
97
- return view;
98
- }
99
-
100
- // 6. Data: Find
101
- async findData(objectName: string, query: any) {
102
- return await this.engine.find(objectName, query);
103
- }
104
-
105
- // 7. Data: Query (Advanced AST)
106
- async queryData(objectName: string, body: any) {
107
- return await this.engine.find(objectName, body);
108
- }
109
-
110
- // 8. Data: Get
111
- async getData(objectName: string, id: string) {
112
- return await this.engine.get(objectName, id);
113
- }
114
-
115
- // 9. Data: Create
116
- async createData(objectName: string, body: any) {
117
- return await this.engine.create(objectName, body);
118
- }
119
-
120
- // 10. Data: Update
121
- async updateData(objectName: string, id: string, body: any) {
122
- return await this.engine.update(objectName, id, body);
123
- }
124
-
125
- // 11. Data: Delete
126
- async deleteData(objectName: string, id: string) {
127
- return await this.engine.delete(objectName, id);
128
- }
129
- }
package/src/types.ts DELETED
@@ -1,11 +0,0 @@
1
- import { ObjectStackKernel } from './kernel';
2
-
3
- export interface RuntimeContext {
4
- engine: ObjectStackKernel;
5
- }
6
-
7
- export interface RuntimePlugin {
8
- name: string;
9
- install?: (ctx: RuntimeContext) => void | Promise<void>;
10
- onStart?: (ctx: RuntimeContext) => void | Promise<void>;
11
- }