@coasys/ad4m 0.12.0-rc1-dev → 0.12.0-rc1-dev.2
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/jest.config.js +4 -4
- package/lib/index.cjs +5999 -6044
- package/lib/index.js +5977 -6038
- package/lib/src/Ad4mClient.d.ts +16 -9
- package/lib/src/Ad4mClient.js +25 -46
- package/lib/src/Ad4mClient.test.d.ts +1 -0
- package/lib/src/Ad4mClient.test.js +1204 -0
- package/lib/src/Literal.d.ts +2 -1
- package/lib/src/Literal.js +35 -38
- package/lib/src/Literal.test.d.ts +1 -0
- package/lib/src/Literal.test.js +41 -0
- package/lib/src/SmartLiteral.d.ts +2 -1
- package/lib/src/SmartLiteral.js +15 -30
- package/lib/src/agent/AgentClient.d.ts +4 -1
- package/lib/src/agent/AgentClient.js +99 -79
- package/lib/src/ai/AIClient.d.ts +2 -1
- package/lib/src/ai/AIClient.js +220 -224
- package/lib/src/expression/ExpressionClient.d.ts +1 -1
- package/lib/src/expression/ExpressionClient.js +86 -73
- package/lib/src/index.d.ts +1 -1
- package/lib/src/index.js +1 -1
- package/lib/src/language/LanguageClient.d.ts +1 -1
- package/lib/src/language/LanguageClient.js +52 -52
- package/lib/src/model/Ad4mModel.d.ts +157 -937
- package/lib/src/model/Ad4mModel.js +212 -2797
- package/lib/src/model/Ad4mModel.test.js +1002 -561
- package/lib/src/model/decorators.d.ts +94 -426
- package/lib/src/model/decorators.js +333 -741
- package/lib/src/model/mutation.d.ts +81 -0
- package/lib/src/model/mutation.js +317 -0
- package/lib/src/model/parentUtils.d.ts +32 -0
- package/lib/src/model/parentUtils.js +64 -0
- package/lib/src/model/prolog/generatePrologFacts.d.ts +36 -0
- package/lib/src/model/prolog/generatePrologFacts.js +139 -0
- package/lib/src/model/prolog/index.d.ts +1 -0
- package/lib/src/model/prolog/index.js +1 -0
- package/lib/src/model/query/ModelQueryBuilder.d.ts +82 -0
- package/lib/src/model/query/ModelQueryBuilder.js +115 -0
- package/lib/src/model/query/QueryBuilder.d.ts +101 -0
- package/lib/src/model/query/QueryBuilder.js +193 -0
- package/lib/src/model/query/SurrealQueryBuilder.d.ts +48 -0
- package/lib/src/model/query/SurrealQueryBuilder.js +371 -0
- package/lib/src/model/query/fetchInstance.d.ts +24 -0
- package/lib/src/model/query/fetchInstance.js +144 -0
- package/lib/src/model/query/hydration.d.ts +54 -0
- package/lib/src/model/query/hydration.js +214 -0
- package/lib/src/model/query/operations.d.ts +59 -0
- package/lib/src/model/query/operations.js +384 -0
- package/lib/src/model/query/snapshot.d.ts +59 -0
- package/lib/src/model/query/snapshot.js +106 -0
- package/lib/src/model/query/surrealCompiler.d.ts +42 -0
- package/lib/src/model/query/surrealCompiler.js +304 -0
- package/lib/src/model/schema/fromJSONSchema.d.ts +67 -0
- package/lib/src/model/schema/fromJSONSchema.js +264 -0
- package/lib/src/model/schema/metadata.d.ts +15 -0
- package/lib/src/model/schema/metadata.js +112 -0
- package/lib/src/model/subscription.d.ts +47 -0
- package/lib/src/model/subscription.js +231 -0
- package/lib/src/model/transaction.d.ts +27 -0
- package/lib/src/model/transaction.js +32 -0
- package/lib/src/model/types.d.ts +195 -0
- package/lib/src/model/types.js +8 -0
- package/lib/src/model/util.js +7 -7
- package/lib/src/neighbourhood/NeighbourhoodClient.d.ts +5 -1
- package/lib/src/neighbourhood/NeighbourhoodClient.js +263 -181
- package/lib/src/neighbourhood/NeighbourhoodProxy.d.ts +2 -1
- package/lib/src/neighbourhood/NeighbourhoodProxy.js +13 -28
- package/lib/src/neighbourhood/NeighbourhoodProxy.test.js +1 -5
- package/lib/src/perspectives/PerspectiveClient.d.ts +9 -9
- package/lib/src/perspectives/PerspectiveClient.js +316 -240
- package/lib/src/perspectives/PerspectiveProxy.d.ts +54 -92
- package/lib/src/perspectives/PerspectiveProxy.js +803 -444
- package/lib/src/runtime/RuntimeClient.d.ts +5 -1
- package/lib/src/runtime/RuntimeClient.js +412 -238
- package/lib/src/shacl/SHACLFlow.test.js +164 -105
- package/lib/src/shacl/SHACLShape.d.ts +17 -3
- package/lib/src/shacl/SHACLShape.js +127 -90
- package/lib/src/shacl/SHACLShape.test.js +244 -176
- package/lib/src/utils.d.ts +8 -0
- package/lib/src/utils.js +21 -7
- package/package.json +8 -13
- package/src/Ad4mClient.test.ts +1564 -1315
- package/src/Ad4mClient.ts +55 -54
- package/src/Literal.test.ts +45 -26
- package/src/Literal.ts +82 -66
- package/src/SmartLiteral.ts +74 -58
- package/src/agent/AgentClient.ts +144 -99
- package/src/ai/AIClient.ts +409 -359
- package/src/expression/ExpressionClient.ts +142 -96
- package/src/index.ts +1 -1
- package/src/language/LanguageClient.ts +137 -113
- package/src/model/Ad4mModel.test.ts +1128 -647
- package/src/model/Ad4mModel.ts +485 -3247
- package/src/model/IMPROVEMENTS.md +482 -0
- package/src/model/PREDICATE_COLLISION_FIX.md +651 -0
- package/src/model/README.md +317 -0
- package/src/model/SHACL_MUTATION_DECOUPLING.md +356 -0
- package/src/model/SUBSCRIPTION_STRATEGY.md +317 -0
- package/src/model/TC39_STAGE_3_DECORATORS.md +609 -0
- package/src/model/decorators.ts +689 -1046
- package/src/model/mutation.ts +461 -0
- package/src/model/parentUtils.ts +91 -0
- package/src/model/prolog/generatePrologFacts.ts +176 -0
- package/src/model/prolog/index.ts +1 -0
- package/src/model/query/ModelQueryBuilder.ts +173 -0
- package/src/model/query/fetchInstance.ts +181 -0
- package/src/model/query/hydration.ts +273 -0
- package/src/model/query/operations.ts +510 -0
- package/src/model/query/snapshot.ts +126 -0
- package/src/model/query/surrealCompiler.ts +376 -0
- package/src/model/schema/fromJSONSchema.ts +411 -0
- package/src/model/schema/metadata.ts +148 -0
- package/src/model/subscription.ts +307 -0
- package/src/model/transaction.ts +60 -0
- package/src/model/types.ts +207 -0
- package/src/model/util.ts +47 -48
- package/src/neighbourhood/NeighbourhoodClient.ts +393 -251
- package/src/neighbourhood/NeighbourhoodProxy.test.ts +3 -7
- package/src/neighbourhood/NeighbourhoodProxy.ts +58 -40
- package/src/perspectives/PerspectiveClient.ts +779 -540
- package/src/perspectives/PerspectiveProxy.ts +2860 -2053
- package/src/runtime/RuntimeClient.ts +775 -476
- package/src/shacl/SHACLFlow.test.ts +196 -129
- package/src/shacl/SHACLShape.test.ts +284 -190
- package/src/shacl/SHACLShape.ts +225 -172
- package/src/utils.ts +31 -17
- package/tsconfig.json +9 -10
- package/src/model/Subject.ts +0 -159
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mutation helpers for Ad4mModel — extracted Phase 3c.
|
|
3
|
+
*
|
|
4
|
+
* All persistence functions that previously lived as private methods on Ad4mModel.
|
|
5
|
+
* Each takes a `MutationContext` (perspective, id, instance)
|
|
6
|
+
* instead of using `this`, making them independently testable and composable.
|
|
7
|
+
*
|
|
8
|
+
* Functions exported here cover the full write path:
|
|
9
|
+
* - Pure action builders: `generatePropertySetterAction`, `generateRelationAction`
|
|
10
|
+
* - Per-field setters: `setProperty`, `setRelationSetter/Adder/Remover`
|
|
11
|
+
* - Instance-level persistence: `cleanCopy`, `innerUpdate`, `saveInstance`
|
|
12
|
+
*/
|
|
13
|
+
import type { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
|
|
14
|
+
import type { PropertyOptions, RelationOptions } from "./decorators";
|
|
15
|
+
/**
|
|
16
|
+
* Bundles the per-instance state needed by every mutation function,
|
|
17
|
+
* replacing the private `this.#*` fields from Ad4mModel.
|
|
18
|
+
*/
|
|
19
|
+
export interface MutationContext {
|
|
20
|
+
/** The perspective that owns this instance. */
|
|
21
|
+
perspective: PerspectiveProxy;
|
|
22
|
+
/** URI of the instance's root node in the graph (the base expression). */
|
|
23
|
+
id: string;
|
|
24
|
+
/** The Ad4mModel instance itself (for `Object.entries`, prototype lookups, etc.) */
|
|
25
|
+
instance: any;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Builds a `setSingleTarget` action descriptor from property metadata.
|
|
29
|
+
* Throws if the property is a flag, read-only, or has no predicate.
|
|
30
|
+
*/
|
|
31
|
+
export declare function generatePropertySetterAction(key: string, metadata: PropertyOptions): any[];
|
|
32
|
+
/**
|
|
33
|
+
* Builds an `addLink` / `removeLink` / `relationSetter` action descriptor
|
|
34
|
+
* from relation metadata.
|
|
35
|
+
*/
|
|
36
|
+
export declare function generateRelationAction(key: string, metadata: RelationOptions, actionType: "adder" | "remover" | "setter"): any[];
|
|
37
|
+
/**
|
|
38
|
+
* Persists a single scalar property value for `ctx.instance`.
|
|
39
|
+
*
|
|
40
|
+
* Values that already carry a URI scheme are passed through unchanged.
|
|
41
|
+
* Raw scalars are encoded as `literal://` URIs.
|
|
42
|
+
* When a `resolveLanguage` is set, the value is first stored via `createExpression`.
|
|
43
|
+
*/
|
|
44
|
+
export declare function setProperty(ctx: MutationContext, key: string, value: any, batchId?: string): Promise<void>;
|
|
45
|
+
/** Sets (replaces) the full set of targets for a relation. */
|
|
46
|
+
export declare function setRelationSetter(ctx: MutationContext, key: string, value: any, batchId?: string): Promise<void>;
|
|
47
|
+
/** Adds one or more targets to a relation without removing existing ones. */
|
|
48
|
+
export declare function setRelationAdder(ctx: MutationContext, key: string, value: any, batchId?: string): Promise<void>;
|
|
49
|
+
/** Removes one or more targets from a relation. */
|
|
50
|
+
export declare function setRelationRemover(ctx: MutationContext, key: string, value: any, batchId?: string): Promise<void>;
|
|
51
|
+
/**
|
|
52
|
+
* Returns a shallow copy of `instance` with `null`, `undefined`,
|
|
53
|
+
* `author`, and `timestamp` fields omitted.
|
|
54
|
+
*/
|
|
55
|
+
export declare function cleanCopy(instance: any): Record<string, any>;
|
|
56
|
+
/**
|
|
57
|
+
* Iterates all instance fields and persists each one according to its metadata.
|
|
58
|
+
*
|
|
59
|
+
* - Fields with an `.action` shape → dispatched to the matching relation mutator.
|
|
60
|
+
* - Arrays → treated as relation setters (including empty arrays to clear relations).
|
|
61
|
+
* - Scalar values → written via `setProperty` when `setProperties` is `true` and the
|
|
62
|
+
* field is not a relation or a flag.
|
|
63
|
+
*
|
|
64
|
+
* @note `#subjectClassName` was written here in the original implementation but
|
|
65
|
+
* is never read anywhere — the write is intentionally omitted.
|
|
66
|
+
*/
|
|
67
|
+
export declare function innerUpdate(ctx: MutationContext, setProperties?: boolean, batchId?: string): Promise<void>;
|
|
68
|
+
/**
|
|
69
|
+
* Persists `ctx.instance` to `ctx.perspective`.
|
|
70
|
+
*
|
|
71
|
+
* Auto-detects create vs update by checking whether any links already exist
|
|
72
|
+
* for `ctx.id`.
|
|
73
|
+
*
|
|
74
|
+
* - **Create path**: `createSubject` → `innerUpdate(false)` (relations only).
|
|
75
|
+
* - **Update path**: `innerUpdate(true)` (properties + relations).
|
|
76
|
+
*
|
|
77
|
+
* @param ctx - Mutation context (perspective, id, instance).
|
|
78
|
+
* @param batchId - Optional caller-managed batch. When omitted an internal batch
|
|
79
|
+
* is created, committed, and the instance is rehydrated automatically.
|
|
80
|
+
*/
|
|
81
|
+
export declare function saveInstance(ctx: MutationContext, batchId?: string, alreadyExists?: boolean): Promise<void>;
|
|
@@ -0,0 +1,317 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Mutation helpers for Ad4mModel — extracted Phase 3c.
|
|
3
|
+
*
|
|
4
|
+
* All persistence functions that previously lived as private methods on Ad4mModel.
|
|
5
|
+
* Each takes a `MutationContext` (perspective, id, instance)
|
|
6
|
+
* instead of using `this`, making them independently testable and composable.
|
|
7
|
+
*
|
|
8
|
+
* Functions exported here cover the full write path:
|
|
9
|
+
* - Pure action builders: `generatePropertySetterAction`, `generateRelationAction`
|
|
10
|
+
* - Per-field setters: `setProperty`, `setRelationSetter/Adder/Remover`
|
|
11
|
+
* - Instance-level persistence: `cleanCopy`, `innerUpdate`, `saveInstance`
|
|
12
|
+
*/
|
|
13
|
+
import { Literal } from "../Literal";
|
|
14
|
+
import { getPropertiesMetadata, getRelationsMetadata, propertyRegistry, } from "./decorators";
|
|
15
|
+
import { formatSurrealValue } from "./query/surrealCompiler";
|
|
16
|
+
import { fetchInstanceData } from "./query/fetchInstance";
|
|
17
|
+
import { isDirty } from "./query/snapshot";
|
|
18
|
+
import { getModelMetadata as _getModelMetadata } from "./schema/metadata";
|
|
19
|
+
// ── Pure action builders ───────────────────────────────────────────────────
|
|
20
|
+
/**
|
|
21
|
+
* Builds a `setSingleTarget` action descriptor from property metadata.
|
|
22
|
+
* Throws if the property is a flag, read-only, or has no predicate.
|
|
23
|
+
*/
|
|
24
|
+
export function generatePropertySetterAction(key, metadata) {
|
|
25
|
+
if (metadata.flag) {
|
|
26
|
+
throw new Error(`Property "${key}" is a @Flag and cannot be set after creation`);
|
|
27
|
+
}
|
|
28
|
+
if (metadata.readOnly === true) {
|
|
29
|
+
throw new Error(`Property "${key}" is read-only and cannot be written`);
|
|
30
|
+
}
|
|
31
|
+
if (!metadata.through) {
|
|
32
|
+
throw new Error(`Property "${key}" has no 'through' predicate defined`);
|
|
33
|
+
}
|
|
34
|
+
return [
|
|
35
|
+
{
|
|
36
|
+
action: "setSingleTarget",
|
|
37
|
+
source: "this",
|
|
38
|
+
predicate: metadata.through,
|
|
39
|
+
target: "value",
|
|
40
|
+
...(metadata.local && { local: true }),
|
|
41
|
+
},
|
|
42
|
+
];
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* Builds an `addLink` / `removeLink` / `relationSetter` action descriptor
|
|
46
|
+
* from relation metadata.
|
|
47
|
+
*/
|
|
48
|
+
export function generateRelationAction(key, metadata, actionType) {
|
|
49
|
+
if (!metadata.through) {
|
|
50
|
+
throw new Error(`Relation "${key}" has no 'through' predicate defined`);
|
|
51
|
+
}
|
|
52
|
+
const actionMap = {
|
|
53
|
+
adder: "addLink",
|
|
54
|
+
remover: "removeLink",
|
|
55
|
+
setter: "relationSetter",
|
|
56
|
+
};
|
|
57
|
+
return [
|
|
58
|
+
{
|
|
59
|
+
action: actionMap[actionType],
|
|
60
|
+
source: "this",
|
|
61
|
+
predicate: metadata.through,
|
|
62
|
+
target: "value",
|
|
63
|
+
...(metadata.local && { local: true }),
|
|
64
|
+
},
|
|
65
|
+
];
|
|
66
|
+
}
|
|
67
|
+
// ── Property / relation setters ────────────────────────────────────────────
|
|
68
|
+
/**
|
|
69
|
+
* Persists a single scalar property value for `ctx.instance`.
|
|
70
|
+
*
|
|
71
|
+
* Values that already carry a URI scheme are passed through unchanged.
|
|
72
|
+
* Raw scalars are encoded as `literal://` URIs.
|
|
73
|
+
* When a `resolveLanguage` is set, the value is first stored via `createExpression`.
|
|
74
|
+
*/
|
|
75
|
+
export async function setProperty(ctx, key, value, batchId) {
|
|
76
|
+
const proto = Object.getPrototypeOf(ctx.instance);
|
|
77
|
+
const metadata = getPropertiesMetadata(proto.constructor)?.[key];
|
|
78
|
+
if (!metadata) {
|
|
79
|
+
throw new Error(`setProperty called with unknown key "${key}" — ensure the field has a @Property decorator`);
|
|
80
|
+
}
|
|
81
|
+
const actions = generatePropertySetterAction(key, metadata);
|
|
82
|
+
const resolveLanguage = metadata.resolveLanguage;
|
|
83
|
+
// Skip empty/null/undefined to avoid storing invalid empty literals.
|
|
84
|
+
if (value === undefined || value === null || value === "")
|
|
85
|
+
return;
|
|
86
|
+
if (resolveLanguage) {
|
|
87
|
+
value = await ctx.perspective.createExpression(value, resolveLanguage);
|
|
88
|
+
}
|
|
89
|
+
else if (typeof value !== "string" ||
|
|
90
|
+
!/^[a-zA-Z][a-zA-Z0-9+\-.]*:/.test(value)) {
|
|
91
|
+
// Encode raw scalars as literal:// URIs — mirrors Rust's resolve_property_value.
|
|
92
|
+
value = Literal.from(value).toUrl();
|
|
93
|
+
}
|
|
94
|
+
await ctx.perspective.executeAction(actions, ctx.id, [{ name: "value", value }], batchId);
|
|
95
|
+
}
|
|
96
|
+
/** Normalises an Ad4mModel instance to its id URI, passing other values through unchanged. */
|
|
97
|
+
const toId = (v) => v && typeof v === "object" && typeof v.id === "string" ? v.id : v;
|
|
98
|
+
/** Sets (replaces) the full set of targets for a relation. */
|
|
99
|
+
export async function setRelationSetter(ctx, key, value, batchId) {
|
|
100
|
+
const proto = Object.getPrototypeOf(ctx.instance);
|
|
101
|
+
const metadata = getRelationsMetadata(proto.constructor)?.[key];
|
|
102
|
+
if (!metadata) {
|
|
103
|
+
console.warn(`Relation "${key}" has no metadata, skipping`);
|
|
104
|
+
return;
|
|
105
|
+
}
|
|
106
|
+
const actions = generateRelationAction(key, metadata, "setter");
|
|
107
|
+
if (value != null) {
|
|
108
|
+
if (Array.isArray(value)) {
|
|
109
|
+
await ctx.perspective.executeAction(actions, ctx.id, value.map((v) => ({ name: "value", value: toId(v) })), batchId);
|
|
110
|
+
}
|
|
111
|
+
else {
|
|
112
|
+
await ctx.perspective.executeAction(actions, ctx.id, [{ name: "value", value: toId(value) }], batchId);
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
}
|
|
116
|
+
/** Adds one or more targets to a relation without removing existing ones. */
|
|
117
|
+
export async function setRelationAdder(ctx, key, value, batchId) {
|
|
118
|
+
const proto = Object.getPrototypeOf(ctx.instance);
|
|
119
|
+
const metadata = getRelationsMetadata(proto.constructor)?.[key];
|
|
120
|
+
if (!metadata) {
|
|
121
|
+
console.warn(`Relation "${key}" has no metadata, skipping`);
|
|
122
|
+
return;
|
|
123
|
+
}
|
|
124
|
+
const actions = generateRelationAction(key, metadata, "adder");
|
|
125
|
+
if (value != null) {
|
|
126
|
+
if (Array.isArray(value)) {
|
|
127
|
+
await Promise.all(value.map((v) => ctx.perspective.executeAction(actions, ctx.id, [{ name: "value", value: toId(v) }], batchId)));
|
|
128
|
+
}
|
|
129
|
+
else {
|
|
130
|
+
await ctx.perspective.executeAction(actions, ctx.id, [{ name: "value", value: toId(value) }], batchId);
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
/** Removes one or more targets from a relation. */
|
|
135
|
+
export async function setRelationRemover(ctx, key, value, batchId) {
|
|
136
|
+
const proto = Object.getPrototypeOf(ctx.instance);
|
|
137
|
+
const metadata = getRelationsMetadata(proto.constructor)?.[key];
|
|
138
|
+
if (!metadata) {
|
|
139
|
+
console.warn(`Relation "${key}" has no metadata, skipping`);
|
|
140
|
+
return;
|
|
141
|
+
}
|
|
142
|
+
const actions = generateRelationAction(key, metadata, "remover");
|
|
143
|
+
if (value != null) {
|
|
144
|
+
if (Array.isArray(value)) {
|
|
145
|
+
await Promise.all(value.map((v) => ctx.perspective.executeAction(actions, ctx.id, [{ name: "value", value: toId(v) }], batchId)));
|
|
146
|
+
}
|
|
147
|
+
else {
|
|
148
|
+
await ctx.perspective.executeAction(actions, ctx.id, [{ name: "value", value: toId(value) }], batchId);
|
|
149
|
+
}
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
// ── Persistence ────────────────────────────────────────────────────────────
|
|
153
|
+
/**
|
|
154
|
+
* Returns a shallow copy of `instance` with `null`, `undefined`,
|
|
155
|
+
* `author`, and `timestamp` fields omitted.
|
|
156
|
+
*/
|
|
157
|
+
export function cleanCopy(instance) {
|
|
158
|
+
const clean = {};
|
|
159
|
+
for (const [key, value] of Object.entries(instance)) {
|
|
160
|
+
if (value !== undefined &&
|
|
161
|
+
value !== null &&
|
|
162
|
+
key !== "author" &&
|
|
163
|
+
key !== "timestamp") {
|
|
164
|
+
clean[key] = value;
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return clean;
|
|
168
|
+
}
|
|
169
|
+
/**
|
|
170
|
+
* Iterates all instance fields and persists each one according to its metadata.
|
|
171
|
+
*
|
|
172
|
+
* - Fields with an `.action` shape → dispatched to the matching relation mutator.
|
|
173
|
+
* - Arrays → treated as relation setters (including empty arrays to clear relations).
|
|
174
|
+
* - Scalar values → written via `setProperty` when `setProperties` is `true` and the
|
|
175
|
+
* field is not a relation or a flag.
|
|
176
|
+
*
|
|
177
|
+
* @note `#subjectClassName` was written here in the original implementation but
|
|
178
|
+
* is never read anywhere — the write is intentionally omitted.
|
|
179
|
+
*/
|
|
180
|
+
export async function innerUpdate(ctx, setProperties = true, batchId) {
|
|
181
|
+
const proto = Object.getPrototypeOf(ctx.instance);
|
|
182
|
+
for (const [key, value] of Object.entries(ctx.instance)) {
|
|
183
|
+
if (value !== undefined && value !== null) {
|
|
184
|
+
if (value?.action) {
|
|
185
|
+
switch (value.action) {
|
|
186
|
+
case "setter":
|
|
187
|
+
await setRelationSetter(ctx, key, value.value, batchId);
|
|
188
|
+
break;
|
|
189
|
+
case "adder":
|
|
190
|
+
await setRelationAdder(ctx, key, value.value, batchId);
|
|
191
|
+
break;
|
|
192
|
+
case "remover":
|
|
193
|
+
await setRelationRemover(ctx, key, value.value, batchId);
|
|
194
|
+
break;
|
|
195
|
+
default:
|
|
196
|
+
await setRelationSetter(ctx, key, value.value, batchId);
|
|
197
|
+
break;
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
else if (Array.isArray(value)) {
|
|
201
|
+
// All arrays (including empty) treated as relation setters.
|
|
202
|
+
// Skip if the set hasn't changed since the last hydration/save.
|
|
203
|
+
if (!isDirty(ctx.instance, key, value))
|
|
204
|
+
continue;
|
|
205
|
+
await setRelationSetter(ctx, key, value, batchId);
|
|
206
|
+
}
|
|
207
|
+
else if (value !== undefined && value !== null && value !== "") {
|
|
208
|
+
if (setProperties) {
|
|
209
|
+
// Skip relation fields — they are not scalar properties.
|
|
210
|
+
if (getRelationsMetadata(proto.constructor)?.[key])
|
|
211
|
+
continue;
|
|
212
|
+
const propMeta = getPropertiesMetadata(proto.constructor)?.[key];
|
|
213
|
+
// No @Property decorator for this key — skip silently.
|
|
214
|
+
// This covers generated relation methods (addX / removeX / setX)
|
|
215
|
+
// that appear as own enumerable properties on the instance, and
|
|
216
|
+
// base-class fields like `author` / `createdAt` that have no
|
|
217
|
+
// associated predicate.
|
|
218
|
+
if (!propMeta)
|
|
219
|
+
continue;
|
|
220
|
+
// Skip flag fields — flags are immutable, written once by createSubject.
|
|
221
|
+
if (propMeta.flag)
|
|
222
|
+
continue;
|
|
223
|
+
// Skip if the value hasn't changed since the last hydration/save.
|
|
224
|
+
if (!isDirty(ctx.instance, key, value))
|
|
225
|
+
continue;
|
|
226
|
+
await setProperty(ctx, key, value, batchId);
|
|
227
|
+
}
|
|
228
|
+
}
|
|
229
|
+
}
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
/**
|
|
233
|
+
* Persists `ctx.instance` to `ctx.perspective`.
|
|
234
|
+
*
|
|
235
|
+
* Auto-detects create vs update by checking whether any links already exist
|
|
236
|
+
* for `ctx.id`.
|
|
237
|
+
*
|
|
238
|
+
* - **Create path**: `createSubject` → `innerUpdate(false)` (relations only).
|
|
239
|
+
* - **Update path**: `innerUpdate(true)` (properties + relations).
|
|
240
|
+
*
|
|
241
|
+
* @param ctx - Mutation context (perspective, id, instance).
|
|
242
|
+
* @param batchId - Optional caller-managed batch. When omitted an internal batch
|
|
243
|
+
* is created, committed, and the instance is rehydrated automatically.
|
|
244
|
+
*/
|
|
245
|
+
export async function saveInstance(ctx, batchId, alreadyExists) {
|
|
246
|
+
const safeBase = formatSurrealValue(ctx.id);
|
|
247
|
+
// Skip the DB round-trip when the caller already knows the instance was
|
|
248
|
+
// saved once (e.g. the second save() call inside the same uncommitted batch).
|
|
249
|
+
let isNew;
|
|
250
|
+
if (alreadyExists === true) {
|
|
251
|
+
isNew = false;
|
|
252
|
+
}
|
|
253
|
+
else {
|
|
254
|
+
const existingLinks = await ctx.perspective.querySurrealDB(`SELECT 1 FROM link WHERE in.uri = ${safeBase} LIMIT 1`);
|
|
255
|
+
isNew = !existingLinks || existingLinks.length === 0;
|
|
256
|
+
}
|
|
257
|
+
let batchCreatedHere = false;
|
|
258
|
+
if (!batchId) {
|
|
259
|
+
batchId = await ctx.perspective.createBatch();
|
|
260
|
+
batchCreatedHere = true;
|
|
261
|
+
}
|
|
262
|
+
if (isNew) {
|
|
263
|
+
// ── CREATE PATH ─────────────────────────────────────────────────────────
|
|
264
|
+
// Build initialValues from decorator-registered scalar properties only.
|
|
265
|
+
// We must NOT use Object.entries(ctx.instance) because TypeScript `private`
|
|
266
|
+
// fields (e.g. _perspective) are regular enumerable properties at runtime
|
|
267
|
+
// and would cause JSON.stringify to throw a circular-reference error when
|
|
268
|
+
// the PerspectiveProxy's Apollo InMemoryCache is encountered.
|
|
269
|
+
const proto = Object.getPrototypeOf(ctx.instance);
|
|
270
|
+
const ownPropKeys = new Set(Object.keys(propertyRegistry.get(proto.constructor) ?? {}));
|
|
271
|
+
const allPropMeta = getPropertiesMetadata(proto.constructor);
|
|
272
|
+
const initialValues = {};
|
|
273
|
+
for (const key of ownPropKeys) {
|
|
274
|
+
const propMeta = allPropMeta[key];
|
|
275
|
+
if (!propMeta)
|
|
276
|
+
continue;
|
|
277
|
+
if (propMeta.flag)
|
|
278
|
+
continue; // flags handled by constructor actions
|
|
279
|
+
const value = ctx.instance[key];
|
|
280
|
+
if (value !== undefined &&
|
|
281
|
+
value !== null &&
|
|
282
|
+
!(Array.isArray(value) && value.length > 0) &&
|
|
283
|
+
!value?.action) {
|
|
284
|
+
initialValues[key] = value;
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
const className = await ctx.perspective.stringOrTemplateObjectToSubjectClassName(ctx.instance);
|
|
288
|
+
await ctx.perspective.createSubject(className, ctx.id, initialValues, batchId);
|
|
289
|
+
await innerUpdate(ctx, false, batchId);
|
|
290
|
+
// Write inherited properties not present in the class's own SHACL shape.
|
|
291
|
+
// When a derived class uses sh:node to reference its parent shape, createSubject
|
|
292
|
+
// only writes properties defined in the derived shape. Inherited @Property fields
|
|
293
|
+
// (registered on the parent constructor) are silently ignored by the Rust backend.
|
|
294
|
+
// We detect them by comparing the full merged metadata against the own-only registry.
|
|
295
|
+
for (const [key, propMeta] of Object.entries(allPropMeta)) {
|
|
296
|
+
if (ownPropKeys.has(key))
|
|
297
|
+
continue; // already handled by createSubject
|
|
298
|
+
if (propMeta.flag)
|
|
299
|
+
continue; // flags are immutable
|
|
300
|
+
const value = ctx.instance[key];
|
|
301
|
+
if (value !== undefined && value !== null && value !== "") {
|
|
302
|
+
await setProperty(ctx, key, value, batchId);
|
|
303
|
+
}
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
else {
|
|
307
|
+
// ── UPDATE PATH ─────────────────────────────────────────────────────────
|
|
308
|
+
// Instance already exists — update properties and relations only.
|
|
309
|
+
await innerUpdate(ctx, true, batchId);
|
|
310
|
+
}
|
|
311
|
+
if (batchCreatedHere) {
|
|
312
|
+
await ctx.perspective.commitBatch(batchId);
|
|
313
|
+
// Rehydrate the instance so callers see the persisted state.
|
|
314
|
+
const metadata = _getModelMetadata(ctx.instance.constructor);
|
|
315
|
+
await fetchInstanceData(ctx.instance, ctx.perspective, ctx.id, metadata);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ModelMetadata, ParentQuery, ParentQueryByPredicate } from "./types";
|
|
2
|
+
/**
|
|
3
|
+
* Resolves the predicate URI for a parent→child relation, used by
|
|
4
|
+
* `Ad4mModel.create()` and the `useLiveQuery` hooks.
|
|
5
|
+
*
|
|
6
|
+
* If `field` is supplied, that exact relation is looked up on the parent.
|
|
7
|
+
* Otherwise, all forward relations on the parent whose `relatedModel()` factory
|
|
8
|
+
* returns `childCtor` are scanned; the result is used when exactly one match
|
|
9
|
+
* is found.
|
|
10
|
+
*
|
|
11
|
+
* @throws if the field doesn't exist, or inference is ambiguous / impossible.
|
|
12
|
+
*
|
|
13
|
+
* @example
|
|
14
|
+
* // Explicit field
|
|
15
|
+
* resolveParentPredicate(Channel.getModelMetadata(), Poll, 'polls')
|
|
16
|
+
*
|
|
17
|
+
* // Inferred — only one @HasMany on Channel points to Poll
|
|
18
|
+
* resolveParentPredicate(Channel.getModelMetadata(), Poll)
|
|
19
|
+
*/
|
|
20
|
+
export declare function resolveParentPredicate(parentMeta: ModelMetadata, childCtor: (new (...args: any[]) => any) | undefined, field?: string): string;
|
|
21
|
+
/**
|
|
22
|
+
* Normalises a `Query.parent` option to its raw `{ id, predicate }` form.
|
|
23
|
+
*
|
|
24
|
+
* When the model-backed form `{ id, model, field? }` is supplied, the predicate
|
|
25
|
+
* is resolved via {@link resolveParentPredicate}. The raw `{ id, predicate }`
|
|
26
|
+
* form is returned as-is.
|
|
27
|
+
*
|
|
28
|
+
* @param lf - Either the raw or model-backed `parent` query value.
|
|
29
|
+
* @param childCtor - The child model constructor. Required when `field` is
|
|
30
|
+
* omitted (used for inference across `@HasMany` metadata).
|
|
31
|
+
*/
|
|
32
|
+
export declare function normalizeParentQuery(lf: ParentQuery, childCtor?: new (...args: any[]) => any): ParentQueryByPredicate;
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Resolves the predicate URI for a parent→child relation, used by
|
|
3
|
+
* `Ad4mModel.create()` and the `useLiveQuery` hooks.
|
|
4
|
+
*
|
|
5
|
+
* If `field` is supplied, that exact relation is looked up on the parent.
|
|
6
|
+
* Otherwise, all forward relations on the parent whose `relatedModel()` factory
|
|
7
|
+
* returns `childCtor` are scanned; the result is used when exactly one match
|
|
8
|
+
* is found.
|
|
9
|
+
*
|
|
10
|
+
* @throws if the field doesn't exist, or inference is ambiguous / impossible.
|
|
11
|
+
*
|
|
12
|
+
* @example
|
|
13
|
+
* // Explicit field
|
|
14
|
+
* resolveParentPredicate(Channel.getModelMetadata(), Poll, 'polls')
|
|
15
|
+
*
|
|
16
|
+
* // Inferred — only one @HasMany on Channel points to Poll
|
|
17
|
+
* resolveParentPredicate(Channel.getModelMetadata(), Poll)
|
|
18
|
+
*/
|
|
19
|
+
export function resolveParentPredicate(parentMeta, childCtor, field) {
|
|
20
|
+
if (field) {
|
|
21
|
+
const predicate = parentMeta.relations[field]?.predicate;
|
|
22
|
+
if (!predicate) {
|
|
23
|
+
throw new Error(`resolveParentPredicate: field "${field}" not found in parent model ` +
|
|
24
|
+
`"${parentMeta.className}" relations. Check that @HasMany is declared on that field.`);
|
|
25
|
+
}
|
|
26
|
+
return predicate;
|
|
27
|
+
}
|
|
28
|
+
if (!childCtor) {
|
|
29
|
+
throw new Error(`resolveParentPredicate: either "field" or a child model constructor must be provided.`);
|
|
30
|
+
}
|
|
31
|
+
const matches = Object.values(parentMeta.relations).filter((r) => r.direction !== "reverse" && r.relatedModel?.() === childCtor);
|
|
32
|
+
if (matches.length === 1)
|
|
33
|
+
return matches[0].predicate;
|
|
34
|
+
if (matches.length === 0) {
|
|
35
|
+
throw new Error(`resolveParentPredicate: no forward relation pointing to ` +
|
|
36
|
+
`"${childCtor.name ?? String(childCtor)}" found on parent "${parentMeta.className}". ` +
|
|
37
|
+
`Provide "field" explicitly or add a typed @HasMany(() => ${childCtor.name ?? "ChildModel"}) on the parent.`);
|
|
38
|
+
}
|
|
39
|
+
// matches.length > 1
|
|
40
|
+
const fieldNames = Object.entries(parentMeta.relations)
|
|
41
|
+
.filter((r) => r[1].direction !== "reverse" && r[1].relatedModel?.() === childCtor)
|
|
42
|
+
.map(([k]) => k)
|
|
43
|
+
.join(", ");
|
|
44
|
+
throw new Error(`resolveParentPredicate: multiple relations on "${parentMeta.className}" point to ` +
|
|
45
|
+
`"${childCtor.name ?? String(childCtor)}" (fields: ${fieldNames}). ` +
|
|
46
|
+
`Provide "field" to disambiguate.`);
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Normalises a `Query.parent` option to its raw `{ id, predicate }` form.
|
|
50
|
+
*
|
|
51
|
+
* When the model-backed form `{ id, model, field? }` is supplied, the predicate
|
|
52
|
+
* is resolved via {@link resolveParentPredicate}. The raw `{ id, predicate }`
|
|
53
|
+
* form is returned as-is.
|
|
54
|
+
*
|
|
55
|
+
* @param lf - Either the raw or model-backed `parent` query value.
|
|
56
|
+
* @param childCtor - The child model constructor. Required when `field` is
|
|
57
|
+
* omitted (used for inference across `@HasMany` metadata).
|
|
58
|
+
*/
|
|
59
|
+
export function normalizeParentQuery(lf, childCtor) {
|
|
60
|
+
if ("predicate" in lf)
|
|
61
|
+
return lf;
|
|
62
|
+
const predicate = resolveParentPredicate(lf.model.getModelMetadata(), childCtor, lf.field);
|
|
63
|
+
return { id: lf.id, predicate };
|
|
64
|
+
}
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import type { Ad4mModel } from "../Ad4mModel";
|
|
2
|
+
import type { ModelMetadata } from "../Ad4mModel";
|
|
3
|
+
type ModelClass = typeof Ad4mModel & {
|
|
4
|
+
getModelMetadata(): ModelMetadata;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Generate Prolog predicate facts from a model class's SHACL metadata.
|
|
8
|
+
*
|
|
9
|
+
* Given a model class decorated with `@ModelOptions` (and its `@Flag`,
|
|
10
|
+
* `@Property`, `@Collection` decorators), this function emits a string of
|
|
11
|
+
* Prolog clauses that can be prepended to any `perspective.infer()` call.
|
|
12
|
+
*
|
|
13
|
+
* The generated predicates are:
|
|
14
|
+
* - **Instance recognizer** — `modelName(X)` — matches instances of the model
|
|
15
|
+
* - **Property getters** — `modelName_propName(X, Value)` — one per property
|
|
16
|
+
* - **Collection getters** — `modelName_collName(X, Values)` — one per collection
|
|
17
|
+
*
|
|
18
|
+
* @example
|
|
19
|
+
* ```typescript
|
|
20
|
+
* import { generatePrologFacts } from '@coasys/ad4m/model/prolog';
|
|
21
|
+
*
|
|
22
|
+
* const facts = generatePrologFacts(Poll);
|
|
23
|
+
* const result = await perspective.infer(`
|
|
24
|
+
* ${facts}
|
|
25
|
+
* recent_popular_poll(X) :-
|
|
26
|
+
* poll(X),
|
|
27
|
+
* poll_vote_count(X, N), N > 10,
|
|
28
|
+
* poll_created_at(X, T), T > ${yesterday}.
|
|
29
|
+
* `);
|
|
30
|
+
* ```
|
|
31
|
+
*
|
|
32
|
+
* @param ModelClass - A class decorated with `@ModelOptions` that extends `Ad4mModel`
|
|
33
|
+
* @returns A multi-line Prolog string ready for use with `perspective.infer()`
|
|
34
|
+
*/
|
|
35
|
+
export declare function generatePrologFacts(ModelClass: ModelClass): string;
|
|
36
|
+
export {};
|
|
@@ -0,0 +1,139 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Convert a camelCase or PascalCase identifier to snake_case.
|
|
3
|
+
* Examples: "TestPost" -> "test_post", "createdAt" -> "created_at"
|
|
4
|
+
*/
|
|
5
|
+
function toSnakeCase(str) {
|
|
6
|
+
return str
|
|
7
|
+
.replace(/([A-Z])/g, "_$1")
|
|
8
|
+
.toLowerCase()
|
|
9
|
+
.replace(/^_/, "");
|
|
10
|
+
}
|
|
11
|
+
/**
|
|
12
|
+
* Build the instance recognizer clause for a model.
|
|
13
|
+
*
|
|
14
|
+
* Uses @Flag properties (fixed-value predicates) as the primary recognition
|
|
15
|
+
* strategy, since they uniquely identify the type in the graph. Falls back to
|
|
16
|
+
* required properties if no flags are present.
|
|
17
|
+
*
|
|
18
|
+
* Examples:
|
|
19
|
+
* poll(X) :- triple(X, 'flux://entry_type', 'flux://has_poll').
|
|
20
|
+
* note(X) :- triple(X, 'ad4m://title', _).
|
|
21
|
+
*/
|
|
22
|
+
function buildInstanceClause(predicateName, metadata) {
|
|
23
|
+
const props = metadata.properties;
|
|
24
|
+
// Collect flags first — these are the strongest recognizers
|
|
25
|
+
const flags = Object.values(props).filter((p) => p.flag && p.predicate && p.initial);
|
|
26
|
+
if (flags.length > 0) {
|
|
27
|
+
const conditions = flags
|
|
28
|
+
.map((p) => `triple(X, '${p.predicate}', '${p.initial}')`)
|
|
29
|
+
.join(",\n ");
|
|
30
|
+
return `${predicateName}(X) :-\n ${conditions}.`;
|
|
31
|
+
}
|
|
32
|
+
// Fallback: required non-flag properties
|
|
33
|
+
const required = Object.values(props).filter((p) => p.required && p.predicate && !p.flag);
|
|
34
|
+
if (required.length > 0) {
|
|
35
|
+
const conditions = required
|
|
36
|
+
.map((p) => `triple(X, '${p.predicate}', _)`)
|
|
37
|
+
.join(",\n ");
|
|
38
|
+
return `${predicateName}(X) :-\n ${conditions}.`;
|
|
39
|
+
}
|
|
40
|
+
return null;
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Build a property getter clause for a single property.
|
|
44
|
+
*
|
|
45
|
+
* Example:
|
|
46
|
+
* poll_title(X, Value) :- triple(X, 'rdf://title', Value).
|
|
47
|
+
*/
|
|
48
|
+
function buildPropertyClause(modelPredicateName, prop) {
|
|
49
|
+
// Flags are handled by the instance clause, not individual getters
|
|
50
|
+
if (prop.flag)
|
|
51
|
+
return null;
|
|
52
|
+
// No predicate = no clause
|
|
53
|
+
if (!prop.predicate)
|
|
54
|
+
return null;
|
|
55
|
+
const clauseName = `${modelPredicateName}_${toSnakeCase(prop.name)}`;
|
|
56
|
+
return `${clauseName}(X, Value) :- triple(X, '${prop.predicate}', Value).`;
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* Build a collection getter clause.
|
|
60
|
+
*
|
|
61
|
+
* Forward relations (@HasMany / @HasOne):
|
|
62
|
+
* poll_entries(X, Values) :- findall(V, triple(X, 'flux://entry', V), Values).
|
|
63
|
+
*
|
|
64
|
+
* Reverse relations (@BelongsToMany / @BelongsToOne):
|
|
65
|
+
* The link points TO this instance (OtherNode → predicate → ThisNode), so
|
|
66
|
+
* the subject and object are swapped:
|
|
67
|
+
* test_tag_posts(X, Values) :- findall(V, triple(V, 'test://has_tag', X), Values).
|
|
68
|
+
*/
|
|
69
|
+
function buildCollectionClause(modelPredicateName, coll) {
|
|
70
|
+
if (!coll.predicate)
|
|
71
|
+
return null;
|
|
72
|
+
const clauseName = `${modelPredicateName}_${toSnakeCase(coll.name)}`;
|
|
73
|
+
if (coll.direction === "reverse") {
|
|
74
|
+
// Reverse traversal: find nodes whose outgoing predicate link points to X
|
|
75
|
+
return `${clauseName}(X, Values) :- findall(V, triple(V, '${coll.predicate}', X), Values).`;
|
|
76
|
+
}
|
|
77
|
+
return `${clauseName}(X, Values) :- findall(V, triple(X, '${coll.predicate}', V), Values).`;
|
|
78
|
+
}
|
|
79
|
+
/**
|
|
80
|
+
* Generate Prolog predicate facts from a model class's SHACL metadata.
|
|
81
|
+
*
|
|
82
|
+
* Given a model class decorated with `@ModelOptions` (and its `@Flag`,
|
|
83
|
+
* `@Property`, `@Collection` decorators), this function emits a string of
|
|
84
|
+
* Prolog clauses that can be prepended to any `perspective.infer()` call.
|
|
85
|
+
*
|
|
86
|
+
* The generated predicates are:
|
|
87
|
+
* - **Instance recognizer** — `modelName(X)` — matches instances of the model
|
|
88
|
+
* - **Property getters** — `modelName_propName(X, Value)` — one per property
|
|
89
|
+
* - **Collection getters** — `modelName_collName(X, Values)` — one per collection
|
|
90
|
+
*
|
|
91
|
+
* @example
|
|
92
|
+
* ```typescript
|
|
93
|
+
* import { generatePrologFacts } from '@coasys/ad4m/model/prolog';
|
|
94
|
+
*
|
|
95
|
+
* const facts = generatePrologFacts(Poll);
|
|
96
|
+
* const result = await perspective.infer(`
|
|
97
|
+
* ${facts}
|
|
98
|
+
* recent_popular_poll(X) :-
|
|
99
|
+
* poll(X),
|
|
100
|
+
* poll_vote_count(X, N), N > 10,
|
|
101
|
+
* poll_created_at(X, T), T > ${yesterday}.
|
|
102
|
+
* `);
|
|
103
|
+
* ```
|
|
104
|
+
*
|
|
105
|
+
* @param ModelClass - A class decorated with `@ModelOptions` that extends `Ad4mModel`
|
|
106
|
+
* @returns A multi-line Prolog string ready for use with `perspective.infer()`
|
|
107
|
+
*/
|
|
108
|
+
export function generatePrologFacts(ModelClass) {
|
|
109
|
+
const metadata = ModelClass.getModelMetadata();
|
|
110
|
+
const predicateName = toSnakeCase(metadata.className);
|
|
111
|
+
const lines = [];
|
|
112
|
+
lines.push(`% ${metadata.className} — generated Prolog facts`);
|
|
113
|
+
// Instance recognizer
|
|
114
|
+
const instanceClause = buildInstanceClause(predicateName, metadata);
|
|
115
|
+
if (instanceClause) {
|
|
116
|
+
lines.push("");
|
|
117
|
+
lines.push(`% Instance recognizer`);
|
|
118
|
+
lines.push(instanceClause);
|
|
119
|
+
}
|
|
120
|
+
// Property getters
|
|
121
|
+
const propClauses = Object.values(metadata.properties)
|
|
122
|
+
.map((p) => buildPropertyClause(predicateName, p))
|
|
123
|
+
.filter((c) => c !== null);
|
|
124
|
+
if (propClauses.length > 0) {
|
|
125
|
+
lines.push("");
|
|
126
|
+
lines.push(`% Field getters`);
|
|
127
|
+
lines.push(...propClauses);
|
|
128
|
+
}
|
|
129
|
+
// Relation getters
|
|
130
|
+
const collClauses = Object.values(metadata.relations)
|
|
131
|
+
.map((c) => buildCollectionClause(predicateName, c))
|
|
132
|
+
.filter((c) => c !== null);
|
|
133
|
+
if (collClauses.length > 0) {
|
|
134
|
+
lines.push("");
|
|
135
|
+
lines.push(`% Relation getters`);
|
|
136
|
+
lines.push(...collClauses);
|
|
137
|
+
}
|
|
138
|
+
return lines.join("\n");
|
|
139
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generatePrologFacts } from './generatePrologFacts';
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export { generatePrologFacts } from './generatePrologFacts';
|