|
| 1 | +// Copyright (c) 2025 ObjectStack. Licensed under the Apache-2.0 license. |
| 2 | +// |
| 3 | +// Embedding the ObjectQL engine as a plain library (ADR-0076). |
| 4 | +// |
| 5 | +// This imports from `@objectstack/objectql/core` — the LEAN entry. It pulls the |
| 6 | +// data engine (query/CRUD/hooks/validation) only: NO kernel, NO ObjectQLPlugin, |
| 7 | +// and NOT `@objectstack/metadata-protocol` (the 268KB metadata-management layer). |
| 8 | +// Ideal for a thin, latency-sensitive host (e.g. a gateway) that wants the |
| 9 | +// engine and the *same* object definitions as the full platform, without the |
| 10 | +// platform itself. |
| 11 | +// |
| 12 | +// The object below is an ordinary `ObjectSchema.create({...})` — the exact same |
| 13 | +// shape you would ship in a `*.object.ts` to a full ObjectStack backend. One |
| 14 | +// object model, two hosts; only the installed capability set differs. |
| 15 | + |
| 16 | +import { ObjectQL } from '@objectstack/objectql/core'; |
| 17 | +import { InMemoryDriver } from '@objectstack/driver-memory'; |
| 18 | +import { ObjectSchema, Field, type ServiceObject } from '@objectstack/spec/data'; |
| 19 | + |
| 20 | +export const Account = ObjectSchema.create({ |
| 21 | + name: 'account', |
| 22 | + label: 'Account', |
| 23 | + pluralLabel: 'Accounts', |
| 24 | + fields: { |
| 25 | + name: Field.text({ label: 'Name', required: true }), |
| 26 | + industry: Field.text({ label: 'Industry' }), |
| 27 | + active: Field.boolean({ label: 'Active' }), |
| 28 | + }, |
| 29 | +}); |
| 30 | + |
| 31 | +export interface AccountRow { |
| 32 | + id: string; |
| 33 | + name: string; |
| 34 | + industry?: string; |
| 35 | + active?: boolean; |
| 36 | +} |
| 37 | + |
| 38 | +/** Boot a standalone engine, register one object, do CRUD, return active rows. */ |
| 39 | +export async function runEmbeddedEngine(): Promise<AccountRow[]> { |
| 40 | + const engine = new ObjectQL(); |
| 41 | + engine.registerDriver(new InMemoryDriver({ persistence: false }), true); |
| 42 | + await engine.init(); |
| 43 | + |
| 44 | + // Register the object directly — the registry lives in the core engine, so no |
| 45 | + // kernel/plugin/metadata-protocol is involved. (`ObjectSchema.create` is the |
| 46 | + // authoring shape; `registerObject` takes the canonical `ServiceObject`.) |
| 47 | + engine.registry.registerObject(Account as ServiceObject, 'example-embed'); |
| 48 | + |
| 49 | + await engine.insert('account', { name: 'Acme', industry: 'Manufacturing', active: true }); |
| 50 | + await engine.insert('account', { name: 'Globex', industry: 'Energy', active: false }); |
| 51 | + await engine.insert('account', { name: 'Initech', industry: 'Software', active: true }); |
| 52 | + |
| 53 | + return engine.find('account', { |
| 54 | + where: { active: true }, |
| 55 | + orderBy: [{ field: 'name', order: 'asc' }], |
| 56 | + }) as Promise<AccountRow[]>; |
| 57 | +} |
| 58 | + |
| 59 | +// Allow `node`/`tsx`-style direct execution to print the result. |
| 60 | +if (import.meta.url === `file://${process.argv[1]}`) { |
| 61 | + runEmbeddedEngine() |
| 62 | + .then((rows) => { |
| 63 | + // eslint-disable-next-line no-console |
| 64 | + console.log(`Active accounts (${rows.length}):`, rows.map((r) => r.name).join(', ')); |
| 65 | + }) |
| 66 | + .catch((err) => { |
| 67 | + // eslint-disable-next-line no-console |
| 68 | + console.error(err); |
| 69 | + process.exit(1); |
| 70 | + }); |
| 71 | +} |
0 commit comments