@objectstack/objectql 0.0.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/LICENSE ADDED
@@ -0,0 +1,202 @@
1
+ Apache License
2
+ Version 2.0, January 2004
3
+ http://www.apache.org/licenses/
4
+
5
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
6
+
7
+ 1. Definitions.
8
+
9
+ "License" shall mean the terms and conditions for use, reproduction,
10
+ and distribution as defined by Sections 1 through 9 of this document.
11
+
12
+ "Licensor" shall mean the copyright owner or entity authorized by
13
+ the copyright owner that is granting the License.
14
+
15
+ "Legal Entity" shall mean the union of the acting entity and all
16
+ other entities that control, are controlled by, or are under common
17
+ control with that entity. For the purposes of this definition,
18
+ "control" means (i) the power, direct or indirect, to cause the
19
+ direction or management of such entity, whether by contract or
20
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
21
+ outstanding shares, or (iii) beneficial ownership of such entity.
22
+
23
+ "You" (or "Your") shall mean an individual or Legal Entity
24
+ exercising permissions granted by this License.
25
+
26
+ "Source" form shall mean the preferred form for making modifications,
27
+ including but not limited to software source code, documentation
28
+ source, and configuration files.
29
+
30
+ "Object" form shall mean any form resulting from mechanical
31
+ transformation or translation of a Source form, including but
32
+ not limited to compiled object code, generated documentation,
33
+ and conversions to other media types.
34
+
35
+ "Work" shall mean the work of authorship, whether in Source or
36
+ Object form, made available under the License, as indicated by a
37
+ copyright notice that is included in or attached to the work
38
+ (an example is provided in the Appendix below).
39
+
40
+ "Derivative Works" shall mean any work, whether in Source or Object
41
+ form, that is based on (or derived from) the Work and for which the
42
+ editorial revisions, annotations, elaborations, or other modifications
43
+ represent, as a whole, an original work of authorship. For the purposes
44
+ of this License, Derivative Works shall not include works that remain
45
+ separable from, or merely link (or bind by name) to the interfaces of,
46
+ the Work and Derivative Works thereof.
47
+
48
+ "Contribution" shall mean any work of authorship, including
49
+ the original version of the Work and any modifications or additions
50
+ to that Work or Derivative Works thereof, that is intentionally
51
+ submitted to Licensor for inclusion in the Work by the copyright owner
52
+ or by an individual or Legal Entity authorized to submit on behalf of
53
+ the copyright owner. For the purposes of this definition, "submitted"
54
+ means any form of electronic, verbal, or written communication sent
55
+ to the Licensor or its representatives, including but not limited to
56
+ communication on electronic mailing lists, source code control systems,
57
+ and issue tracking systems that are managed by, or on behalf of, the
58
+ Licensor for the purpose of discussing and improving the Work, but
59
+ excluding communication that is conspicuously marked or otherwise
60
+ designated in writing by the copyright owner as "Not a Contribution."
61
+
62
+ "Contributor" shall mean Licensor and any individual or Legal Entity
63
+ on behalf of whom a Contribution has been received by Licensor and
64
+ subsequently incorporated within the Work.
65
+
66
+ 2. Grant of Copyright License. Subject to the terms and conditions of
67
+ this License, each Contributor hereby grants to You a perpetual,
68
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
69
+ copyright license to reproduce, prepare Derivative Works of,
70
+ publicly display, publicly perform, sublicense, and distribute the
71
+ Work and such Derivative Works in Source or Object form.
72
+
73
+ 3. Grant of Patent License. Subject to the terms and conditions of
74
+ this License, each Contributor hereby grants to You a perpetual,
75
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
76
+ (except as stated in this section) patent license to make, have made,
77
+ use, offer to sell, sell, import, and otherwise transfer the Work,
78
+ where such license applies only to those patent claims licensable
79
+ by such Contributor that are necessarily infringed by their
80
+ Contribution(s) alone or by combination of their Contribution(s)
81
+ with the Work to which such Contribution(s) was submitted. If You
82
+ institute patent litigation against any entity (including a
83
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
84
+ or a Contribution incorporated within the Work constitutes direct
85
+ or contributory patent infringement, then any patent licenses
86
+ granted to You under this License for that Work shall terminate
87
+ as of the date such litigation is filed.
88
+
89
+ 4. Redistribution. You may reproduce and distribute copies of the
90
+ Work or Derivative Works thereof in any medium, with or without
91
+ modifications, and in Source or Object form, provided that You
92
+ meet the following conditions:
93
+
94
+ (a) You must give any other recipients of the Work or
95
+ Derivative Works a copy of this License; and
96
+
97
+ (b) You must cause any modified files to carry prominent notices
98
+ stating that You changed the files; and
99
+
100
+ (c) You must retain, in the Source form of any Derivative Works
101
+ that You distribute, all copyright, patent, trademark, and
102
+ attribution notices from the Source form of the Work,
103
+ excluding those notices that do not pertain to any part of
104
+ the Derivative Works; and
105
+
106
+ (d) If the Work includes a "NOTICE" text file as part of its
107
+ distribution, then any Derivative Works that You distribute
108
+ must include a readable copy of the attribution notices
109
+ contained within such NOTICE file, excluding those notices
110
+ that do not pertain to any part of the Derivative Works,
111
+ in at least one of the following places: within a NOTICE
112
+ text file distributed as part of the Derivative Works; within
113
+ the Source form or documentation, if provided along with
114
+ the Derivative Works; or, within a display generated by the
115
+ Derivative Works, if and wherever such third-party notices
116
+ normally appear. The contents of the NOTICE file are for
117
+ informational purposes only and do not modify the License.
118
+ You may add Your own attribution notices within Derivative
119
+ Works that You distribute, alongside or as an addendum to
120
+ the NOTICE text from the Work, provided that such additional
121
+ attribution notices cannot be construed as modifying the
122
+ License.
123
+
124
+ You may add Your own copyright statement to Your modifications and
125
+ may provide additional or different license terms and conditions
126
+ for use, reproduction, or distribution of Your modifications, or
127
+ for any such Derivative Works as a whole, provided Your use,
128
+ reproduction, and distribution of the Work otherwise complies with
129
+ the conditions stated in this License.
130
+
131
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
132
+ any Contribution intentionally submitted for inclusion in the Work
133
+ by You to the Licensor shall be under the terms and conditions of
134
+ this License, without any additional terms or conditions.
135
+ Notwithstanding the above, nothing herein shall supersede or modify
136
+ the terms of any separate license agreement you may have executed
137
+ with Licensor regarding such Contributions.
138
+
139
+ 6. Trademarks. This License does not grant permission to use the trade
140
+ names, trademarks, service marks, or product names of the Licensor,
141
+ except as required for reasonable and customary use in describing the
142
+ origin of the Work and reproducing the content of the NOTICE file.
143
+
144
+ 7. Disclaimer of Warranty. Unless required by applicable law or
145
+ agreed to in writing, Licensor provides the Work (and each
146
+ Contributor provides its Contributions) on an "AS IS" BASIS,
147
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
148
+ implied, including, without limitation, any warranties or conditions
149
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
150
+ PARTICULAR PURPOSE. You are solely responsible for determining the
151
+ appropriateness of using or redistributing the Work and assume any
152
+ risks associated with Your exercise of permissions under this License.
153
+
154
+ 8. Limitation of Liability. In no event and under no legal theory,
155
+ whether in tort (including negligence), contract, or otherwise,
156
+ unless required by applicable law (such as deliberate and grossly
157
+ negligent acts) or agreed to in writing, shall any Contributor be
158
+ liable to You for damages, including any direct, indirect, special,
159
+ incidental, or consequential damages of any character arising as a
160
+ result of this License or out of the use or inability to use the
161
+ Work (including but not limited to damages for loss of goodwill,
162
+ work stoppage, computer failure or malfunction, or any and all
163
+ other commercial damages or losses), even if such Contributor
164
+ has been advised of the possibility of such damages.
165
+
166
+ 9. Accepting Warranty or Additional Liability. While redistributing
167
+ the Work or Derivative Works thereof, You may choose to offer,
168
+ and charge a fee for, acceptance of support, warranty, indemnity,
169
+ or other liability obligations and/or rights consistent with this
170
+ License. However, in accepting such obligations, You may act only
171
+ on Your own behalf and on Your sole responsibility, not on behalf
172
+ of any other Contributor, and only if You agree to indemnify,
173
+ defend, and hold each Contributor harmless for any liability
174
+ incurred by, or claims asserted against, such Contributor by reason
175
+ of your accepting any such warranty or additional liability.
176
+
177
+ END OF TERMS AND CONDITIONS
178
+
179
+ APPENDIX: How to apply the Apache License to your work.
180
+
181
+ To apply the Apache License to your work, attach the following
182
+ boilerplate notice, with the fields enclosed by brackets "[]"
183
+ replaced with your own identifying information. (Don't include
184
+ the brackets!) The text should be enclosed in the appropriate
185
+ comment syntax for the file format. We also recommend that a
186
+ file or class name and description of purpose be included on the
187
+ same "printed page" as the copyright notice for easier
188
+ identification within third-party archives.
189
+
190
+ Copyright 2026 ObjectStack
191
+
192
+ Licensed under the Apache License, Version 2.0 (the "License");
193
+ you may not use this file except in compliance with the License.
194
+ You may obtain a copy of the License at
195
+
196
+ http://www.apache.org/licenses/LICENSE-2.0
197
+
198
+ Unless required by applicable law or agreed to in writing, software
199
+ distributed under the License is distributed on an "AS IS" BASIS,
200
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
201
+ See the License for the specific language governing permissions and
202
+ limitations under the License.
@@ -0,0 +1,40 @@
1
+ import { DriverInterface, DriverOptions } from '@objectstack/spec';
2
+ /**
3
+ * Host Context provided to plugins
4
+ */
5
+ export interface PluginContext {
6
+ ql: ObjectQL;
7
+ logger: Console;
8
+ [key: string]: any;
9
+ }
10
+ /**
11
+ * ObjectQL Engine
12
+ */
13
+ export declare class ObjectQL {
14
+ private drivers;
15
+ private defaultDriver;
16
+ private plugins;
17
+ private hostContext;
18
+ constructor(hostContext?: Record<string, any>);
19
+ /**
20
+ * Load and Register a Plugin
21
+ */
22
+ use(manifestPart: any, runtimePart?: any): Promise<void>;
23
+ /**
24
+ * Register a new storage driver
25
+ */
26
+ registerDriver(driver: DriverInterface, isDefault?: boolean): void;
27
+ /**
28
+ * Helper to get the target driver
29
+ */
30
+ private getDriver;
31
+ /**
32
+ * Initialize the engine and all registered drivers
33
+ */
34
+ init(): Promise<void>;
35
+ destroy(): Promise<void>;
36
+ find(object: string, filters?: any, options?: DriverOptions): Promise<Record<string, any>[]>;
37
+ insert(object: string, data: Record<string, any>, options?: DriverOptions): Promise<Record<string, any>>;
38
+ update(object: string, id: string, data: Record<string, any>, options?: DriverOptions): Promise<Record<string, any>>;
39
+ delete(object: string, id: string, options?: DriverOptions): Promise<boolean>;
40
+ }
package/dist/engine.js ADDED
@@ -0,0 +1,139 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.ObjectQL = void 0;
4
+ const registry_1 = require("./registry");
5
+ /**
6
+ * ObjectQL Engine
7
+ */
8
+ class ObjectQL {
9
+ constructor(hostContext = {}) {
10
+ this.drivers = new Map();
11
+ this.defaultDriver = null;
12
+ this.plugins = new Map();
13
+ // Host provided context additions (e.g. Server router)
14
+ this.hostContext = {};
15
+ this.hostContext = hostContext;
16
+ console.log(`[ObjectQL] Engine Instance Created`);
17
+ }
18
+ /**
19
+ * Load and Register a Plugin
20
+ */
21
+ async use(manifestPart, runtimePart) {
22
+ // 1. Validate / Register Manifest
23
+ if (manifestPart) {
24
+ // In a real scenario, we might strictly parse this using Zod
25
+ // For now, simple ID check
26
+ const id = manifestPart.id;
27
+ if (!id) {
28
+ console.warn('[ObjectQL] Plugin manifest missing ID', manifestPart);
29
+ return;
30
+ }
31
+ console.log(`[ObjectQL] Loading Plugin: ${id}`);
32
+ registry_1.SchemaRegistry.registerPlugin(manifestPart);
33
+ // Register contributions
34
+ if (manifestPart.contributes?.kinds) {
35
+ for (const kind of manifestPart.contributes.kinds) {
36
+ registry_1.SchemaRegistry.registerKind(kind);
37
+ }
38
+ }
39
+ }
40
+ // 2. Execute Runtime
41
+ if (runtimePart) {
42
+ const pluginDef = runtimePart.default || runtimePart;
43
+ if (pluginDef.onEnable) {
44
+ const context = {
45
+ ql: this,
46
+ logger: console,
47
+ // Expose the driver registry helper explicitly if needed
48
+ drivers: this, // Since `registerDriver` is on `this`, we can alias it or expose `this`
49
+ ...this.hostContext
50
+ };
51
+ await pluginDef.onEnable(context);
52
+ }
53
+ }
54
+ }
55
+ /**
56
+ * Register a new storage driver
57
+ */
58
+ registerDriver(driver, isDefault = false) {
59
+ if (this.drivers.has(driver.name)) {
60
+ console.warn(`[ObjectQL] Driver ${driver.name} is already registered. Skipping.`);
61
+ return;
62
+ }
63
+ this.drivers.set(driver.name, driver);
64
+ console.log(`[ObjectQL] Registered driver: ${driver.name} v${driver.version}`);
65
+ if (isDefault || this.drivers.size === 1) {
66
+ this.defaultDriver = driver.name;
67
+ }
68
+ }
69
+ /**
70
+ * Helper to get the target driver
71
+ */
72
+ getDriver(object) {
73
+ // TODO: Look up Object definition to see if it specifies a specific datasource/driver
74
+ // For now, always return default
75
+ if (!this.defaultDriver) {
76
+ throw new Error('[ObjectQL] No drivers registered!');
77
+ }
78
+ return this.drivers.get(this.defaultDriver);
79
+ }
80
+ /**
81
+ * Initialize the engine and all registered drivers
82
+ */
83
+ async init() {
84
+ console.log('[ObjectQL] Initializing drivers...');
85
+ for (const [name, driver] of this.drivers) {
86
+ try {
87
+ await driver.connect();
88
+ }
89
+ catch (e) {
90
+ console.error(`[ObjectQL] Failed to connect driver ${name}`, e);
91
+ }
92
+ }
93
+ // In a real app, we would sync schemas here
94
+ }
95
+ async destroy() {
96
+ for (const driver of this.drivers.values()) {
97
+ await driver.disconnect();
98
+ }
99
+ }
100
+ // ============================================
101
+ // Data Access Methods
102
+ // ============================================
103
+ async find(object, filters = {}, options) {
104
+ const driver = this.getDriver(object);
105
+ console.log(`[ObjectQL] Finding ${object} on ${driver.name}...`);
106
+ // Transform simplified filters to QueryAST
107
+ // This is a simplified "Mock" transform.
108
+ // Real implementation would parse complex JSON or FilterBuilders.
109
+ const ast = {
110
+ object, // Add missing required field
111
+ // Pass through if it looks like AST, otherwise empty
112
+ // In this demo, we assume the caller passes a simplified object or raw AST
113
+ filters: filters.filters || undefined,
114
+ top: filters.top || 100,
115
+ sort: filters.sort || []
116
+ };
117
+ return driver.find(object, ast, options);
118
+ }
119
+ async insert(object, data, options) {
120
+ const driver = this.getDriver(object);
121
+ console.log(`[ObjectQL] Creating ${object} on ${driver.name}...`);
122
+ // 1. Validate Schema
123
+ // 2. Run "Before Insert" Triggers
124
+ const result = await driver.create(object, data, options);
125
+ // 3. Run "After Insert" Triggers
126
+ return result;
127
+ }
128
+ async update(object, id, data, options) {
129
+ const driver = this.getDriver(object);
130
+ console.log(`[ObjectQL] Updating ${object} ${id}...`);
131
+ return driver.update(object, id, data, options);
132
+ }
133
+ async delete(object, id, options) {
134
+ const driver = this.getDriver(object);
135
+ console.log(`[ObjectQL] Deleting ${object} ${id}...`);
136
+ return driver.delete(object, id, options);
137
+ }
138
+ }
139
+ exports.ObjectQL = ObjectQL;
@@ -0,0 +1,3 @@
1
+ export { ObjectQL } from './engine';
2
+ export { SchemaRegistry } from './registry';
3
+ export type { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec';
package/dist/index.js ADDED
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaRegistry = exports.ObjectQL = void 0;
4
+ // Export core engine
5
+ var engine_1 = require("./engine");
6
+ Object.defineProperty(exports, "ObjectQL", { enumerable: true, get: function () { return engine_1.ObjectQL; } });
7
+ var registry_1 = require("./registry");
8
+ Object.defineProperty(exports, "SchemaRegistry", { enumerable: true, get: function () { return registry_1.SchemaRegistry; } });
@@ -0,0 +1,55 @@
1
+ import { ServiceObject, App, ObjectStackManifest } from '@objectstack/spec';
2
+ /**
3
+ * Global Schema Registry
4
+ * Unified storage for all metadata types (Objects, Apps, Flows, Layouts, etc.)
5
+ */
6
+ export declare class SchemaRegistry {
7
+ private static metadata;
8
+ /**
9
+ * Universal Register Method
10
+ * @param type The category of metadata (e.g., 'object', 'app', 'plugin')
11
+ * @param item The metadata item itself
12
+ * @param keyField The property to use as the unique key (default: 'name')
13
+ */
14
+ static registerItem<T>(type: string, item: T, keyField?: keyof T): void;
15
+ /**
16
+ * Universal Get Method
17
+ */
18
+ static getItem<T>(type: string, name: string): T | undefined;
19
+ /**
20
+ * Universal List Method
21
+ */
22
+ static listItems<T>(type: string): T[];
23
+ /**
24
+ * Get all registered metadata types (Kinds)
25
+ */
26
+ static getRegisteredTypes(): string[];
27
+ /**
28
+ * Object Helpers
29
+ */
30
+ static registerObject(schema: ServiceObject): void;
31
+ static getObject(name: string): ServiceObject | undefined;
32
+ static getAllObjects(): ServiceObject[];
33
+ /**
34
+ * App Helpers
35
+ */
36
+ static registerApp(app: App): void;
37
+ static getApp(name: string): App | undefined;
38
+ static getAllApps(): App[];
39
+ /**
40
+ * Plugin Helpers
41
+ */
42
+ static registerPlugin(manifest: ObjectStackManifest): void;
43
+ static getAllPlugins(): ObjectStackManifest[];
44
+ /**
45
+ * Kind (Metadata Type) Helpers
46
+ */
47
+ static registerKind(kind: {
48
+ id: string;
49
+ globs: string[];
50
+ }): void;
51
+ static getAllKinds(): {
52
+ id: string;
53
+ globs: string[];
54
+ }[];
55
+ }
@@ -0,0 +1,93 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.SchemaRegistry = void 0;
4
+ /**
5
+ * Global Schema Registry
6
+ * Unified storage for all metadata types (Objects, Apps, Flows, Layouts, etc.)
7
+ */
8
+ class SchemaRegistry {
9
+ /**
10
+ * Universal Register Method
11
+ * @param type The category of metadata (e.g., 'object', 'app', 'plugin')
12
+ * @param item The metadata item itself
13
+ * @param keyField The property to use as the unique key (default: 'name')
14
+ */
15
+ static registerItem(type, item, keyField = 'name') {
16
+ if (!this.metadata.has(type)) {
17
+ this.metadata.set(type, new Map());
18
+ }
19
+ const collection = this.metadata.get(type);
20
+ const key = String(item[keyField]);
21
+ if (collection.has(key)) {
22
+ console.warn(`[Registry] Overwriting ${type}: ${key}`);
23
+ }
24
+ collection.set(key, item);
25
+ console.log(`[Registry] Registered ${type}: ${key}`);
26
+ }
27
+ /**
28
+ * Universal Get Method
29
+ */
30
+ static getItem(type, name) {
31
+ return this.metadata.get(type)?.get(name);
32
+ }
33
+ /**
34
+ * Universal List Method
35
+ */
36
+ static listItems(type) {
37
+ return Array.from(this.metadata.get(type)?.values() || []);
38
+ }
39
+ /**
40
+ * Get all registered metadata types (Kinds)
41
+ */
42
+ static getRegisteredTypes() {
43
+ return Array.from(this.metadata.keys());
44
+ }
45
+ // ==========================================
46
+ // Typed Helper Methods (Shortcuts)
47
+ // ==========================================
48
+ /**
49
+ * Object Helpers
50
+ */
51
+ static registerObject(schema) {
52
+ this.registerItem('object', schema, 'name');
53
+ }
54
+ static getObject(name) {
55
+ return this.getItem('object', name);
56
+ }
57
+ static getAllObjects() {
58
+ return this.listItems('object');
59
+ }
60
+ /**
61
+ * App Helpers
62
+ */
63
+ static registerApp(app) {
64
+ this.registerItem('app', app, 'name');
65
+ }
66
+ static getApp(name) {
67
+ return this.getItem('app', name);
68
+ }
69
+ static getAllApps() {
70
+ return this.listItems('app');
71
+ }
72
+ /**
73
+ * Plugin Helpers
74
+ */
75
+ static registerPlugin(manifest) {
76
+ this.registerItem('plugin', manifest, 'id');
77
+ }
78
+ static getAllPlugins() {
79
+ return this.listItems('plugin');
80
+ }
81
+ /**
82
+ * Kind (Metadata Type) Helpers
83
+ */
84
+ static registerKind(kind) {
85
+ this.registerItem('kind', kind, 'id');
86
+ }
87
+ static getAllKinds() {
88
+ return this.listItems('kind');
89
+ }
90
+ }
91
+ exports.SchemaRegistry = SchemaRegistry;
92
+ // Nested Map: Type -> Name/ID -> MetadataItem
93
+ SchemaRegistry.metadata = new Map();
package/package.json ADDED
@@ -0,0 +1,17 @@
1
+ {
2
+ "name": "@objectstack/objectql",
3
+ "version": "0.0.1",
4
+ "description": "ObjectQL Reference Implementation",
5
+ "main": "dist/index.js",
6
+ "types": "dist/index.d.ts",
7
+ "dependencies": {
8
+ "@objectstack/spec": "0.1.1"
9
+ },
10
+ "devDependencies": {
11
+ "typescript": "^5.0.0"
12
+ },
13
+ "scripts": {
14
+ "build": "tsc",
15
+ "dev": "tsc -w"
16
+ }
17
+ }
package/src/engine.ts ADDED
@@ -0,0 +1,168 @@
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
+ }
13
+
14
+ /**
15
+ * ObjectQL Engine
16
+ */
17
+ export class ObjectQL {
18
+ private drivers = new Map<string, DriverInterface>();
19
+ 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> = {};
24
+
25
+ constructor(hostContext: Record<string, any> = {}) {
26
+ this.hostContext = hostContext;
27
+ console.log(`[ObjectQL] Engine Instance Created`);
28
+ }
29
+
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
+
72
+ /**
73
+ * Register a new storage driver
74
+ */
75
+ registerDriver(driver: DriverInterface, isDefault: boolean = false) {
76
+ if (this.drivers.has(driver.name)) {
77
+ console.warn(`[ObjectQL] Driver ${driver.name} is already registered. Skipping.`);
78
+ return;
79
+ }
80
+
81
+ this.drivers.set(driver.name, driver);
82
+ console.log(`[ObjectQL] Registered driver: ${driver.name} v${driver.version}`);
83
+
84
+ if (isDefault || this.drivers.size === 1) {
85
+ this.defaultDriver = driver.name;
86
+ }
87
+ }
88
+
89
+ /**
90
+ * Helper to get the target driver
91
+ */
92
+ private getDriver(object: string): DriverInterface {
93
+ // TODO: Look up Object definition to see if it specifies a specific datasource/driver
94
+ // For now, always return default
95
+ if (!this.defaultDriver) {
96
+ throw new Error('[ObjectQL] No drivers registered!');
97
+ }
98
+ return this.drivers.get(this.defaultDriver)!;
99
+ }
100
+
101
+ /**
102
+ * Initialize the engine and all registered drivers
103
+ */
104
+ async init() {
105
+ console.log('[ObjectQL] Initializing drivers...');
106
+ for (const [name, driver] of this.drivers) {
107
+ try {
108
+ await driver.connect();
109
+ } catch (e) {
110
+ console.error(`[ObjectQL] Failed to connect driver ${name}`, e);
111
+ }
112
+ }
113
+ // In a real app, we would sync schemas here
114
+ }
115
+
116
+ async destroy() {
117
+ for (const driver of this.drivers.values()) {
118
+ await driver.disconnect();
119
+ }
120
+ }
121
+
122
+ // ============================================
123
+ // Data Access Methods
124
+ // ============================================
125
+
126
+ async find(object: string, filters: any = {}, options?: DriverOptions) {
127
+ const driver = this.getDriver(object);
128
+ console.log(`[ObjectQL] Finding ${object} on ${driver.name}...`);
129
+
130
+ // Transform simplified filters to QueryAST
131
+ // This is a simplified "Mock" transform.
132
+ // Real implementation would parse complex JSON or FilterBuilders.
133
+ const ast: QueryAST = {
134
+ object, // Add missing required field
135
+ // Pass through if it looks like AST, otherwise empty
136
+ // In this demo, we assume the caller passes a simplified object or raw AST
137
+ filters: filters.filters || undefined,
138
+ top: filters.top || 100,
139
+ sort: filters.sort || []
140
+ };
141
+
142
+ return driver.find(object, ast, options);
143
+ }
144
+
145
+ async insert(object: string, data: Record<string, any>, options?: DriverOptions) {
146
+ const driver = this.getDriver(object);
147
+ console.log(`[ObjectQL] Creating ${object} on ${driver.name}...`);
148
+ // 1. Validate Schema
149
+ // 2. Run "Before Insert" Triggers
150
+
151
+ const result = await driver.create(object, data, options);
152
+
153
+ // 3. Run "After Insert" Triggers
154
+ return result;
155
+ }
156
+
157
+ async update(object: string, id: string, data: Record<string, any>, options?: DriverOptions) {
158
+ const driver = this.getDriver(object);
159
+ console.log(`[ObjectQL] Updating ${object} ${id}...`);
160
+ return driver.update(object, id, data, options);
161
+ }
162
+
163
+ async delete(object: string, id: string, options?: DriverOptions) {
164
+ const driver = this.getDriver(object);
165
+ console.log(`[ObjectQL] Deleting ${object} ${id}...`);
166
+ return driver.delete(object, id, options);
167
+ }
168
+ }
package/src/index.ts ADDED
@@ -0,0 +1,6 @@
1
+ // Export core engine
2
+ export { ObjectQL } from './engine';
3
+ export { SchemaRegistry } from './registry';
4
+
5
+ // Re-export common types from spec for convenience
6
+ export type { DriverInterface, DriverOptions, QueryAST } from '@objectstack/spec';
@@ -0,0 +1,107 @@
1
+ import { ServiceObject, App, ObjectStackManifest } from '@objectstack/spec';
2
+
3
+ /**
4
+ * Global Schema Registry
5
+ * Unified storage for all metadata types (Objects, Apps, Flows, Layouts, etc.)
6
+ */
7
+ export class SchemaRegistry {
8
+ // Nested Map: Type -> Name/ID -> MetadataItem
9
+ private static metadata = new Map<string, Map<string, any>>();
10
+
11
+ /**
12
+ * Universal Register Method
13
+ * @param type The category of metadata (e.g., 'object', 'app', 'plugin')
14
+ * @param item The metadata item itself
15
+ * @param keyField The property to use as the unique key (default: 'name')
16
+ */
17
+ static registerItem<T>(type: string, item: T, keyField: keyof T = 'name' as keyof T) {
18
+ if (!this.metadata.has(type)) {
19
+ this.metadata.set(type, new Map());
20
+ }
21
+ const collection = this.metadata.get(type)!;
22
+ const key = String(item[keyField]);
23
+
24
+ if (collection.has(key)) {
25
+ console.warn(`[Registry] Overwriting ${type}: ${key}`);
26
+ }
27
+ collection.set(key, item);
28
+ console.log(`[Registry] Registered ${type}: ${key}`);
29
+ }
30
+
31
+ /**
32
+ * Universal Get Method
33
+ */
34
+ static getItem<T>(type: string, name: string): T | undefined {
35
+ return this.metadata.get(type)?.get(name) as T;
36
+ }
37
+
38
+ /**
39
+ * Universal List Method
40
+ */
41
+ static listItems<T>(type: string): T[] {
42
+ return Array.from(this.metadata.get(type)?.values() || []) as T[];
43
+ }
44
+
45
+ /**
46
+ * Get all registered metadata types (Kinds)
47
+ */
48
+ static getRegisteredTypes(): string[] {
49
+ return Array.from(this.metadata.keys());
50
+ }
51
+
52
+ // ==========================================
53
+ // Typed Helper Methods (Shortcuts)
54
+ // ==========================================
55
+
56
+ /**
57
+ * Object Helpers
58
+ */
59
+ static registerObject(schema: ServiceObject) {
60
+ this.registerItem('object', schema, 'name');
61
+ }
62
+
63
+ static getObject(name: string): ServiceObject | undefined {
64
+ return this.getItem<ServiceObject>('object', name);
65
+ }
66
+
67
+ static getAllObjects(): ServiceObject[] {
68
+ return this.listItems<ServiceObject>('object');
69
+ }
70
+
71
+ /**
72
+ * App Helpers
73
+ */
74
+ static registerApp(app: App) {
75
+ this.registerItem('app', app, 'name');
76
+ }
77
+
78
+ static getApp(name: string): App | undefined {
79
+ return this.getItem<App>('app', name);
80
+ }
81
+
82
+ static getAllApps(): App[] {
83
+ return this.listItems<App>('app');
84
+ }
85
+
86
+ /**
87
+ * Plugin Helpers
88
+ */
89
+ static registerPlugin(manifest: ObjectStackManifest) {
90
+ this.registerItem('plugin', manifest, 'id');
91
+ }
92
+
93
+ static getAllPlugins(): ObjectStackManifest[] {
94
+ return this.listItems<ObjectStackManifest>('plugin');
95
+ }
96
+
97
+ /**
98
+ * Kind (Metadata Type) Helpers
99
+ */
100
+ static registerKind(kind: { id: string, globs: string[] }) {
101
+ this.registerItem('kind', kind, 'id');
102
+ }
103
+
104
+ static getAllKinds(): { id: string, globs: string[] }[] {
105
+ return this.listItems('kind');
106
+ }
107
+ }
package/tsconfig.json ADDED
@@ -0,0 +1,13 @@
1
+ {
2
+ "compilerOptions": {
3
+ "target": "ES2020",
4
+ "module": "commonjs",
5
+ "declaration": true,
6
+ "outDir": "./dist",
7
+ "strict": true,
8
+ "esModuleInterop": true,
9
+ "skipLibCheck": true,
10
+ "forceConsistentCasingInFileNames": true
11
+ },
12
+ "include": ["src/**/*"]
13
+ }