@coasys/ad4m 0.12.0-rc1-dev.1 → 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 -6046
- package/lib/index.js +5977 -6040
- 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 -2799
- 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 -3249
- 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,411 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Dynamic model creation from JSON Schema.
|
|
3
|
+
*
|
|
4
|
+
* Extracted from Ad4mModel.ts so it can be tested and reused independently.
|
|
5
|
+
* The main entry point — `createModelFromJSONSchema` — takes the `Ad4mModel`
|
|
6
|
+
* class as `BaseClass` to avoid a circular import between this module and the
|
|
7
|
+
* class file.
|
|
8
|
+
*/
|
|
9
|
+
|
|
10
|
+
import {
|
|
11
|
+
Model,
|
|
12
|
+
PropertyOptions,
|
|
13
|
+
propertyRegistry,
|
|
14
|
+
relationRegistry,
|
|
15
|
+
} from "../decorators";
|
|
16
|
+
import { capitalize, propertyNameToSetterName } from "../util";
|
|
17
|
+
|
|
18
|
+
// ── JSON Schema type definitions ────────────────────────────────────────────
|
|
19
|
+
|
|
20
|
+
export interface JSONSchemaProperty {
|
|
21
|
+
type: string | string[];
|
|
22
|
+
items?: JSONSchemaProperty;
|
|
23
|
+
properties?: { [key: string]: JSONSchemaProperty };
|
|
24
|
+
required?: string[];
|
|
25
|
+
"x-ad4m"?: {
|
|
26
|
+
through?: string;
|
|
27
|
+
resolveLanguage?: string;
|
|
28
|
+
local?: boolean;
|
|
29
|
+
readOnly?: boolean;
|
|
30
|
+
initial?: string;
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface JSONSchema {
|
|
35
|
+
$schema?: string;
|
|
36
|
+
title?: string;
|
|
37
|
+
$id?: string;
|
|
38
|
+
type?: string;
|
|
39
|
+
properties?: { [key: string]: JSONSchemaProperty };
|
|
40
|
+
required?: string[];
|
|
41
|
+
"x-ad4m"?: {
|
|
42
|
+
namespace?: string;
|
|
43
|
+
className?: string;
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export interface JSONSchemaToModelOptions {
|
|
48
|
+
name: string;
|
|
49
|
+
namespace?: string;
|
|
50
|
+
predicateTemplate?: string;
|
|
51
|
+
predicateGenerator?: (title: string, property: string) => string;
|
|
52
|
+
propertyMapping?: Record<string, string>;
|
|
53
|
+
resolveLanguage?: string;
|
|
54
|
+
local?: boolean;
|
|
55
|
+
propertyOptions?: Record<string, Partial<PropertyOptions>>;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
// ── Internal schema helpers ─────────────────────────────────────────────────
|
|
59
|
+
|
|
60
|
+
export function normalizeNamespaceString(namespace: string): string {
|
|
61
|
+
if (!namespace) return "";
|
|
62
|
+
if (namespace.includes("://")) {
|
|
63
|
+
const [scheme, rest] = namespace.split("://");
|
|
64
|
+
const path = (rest || "").replace(/\/+$/, "");
|
|
65
|
+
return `${scheme}://${path}`;
|
|
66
|
+
} else {
|
|
67
|
+
return namespace.replace(/\/+$/, "");
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function normalizeSchemaType(
|
|
72
|
+
type?: string | string[],
|
|
73
|
+
): string | undefined {
|
|
74
|
+
if (!type) return undefined;
|
|
75
|
+
if (typeof type === "string") return type;
|
|
76
|
+
if (Array.isArray(type) && type.length > 0) {
|
|
77
|
+
const nonNull = type.find((t) => t !== "null");
|
|
78
|
+
return nonNull || type[0];
|
|
79
|
+
}
|
|
80
|
+
return undefined;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
export function isSchemaType(
|
|
84
|
+
schema: JSONSchemaProperty,
|
|
85
|
+
expectedType: string,
|
|
86
|
+
): boolean {
|
|
87
|
+
return normalizeSchemaType(schema.type) === expectedType;
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
export function isArrayType(schema: JSONSchemaProperty): boolean {
|
|
91
|
+
return isSchemaType(schema, "array");
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
export function isObjectType(schema: JSONSchemaProperty): boolean {
|
|
95
|
+
return isSchemaType(schema, "object");
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
export function isNumericType(schema: JSONSchemaProperty): boolean {
|
|
99
|
+
const normalized = normalizeSchemaType(schema.type);
|
|
100
|
+
return normalized === "number" || normalized === "integer";
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
// ── Predicate + namespace resolution ───────────────────────────────────────
|
|
104
|
+
|
|
105
|
+
export function determineNamespace(
|
|
106
|
+
schema: JSONSchema,
|
|
107
|
+
options: JSONSchemaToModelOptions,
|
|
108
|
+
): string {
|
|
109
|
+
if (options.namespace) return options.namespace;
|
|
110
|
+
if (schema["x-ad4m"]?.namespace) return schema["x-ad4m"].namespace;
|
|
111
|
+
if (schema.title) return `${schema.title.toLowerCase()}://`;
|
|
112
|
+
|
|
113
|
+
if (schema.$id) {
|
|
114
|
+
try {
|
|
115
|
+
const url = new URL(schema.$id);
|
|
116
|
+
const pathParts = url.pathname.split("/").filter((p) => p);
|
|
117
|
+
if (pathParts.length > 0) {
|
|
118
|
+
const lastPart = pathParts[pathParts.length - 1];
|
|
119
|
+
const baseName = lastPart
|
|
120
|
+
.replace(/\.schema\.json$/, "")
|
|
121
|
+
.replace(/\.json$/, "");
|
|
122
|
+
return `${baseName.toLowerCase()}://`;
|
|
123
|
+
}
|
|
124
|
+
} catch {
|
|
125
|
+
// not a valid URL — fall through to error
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
throw new Error(
|
|
130
|
+
`Cannot infer namespace for JSON Schema. Please provide one of:\n` +
|
|
131
|
+
` - options.namespace\n` +
|
|
132
|
+
` - schema["x-ad4m"].namespace\n` +
|
|
133
|
+
` - schema.title\n` +
|
|
134
|
+
` - valid schema.$id`,
|
|
135
|
+
);
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export function determinePredicate(
|
|
139
|
+
schema: JSONSchema,
|
|
140
|
+
propertyName: string,
|
|
141
|
+
propertySchema: JSONSchemaProperty,
|
|
142
|
+
namespace: string,
|
|
143
|
+
options: JSONSchemaToModelOptions,
|
|
144
|
+
): string {
|
|
145
|
+
if (options.propertyMapping?.[propertyName]) {
|
|
146
|
+
return options.propertyMapping[propertyName];
|
|
147
|
+
}
|
|
148
|
+
if (propertySchema["x-ad4m"]?.through) {
|
|
149
|
+
return propertySchema["x-ad4m"].through;
|
|
150
|
+
}
|
|
151
|
+
if (options.predicateTemplate) {
|
|
152
|
+
const normalizedNs = normalizeNamespaceString(namespace);
|
|
153
|
+
const [scheme, rest] = normalizedNs.includes("://")
|
|
154
|
+
? normalizedNs.split("://")
|
|
155
|
+
: ["", normalizedNs];
|
|
156
|
+
const nsNoScheme = rest || "";
|
|
157
|
+
return options.predicateTemplate
|
|
158
|
+
.replace("${namespace}", nsNoScheme)
|
|
159
|
+
.replace("${scheme}", scheme)
|
|
160
|
+
.replace("${ns}", nsNoScheme)
|
|
161
|
+
.replace("${title}", schema.title || "")
|
|
162
|
+
.replace("${property}", propertyName);
|
|
163
|
+
}
|
|
164
|
+
if (options.predicateGenerator) {
|
|
165
|
+
return options.predicateGenerator(schema.title || "", propertyName);
|
|
166
|
+
}
|
|
167
|
+
const normalizedNs = normalizeNamespaceString(namespace);
|
|
168
|
+
if (normalizedNs.includes("://")) {
|
|
169
|
+
return `${normalizedNs}${propertyName}`;
|
|
170
|
+
} else {
|
|
171
|
+
return `${normalizedNs}://${propertyName}`;
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
export function getPropertyOption(
|
|
176
|
+
propertyName: string,
|
|
177
|
+
propertySchema: JSONSchemaProperty,
|
|
178
|
+
options: JSONSchemaToModelOptions,
|
|
179
|
+
optionName: keyof PropertyOptions,
|
|
180
|
+
defaultValue?: any,
|
|
181
|
+
): any {
|
|
182
|
+
if (options.propertyOptions?.[propertyName]?.[optionName] !== undefined) {
|
|
183
|
+
return options.propertyOptions[propertyName][optionName];
|
|
184
|
+
}
|
|
185
|
+
if (
|
|
186
|
+
propertySchema["x-ad4m"]?.[
|
|
187
|
+
optionName as keyof JSONSchemaProperty["x-ad4m"]
|
|
188
|
+
] !== undefined
|
|
189
|
+
) {
|
|
190
|
+
return propertySchema["x-ad4m"][
|
|
191
|
+
optionName as keyof JSONSchemaProperty["x-ad4m"]
|
|
192
|
+
];
|
|
193
|
+
}
|
|
194
|
+
if (options[optionName as keyof JSONSchemaToModelOptions] !== undefined) {
|
|
195
|
+
return options[optionName as keyof JSONSchemaToModelOptions];
|
|
196
|
+
}
|
|
197
|
+
return defaultValue;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
export function getDefaultValueForType(type?: string): any {
|
|
201
|
+
switch (type) {
|
|
202
|
+
case "string":
|
|
203
|
+
return "";
|
|
204
|
+
case "number":
|
|
205
|
+
return 0;
|
|
206
|
+
case "integer":
|
|
207
|
+
return 0;
|
|
208
|
+
case "boolean":
|
|
209
|
+
return false;
|
|
210
|
+
case "array":
|
|
211
|
+
return [];
|
|
212
|
+
case "object":
|
|
213
|
+
return {};
|
|
214
|
+
default:
|
|
215
|
+
return "";
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
// ── Main factory ────────────────────────────────────────────────────────────
|
|
220
|
+
|
|
221
|
+
/**
|
|
222
|
+
* Creates an Ad4mModel subclass dynamically from a JSON Schema definition.
|
|
223
|
+
*
|
|
224
|
+
* Takes `BaseClass` as a parameter (rather than importing `Ad4mModel` directly)
|
|
225
|
+
* to avoid a circular dependency between this module and `Ad4mModel.ts`.
|
|
226
|
+
*
|
|
227
|
+
* `Ad4mModel.fromJSONSchema()` is a thin wrapper that calls this with `this`.
|
|
228
|
+
*/
|
|
229
|
+
export function createModelFromJSONSchema(
|
|
230
|
+
BaseClass: any,
|
|
231
|
+
schema: JSONSchema,
|
|
232
|
+
options: JSONSchemaToModelOptions,
|
|
233
|
+
): any {
|
|
234
|
+
if (
|
|
235
|
+
schema?.properties &&
|
|
236
|
+
Object.prototype.hasOwnProperty.call(schema.properties, "author")
|
|
237
|
+
) {
|
|
238
|
+
throw new Error(
|
|
239
|
+
'JSON Schema must not define a top-level "author" property because Ad4mModel already exposes it. Please rename the property (e.g., "writer").',
|
|
240
|
+
);
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
const namespace = determineNamespace(schema, options);
|
|
244
|
+
const DynamicModelClass = class extends BaseClass {};
|
|
245
|
+
|
|
246
|
+
if (!options.name || options.name.trim() === "") {
|
|
247
|
+
throw new Error("options.name is required and cannot be empty");
|
|
248
|
+
}
|
|
249
|
+
(DynamicModelClass as any).className = options.name;
|
|
250
|
+
(DynamicModelClass.prototype as any).className = options.name;
|
|
251
|
+
|
|
252
|
+
const properties: any = {};
|
|
253
|
+
const relations: any = {};
|
|
254
|
+
|
|
255
|
+
if (schema.properties) {
|
|
256
|
+
for (const [propertyName, propertySchema] of Object.entries(
|
|
257
|
+
schema.properties,
|
|
258
|
+
)) {
|
|
259
|
+
const predicate = determinePredicate(
|
|
260
|
+
schema,
|
|
261
|
+
propertyName,
|
|
262
|
+
propertySchema,
|
|
263
|
+
namespace,
|
|
264
|
+
options,
|
|
265
|
+
);
|
|
266
|
+
const isRequired = schema.required?.includes(propertyName) || false;
|
|
267
|
+
const propertyType = normalizeSchemaType(propertySchema.type);
|
|
268
|
+
const isArray = isArrayType(propertySchema);
|
|
269
|
+
|
|
270
|
+
if (isArray) {
|
|
271
|
+
relations[propertyName] = {
|
|
272
|
+
through: predicate,
|
|
273
|
+
local: getPropertyOption(
|
|
274
|
+
propertyName,
|
|
275
|
+
propertySchema,
|
|
276
|
+
options,
|
|
277
|
+
"local",
|
|
278
|
+
),
|
|
279
|
+
};
|
|
280
|
+
|
|
281
|
+
Object.defineProperty(DynamicModelClass.prototype, propertyName, {
|
|
282
|
+
configurable: true,
|
|
283
|
+
writable: true,
|
|
284
|
+
value: [],
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
const adderName = `add${capitalize(propertyName)}`;
|
|
288
|
+
const removerName = `remove${capitalize(propertyName)}`;
|
|
289
|
+
const setterName = `set${capitalize(propertyName)}`;
|
|
290
|
+
|
|
291
|
+
(DynamicModelClass.prototype as any)[adderName] = function () {};
|
|
292
|
+
(DynamicModelClass.prototype as any)[removerName] = function () {};
|
|
293
|
+
(DynamicModelClass.prototype as any)[setterName] = function () {};
|
|
294
|
+
} else {
|
|
295
|
+
let resolveLanguage = getPropertyOption(
|
|
296
|
+
propertyName,
|
|
297
|
+
propertySchema,
|
|
298
|
+
options,
|
|
299
|
+
"resolveLanguage",
|
|
300
|
+
);
|
|
301
|
+
if (!resolveLanguage && options.resolveLanguage) {
|
|
302
|
+
resolveLanguage = options.resolveLanguage;
|
|
303
|
+
}
|
|
304
|
+
const local = getPropertyOption(
|
|
305
|
+
propertyName,
|
|
306
|
+
propertySchema,
|
|
307
|
+
options,
|
|
308
|
+
"local",
|
|
309
|
+
);
|
|
310
|
+
const readOnly = getPropertyOption(
|
|
311
|
+
propertyName,
|
|
312
|
+
propertySchema,
|
|
313
|
+
options,
|
|
314
|
+
"readOnly",
|
|
315
|
+
);
|
|
316
|
+
let initial = getPropertyOption(
|
|
317
|
+
propertyName,
|
|
318
|
+
propertySchema,
|
|
319
|
+
options,
|
|
320
|
+
"initial",
|
|
321
|
+
);
|
|
322
|
+
|
|
323
|
+
if (isObjectType(propertySchema) && !resolveLanguage) {
|
|
324
|
+
resolveLanguage = "literal";
|
|
325
|
+
console.warn(
|
|
326
|
+
`Property "${propertyName}" is an object type. It will be stored as JSON. Consider flattening complex objects for better semantic querying.`,
|
|
327
|
+
);
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
// No auto-assignment for numeric types — resolveLanguage === undefined
|
|
331
|
+
// is now the implicit 'literal' default throughout the model layer.
|
|
332
|
+
|
|
333
|
+
if (isRequired && !initial) {
|
|
334
|
+
if (isObjectType(propertySchema)) {
|
|
335
|
+
initial = "literal://json:{}";
|
|
336
|
+
} else {
|
|
337
|
+
initial = "ad4m://undefined";
|
|
338
|
+
}
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
properties[propertyName] = {
|
|
342
|
+
through: predicate,
|
|
343
|
+
required: isRequired,
|
|
344
|
+
...(readOnly && { readOnly }),
|
|
345
|
+
...(resolveLanguage && { resolveLanguage }),
|
|
346
|
+
...(local !== undefined && { local }),
|
|
347
|
+
...(initial && { initial }),
|
|
348
|
+
};
|
|
349
|
+
|
|
350
|
+
Object.defineProperty(DynamicModelClass.prototype, propertyName, {
|
|
351
|
+
configurable: true,
|
|
352
|
+
writable: true,
|
|
353
|
+
value: getDefaultValueForType(propertyType),
|
|
354
|
+
});
|
|
355
|
+
|
|
356
|
+
if (!readOnly) {
|
|
357
|
+
const setterName = propertyNameToSetterName(propertyName);
|
|
358
|
+
(DynamicModelClass.prototype as any)[setterName] = function () {};
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// Ensure at least one property has an initial value (needed for a valid SDNA constructor)
|
|
365
|
+
const hasPropertyWithInitial = Object.values(properties).some(
|
|
366
|
+
(prop: any) => prop.initial,
|
|
367
|
+
);
|
|
368
|
+
|
|
369
|
+
if (!hasPropertyWithInitial) {
|
|
370
|
+
const typeProperty = `ad4m://type`;
|
|
371
|
+
let typeValue: string;
|
|
372
|
+
if (namespace.includes("://")) {
|
|
373
|
+
const [scheme, rest] = namespace.split("://");
|
|
374
|
+
const path = (rest || "").replace(/\/+$/, "");
|
|
375
|
+
typeValue = path
|
|
376
|
+
? `${scheme}://${path}/instance`
|
|
377
|
+
: `${scheme}://instance`;
|
|
378
|
+
} else {
|
|
379
|
+
typeValue = `${namespace.replace(/\/+$/, "")}/instance`;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
properties["__ad4m_type"] = {
|
|
383
|
+
through: typeProperty,
|
|
384
|
+
required: true,
|
|
385
|
+
readOnly: true,
|
|
386
|
+
initial: typeValue,
|
|
387
|
+
flag: true,
|
|
388
|
+
};
|
|
389
|
+
|
|
390
|
+
Object.defineProperty(DynamicModelClass.prototype, "__ad4m_type", {
|
|
391
|
+
configurable: true,
|
|
392
|
+
writable: false,
|
|
393
|
+
value: typeValue,
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
console.warn(
|
|
397
|
+
`No properties with initial values found. Added automatic type flag: ${typeProperty} = ${typeValue}`,
|
|
398
|
+
);
|
|
399
|
+
}
|
|
400
|
+
|
|
401
|
+
propertyRegistry.set(DynamicModelClass, properties);
|
|
402
|
+
relationRegistry.set(DynamicModelClass, relations);
|
|
403
|
+
|
|
404
|
+
(DynamicModelClass.prototype as any).__jsonSchema = schema;
|
|
405
|
+
(DynamicModelClass.prototype as any).__jsonSchemaOptions = options;
|
|
406
|
+
|
|
407
|
+
const ModelDecorator = Model({ name: options.name });
|
|
408
|
+
ModelDecorator(DynamicModelClass);
|
|
409
|
+
|
|
410
|
+
return DynamicModelClass;
|
|
411
|
+
}
|
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Metadata extraction helper for Ad4mModel.
|
|
3
|
+
*
|
|
4
|
+
* - `getModelMetadata(ctor)` — builds a {@link ModelMetadata} descriptor from
|
|
5
|
+
* the decorator registries, or falls back to the attached JSON Schema.
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
import { getPropertiesMetadata, getRelationsMetadata } from "../decorators";
|
|
9
|
+
import type { PropertyOptions, RelationOptions } from "../decorators";
|
|
10
|
+
import {
|
|
11
|
+
isArrayType,
|
|
12
|
+
determinePredicate,
|
|
13
|
+
determineNamespace,
|
|
14
|
+
} from "./fromJSONSchema";
|
|
15
|
+
import type { JSONSchemaProperty } from "./fromJSONSchema";
|
|
16
|
+
import type {
|
|
17
|
+
ModelMetadata,
|
|
18
|
+
PropertyMetadata,
|
|
19
|
+
RelationMetadata,
|
|
20
|
+
} from "../types";
|
|
21
|
+
|
|
22
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
23
|
+
// getModelMetadataForClass
|
|
24
|
+
// ─────────────────────────────────────────────────────────────────────────────
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Builds a {@link ModelMetadata} descriptor for `ctor` from its decorator
|
|
28
|
+
* registries, falling back to the attached JSON Schema if no decorators are
|
|
29
|
+
* found.
|
|
30
|
+
*
|
|
31
|
+
* The matching `Ad4mModel.getModelMetadata()` static method delegates here.
|
|
32
|
+
*/
|
|
33
|
+
export function getModelMetadata(ctor: any): ModelMetadata {
|
|
34
|
+
const prototype = ctor.prototype as any;
|
|
35
|
+
|
|
36
|
+
if (!prototype.className || prototype.className === "Ad4mModel") {
|
|
37
|
+
throw new Error("Model class must be decorated with @Model");
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
const className: string = prototype.className;
|
|
41
|
+
|
|
42
|
+
// ── Extract from decorator registries ──────────────────────────────────────
|
|
43
|
+
const propertiesMetadata: Record<string, PropertyMetadata> = {};
|
|
44
|
+
const prototypeProperties = getPropertiesMetadata(ctor);
|
|
45
|
+
|
|
46
|
+
for (const [propertyName, opts] of Object.entries(prototypeProperties)) {
|
|
47
|
+
const options = opts as PropertyOptions & {
|
|
48
|
+
required?: boolean;
|
|
49
|
+
flag?: boolean;
|
|
50
|
+
};
|
|
51
|
+
propertiesMetadata[propertyName] = {
|
|
52
|
+
name: propertyName,
|
|
53
|
+
predicate: options.through || "",
|
|
54
|
+
required: options.required || false,
|
|
55
|
+
...(options.readOnly !== undefined && { readOnly: options.readOnly }),
|
|
56
|
+
...(options.initial !== undefined && { initial: options.initial }),
|
|
57
|
+
...(options.resolveLanguage !== undefined && {
|
|
58
|
+
resolveLanguage: options.resolveLanguage,
|
|
59
|
+
}),
|
|
60
|
+
...(options.getter !== undefined && { getter: options.getter }),
|
|
61
|
+
...(options.local !== undefined && { local: options.local }),
|
|
62
|
+
...(options.transform !== undefined && { transform: options.transform }),
|
|
63
|
+
...(options.flag !== undefined && { flag: options.flag }),
|
|
64
|
+
};
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
const relationsMetadata: Record<string, RelationMetadata> = {};
|
|
68
|
+
const prototypeRelations = getRelationsMetadata(ctor);
|
|
69
|
+
|
|
70
|
+
for (const [relationName, opts] of Object.entries(prototypeRelations)) {
|
|
71
|
+
const options = opts as RelationOptions;
|
|
72
|
+
relationsMetadata[relationName] = {
|
|
73
|
+
name: relationName,
|
|
74
|
+
predicate: options.through || "",
|
|
75
|
+
...(options.local !== undefined && { local: options.local }),
|
|
76
|
+
...(options.getter !== undefined && { getter: options.getter }),
|
|
77
|
+
...((opts as any).direction !== undefined && {
|
|
78
|
+
direction: (opts as any).direction,
|
|
79
|
+
}),
|
|
80
|
+
...((opts as any).maxCount !== undefined && {
|
|
81
|
+
maxCount: (opts as any).maxCount,
|
|
82
|
+
}),
|
|
83
|
+
...((opts as any).relatedModel !== undefined && {
|
|
84
|
+
relatedModel: (opts as any).relatedModel,
|
|
85
|
+
}),
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// ── Fallback: derive from attached JSON Schema if registries are empty ──────
|
|
90
|
+
const hasMetadata =
|
|
91
|
+
Object.keys(propertiesMetadata).length > 0 ||
|
|
92
|
+
Object.keys(relationsMetadata).length > 0;
|
|
93
|
+
|
|
94
|
+
if (!hasMetadata && prototype.__jsonSchema) {
|
|
95
|
+
const schema = prototype.__jsonSchema;
|
|
96
|
+
const schemaOptions = prototype.__jsonSchemaOptions || {};
|
|
97
|
+
|
|
98
|
+
if (schema.properties) {
|
|
99
|
+
for (const [propertyName, propertySchema] of Object.entries(
|
|
100
|
+
schema.properties,
|
|
101
|
+
)) {
|
|
102
|
+
const isArray = isArrayType(propertySchema as JSONSchemaProperty);
|
|
103
|
+
const predicate = determinePredicate(
|
|
104
|
+
schema,
|
|
105
|
+
propertyName,
|
|
106
|
+
propertySchema as JSONSchemaProperty,
|
|
107
|
+
determineNamespace(schema, schemaOptions),
|
|
108
|
+
schemaOptions,
|
|
109
|
+
);
|
|
110
|
+
|
|
111
|
+
if (isArray) {
|
|
112
|
+
relationsMetadata[propertyName] = {
|
|
113
|
+
name: propertyName,
|
|
114
|
+
predicate,
|
|
115
|
+
...((propertySchema as any)["x-ad4m"]?.local !== undefined && {
|
|
116
|
+
local: (propertySchema as any)["x-ad4m"].local,
|
|
117
|
+
}),
|
|
118
|
+
};
|
|
119
|
+
} else {
|
|
120
|
+
propertiesMetadata[propertyName] = {
|
|
121
|
+
name: propertyName,
|
|
122
|
+
predicate,
|
|
123
|
+
required: schema.required?.includes(propertyName) || false,
|
|
124
|
+
...((propertySchema as any)["x-ad4m"]?.readOnly && {
|
|
125
|
+
readOnly: true,
|
|
126
|
+
}),
|
|
127
|
+
...((propertySchema as any)["x-ad4m"]?.resolveLanguage && {
|
|
128
|
+
resolveLanguage: (propertySchema as any)["x-ad4m"]
|
|
129
|
+
.resolveLanguage,
|
|
130
|
+
}),
|
|
131
|
+
...((propertySchema as any)["x-ad4m"]?.initial && {
|
|
132
|
+
initial: (propertySchema as any)["x-ad4m"].initial,
|
|
133
|
+
}),
|
|
134
|
+
...((propertySchema as any)["x-ad4m"]?.local !== undefined && {
|
|
135
|
+
local: (propertySchema as any)["x-ad4m"].local,
|
|
136
|
+
}),
|
|
137
|
+
};
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
className,
|
|
145
|
+
properties: propertiesMetadata,
|
|
146
|
+
relations: relationsMetadata,
|
|
147
|
+
};
|
|
148
|
+
}
|