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