@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
|
@@ -8,18 +8,46 @@ var __metadata = (this && this.__metadata) || function (k, v) {
|
|
|
8
8
|
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
9
|
};
|
|
10
10
|
import { Ad4mModel } from "./Ad4mModel";
|
|
11
|
-
import {
|
|
11
|
+
import { Model, Property, HasMany, Flag } from "./decorators";
|
|
12
|
+
import { Literal } from "../Literal";
|
|
13
|
+
import { resolveParentPredicate, normalizeParentQuery } from "./parentUtils";
|
|
14
|
+
// ── Shared test fixtures ──────────────────────────────────────────────────────
|
|
15
|
+
// Defined once at module level — used by queryToSurrealQL,
|
|
16
|
+
// instancesFromSurrealResult, and hydration describe blocks alike.
|
|
17
|
+
let Recipe = class Recipe extends Ad4mModel {
|
|
18
|
+
constructor() {
|
|
19
|
+
super(...arguments);
|
|
20
|
+
this.name = "";
|
|
21
|
+
this.rating = 0;
|
|
22
|
+
this.ingredients = [];
|
|
23
|
+
}
|
|
24
|
+
};
|
|
25
|
+
__decorate([
|
|
26
|
+
Property({ through: "recipe://name", required: true }),
|
|
27
|
+
__metadata("design:type", String)
|
|
28
|
+
], Recipe.prototype, "name", void 0);
|
|
29
|
+
__decorate([
|
|
30
|
+
Property({ through: "recipe://rating", required: true }),
|
|
31
|
+
__metadata("design:type", Number)
|
|
32
|
+
], Recipe.prototype, "rating", void 0);
|
|
33
|
+
__decorate([
|
|
34
|
+
HasMany({ through: "recipe://ingredient" }),
|
|
35
|
+
__metadata("design:type", Array)
|
|
36
|
+
], Recipe.prototype, "ingredients", void 0);
|
|
37
|
+
Recipe = __decorate([
|
|
38
|
+
Model({ name: "Recipe" })
|
|
39
|
+
], Recipe);
|
|
12
40
|
describe("Ad4mModel.getModelMetadata()", () => {
|
|
13
41
|
it("should extract basic model metadata with className", () => {
|
|
14
42
|
let SimpleModel = class SimpleModel extends Ad4mModel {
|
|
15
43
|
};
|
|
16
44
|
SimpleModel = __decorate([
|
|
17
|
-
|
|
45
|
+
Model({ name: "SimpleModel" })
|
|
18
46
|
], SimpleModel);
|
|
19
47
|
const metadata = SimpleModel.getModelMetadata();
|
|
20
48
|
expect(metadata.className).toBe("SimpleModel");
|
|
21
49
|
expect(metadata.properties).toEqual({});
|
|
22
|
-
expect(metadata.
|
|
50
|
+
expect(metadata.relations).toEqual({});
|
|
23
51
|
});
|
|
24
52
|
it("should extract property metadata with all fields", () => {
|
|
25
53
|
let PropertyModel = class PropertyModel extends Ad4mModel {
|
|
@@ -32,15 +60,19 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
32
60
|
}
|
|
33
61
|
};
|
|
34
62
|
__decorate([
|
|
35
|
-
Property({
|
|
63
|
+
Property({
|
|
64
|
+
through: "test://name",
|
|
65
|
+
resolveLanguage: "literal",
|
|
66
|
+
required: true,
|
|
67
|
+
}),
|
|
36
68
|
__metadata("design:type", String)
|
|
37
69
|
], PropertyModel.prototype, "name", void 0);
|
|
38
70
|
__decorate([
|
|
39
|
-
|
|
71
|
+
Property({ through: "test://optional" }),
|
|
40
72
|
__metadata("design:type", String)
|
|
41
73
|
], PropertyModel.prototype, "optional", void 0);
|
|
42
74
|
__decorate([
|
|
43
|
-
|
|
75
|
+
Property({ through: "test://readonly", readOnly: true }),
|
|
44
76
|
__metadata("design:type", String)
|
|
45
77
|
], PropertyModel.prototype, "readonly", void 0);
|
|
46
78
|
__decorate([
|
|
@@ -48,7 +80,7 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
48
80
|
__metadata("design:type", String)
|
|
49
81
|
], PropertyModel.prototype, "type", void 0);
|
|
50
82
|
PropertyModel = __decorate([
|
|
51
|
-
|
|
83
|
+
Model({ name: "PropertyModel" })
|
|
52
84
|
], PropertyModel);
|
|
53
85
|
const metadata = PropertyModel.getModelMetadata();
|
|
54
86
|
// Should have 4 properties
|
|
@@ -56,59 +88,46 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
56
88
|
// Verify "name" property
|
|
57
89
|
expect(metadata.properties.name.predicate).toBe("test://name");
|
|
58
90
|
expect(metadata.properties.name.required).toBe(true);
|
|
59
|
-
expect(metadata.properties.name.
|
|
91
|
+
expect(metadata.properties.name.readOnly).toBeFalsy();
|
|
60
92
|
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
61
93
|
// Verify "optional" property
|
|
62
94
|
expect(metadata.properties.optional.predicate).toBe("test://optional");
|
|
63
|
-
expect(metadata.properties.optional.
|
|
95
|
+
expect(metadata.properties.optional.readOnly).toBeFalsy();
|
|
64
96
|
// Verify "readonly" property
|
|
65
97
|
expect(metadata.properties.readonly.predicate).toBe("test://readonly");
|
|
66
|
-
expect(metadata.properties.readonly.
|
|
67
|
-
expect(metadata.properties.readonly.prologGetter).toBe("custom_getter");
|
|
98
|
+
expect(metadata.properties.readonly.readOnly).toBe(true);
|
|
68
99
|
// Verify "type" property (flag)
|
|
69
100
|
expect(metadata.properties.type.predicate).toBe("test://type");
|
|
70
101
|
expect(metadata.properties.type.flag).toBe(true);
|
|
71
102
|
expect(metadata.properties.type.initial).toBe("test://flag");
|
|
72
103
|
});
|
|
73
|
-
it("should extract collection metadata
|
|
104
|
+
it("should extract collection metadata", () => {
|
|
74
105
|
let CollectionModel = class CollectionModel extends Ad4mModel {
|
|
75
106
|
constructor() {
|
|
76
107
|
super(...arguments);
|
|
77
108
|
this.items = [];
|
|
78
|
-
this.filtered = [];
|
|
79
109
|
this.local = [];
|
|
80
110
|
}
|
|
81
111
|
};
|
|
82
112
|
__decorate([
|
|
83
|
-
|
|
113
|
+
HasMany({ through: "test://items" }),
|
|
84
114
|
__metadata("design:type", Array)
|
|
85
115
|
], CollectionModel.prototype, "items", void 0);
|
|
86
116
|
__decorate([
|
|
87
|
-
|
|
88
|
-
through: "test://filtered",
|
|
89
|
-
where: { condition: "triple(Target, 'test://active', 'true')" }
|
|
90
|
-
}),
|
|
91
|
-
__metadata("design:type", Array)
|
|
92
|
-
], CollectionModel.prototype, "filtered", void 0);
|
|
93
|
-
__decorate([
|
|
94
|
-
Collection({ through: "test://local", local: true }),
|
|
117
|
+
HasMany({ through: "test://local", local: true }),
|
|
95
118
|
__metadata("design:type", Array)
|
|
96
119
|
], CollectionModel.prototype, "local", void 0);
|
|
97
120
|
CollectionModel = __decorate([
|
|
98
|
-
|
|
121
|
+
Model({ name: "CollectionModel" })
|
|
99
122
|
], CollectionModel);
|
|
100
123
|
const metadata = CollectionModel.getModelMetadata();
|
|
101
|
-
// Should have
|
|
102
|
-
expect(Object.keys(metadata.
|
|
124
|
+
// Should have 2 collections
|
|
125
|
+
expect(Object.keys(metadata.relations)).toHaveLength(2);
|
|
103
126
|
// Verify "items" collection
|
|
104
|
-
expect(metadata.
|
|
105
|
-
expect(metadata.collections.items.where).toBeUndefined();
|
|
106
|
-
// Verify "filtered" collection
|
|
107
|
-
expect(metadata.collections.filtered.predicate).toBe("test://filtered");
|
|
108
|
-
expect(metadata.collections.filtered.where?.condition).toBe("triple(Target, 'test://active', 'true')");
|
|
127
|
+
expect(metadata.relations.items.predicate).toBe("test://items");
|
|
109
128
|
// Verify "local" collection
|
|
110
|
-
expect(metadata.
|
|
111
|
-
expect(metadata.
|
|
129
|
+
expect(metadata.relations.local.predicate).toBe("test://local");
|
|
130
|
+
expect(metadata.relations.local.local).toBe(true);
|
|
112
131
|
});
|
|
113
132
|
it("should extract transform function from property metadata", () => {
|
|
114
133
|
let TransformModel = class TransformModel extends Ad4mModel {
|
|
@@ -118,14 +137,14 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
118
137
|
}
|
|
119
138
|
};
|
|
120
139
|
__decorate([
|
|
121
|
-
|
|
140
|
+
Property({
|
|
122
141
|
through: "test://data",
|
|
123
|
-
transform: (value) => value.toUpperCase()
|
|
142
|
+
transform: (value) => value.toUpperCase(),
|
|
124
143
|
}),
|
|
125
144
|
__metadata("design:type", String)
|
|
126
145
|
], TransformModel.prototype, "data", void 0);
|
|
127
146
|
TransformModel = __decorate([
|
|
128
|
-
|
|
147
|
+
Model({ name: "TransformModel" })
|
|
129
148
|
], TransformModel);
|
|
130
149
|
const metadata = TransformModel.getModelMetadata();
|
|
131
150
|
// Assert transform is a function
|
|
@@ -134,35 +153,32 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
134
153
|
const transformed = metadata.properties.data.transform("test");
|
|
135
154
|
expect(transformed).toBe("TEST");
|
|
136
155
|
});
|
|
137
|
-
it("should extract custom getter
|
|
156
|
+
it("should extract custom SurrealQL getter from property metadata", () => {
|
|
138
157
|
let CustomModel = class CustomModel extends Ad4mModel {
|
|
139
158
|
constructor() {
|
|
140
159
|
super(...arguments);
|
|
141
|
-
this.computed =
|
|
160
|
+
this.computed = "";
|
|
142
161
|
}
|
|
143
162
|
};
|
|
144
163
|
__decorate([
|
|
145
|
-
|
|
164
|
+
Property({
|
|
146
165
|
through: "test://computed",
|
|
147
|
-
|
|
148
|
-
prologSetter: "Value is V / 2, Actions = [{action: 'setSingleTarget', source: 'this', predicate: 'test://value', target: Value}]"
|
|
166
|
+
getter: "(<-link[WHERE predicate = 'test://value'].in.uri)[0]",
|
|
149
167
|
}),
|
|
150
|
-
__metadata("design:type",
|
|
168
|
+
__metadata("design:type", String)
|
|
151
169
|
], CustomModel.prototype, "computed", void 0);
|
|
152
170
|
CustomModel = __decorate([
|
|
153
|
-
|
|
171
|
+
Model({ name: "CustomModel" })
|
|
154
172
|
], CustomModel);
|
|
155
173
|
const metadata = CustomModel.getModelMetadata();
|
|
156
|
-
// Assert
|
|
157
|
-
expect(metadata.properties.computed.
|
|
158
|
-
expect(metadata.properties.computed.prologSetter).toContain("Value is V / 2");
|
|
159
|
-
expect(metadata.properties.computed.prologSetter).toContain("setSingleTarget");
|
|
174
|
+
// Assert getter contains the custom SurrealQL code
|
|
175
|
+
expect(metadata.properties.computed.getter).toContain("test://value");
|
|
160
176
|
});
|
|
161
|
-
it("should
|
|
177
|
+
it("should register relatedModel factory for typed @HasMany", () => {
|
|
162
178
|
let Comment = class Comment extends Ad4mModel {
|
|
163
179
|
};
|
|
164
180
|
Comment = __decorate([
|
|
165
|
-
|
|
181
|
+
Model({ name: "Comment" })
|
|
166
182
|
], Comment);
|
|
167
183
|
let Post = class Post extends Ad4mModel {
|
|
168
184
|
constructor() {
|
|
@@ -171,24 +187,22 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
171
187
|
}
|
|
172
188
|
};
|
|
173
189
|
__decorate([
|
|
174
|
-
|
|
175
|
-
through: "post://comment",
|
|
176
|
-
where: { isInstance: Comment }
|
|
177
|
-
}),
|
|
190
|
+
HasMany(() => Comment, { through: "post://comment" }),
|
|
178
191
|
__metadata("design:type", Array)
|
|
179
192
|
], Post.prototype, "comments", void 0);
|
|
180
193
|
Post = __decorate([
|
|
181
|
-
|
|
194
|
+
Model({ name: "Post" })
|
|
182
195
|
], Post);
|
|
183
196
|
const metadata = Post.getModelMetadata();
|
|
184
|
-
//
|
|
185
|
-
expect(metadata.
|
|
197
|
+
// relatedModel factory should be registered and return Comment
|
|
198
|
+
expect(metadata.relations.comments.relatedModel).toBeDefined();
|
|
199
|
+
expect(metadata.relations.comments.relatedModel()).toBe(Comment);
|
|
186
200
|
});
|
|
187
201
|
it("should throw error for class without @ModelOptions decorator", () => {
|
|
188
202
|
class NoDecoratorModel extends Ad4mModel {
|
|
189
203
|
}
|
|
190
204
|
// Assert that calling getModelMetadata throws an error
|
|
191
|
-
expect(() => NoDecoratorModel.getModelMetadata()).toThrow("Model class must be decorated with @
|
|
205
|
+
expect(() => NoDecoratorModel.getModelMetadata()).toThrow("Model class must be decorated with @Model");
|
|
192
206
|
});
|
|
193
207
|
it("should handle complex model with mixed property and collection types", () => {
|
|
194
208
|
let Recipe = class Recipe extends Ad4mModel {
|
|
@@ -202,27 +216,34 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
202
216
|
}
|
|
203
217
|
};
|
|
204
218
|
__decorate([
|
|
205
|
-
Property({
|
|
219
|
+
Property({
|
|
220
|
+
through: "recipe://name",
|
|
221
|
+
required: true,
|
|
222
|
+
}),
|
|
206
223
|
__metadata("design:type", String)
|
|
207
224
|
], Recipe.prototype, "name", void 0);
|
|
208
225
|
__decorate([
|
|
209
|
-
|
|
226
|
+
Property({ through: "recipe://description" }),
|
|
210
227
|
__metadata("design:type", String)
|
|
211
228
|
], Recipe.prototype, "description", void 0);
|
|
212
229
|
__decorate([
|
|
213
|
-
|
|
230
|
+
Property({
|
|
231
|
+
through: "recipe://rating",
|
|
232
|
+
getter: "avg_rating_surreal(Base, Value)",
|
|
233
|
+
readOnly: true,
|
|
234
|
+
}),
|
|
214
235
|
__metadata("design:type", Number)
|
|
215
236
|
], Recipe.prototype, "rating", void 0);
|
|
216
237
|
__decorate([
|
|
217
|
-
|
|
238
|
+
HasMany({ through: "recipe://ingredient" }),
|
|
218
239
|
__metadata("design:type", Array)
|
|
219
240
|
], Recipe.prototype, "ingredients", void 0);
|
|
220
241
|
__decorate([
|
|
221
|
-
|
|
242
|
+
HasMany({ through: "recipe://step", local: true }),
|
|
222
243
|
__metadata("design:type", Array)
|
|
223
244
|
], Recipe.prototype, "steps", void 0);
|
|
224
245
|
Recipe = __decorate([
|
|
225
|
-
|
|
246
|
+
Model({ name: "Recipe" })
|
|
226
247
|
], Recipe);
|
|
227
248
|
const metadata = Recipe.getModelMetadata();
|
|
228
249
|
// Assert className
|
|
@@ -233,18 +254,18 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
233
254
|
expect(metadata.properties.description).toBeDefined();
|
|
234
255
|
expect(metadata.properties.rating).toBeDefined();
|
|
235
256
|
// Assert collections has 2 entries
|
|
236
|
-
expect(Object.keys(metadata.
|
|
237
|
-
expect(metadata.
|
|
238
|
-
expect(metadata.
|
|
257
|
+
expect(Object.keys(metadata.relations)).toHaveLength(2);
|
|
258
|
+
expect(metadata.relations.ingredients).toBeDefined();
|
|
259
|
+
expect(metadata.relations.steps).toBeDefined();
|
|
239
260
|
// Verify all metadata fields are correctly extracted
|
|
240
261
|
expect(metadata.properties.name.predicate).toBe("recipe://name");
|
|
241
|
-
expect(metadata.properties.name.resolveLanguage).
|
|
262
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined(); // "literal" is now the implicit default — no need to store it
|
|
242
263
|
expect(metadata.properties.description.predicate).toBe("recipe://description");
|
|
243
264
|
expect(metadata.properties.rating.predicate).toBe("recipe://rating");
|
|
244
|
-
expect(metadata.properties.rating.
|
|
245
|
-
expect(metadata.
|
|
246
|
-
expect(metadata.
|
|
247
|
-
expect(metadata.
|
|
265
|
+
expect(metadata.properties.rating.getter).toBe("avg_rating_surreal(Base, Value)");
|
|
266
|
+
expect(metadata.relations.ingredients.predicate).toBe("recipe://ingredient");
|
|
267
|
+
expect(metadata.relations.steps.predicate).toBe("recipe://step");
|
|
268
|
+
expect(metadata.relations.steps.local).toBe(true);
|
|
248
269
|
});
|
|
249
270
|
});
|
|
250
271
|
describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
@@ -255,14 +276,13 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
255
276
|
properties: {
|
|
256
277
|
name: { type: "string" },
|
|
257
278
|
price: { type: "number" },
|
|
258
|
-
description: { type: "string" }
|
|
279
|
+
description: { type: "string" },
|
|
259
280
|
},
|
|
260
|
-
required: ["name", "price"]
|
|
281
|
+
required: ["name", "price"],
|
|
261
282
|
};
|
|
262
283
|
const ProductClass = Ad4mModel.fromJSONSchema(schema, {
|
|
263
284
|
name: "Product",
|
|
264
285
|
namespace: "product://",
|
|
265
|
-
resolveLanguage: "literal"
|
|
266
286
|
});
|
|
267
287
|
const metadata = ProductClass.getModelMetadata();
|
|
268
288
|
// Verify className
|
|
@@ -272,12 +292,12 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
272
292
|
expect(metadata.properties.name).toBeDefined();
|
|
273
293
|
expect(metadata.properties.name.predicate).toBe("product://name");
|
|
274
294
|
expect(metadata.properties.name.required).toBe(true);
|
|
275
|
-
expect(metadata.properties.name.
|
|
276
|
-
expect(metadata.properties.name.resolveLanguage).
|
|
295
|
+
expect(metadata.properties.name.readOnly).toBeFalsy();
|
|
296
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
277
297
|
expect(metadata.properties.price).toBeDefined();
|
|
278
298
|
expect(metadata.properties.price.predicate).toBe("product://price");
|
|
279
299
|
expect(metadata.properties.price.required).toBe(true);
|
|
280
|
-
expect(metadata.properties.price.resolveLanguage).
|
|
300
|
+
expect(metadata.properties.price.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
281
301
|
expect(metadata.properties.description).toBeDefined();
|
|
282
302
|
expect(metadata.properties.description.predicate).toBe("product://description");
|
|
283
303
|
expect(metadata.properties.description.required).toBe(false);
|
|
@@ -290,28 +310,28 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
290
310
|
title: { type: "string" },
|
|
291
311
|
tags: {
|
|
292
312
|
type: "array",
|
|
293
|
-
items: { type: "string" }
|
|
313
|
+
items: { type: "string" },
|
|
294
314
|
},
|
|
295
315
|
comments: {
|
|
296
316
|
type: "array",
|
|
297
|
-
items: { type: "string" }
|
|
298
|
-
}
|
|
317
|
+
items: { type: "string" },
|
|
318
|
+
},
|
|
299
319
|
},
|
|
300
|
-
required: ["title"]
|
|
320
|
+
required: ["title"],
|
|
301
321
|
};
|
|
302
322
|
const PostClass = Ad4mModel.fromJSONSchema(schema, {
|
|
303
323
|
name: "Post",
|
|
304
|
-
namespace: "post://"
|
|
324
|
+
namespace: "post://",
|
|
305
325
|
});
|
|
306
326
|
const metadata = PostClass.getModelMetadata();
|
|
307
327
|
// Verify className
|
|
308
328
|
expect(metadata.className).toBe("Post");
|
|
309
|
-
// Verify
|
|
310
|
-
expect(Object.keys(metadata.
|
|
311
|
-
expect(metadata.
|
|
312
|
-
expect(metadata.
|
|
313
|
-
expect(metadata.
|
|
314
|
-
expect(metadata.
|
|
329
|
+
// Verify relations are extracted
|
|
330
|
+
expect(Object.keys(metadata.relations).length).toBeGreaterThan(0);
|
|
331
|
+
expect(metadata.relations.tags).toBeDefined();
|
|
332
|
+
expect(metadata.relations.tags.predicate).toBe("post://tags");
|
|
333
|
+
expect(metadata.relations.comments).toBeDefined();
|
|
334
|
+
expect(metadata.relations.comments.predicate).toBe("post://comments");
|
|
315
335
|
// Verify properties (should include at least title)
|
|
316
336
|
expect(metadata.properties.title).toBeDefined();
|
|
317
337
|
expect(metadata.properties.title.predicate).toBe("post://title");
|
|
@@ -321,7 +341,7 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
321
341
|
const schema = {
|
|
322
342
|
title: "Contact",
|
|
323
343
|
"x-ad4m": {
|
|
324
|
-
namespace: "contact://"
|
|
344
|
+
namespace: "contact://",
|
|
325
345
|
},
|
|
326
346
|
type: "object",
|
|
327
347
|
properties: {
|
|
@@ -329,28 +349,26 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
329
349
|
type: "string",
|
|
330
350
|
"x-ad4m": {
|
|
331
351
|
through: "foaf://name",
|
|
332
|
-
|
|
333
|
-
writable: true
|
|
334
|
-
}
|
|
352
|
+
},
|
|
335
353
|
},
|
|
336
354
|
email: {
|
|
337
355
|
type: "string",
|
|
338
356
|
"x-ad4m": {
|
|
339
357
|
through: "foaf://mbox",
|
|
340
|
-
local: true
|
|
341
|
-
}
|
|
342
|
-
}
|
|
358
|
+
local: true,
|
|
359
|
+
},
|
|
360
|
+
},
|
|
343
361
|
},
|
|
344
|
-
required: ["name"]
|
|
362
|
+
required: ["name"],
|
|
345
363
|
};
|
|
346
364
|
const ContactClass = Ad4mModel.fromJSONSchema(schema, {
|
|
347
|
-
name: "Contact"
|
|
365
|
+
name: "Contact",
|
|
348
366
|
});
|
|
349
367
|
const metadata = ContactClass.getModelMetadata();
|
|
350
368
|
// Verify x-ad4m metadata is respected
|
|
351
369
|
expect(metadata.properties.name.predicate).toBe("foaf://name");
|
|
352
|
-
expect(metadata.properties.name.resolveLanguage).
|
|
353
|
-
expect(metadata.properties.name.
|
|
370
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
371
|
+
expect(metadata.properties.name.readOnly).toBeFalsy();
|
|
354
372
|
expect(metadata.properties.name.required).toBe(true);
|
|
355
373
|
expect(metadata.properties.email.predicate).toBe("foaf://mbox");
|
|
356
374
|
expect(metadata.properties.email.local).toBe(true);
|
|
@@ -361,17 +379,17 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
361
379
|
type: "object",
|
|
362
380
|
properties: {
|
|
363
381
|
username: { type: "string" },
|
|
364
|
-
fullName: { type: "string" }
|
|
382
|
+
fullName: { type: "string" },
|
|
365
383
|
},
|
|
366
|
-
required: ["username"]
|
|
384
|
+
required: ["username"],
|
|
367
385
|
};
|
|
368
386
|
const UserClass = Ad4mModel.fromJSONSchema(schema, {
|
|
369
387
|
name: "User",
|
|
370
388
|
namespace: "user://",
|
|
371
389
|
propertyMapping: {
|
|
372
390
|
username: "custom://identifier",
|
|
373
|
-
fullName: "custom://name"
|
|
374
|
-
}
|
|
391
|
+
fullName: "custom://name",
|
|
392
|
+
},
|
|
375
393
|
});
|
|
376
394
|
const metadata = UserClass.getModelMetadata();
|
|
377
395
|
// Verify property mappings are applied
|
|
@@ -388,22 +406,21 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
388
406
|
published: { type: "boolean" },
|
|
389
407
|
authors: {
|
|
390
408
|
type: "array",
|
|
391
|
-
items: { type: "string" }
|
|
409
|
+
items: { type: "string" },
|
|
392
410
|
},
|
|
393
411
|
tags: {
|
|
394
412
|
type: "array",
|
|
395
413
|
items: { type: "string" },
|
|
396
414
|
"x-ad4m": {
|
|
397
|
-
local: true
|
|
398
|
-
}
|
|
399
|
-
}
|
|
415
|
+
local: true,
|
|
416
|
+
},
|
|
417
|
+
},
|
|
400
418
|
},
|
|
401
|
-
required: ["title", "published"]
|
|
419
|
+
required: ["title", "published"],
|
|
402
420
|
};
|
|
403
421
|
const ArticleClass = Ad4mModel.fromJSONSchema(schema, {
|
|
404
422
|
name: "Article",
|
|
405
423
|
namespace: "article://",
|
|
406
|
-
resolveLanguage: "literal"
|
|
407
424
|
});
|
|
408
425
|
const metadata = ArticleClass.getModelMetadata();
|
|
409
426
|
// Verify className
|
|
@@ -412,29 +429,30 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
412
429
|
expect(metadata.properties.title).toBeDefined();
|
|
413
430
|
expect(metadata.properties.title.predicate).toBe("article://title");
|
|
414
431
|
expect(metadata.properties.title.required).toBe(true);
|
|
415
|
-
expect(metadata.properties.title.resolveLanguage).
|
|
432
|
+
expect(metadata.properties.title.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
416
433
|
expect(metadata.properties.views).toBeDefined();
|
|
417
434
|
expect(metadata.properties.views.predicate).toBe("article://views");
|
|
418
|
-
expect(metadata.properties.views.resolveLanguage).
|
|
435
|
+
expect(metadata.properties.views.resolveLanguage).toBeUndefined(); // implicit literal default
|
|
419
436
|
expect(metadata.properties.published).toBeDefined();
|
|
420
437
|
expect(metadata.properties.published.predicate).toBe("article://published");
|
|
421
438
|
expect(metadata.properties.published.required).toBe(true);
|
|
422
|
-
// Verify
|
|
423
|
-
expect(metadata.
|
|
424
|
-
expect(metadata.
|
|
425
|
-
expect(metadata.
|
|
426
|
-
expect(metadata.
|
|
427
|
-
expect(metadata.
|
|
439
|
+
// Verify relations
|
|
440
|
+
expect(metadata.relations.authors).toBeDefined();
|
|
441
|
+
expect(metadata.relations.authors.predicate).toBe("article://authors");
|
|
442
|
+
expect(metadata.relations.tags).toBeDefined();
|
|
443
|
+
expect(metadata.relations.tags.predicate).toBe("article://tags");
|
|
444
|
+
expect(metadata.relations.tags.local).toBe(true);
|
|
428
445
|
});
|
|
429
446
|
it("should handle models with only an auto-generated type flag", () => {
|
|
447
|
+
const warnSpy = jest.spyOn(console, "warn").mockImplementation(() => { });
|
|
430
448
|
const schema = {
|
|
431
449
|
title: "EmptyModel",
|
|
432
450
|
type: "object",
|
|
433
|
-
properties: {}
|
|
451
|
+
properties: {},
|
|
434
452
|
};
|
|
435
453
|
const EmptyModelClass = Ad4mModel.fromJSONSchema(schema, {
|
|
436
454
|
name: "EmptyModel",
|
|
437
|
-
namespace: "empty://"
|
|
455
|
+
namespace: "empty://",
|
|
438
456
|
});
|
|
439
457
|
const metadata = EmptyModelClass.getModelMetadata();
|
|
440
458
|
// Should have className
|
|
@@ -444,6 +462,9 @@ describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
|
444
462
|
expect(metadata.properties.__ad4m_type.predicate).toBe("ad4m://type");
|
|
445
463
|
expect(metadata.properties.__ad4m_type.initial).toBe("empty://instance");
|
|
446
464
|
expect(metadata.properties.__ad4m_type.flag).toBe(true);
|
|
465
|
+
// The warning is the expected fallback signal for empty schemas
|
|
466
|
+
expect(warnSpy).toHaveBeenCalledWith(expect.stringContaining("No properties with initial values found"));
|
|
467
|
+
warnSpy.mockRestore();
|
|
447
468
|
});
|
|
448
469
|
});
|
|
449
470
|
describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
@@ -451,58 +472,40 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
451
472
|
const mockPerspective = {};
|
|
452
473
|
// Helper function to normalize whitespace in queries for easier comparison
|
|
453
474
|
function normalizeQuery(query) {
|
|
454
|
-
return query.replace(/\s+/g,
|
|
475
|
+
return query.replace(/\s+/g, " ").trim();
|
|
455
476
|
}
|
|
456
|
-
// Test Recipe model
|
|
457
|
-
let Recipe = class Recipe extends Ad4mModel {
|
|
458
|
-
constructor() {
|
|
459
|
-
super(...arguments);
|
|
460
|
-
this.name = "";
|
|
461
|
-
this.rating = 0;
|
|
462
|
-
this.ingredients = [];
|
|
463
|
-
}
|
|
464
|
-
};
|
|
465
|
-
__decorate([
|
|
466
|
-
Property({ through: "recipe://name" }),
|
|
467
|
-
__metadata("design:type", String)
|
|
468
|
-
], Recipe.prototype, "name", void 0);
|
|
469
|
-
__decorate([
|
|
470
|
-
Property({ through: "recipe://rating" }),
|
|
471
|
-
__metadata("design:type", Number)
|
|
472
|
-
], Recipe.prototype, "rating", void 0);
|
|
473
|
-
__decorate([
|
|
474
|
-
Collection({ through: "recipe://ingredient" }),
|
|
475
|
-
__metadata("design:type", Array)
|
|
476
|
-
], Recipe.prototype, "ingredients", void 0);
|
|
477
|
-
Recipe = __decorate([
|
|
478
|
-
ModelOptions({ name: "Recipe" })
|
|
479
|
-
], Recipe);
|
|
480
477
|
it("should generate basic query with no filters", async () => {
|
|
481
478
|
const query = await Recipe.queryToSurrealQL(mockPerspective, {});
|
|
482
479
|
expect(query).toContain("id AS source");
|
|
483
480
|
expect(query).toContain("uri AS source_uri");
|
|
484
481
|
expect(query).toContain("FROM node");
|
|
485
|
-
expect(query).toContain("
|
|
482
|
+
expect(query).toContain("(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links");
|
|
486
483
|
expect(query).toContain("WHERE");
|
|
487
484
|
// Should have graph traversal filters for required properties
|
|
488
485
|
expect(query).toContain("count(->link[WHERE");
|
|
489
486
|
});
|
|
490
487
|
it("should generate query with simple property filter", async () => {
|
|
491
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
488
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
489
|
+
where: { name: "Pasta" },
|
|
490
|
+
});
|
|
492
491
|
// Should have graph traversal filters for required properties and user filter
|
|
493
|
-
expect(query).toContain("count(->link[WHERE
|
|
494
|
-
expect(query).toContain("count(->link[WHERE
|
|
495
|
-
expect(query).toContain("count(->link[WHERE
|
|
492
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name']) > 0");
|
|
493
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating']) > 0");
|
|
494
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
496
495
|
});
|
|
497
496
|
it("should generate query with multiple property filters", async () => {
|
|
498
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
497
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
498
|
+
where: { name: "Pasta", rating: 5 },
|
|
499
|
+
});
|
|
499
500
|
expect(query).toContain("WHERE");
|
|
500
|
-
expect(query).toContain("count(->link[WHERE
|
|
501
|
-
expect(query).toContain("count(->link[WHERE
|
|
501
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
502
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating' AND fn::parse_literal(out.uri) = 5]) > 0");
|
|
502
503
|
expect(query).toContain("AND");
|
|
503
504
|
});
|
|
504
505
|
it("should handle gt operator (filtered in JavaScript, not SQL)", async () => {
|
|
505
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
506
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
507
|
+
where: { rating: { gt: 4 } },
|
|
508
|
+
});
|
|
506
509
|
// Comparison operators for regular properties are now filtered in JavaScript post-query
|
|
507
510
|
// The SQL just filters on the predicate existing
|
|
508
511
|
expect(query).toContain("FROM node");
|
|
@@ -510,72 +513,75 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
510
513
|
expect(query).not.toContain("target > 4");
|
|
511
514
|
});
|
|
512
515
|
it("should handle lt operator (filtered in JavaScript, not SQL)", async () => {
|
|
513
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
516
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
517
|
+
where: { rating: { lt: 3 } },
|
|
518
|
+
});
|
|
514
519
|
// Comparison operators are filtered in JavaScript post-query
|
|
515
520
|
expect(query).toContain("FROM node");
|
|
516
521
|
expect(query).not.toContain("target < 3");
|
|
517
522
|
});
|
|
518
523
|
it("should handle gte and lte operators (filtered in JavaScript, not SQL)", async () => {
|
|
519
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
524
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
525
|
+
where: { rating: { gte: 3, lte: 5 } },
|
|
526
|
+
});
|
|
520
527
|
// Comparison operators are filtered in JavaScript post-query
|
|
521
528
|
expect(query).toContain("FROM node");
|
|
522
529
|
expect(query).not.toContain("target >= 3");
|
|
523
530
|
expect(query).not.toContain("target <= 5");
|
|
524
531
|
});
|
|
525
532
|
it("should handle not operator with single value", async () => {
|
|
526
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
533
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
534
|
+
where: { name: { not: "Salad" } },
|
|
535
|
+
});
|
|
527
536
|
// Not operator uses graph traversal with count = 0
|
|
528
|
-
expect(query).toContain("count(->link[WHERE
|
|
537
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Salad']) = 0");
|
|
529
538
|
});
|
|
530
539
|
it("should handle not operator with array (NOT IN)", async () => {
|
|
531
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
540
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
541
|
+
where: { name: { not: ["Salad", "Soup"] } },
|
|
542
|
+
});
|
|
532
543
|
// Not operator with array uses graph traversal with count = 0
|
|
533
|
-
expect(query).toContain("count(->link[WHERE
|
|
544
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) IN ['Salad', 'Soup']]) = 0");
|
|
534
545
|
});
|
|
535
546
|
it("should handle between operator (filtered in JavaScript, not SQL)", async () => {
|
|
536
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
547
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
548
|
+
where: { rating: { between: [3, 5] } },
|
|
549
|
+
});
|
|
537
550
|
// Between operator for regular properties is now filtered in JavaScript post-query
|
|
538
551
|
expect(query).toContain("FROM node");
|
|
539
552
|
expect(query).not.toContain("target >= 3");
|
|
540
553
|
expect(query).not.toContain("target <= 5");
|
|
541
554
|
});
|
|
542
555
|
it("should handle contains operator on string property (filtered in JavaScript, not SQL)", async () => {
|
|
543
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
556
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
557
|
+
where: { name: { contains: "Past" } },
|
|
558
|
+
});
|
|
544
559
|
// Contains operator for regular properties is now filtered in JavaScript post-query
|
|
545
560
|
expect(query).toContain("FROM node");
|
|
546
561
|
expect(query).not.toContain("target CONTAINS 'Past'");
|
|
547
562
|
});
|
|
548
563
|
it("should handle contains operator on regular property with substring (filtered in JavaScript, not SQL)", async () => {
|
|
549
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
564
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
565
|
+
where: { name: { contains: "Salad" } },
|
|
566
|
+
});
|
|
550
567
|
// Contains operator is filtered in JavaScript post-query
|
|
551
568
|
expect(query).toContain("FROM node");
|
|
552
569
|
expect(query).not.toContain("target CONTAINS 'Salad'");
|
|
553
570
|
});
|
|
554
|
-
it.skip("should handle contains operator on special field (author)", async () => {
|
|
555
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { author: { contains: "alice" } } });
|
|
556
|
-
expect(query).toContain("WHERE author CONTAINS 'alice'");
|
|
557
|
-
// Should not use a subquery pattern
|
|
558
|
-
expect(query).not.toContain("SELECT source FROM node");
|
|
559
|
-
});
|
|
560
|
-
it.skip("should handle contains operator on special field (base)", async () => {
|
|
561
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { contains: "test" } } });
|
|
562
|
-
expect(query).toContain("WHERE source CONTAINS 'test'");
|
|
563
|
-
});
|
|
564
571
|
it("should handle array values (IN clause)", async () => {
|
|
565
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
566
|
-
|
|
567
|
-
|
|
568
|
-
|
|
569
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { author: "did:key:alice" } });
|
|
570
|
-
expect(query).toContain("WHERE author = 'did:key:alice'");
|
|
571
|
-
// Ensure it's NOT using a subquery pattern for author in WHERE clause
|
|
572
|
-
expect(normalizeQuery(query)).not.toMatch(/WHERE.*source IN.*SELECT source FROM node.*author/);
|
|
572
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
573
|
+
where: { name: ["Pasta", "Pizza"] },
|
|
574
|
+
});
|
|
575
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) IN ['Pasta', 'Pizza']]) > 0");
|
|
573
576
|
});
|
|
574
577
|
it("should not generate ORDER BY clause in SQL (handled in JavaScript)", async () => {
|
|
575
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
578
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
579
|
+
order: { timestamp: "DESC" },
|
|
580
|
+
});
|
|
576
581
|
// ORDER BY is now handled in JavaScript post-query
|
|
582
|
+
// Note: the links correlated subquery uses ORDER BY timestamp ASC internally for link ordering
|
|
577
583
|
expect(query).toContain("FROM node");
|
|
578
|
-
expect(query).not.toContain("ORDER BY");
|
|
584
|
+
expect(query).not.toContain("ORDER BY timestamp DESC");
|
|
579
585
|
});
|
|
580
586
|
it("should not generate LIMIT clause in SQL (handled in JavaScript)", async () => {
|
|
581
587
|
const query = await Recipe.queryToSurrealQL(mockPerspective, { limit: 10 });
|
|
@@ -584,7 +590,9 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
584
590
|
expect(query).not.toContain("LIMIT");
|
|
585
591
|
});
|
|
586
592
|
it("should not generate START clause in SQL (handled in JavaScript)", async () => {
|
|
587
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
593
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
594
|
+
offset: 20,
|
|
595
|
+
});
|
|
588
596
|
// START/offset is now handled in JavaScript post-query
|
|
589
597
|
expect(query).toContain("FROM node");
|
|
590
598
|
expect(query).not.toContain("START");
|
|
@@ -594,25 +602,28 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
594
602
|
where: { name: "Pasta", rating: { gt: 4 } },
|
|
595
603
|
order: { timestamp: "DESC" },
|
|
596
604
|
limit: 10,
|
|
597
|
-
offset: 20
|
|
605
|
+
offset: 20,
|
|
598
606
|
});
|
|
599
607
|
// WHERE clause uses graph traversal filters
|
|
600
608
|
expect(query).toContain("WHERE");
|
|
601
|
-
expect(query).toContain("count(->link[WHERE
|
|
609
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
602
610
|
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
603
611
|
expect(query).not.toContain("out.uri > 4");
|
|
604
612
|
expect(query).not.toContain("target > 4");
|
|
605
613
|
// ORDER BY, LIMIT, START are now handled in JavaScript post-query
|
|
606
|
-
|
|
614
|
+
// Note: the links subquery uses ORDER BY timestamp ASC internally
|
|
615
|
+
expect(query).not.toContain("ORDER BY timestamp DESC");
|
|
607
616
|
expect(query).not.toContain("LIMIT");
|
|
608
617
|
expect(query).not.toContain("START");
|
|
609
618
|
});
|
|
610
|
-
it("should only select requested
|
|
611
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
619
|
+
it("should only select requested fields", async () => {
|
|
620
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
621
|
+
properties: ["name"],
|
|
622
|
+
});
|
|
612
623
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
613
|
-
expect(query).toContain("
|
|
624
|
+
expect(query).toContain("(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links");
|
|
614
625
|
});
|
|
615
|
-
it("should only select requested
|
|
626
|
+
it("should only select requested relations", async () => {
|
|
616
627
|
let MultiCollectionModel = class MultiCollectionModel extends Ad4mModel {
|
|
617
628
|
constructor() {
|
|
618
629
|
super(...arguments);
|
|
@@ -621,29 +632,33 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
621
632
|
}
|
|
622
633
|
};
|
|
623
634
|
__decorate([
|
|
624
|
-
|
|
635
|
+
HasMany({ through: "test://coll1" }),
|
|
625
636
|
__metadata("design:type", Array)
|
|
626
637
|
], MultiCollectionModel.prototype, "coll1", void 0);
|
|
627
638
|
__decorate([
|
|
628
|
-
|
|
639
|
+
HasMany({ through: "test://coll2" }),
|
|
629
640
|
__metadata("design:type", Array)
|
|
630
641
|
], MultiCollectionModel.prototype, "coll2", void 0);
|
|
631
642
|
MultiCollectionModel = __decorate([
|
|
632
|
-
|
|
643
|
+
Model({ name: "MultiCollectionModel" })
|
|
633
644
|
], MultiCollectionModel);
|
|
634
|
-
const query = await MultiCollectionModel.queryToSurrealQL(mockPerspective, {
|
|
645
|
+
const query = await MultiCollectionModel.queryToSurrealQL(mockPerspective, {});
|
|
635
646
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
636
|
-
expect(query).toContain("
|
|
647
|
+
expect(query).toContain("(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links");
|
|
637
648
|
});
|
|
638
649
|
it("should escape single quotes in string values", async () => {
|
|
639
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
650
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
651
|
+
where: { name: "O'Brien's Recipe" },
|
|
652
|
+
});
|
|
640
653
|
// Single quotes are escaped with backslash in SurrealDB
|
|
641
654
|
expect(query).toContain("O\\'Brien\\'s Recipe");
|
|
642
655
|
});
|
|
643
656
|
it("should handle numeric values without quotes", async () => {
|
|
644
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
657
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
658
|
+
where: { rating: 5 },
|
|
659
|
+
});
|
|
645
660
|
// Numeric values should not have quotes around them
|
|
646
|
-
expect(query).toContain("out.uri = 5");
|
|
661
|
+
expect(query).toContain("fn::parse_literal(out.uri) = 5");
|
|
647
662
|
expect(query).not.toContain("out.uri = '5'");
|
|
648
663
|
});
|
|
649
664
|
it("should handle complex nested query (comparisons, ORDER BY, LIMIT handled in JS)", async () => {
|
|
@@ -651,14 +666,14 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
651
666
|
where: {
|
|
652
667
|
name: "Pasta",
|
|
653
668
|
rating: { gte: 4, lte: 5 },
|
|
654
|
-
author: "did:key:alice"
|
|
669
|
+
author: "did:key:alice",
|
|
655
670
|
},
|
|
656
671
|
order: { rating: "DESC" },
|
|
657
|
-
limit: 5
|
|
672
|
+
limit: 5,
|
|
658
673
|
});
|
|
659
674
|
const normalized = normalizeQuery(query);
|
|
660
675
|
// Verify graph traversal filters are present
|
|
661
|
-
expect(normalized).toContain("count(->link[WHERE
|
|
676
|
+
expect(normalized).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
662
677
|
// Comparison operators (gte, lte) are filtered in JavaScript, not SQL
|
|
663
678
|
expect(normalized).not.toContain("out.uri >= 4");
|
|
664
679
|
expect(normalized).not.toContain("target >= 4");
|
|
@@ -667,7 +682,8 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
667
682
|
// author and timestamp filtering is done in JavaScript post-query
|
|
668
683
|
expect(normalized).not.toContain("author = 'did:key:alice'");
|
|
669
684
|
// ORDER BY and LIMIT are handled in JavaScript post-query
|
|
670
|
-
|
|
685
|
+
// Note: the links correlated subquery uses ORDER BY timestamp ASC internally
|
|
686
|
+
expect(normalized).not.toContain("ORDER BY rating");
|
|
671
687
|
expect(normalized).not.toContain("LIMIT");
|
|
672
688
|
// Verify query structure (FROM node, no GROUP BY)
|
|
673
689
|
expect(normalized).toContain("FROM node");
|
|
@@ -680,44 +696,60 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
680
696
|
expect(query).toContain("FROM node");
|
|
681
697
|
// Should have WHERE clause filtering for required properties using graph traversal
|
|
682
698
|
expect(query).toContain("WHERE");
|
|
683
|
-
expect(query).toContain("count(->link[WHERE
|
|
684
|
-
expect(query).toContain("count(->link[WHERE
|
|
685
|
-
|
|
699
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://name']) > 0");
|
|
700
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating']) > 0");
|
|
701
|
+
// No user-requested ORDER BY or START should appear at the top level
|
|
686
702
|
expect(query).not.toContain("START");
|
|
687
703
|
});
|
|
688
704
|
it("should handle base special field", async () => {
|
|
689
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
705
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
706
|
+
where: { base: "literal://test" },
|
|
707
|
+
});
|
|
690
708
|
expect(query).toContain("uri = 'literal://test'");
|
|
691
709
|
});
|
|
692
710
|
it("should handle base special field with array (IN clause)", async () => {
|
|
693
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
711
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
712
|
+
where: { base: ["literal://test1", "literal://test2"] },
|
|
713
|
+
});
|
|
694
714
|
expect(query).toContain("uri IN ['literal://test1', 'literal://test2']");
|
|
695
715
|
});
|
|
696
716
|
it("should handle base special field with not operator", async () => {
|
|
697
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
717
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
718
|
+
where: { base: { not: "literal://test" } },
|
|
719
|
+
});
|
|
698
720
|
expect(query).toContain("uri != 'literal://test'");
|
|
699
721
|
});
|
|
700
722
|
it("should handle base special field with not operator and array (NOT IN)", async () => {
|
|
701
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
723
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
724
|
+
where: { base: { not: ["literal://test1", "literal://test2"] } },
|
|
725
|
+
});
|
|
702
726
|
expect(query).toContain("uri NOT IN ['literal://test1', 'literal://test2']");
|
|
703
727
|
});
|
|
704
728
|
it("should handle base special field with between operator", async () => {
|
|
705
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
729
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
730
|
+
where: { base: { between: ["literal://a", "literal://z"] } },
|
|
731
|
+
});
|
|
706
732
|
const normalized = normalizeQuery(query);
|
|
707
733
|
expect(normalized).toContain("uri >= 'literal://a' AND uri <= 'literal://z'");
|
|
708
734
|
});
|
|
709
735
|
it("should handle base special field with gt operator", async () => {
|
|
710
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
736
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
737
|
+
where: { base: { gt: "literal://m" } },
|
|
738
|
+
});
|
|
711
739
|
expect(query).toContain("uri > 'literal://m'");
|
|
712
740
|
});
|
|
713
741
|
it("should handle base special field with gte and lte operators", async () => {
|
|
714
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
742
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
743
|
+
where: { base: { gte: "literal://a", lte: "literal://z" } },
|
|
744
|
+
});
|
|
715
745
|
const normalized = normalizeQuery(query);
|
|
716
746
|
expect(normalized).toContain("uri >= 'literal://a'");
|
|
717
747
|
expect(normalized).toContain("uri <= 'literal://z'");
|
|
718
748
|
});
|
|
719
749
|
it("should handle timestamp special field with gt operator (filtered in JavaScript)", async () => {
|
|
720
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
750
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
751
|
+
where: { timestamp: { gt: 1234567890 } },
|
|
752
|
+
});
|
|
721
753
|
// timestamp filtering is done in JavaScript post-query
|
|
722
754
|
expect(query).toContain("FROM node");
|
|
723
755
|
expect(query).not.toContain("timestamp > 1234567890");
|
|
@@ -726,8 +758,8 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
726
758
|
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
727
759
|
where: {
|
|
728
760
|
author: "did:key:alice",
|
|
729
|
-
timestamp: { gt: 1000 }
|
|
730
|
-
}
|
|
761
|
+
timestamp: { gt: 1000 },
|
|
762
|
+
},
|
|
731
763
|
});
|
|
732
764
|
// author and timestamp filtering is done in JavaScript post-query
|
|
733
765
|
expect(query).toContain("FROM node");
|
|
@@ -739,12 +771,12 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
739
771
|
where: {
|
|
740
772
|
name: "Pasta",
|
|
741
773
|
author: "did:key:alice",
|
|
742
|
-
rating: { gt: 4 }
|
|
743
|
-
}
|
|
774
|
+
rating: { gt: 4 },
|
|
775
|
+
},
|
|
744
776
|
});
|
|
745
777
|
const normalized = normalizeQuery(query);
|
|
746
778
|
// Regular properties use graph traversal filters
|
|
747
|
-
expect(normalized).toContain("count(->link[WHERE
|
|
779
|
+
expect(normalized).toContain("count(->link[WHERE predicate = 'recipe://name' AND fn::parse_literal(out.uri) = 'Pasta']) > 0");
|
|
748
780
|
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
749
781
|
expect(normalized).not.toContain("out.uri > 4");
|
|
750
782
|
expect(normalized).not.toContain("target > 4");
|
|
@@ -759,81 +791,97 @@ describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
|
759
791
|
}
|
|
760
792
|
};
|
|
761
793
|
__decorate([
|
|
762
|
-
Property({
|
|
794
|
+
Property({
|
|
795
|
+
through: "task://completed",
|
|
796
|
+
required: true,
|
|
797
|
+
}),
|
|
763
798
|
__metadata("design:type", Boolean)
|
|
764
799
|
], Task.prototype, "completed", void 0);
|
|
765
800
|
Task = __decorate([
|
|
766
|
-
|
|
801
|
+
Model({ name: "Task" })
|
|
767
802
|
], Task);
|
|
768
|
-
const query = await Task.queryToSurrealQL(mockPerspective, {
|
|
769
|
-
|
|
803
|
+
const query = await Task.queryToSurrealQL(mockPerspective, {
|
|
804
|
+
where: { completed: true },
|
|
805
|
+
});
|
|
806
|
+
expect(query).toContain("fn::parse_literal(out.uri) = true");
|
|
770
807
|
});
|
|
771
808
|
it("should handle array of numbers", async () => {
|
|
772
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
773
|
-
|
|
809
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
810
|
+
where: { rating: [4, 5] },
|
|
811
|
+
});
|
|
812
|
+
expect(query).toContain("count(->link[WHERE predicate = 'recipe://rating' AND fn::parse_literal(out.uri) IN [4, 5]]) > 0");
|
|
774
813
|
});
|
|
775
814
|
it("should skip unknown properties in where clause", async () => {
|
|
776
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
815
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
816
|
+
where: { unknownProp: "value" },
|
|
817
|
+
});
|
|
777
818
|
// Should not throw error, just skip the unknown property
|
|
778
819
|
expect(query).toContain("source");
|
|
779
820
|
// Should not contain any condition for unknownProp
|
|
780
821
|
expect(query).not.toContain("unknownProp");
|
|
781
822
|
});
|
|
782
|
-
it.skip("should skip unknown properties in select clause", async () => {
|
|
783
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name", "unknownProp"] });
|
|
784
|
-
// Should include name using aggregation
|
|
785
|
-
expect(query).toContain("array::first(target[WHERE predicate = 'recipe://name']) AS name");
|
|
786
|
-
// Should not error on unknownProp, just skip it
|
|
787
|
-
expect(query).not.toContain("unknownProp");
|
|
788
|
-
});
|
|
789
823
|
it("should handle order by regular property (handled in JavaScript)", async () => {
|
|
790
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
824
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
825
|
+
order: { name: "ASC" },
|
|
826
|
+
});
|
|
791
827
|
// ORDER BY is now handled in JavaScript post-query
|
|
828
|
+
// Note: the links correlated subquery uses ORDER BY timestamp ASC internally
|
|
792
829
|
expect(query).toContain("FROM node");
|
|
793
|
-
expect(query).not.toContain("ORDER BY");
|
|
830
|
+
expect(query).not.toContain("ORDER BY name");
|
|
794
831
|
});
|
|
795
|
-
it("should generate query with only
|
|
796
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
832
|
+
it("should generate query with only fields, no relations", async () => {
|
|
833
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
834
|
+
properties: ["name", "rating"],
|
|
835
|
+
});
|
|
797
836
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
798
|
-
expect(query).toContain("
|
|
837
|
+
expect(query).toContain("(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links");
|
|
799
838
|
});
|
|
800
|
-
it("should generate query with only
|
|
801
|
-
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
839
|
+
it("should generate query with only relations, no fields", async () => {
|
|
840
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
841
|
+
properties: [],
|
|
842
|
+
});
|
|
802
843
|
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
803
|
-
expect(query).toContain("
|
|
844
|
+
expect(query).toContain("(SELECT predicate, out.uri AS target, author, timestamp FROM link WHERE in = $parent.id ORDER BY timestamp ASC) AS links");
|
|
845
|
+
});
|
|
846
|
+
it("should include backward link filter when parent is given as raw predicate form", async () => {
|
|
847
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
848
|
+
parent: { id: "some://parent-uri", predicate: "custom://has_recipe" },
|
|
849
|
+
});
|
|
850
|
+
expect(query).toContain("count(<-link[WHERE predicate = 'custom://has_recipe' AND in.uri = 'some://parent-uri']) > 0");
|
|
851
|
+
});
|
|
852
|
+
it("should resolve model-backed parent form and include correct backward link filter", async () => {
|
|
853
|
+
let MenuForQueryTest = class MenuForQueryTest extends Ad4mModel {
|
|
854
|
+
constructor() {
|
|
855
|
+
super(...arguments);
|
|
856
|
+
this.recipes = [];
|
|
857
|
+
}
|
|
858
|
+
};
|
|
859
|
+
__decorate([
|
|
860
|
+
HasMany(() => Recipe, { through: "menu://has_recipe" }),
|
|
861
|
+
__metadata("design:type", Array)
|
|
862
|
+
], MenuForQueryTest.prototype, "recipes", void 0);
|
|
863
|
+
MenuForQueryTest = __decorate([
|
|
864
|
+
Model({ name: "MenuForQueryTest" })
|
|
865
|
+
], MenuForQueryTest);
|
|
866
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
867
|
+
parent: {
|
|
868
|
+
id: "some://menu-uri",
|
|
869
|
+
model: MenuForQueryTest,
|
|
870
|
+
field: "recipes",
|
|
871
|
+
},
|
|
872
|
+
});
|
|
873
|
+
expect(query).toContain("count(<-link[WHERE predicate = 'menu://has_recipe' AND in.uri = 'some://menu-uri']) > 0");
|
|
804
874
|
});
|
|
805
875
|
});
|
|
806
|
-
describe("Ad4mModel.instancesFromSurrealResult() and
|
|
807
|
-
// Test Recipe model
|
|
808
|
-
let Recipe = class Recipe extends Ad4mModel {
|
|
809
|
-
constructor() {
|
|
810
|
-
super(...arguments);
|
|
811
|
-
this.name = "";
|
|
812
|
-
this.rating = 0;
|
|
813
|
-
this.ingredients = [];
|
|
814
|
-
}
|
|
815
|
-
};
|
|
816
|
-
__decorate([
|
|
817
|
-
Property({ through: "recipe://name" }),
|
|
818
|
-
__metadata("design:type", String)
|
|
819
|
-
], Recipe.prototype, "name", void 0);
|
|
820
|
-
__decorate([
|
|
821
|
-
Property({ through: "recipe://rating" }),
|
|
822
|
-
__metadata("design:type", Number)
|
|
823
|
-
], Recipe.prototype, "rating", void 0);
|
|
824
|
-
__decorate([
|
|
825
|
-
Collection({ through: "recipe://ingredient" }),
|
|
826
|
-
__metadata("design:type", Array)
|
|
827
|
-
], Recipe.prototype, "ingredients", void 0);
|
|
828
|
-
Recipe = __decorate([
|
|
829
|
-
ModelOptions({ name: "Recipe" })
|
|
830
|
-
], Recipe);
|
|
876
|
+
describe("Ad4mModel.instancesFromSurrealResult() — hydration and query dispatch", () => {
|
|
831
877
|
// Mock perspective with both querySurrealDB and infer methods
|
|
832
878
|
const mockPerspective = {
|
|
833
879
|
querySurrealDB: jest.fn(),
|
|
834
880
|
infer: jest.fn(),
|
|
835
|
-
uuid:
|
|
836
|
-
stringOrTemplateObjectToSubjectClassName: jest
|
|
881
|
+
uuid: "test-perspective-uuid",
|
|
882
|
+
stringOrTemplateObjectToSubjectClassName: jest
|
|
883
|
+
.fn()
|
|
884
|
+
.mockResolvedValue("Recipe"),
|
|
837
885
|
};
|
|
838
886
|
beforeEach(() => {
|
|
839
887
|
jest.clearAllMocks();
|
|
@@ -849,24 +897,74 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
849
897
|
source: "node:abc123",
|
|
850
898
|
source_uri: "literal://recipe1",
|
|
851
899
|
links: [
|
|
852
|
-
{
|
|
853
|
-
|
|
854
|
-
|
|
855
|
-
|
|
856
|
-
|
|
857
|
-
|
|
900
|
+
{
|
|
901
|
+
predicate: "recipe://name",
|
|
902
|
+
target: Literal.from("Pasta").toUrl(),
|
|
903
|
+
author: "did:key:alice",
|
|
904
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
905
|
+
},
|
|
906
|
+
{
|
|
907
|
+
predicate: "recipe://rating",
|
|
908
|
+
target: Literal.from(5).toUrl(),
|
|
909
|
+
author: "did:key:alice",
|
|
910
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
911
|
+
},
|
|
912
|
+
{
|
|
913
|
+
predicate: "recipe://ingredient",
|
|
914
|
+
target: "pasta",
|
|
915
|
+
author: "did:key:alice",
|
|
916
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
917
|
+
},
|
|
918
|
+
{
|
|
919
|
+
predicate: "recipe://ingredient",
|
|
920
|
+
target: "tomato",
|
|
921
|
+
author: "did:key:alice",
|
|
922
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
923
|
+
},
|
|
924
|
+
{
|
|
925
|
+
predicate: "recipe://ingredient",
|
|
926
|
+
target: "cheese",
|
|
927
|
+
author: "did:key:alice",
|
|
928
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
929
|
+
},
|
|
930
|
+
],
|
|
858
931
|
},
|
|
859
932
|
{
|
|
860
933
|
source: "node:def456",
|
|
861
934
|
source_uri: "literal://recipe2",
|
|
862
935
|
links: [
|
|
863
|
-
{
|
|
864
|
-
|
|
865
|
-
|
|
866
|
-
|
|
867
|
-
|
|
868
|
-
|
|
869
|
-
|
|
936
|
+
{
|
|
937
|
+
predicate: "recipe://name",
|
|
938
|
+
target: Literal.from("Pizza").toUrl(),
|
|
939
|
+
author: "did:key:bob",
|
|
940
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
941
|
+
},
|
|
942
|
+
{
|
|
943
|
+
predicate: "recipe://rating",
|
|
944
|
+
target: Literal.from(4).toUrl(),
|
|
945
|
+
author: "did:key:bob",
|
|
946
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
947
|
+
},
|
|
948
|
+
{
|
|
949
|
+
predicate: "recipe://ingredient",
|
|
950
|
+
target: "dough",
|
|
951
|
+
author: "did:key:bob",
|
|
952
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
953
|
+
},
|
|
954
|
+
{
|
|
955
|
+
predicate: "recipe://ingredient",
|
|
956
|
+
target: "cheese",
|
|
957
|
+
author: "did:key:bob",
|
|
958
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
959
|
+
},
|
|
960
|
+
{
|
|
961
|
+
predicate: "recipe://ingredient",
|
|
962
|
+
target: "tomato",
|
|
963
|
+
author: "did:key:bob",
|
|
964
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
965
|
+
},
|
|
966
|
+
],
|
|
967
|
+
},
|
|
870
968
|
];
|
|
871
969
|
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
872
970
|
expect(result.results).toHaveLength(2);
|
|
@@ -881,61 +979,193 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
881
979
|
expect(recipe2.name).toBe("Pizza");
|
|
882
980
|
expect(recipe2.rating).toBe(4);
|
|
883
981
|
});
|
|
884
|
-
it("should
|
|
885
|
-
const
|
|
982
|
+
it("should set instance id from source_uri", async () => {
|
|
983
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, [
|
|
886
984
|
{
|
|
887
985
|
source: "node:abc123",
|
|
888
986
|
source_uri: "literal://recipe1",
|
|
889
987
|
links: [
|
|
890
|
-
{
|
|
891
|
-
|
|
892
|
-
|
|
893
|
-
|
|
894
|
-
|
|
988
|
+
{
|
|
989
|
+
predicate: "recipe://name",
|
|
990
|
+
target: Literal.from("Pasta").toUrl(),
|
|
991
|
+
author: "did:key:alice",
|
|
992
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
993
|
+
},
|
|
994
|
+
{
|
|
995
|
+
predicate: "recipe://rating",
|
|
996
|
+
target: Literal.from(5).toUrl(),
|
|
997
|
+
author: "did:key:alice",
|
|
998
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
999
|
+
},
|
|
1000
|
+
],
|
|
1001
|
+
},
|
|
1002
|
+
]);
|
|
1003
|
+
expect(result.results).toHaveLength(1);
|
|
1004
|
+
// id is derived from source_uri (the node's URI in the graph)
|
|
1005
|
+
expect(result.results[0].id).toBe("literal://recipe1");
|
|
1006
|
+
});
|
|
1007
|
+
it("should use latest-wins semantics when multiple links share a predicate", async () => {
|
|
1008
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, [
|
|
1009
|
+
{
|
|
1010
|
+
source: "node:abc123",
|
|
1011
|
+
source_uri: "literal://recipe1",
|
|
1012
|
+
links: [
|
|
1013
|
+
{
|
|
1014
|
+
predicate: "recipe://name",
|
|
1015
|
+
target: Literal.from("Old Name").toUrl(),
|
|
1016
|
+
author: "did:key:alice",
|
|
1017
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1018
|
+
},
|
|
1019
|
+
{
|
|
1020
|
+
// later timestamp — this value should win
|
|
1021
|
+
predicate: "recipe://name",
|
|
1022
|
+
target: Literal.from("New Name").toUrl(),
|
|
1023
|
+
author: "did:key:alice",
|
|
1024
|
+
timestamp: "2023-01-02T00:00:00Z",
|
|
1025
|
+
},
|
|
1026
|
+
{
|
|
1027
|
+
predicate: "recipe://rating",
|
|
1028
|
+
target: Literal.from(5).toUrl(),
|
|
1029
|
+
author: "did:key:alice",
|
|
1030
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1031
|
+
},
|
|
1032
|
+
],
|
|
1033
|
+
},
|
|
1034
|
+
]);
|
|
1035
|
+
expect(result.results).toHaveLength(1);
|
|
1036
|
+
expect(result.results[0].name).toBe("New Name");
|
|
1037
|
+
});
|
|
1038
|
+
it("should apply transform function during hydration", async () => {
|
|
1039
|
+
let TransformRecipe = class TransformRecipe extends Ad4mModel {
|
|
1040
|
+
constructor() {
|
|
1041
|
+
super(...arguments);
|
|
1042
|
+
this.name = "";
|
|
895
1043
|
}
|
|
896
|
-
|
|
897
|
-
|
|
1044
|
+
};
|
|
1045
|
+
__decorate([
|
|
1046
|
+
Property({
|
|
1047
|
+
through: "recipe://name",
|
|
1048
|
+
transform: (v) => v.toUpperCase(),
|
|
1049
|
+
}),
|
|
1050
|
+
__metadata("design:type", String)
|
|
1051
|
+
], TransformRecipe.prototype, "name", void 0);
|
|
1052
|
+
TransformRecipe = __decorate([
|
|
1053
|
+
Model({ name: "TransformRecipe" })
|
|
1054
|
+
], TransformRecipe);
|
|
1055
|
+
const result = await TransformRecipe.instancesFromSurrealResult(mockPerspective, {}, [
|
|
1056
|
+
{
|
|
1057
|
+
source: "node:abc123",
|
|
1058
|
+
source_uri: "literal://tr1",
|
|
1059
|
+
links: [
|
|
1060
|
+
{
|
|
1061
|
+
predicate: "recipe://name",
|
|
1062
|
+
target: Literal.from("pasta").toUrl(),
|
|
1063
|
+
author: "did:key:alice",
|
|
1064
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1065
|
+
},
|
|
1066
|
+
],
|
|
1067
|
+
},
|
|
1068
|
+
]);
|
|
898
1069
|
expect(result.results).toHaveLength(1);
|
|
899
|
-
|
|
900
|
-
expect(recipe.name).toBe("Pasta");
|
|
901
|
-
// rating and ingredients should be removed since only "name" was requested
|
|
902
|
-
expect(recipe.rating).toBeUndefined();
|
|
903
|
-
expect(recipe.ingredients).toBeUndefined();
|
|
904
|
-
// author and timestamp should still be present
|
|
905
|
-
expect(recipe.author).toBe("did:key:alice");
|
|
906
|
-
// Timestamp is converted to Unix epoch (milliseconds)
|
|
907
|
-
expect(recipe.timestamp).toBe(new Date("2023-01-01T00:00:00Z").getTime());
|
|
1070
|
+
expect(result.results[0].name).toBe("PASTA");
|
|
908
1071
|
});
|
|
909
|
-
it("should
|
|
1072
|
+
it("should apply JS-level operator filters — smoke test", async () => {
|
|
1073
|
+
// Ratings 1–5 encoded as Literals so the JS filter receives actual numbers
|
|
1074
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1075
|
+
source: `node:abc${i + 1}`,
|
|
1076
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1077
|
+
links: [
|
|
1078
|
+
{
|
|
1079
|
+
predicate: "recipe://name",
|
|
1080
|
+
target: Literal.from(`Recipe ${i + 1}`).toUrl(),
|
|
1081
|
+
author: "did:key:alice",
|
|
1082
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1083
|
+
},
|
|
1084
|
+
{
|
|
1085
|
+
predicate: "recipe://rating",
|
|
1086
|
+
target: Literal.from(i + 1).toUrl(),
|
|
1087
|
+
author: "did:key:alice",
|
|
1088
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1089
|
+
},
|
|
1090
|
+
],
|
|
1091
|
+
}));
|
|
1092
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1093
|
+
// Only ratings 4 and 5 pass the JS-level gt filter
|
|
1094
|
+
const results = await Recipe.findAll(mockPerspective, {
|
|
1095
|
+
where: { rating: { gt: 3 } },
|
|
1096
|
+
});
|
|
1097
|
+
expect(results).toHaveLength(2);
|
|
1098
|
+
// count() must be consistent with findAll()
|
|
1099
|
+
const count = await Recipe.count(mockPerspective, {
|
|
1100
|
+
where: { rating: { gt: 3 } },
|
|
1101
|
+
});
|
|
1102
|
+
expect(count).toBe(2);
|
|
1103
|
+
});
|
|
1104
|
+
it("should filter fields when query specifies fields", async () => {
|
|
910
1105
|
const surrealResults = [
|
|
911
1106
|
{
|
|
912
1107
|
source: "node:abc123",
|
|
913
1108
|
source_uri: "literal://recipe1",
|
|
914
1109
|
links: [
|
|
915
|
-
{
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
1110
|
+
{
|
|
1111
|
+
predicate: "recipe://name",
|
|
1112
|
+
target: "Pasta",
|
|
1113
|
+
author: "did:key:alice",
|
|
1114
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1115
|
+
},
|
|
1116
|
+
{
|
|
1117
|
+
predicate: "recipe://rating",
|
|
1118
|
+
target: "5",
|
|
1119
|
+
author: "did:key:alice",
|
|
1120
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1121
|
+
},
|
|
1122
|
+
{
|
|
1123
|
+
predicate: "recipe://ingredient",
|
|
1124
|
+
target: "pasta",
|
|
1125
|
+
author: "did:key:alice",
|
|
1126
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1127
|
+
},
|
|
1128
|
+
{
|
|
1129
|
+
predicate: "recipe://ingredient",
|
|
1130
|
+
target: "tomato",
|
|
1131
|
+
author: "did:key:alice",
|
|
1132
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1133
|
+
},
|
|
1134
|
+
],
|
|
1135
|
+
},
|
|
921
1136
|
];
|
|
922
|
-
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {
|
|
1137
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, { properties: ["name"] }, surrealResults);
|
|
923
1138
|
expect(result.results).toHaveLength(1);
|
|
924
1139
|
const recipe = result.results[0];
|
|
925
|
-
expect(recipe.
|
|
926
|
-
//
|
|
927
|
-
expect(recipe.name).toBeUndefined();
|
|
1140
|
+
expect(recipe.name).toBe("Pasta");
|
|
1141
|
+
// rating and ingredients should be removed since only "name" was requested
|
|
928
1142
|
expect(recipe.rating).toBeUndefined();
|
|
1143
|
+
expect(recipe.ingredients).toBeUndefined();
|
|
1144
|
+
// author and timestamp are metadata fields — they are also stripped unless
|
|
1145
|
+
// explicitly listed in `properties`. Integration tests cover the
|
|
1146
|
+
// `properties: ["author", "createdAt"]` case.
|
|
1147
|
+
expect(recipe.author).toBeUndefined();
|
|
1148
|
+
expect(recipe.createdAt).toBeUndefined();
|
|
929
1149
|
});
|
|
930
1150
|
it("should handle results missing base field", async () => {
|
|
931
1151
|
const surrealResults = [
|
|
932
1152
|
{
|
|
933
1153
|
links: [
|
|
934
|
-
{
|
|
935
|
-
|
|
936
|
-
|
|
1154
|
+
{
|
|
1155
|
+
predicate: "recipe://name",
|
|
1156
|
+
target: "Pasta",
|
|
1157
|
+
author: "did:key:alice",
|
|
1158
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1159
|
+
},
|
|
1160
|
+
{
|
|
1161
|
+
predicate: "recipe://rating",
|
|
1162
|
+
target: "5",
|
|
1163
|
+
author: "did:key:alice",
|
|
1164
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1165
|
+
},
|
|
1166
|
+
],
|
|
937
1167
|
// Missing source field
|
|
938
|
-
}
|
|
1168
|
+
},
|
|
939
1169
|
];
|
|
940
1170
|
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
941
1171
|
// Should filter out the invalid result (or handle gracefully)
|
|
@@ -948,11 +1178,26 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
948
1178
|
source: "node:abc123",
|
|
949
1179
|
source_uri: "literal://recipe1",
|
|
950
1180
|
links: [
|
|
951
|
-
{
|
|
952
|
-
|
|
953
|
-
|
|
954
|
-
|
|
955
|
-
|
|
1181
|
+
{
|
|
1182
|
+
predicate: "recipe://name",
|
|
1183
|
+
target: "Pasta",
|
|
1184
|
+
author: "did:key:alice",
|
|
1185
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1186
|
+
},
|
|
1187
|
+
{
|
|
1188
|
+
predicate: "recipe://rating",
|
|
1189
|
+
target: "5",
|
|
1190
|
+
author: "did:key:alice",
|
|
1191
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1192
|
+
},
|
|
1193
|
+
{
|
|
1194
|
+
predicate: "recipe://ingredient",
|
|
1195
|
+
target: "pasta",
|
|
1196
|
+
author: "did:key:alice",
|
|
1197
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1198
|
+
},
|
|
1199
|
+
],
|
|
1200
|
+
},
|
|
956
1201
|
];
|
|
957
1202
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
958
1203
|
const results = await Recipe.findAll(mockPerspective, {});
|
|
@@ -961,30 +1206,32 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
961
1206
|
expect(results).toHaveLength(1);
|
|
962
1207
|
expect(results[0].name).toBe("Pasta");
|
|
963
1208
|
});
|
|
964
|
-
it("should use Prolog when useSurrealDB is false in findAll()", async () => {
|
|
965
|
-
const prologResults = [{
|
|
966
|
-
AllInstances: [
|
|
967
|
-
["literal://recipe1", [["name", "Pasta"]], [["ingredients", ["pasta"]]], "2023-01-01T00:00:00Z", "did:key:alice"]
|
|
968
|
-
],
|
|
969
|
-
TotalCount: 1
|
|
970
|
-
}];
|
|
971
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
972
|
-
const results = await Recipe.findAll(mockPerspective, {}, false);
|
|
973
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
974
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
975
|
-
expect(results).toHaveLength(1);
|
|
976
|
-
});
|
|
977
1209
|
it("should use SurrealDB by default in findAllAndCount()", async () => {
|
|
978
1210
|
const surrealResults = [
|
|
979
1211
|
{
|
|
980
1212
|
source: "node:abc123",
|
|
981
1213
|
source_uri: "literal://recipe1",
|
|
982
1214
|
links: [
|
|
983
|
-
{
|
|
984
|
-
|
|
985
|
-
|
|
986
|
-
|
|
987
|
-
|
|
1215
|
+
{
|
|
1216
|
+
predicate: "recipe://name",
|
|
1217
|
+
target: "Pasta",
|
|
1218
|
+
author: "did:key:alice",
|
|
1219
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1220
|
+
},
|
|
1221
|
+
{
|
|
1222
|
+
predicate: "recipe://rating",
|
|
1223
|
+
target: "5",
|
|
1224
|
+
author: "did:key:alice",
|
|
1225
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1226
|
+
},
|
|
1227
|
+
{
|
|
1228
|
+
predicate: "recipe://ingredient",
|
|
1229
|
+
target: "pasta",
|
|
1230
|
+
author: "did:key:alice",
|
|
1231
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1232
|
+
},
|
|
1233
|
+
],
|
|
1234
|
+
},
|
|
988
1235
|
];
|
|
989
1236
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
990
1237
|
const { results, totalCount } = await Recipe.findAllAndCount(mockPerspective, {});
|
|
@@ -999,11 +1246,26 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
999
1246
|
source: "node:abc123",
|
|
1000
1247
|
source_uri: "literal://recipe1",
|
|
1001
1248
|
links: [
|
|
1002
|
-
{
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1249
|
+
{
|
|
1250
|
+
predicate: "recipe://name",
|
|
1251
|
+
target: "Pasta",
|
|
1252
|
+
author: "did:key:alice",
|
|
1253
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1254
|
+
},
|
|
1255
|
+
{
|
|
1256
|
+
predicate: "recipe://rating",
|
|
1257
|
+
target: "5",
|
|
1258
|
+
author: "did:key:alice",
|
|
1259
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1260
|
+
},
|
|
1261
|
+
{
|
|
1262
|
+
predicate: "recipe://ingredient",
|
|
1263
|
+
target: "pasta",
|
|
1264
|
+
author: "did:key:alice",
|
|
1265
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1266
|
+
},
|
|
1267
|
+
],
|
|
1268
|
+
},
|
|
1007
1269
|
];
|
|
1008
1270
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1009
1271
|
const page = await Recipe.paginate(mockPerspective, 10, 1, {});
|
|
@@ -1020,9 +1282,19 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1020
1282
|
source: `node:abc${i + 1}`,
|
|
1021
1283
|
source_uri: `literal://recipe${i + 1}`,
|
|
1022
1284
|
links: [
|
|
1023
|
-
{
|
|
1024
|
-
|
|
1025
|
-
|
|
1285
|
+
{
|
|
1286
|
+
predicate: "recipe://name",
|
|
1287
|
+
target: `Recipe ${i + 1}`,
|
|
1288
|
+
author: "did:key:alice",
|
|
1289
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1290
|
+
},
|
|
1291
|
+
{
|
|
1292
|
+
predicate: "recipe://rating",
|
|
1293
|
+
target: "5",
|
|
1294
|
+
author: "did:key:alice",
|
|
1295
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1296
|
+
},
|
|
1297
|
+
],
|
|
1026
1298
|
}));
|
|
1027
1299
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1028
1300
|
const count = await Recipe.count(mockPerspective, {});
|
|
@@ -1030,25 +1302,32 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1030
1302
|
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1031
1303
|
expect(count).toBe(5);
|
|
1032
1304
|
});
|
|
1033
|
-
it("should use Prolog when useSurrealDB is false in count()", async () => {
|
|
1034
|
-
const prologResults = [{ TotalCount: 10 }];
|
|
1035
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1036
|
-
const count = await Recipe.count(mockPerspective, {}, false);
|
|
1037
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1038
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1039
|
-
expect(count).toBe(10);
|
|
1040
|
-
});
|
|
1041
1305
|
it("should use SurrealDB by default in ModelQueryBuilder.get()", async () => {
|
|
1042
1306
|
const surrealResults = [
|
|
1043
1307
|
{
|
|
1044
1308
|
source: "node:abc123",
|
|
1045
1309
|
source_uri: "literal://recipe1",
|
|
1046
1310
|
links: [
|
|
1047
|
-
{
|
|
1048
|
-
|
|
1049
|
-
|
|
1050
|
-
|
|
1051
|
-
|
|
1311
|
+
{
|
|
1312
|
+
predicate: "recipe://name",
|
|
1313
|
+
target: "Pasta",
|
|
1314
|
+
author: "did:key:alice",
|
|
1315
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1316
|
+
},
|
|
1317
|
+
{
|
|
1318
|
+
predicate: "recipe://rating",
|
|
1319
|
+
target: "5",
|
|
1320
|
+
author: "did:key:alice",
|
|
1321
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1322
|
+
},
|
|
1323
|
+
{
|
|
1324
|
+
predicate: "recipe://ingredient",
|
|
1325
|
+
target: "pasta",
|
|
1326
|
+
author: "did:key:alice",
|
|
1327
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1328
|
+
},
|
|
1329
|
+
],
|
|
1330
|
+
},
|
|
1052
1331
|
];
|
|
1053
1332
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1054
1333
|
const results = await Recipe.query(mockPerspective)
|
|
@@ -1059,31 +1338,25 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1059
1338
|
expect(results).toHaveLength(1);
|
|
1060
1339
|
expect(results[0].name).toBe("Pasta");
|
|
1061
1340
|
});
|
|
1062
|
-
it("should use Prolog when useSurrealDB(false) in ModelQueryBuilder.get()", async () => {
|
|
1063
|
-
const prologResults = [{
|
|
1064
|
-
AllInstances: [
|
|
1065
|
-
["literal://recipe1", [["name", "Pasta"]], [["ingredients", ["pasta"]]], "2023-01-01T00:00:00Z", "did:key:alice"]
|
|
1066
|
-
],
|
|
1067
|
-
TotalCount: 1
|
|
1068
|
-
}];
|
|
1069
|
-
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1070
|
-
const results = await Recipe.query(mockPerspective)
|
|
1071
|
-
.where({ name: "Pasta" })
|
|
1072
|
-
.useSurrealDB(false)
|
|
1073
|
-
.get();
|
|
1074
|
-
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1075
|
-
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1076
|
-
expect(results).toHaveLength(1);
|
|
1077
|
-
});
|
|
1078
1341
|
it("should use SurrealDB by default in ModelQueryBuilder.count()", async () => {
|
|
1079
1342
|
// count() counts the number of rows returned by the query (one row per source)
|
|
1080
1343
|
const surrealResults = Array.from({ length: 3 }, (_, i) => ({
|
|
1081
1344
|
source: `node:abc${i + 1}`,
|
|
1082
1345
|
source_uri: `literal://recipe${i + 1}`,
|
|
1083
1346
|
links: [
|
|
1084
|
-
{
|
|
1085
|
-
|
|
1086
|
-
|
|
1347
|
+
{
|
|
1348
|
+
predicate: "recipe://name",
|
|
1349
|
+
target: `Recipe ${i + 1}`,
|
|
1350
|
+
author: "did:key:alice",
|
|
1351
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1352
|
+
},
|
|
1353
|
+
{
|
|
1354
|
+
predicate: "recipe://rating",
|
|
1355
|
+
target: "5",
|
|
1356
|
+
author: "did:key:alice",
|
|
1357
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1358
|
+
},
|
|
1359
|
+
],
|
|
1087
1360
|
}));
|
|
1088
1361
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1089
1362
|
const count = await Recipe.query(mockPerspective)
|
|
@@ -1099,11 +1372,26 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1099
1372
|
source: "node:abc123",
|
|
1100
1373
|
source_uri: "literal://recipe1",
|
|
1101
1374
|
links: [
|
|
1102
|
-
{
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1375
|
+
{
|
|
1376
|
+
predicate: "recipe://name",
|
|
1377
|
+
target: "Pasta",
|
|
1378
|
+
author: "did:key:alice",
|
|
1379
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1380
|
+
},
|
|
1381
|
+
{
|
|
1382
|
+
predicate: "recipe://rating",
|
|
1383
|
+
target: "5",
|
|
1384
|
+
author: "did:key:alice",
|
|
1385
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1386
|
+
},
|
|
1387
|
+
{
|
|
1388
|
+
predicate: "recipe://ingredient",
|
|
1389
|
+
target: "pasta",
|
|
1390
|
+
author: "did:key:alice",
|
|
1391
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1392
|
+
},
|
|
1393
|
+
],
|
|
1394
|
+
},
|
|
1107
1395
|
];
|
|
1108
1396
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1109
1397
|
const page = await Recipe.query(mockPerspective)
|
|
@@ -1116,210 +1404,363 @@ describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", ()
|
|
|
1116
1404
|
expect(page.pageNumber).toBe(1);
|
|
1117
1405
|
});
|
|
1118
1406
|
});
|
|
1119
|
-
describe("
|
|
1120
|
-
//
|
|
1121
|
-
|
|
1407
|
+
describe("resolveLanguage implicit literal default", () => {
|
|
1408
|
+
// A plain @Property with no resolveLanguage should behave identically to
|
|
1409
|
+
// resolveLanguage: "literal" — scalar values encoded as literal:// URIs
|
|
1410
|
+
// are unwrapped on read, and no createExpression call is made on write.
|
|
1411
|
+
let ImplicitLiteralModel = class ImplicitLiteralModel extends Ad4mModel {
|
|
1122
1412
|
constructor() {
|
|
1123
1413
|
super(...arguments);
|
|
1124
1414
|
this.name = "";
|
|
1125
|
-
this.
|
|
1126
|
-
this.
|
|
1415
|
+
this.count = 0;
|
|
1416
|
+
this.active = false;
|
|
1127
1417
|
}
|
|
1128
1418
|
};
|
|
1129
1419
|
__decorate([
|
|
1130
|
-
Property({ through: "
|
|
1420
|
+
Property({ through: "test://name" }),
|
|
1131
1421
|
__metadata("design:type", String)
|
|
1132
|
-
],
|
|
1422
|
+
], ImplicitLiteralModel.prototype, "name", void 0);
|
|
1133
1423
|
__decorate([
|
|
1134
|
-
Property({ through: "
|
|
1424
|
+
Property({ through: "test://count" }),
|
|
1135
1425
|
__metadata("design:type", Number)
|
|
1136
|
-
],
|
|
1426
|
+
], ImplicitLiteralModel.prototype, "count", void 0);
|
|
1137
1427
|
__decorate([
|
|
1138
|
-
|
|
1139
|
-
__metadata("design:type",
|
|
1140
|
-
],
|
|
1141
|
-
|
|
1142
|
-
|
|
1143
|
-
],
|
|
1144
|
-
// Mock perspective
|
|
1428
|
+
Property({ through: "test://active" }),
|
|
1429
|
+
__metadata("design:type", Boolean)
|
|
1430
|
+
], ImplicitLiteralModel.prototype, "active", void 0);
|
|
1431
|
+
ImplicitLiteralModel = __decorate([
|
|
1432
|
+
Model({ name: "ImplicitLiteralModel" })
|
|
1433
|
+
], ImplicitLiteralModel);
|
|
1145
1434
|
const mockPerspective = {
|
|
1146
1435
|
querySurrealDB: jest.fn(),
|
|
1147
1436
|
infer: jest.fn(),
|
|
1148
|
-
uuid:
|
|
1149
|
-
stringOrTemplateObjectToSubjectClassName: jest
|
|
1437
|
+
uuid: "test-perspective-uuid",
|
|
1438
|
+
stringOrTemplateObjectToSubjectClassName: jest
|
|
1439
|
+
.fn()
|
|
1440
|
+
.mockResolvedValue("ImplicitLiteralModel"),
|
|
1150
1441
|
};
|
|
1151
|
-
beforeEach(() =>
|
|
1152
|
-
|
|
1153
|
-
|
|
1154
|
-
|
|
1155
|
-
|
|
1156
|
-
|
|
1157
|
-
|
|
1158
|
-
|
|
1159
|
-
|
|
1160
|
-
|
|
1161
|
-
|
|
1162
|
-
|
|
1163
|
-
|
|
1442
|
+
beforeEach(() => jest.clearAllMocks());
|
|
1443
|
+
it('should store undefined resolveLanguage in metadata (not "literal")', () => {
|
|
1444
|
+
const metadata = ImplicitLiteralModel.getModelMetadata();
|
|
1445
|
+
expect(metadata.properties.name.resolveLanguage).toBeUndefined();
|
|
1446
|
+
expect(metadata.properties.count.resolveLanguage).toBeUndefined();
|
|
1447
|
+
expect(metadata.properties.active.resolveLanguage).toBeUndefined();
|
|
1448
|
+
});
|
|
1449
|
+
it("should hydrate literal:// encoded targets for properties with no resolveLanguage", async () => {
|
|
1450
|
+
const surrealResults = [
|
|
1451
|
+
{
|
|
1452
|
+
source: "node:abc123",
|
|
1453
|
+
source_uri: "literal://base",
|
|
1454
|
+
links: [
|
|
1455
|
+
{
|
|
1456
|
+
predicate: "test://name",
|
|
1457
|
+
target: Literal.from("hello world").toUrl(),
|
|
1458
|
+
author: "did:key:alice",
|
|
1459
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1460
|
+
},
|
|
1461
|
+
{
|
|
1462
|
+
predicate: "test://count",
|
|
1463
|
+
target: Literal.from(42).toUrl(),
|
|
1464
|
+
author: "did:key:alice",
|
|
1465
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1466
|
+
},
|
|
1467
|
+
{
|
|
1468
|
+
predicate: "test://active",
|
|
1469
|
+
target: Literal.from(true).toUrl(),
|
|
1470
|
+
author: "did:key:alice",
|
|
1471
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1472
|
+
},
|
|
1473
|
+
],
|
|
1474
|
+
},
|
|
1475
|
+
];
|
|
1164
1476
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1165
|
-
|
|
1166
|
-
|
|
1167
|
-
|
|
1168
|
-
|
|
1169
|
-
expect(count).toBe(
|
|
1170
|
-
expect(
|
|
1477
|
+
const result = await ImplicitLiteralModel.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
1478
|
+
expect(result.results).toHaveLength(1);
|
|
1479
|
+
const instance = result.results[0];
|
|
1480
|
+
expect(instance.name).toBe("hello world");
|
|
1481
|
+
expect(instance.count).toBe(42);
|
|
1482
|
+
expect(instance.active).toBe(true);
|
|
1171
1483
|
});
|
|
1172
|
-
it("should
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1180
|
-
|
|
1181
|
-
|
|
1484
|
+
it("should NOT call perspective.getExpression for properties with no resolveLanguage", async () => {
|
|
1485
|
+
const surrealResults = [
|
|
1486
|
+
{
|
|
1487
|
+
source: "node:abc123",
|
|
1488
|
+
source_uri: "literal://base",
|
|
1489
|
+
links: [
|
|
1490
|
+
{
|
|
1491
|
+
predicate: "test://name",
|
|
1492
|
+
target: Literal.from("test").toUrl(),
|
|
1493
|
+
author: "did:key:alice",
|
|
1494
|
+
timestamp: "2023-01-01T00:00:00Z",
|
|
1495
|
+
},
|
|
1496
|
+
],
|
|
1497
|
+
},
|
|
1498
|
+
];
|
|
1182
1499
|
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1187
|
-
expect(count).toBe(3);
|
|
1188
|
-
expect(count).toBe(findAllResults.length);
|
|
1500
|
+
const getExpression = jest.fn();
|
|
1501
|
+
mockPerspective.getExpression = getExpression;
|
|
1502
|
+
await ImplicitLiteralModel.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
1503
|
+
expect(getExpression).not.toHaveBeenCalled();
|
|
1189
1504
|
});
|
|
1190
|
-
|
|
1191
|
-
|
|
1192
|
-
|
|
1193
|
-
|
|
1194
|
-
|
|
1195
|
-
|
|
1196
|
-
|
|
1197
|
-
|
|
1198
|
-
|
|
1199
|
-
|
|
1200
|
-
|
|
1201
|
-
|
|
1202
|
-
|
|
1203
|
-
|
|
1204
|
-
|
|
1205
|
-
|
|
1206
|
-
|
|
1207
|
-
|
|
1505
|
+
});
|
|
1506
|
+
describe("resolveParentPredicate()", () => {
|
|
1507
|
+
let Comment = class Comment extends Ad4mModel {
|
|
1508
|
+
};
|
|
1509
|
+
Comment = __decorate([
|
|
1510
|
+
Model({ name: "Comment" })
|
|
1511
|
+
], Comment);
|
|
1512
|
+
let Tag = class Tag extends Ad4mModel {
|
|
1513
|
+
};
|
|
1514
|
+
Tag = __decorate([
|
|
1515
|
+
Model({ name: "Tag" })
|
|
1516
|
+
], Tag);
|
|
1517
|
+
let Channel = class Channel extends Ad4mModel {
|
|
1518
|
+
constructor() {
|
|
1519
|
+
super(...arguments);
|
|
1520
|
+
this.comments = [];
|
|
1521
|
+
this.tags = [];
|
|
1522
|
+
}
|
|
1523
|
+
};
|
|
1524
|
+
__decorate([
|
|
1525
|
+
HasMany(() => Comment, { through: "channel://has_comment" }),
|
|
1526
|
+
__metadata("design:type", Array)
|
|
1527
|
+
], Channel.prototype, "comments", void 0);
|
|
1528
|
+
__decorate([
|
|
1529
|
+
HasMany(() => Tag, { through: "channel://has_tag" }),
|
|
1530
|
+
__metadata("design:type", Array)
|
|
1531
|
+
], Channel.prototype, "tags", void 0);
|
|
1532
|
+
Channel = __decorate([
|
|
1533
|
+
Model({ name: "Channel" })
|
|
1534
|
+
], Channel);
|
|
1535
|
+
let AmbiguousParent = class AmbiguousParent extends Ad4mModel {
|
|
1536
|
+
constructor() {
|
|
1537
|
+
super(...arguments);
|
|
1538
|
+
this.primaryComments = [];
|
|
1539
|
+
this.secondaryComments = [];
|
|
1540
|
+
}
|
|
1541
|
+
};
|
|
1542
|
+
__decorate([
|
|
1543
|
+
HasMany(() => Comment, { through: "parent://primary_comments" }),
|
|
1544
|
+
__metadata("design:type", Array)
|
|
1545
|
+
], AmbiguousParent.prototype, "primaryComments", void 0);
|
|
1546
|
+
__decorate([
|
|
1547
|
+
HasMany(() => Comment, { through: "parent://secondary_comments" }),
|
|
1548
|
+
__metadata("design:type", Array)
|
|
1549
|
+
], AmbiguousParent.prototype, "secondaryComments", void 0);
|
|
1550
|
+
AmbiguousParent = __decorate([
|
|
1551
|
+
Model({ name: "AmbiguousParent" })
|
|
1552
|
+
], AmbiguousParent);
|
|
1553
|
+
it("resolves predicate by explicit field name", () => {
|
|
1554
|
+
const meta = Channel.getModelMetadata();
|
|
1555
|
+
expect(resolveParentPredicate(meta, Comment, "comments")).toBe("channel://has_comment");
|
|
1556
|
+
});
|
|
1557
|
+
it("infers predicate from child constructor when field is omitted", () => {
|
|
1558
|
+
const meta = Channel.getModelMetadata();
|
|
1559
|
+
expect(resolveParentPredicate(meta, Comment)).toBe("channel://has_comment");
|
|
1560
|
+
});
|
|
1561
|
+
it("throws when explicit field does not exist on parent", () => {
|
|
1562
|
+
const meta = Channel.getModelMetadata();
|
|
1563
|
+
expect(() => resolveParentPredicate(meta, Comment, "nonExistent")).toThrow(/field "nonExistent" not found/);
|
|
1564
|
+
});
|
|
1565
|
+
it("throws when no relation on parent matches the child type", () => {
|
|
1566
|
+
let UnrelatedParent = class UnrelatedParent extends Ad4mModel {
|
|
1567
|
+
};
|
|
1568
|
+
UnrelatedParent = __decorate([
|
|
1569
|
+
Model({ name: "UnrelatedParent" })
|
|
1570
|
+
], UnrelatedParent);
|
|
1571
|
+
const meta = UnrelatedParent.getModelMetadata();
|
|
1572
|
+
expect(() => resolveParentPredicate(meta, Comment)).toThrow(/no forward relation pointing to "Comment"/);
|
|
1208
1573
|
});
|
|
1209
|
-
it("
|
|
1210
|
-
|
|
1211
|
-
|
|
1212
|
-
|
|
1213
|
-
|
|
1214
|
-
|
|
1215
|
-
|
|
1216
|
-
|
|
1217
|
-
|
|
1218
|
-
|
|
1219
|
-
|
|
1220
|
-
|
|
1221
|
-
|
|
1222
|
-
|
|
1223
|
-
|
|
1224
|
-
|
|
1225
|
-
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1574
|
+
it("throws when neither field nor childCtor is provided", () => {
|
|
1575
|
+
const meta = Channel.getModelMetadata();
|
|
1576
|
+
expect(() => resolveParentPredicate(meta, undefined)).toThrow(/either "field" or a child model constructor must be provided/);
|
|
1577
|
+
});
|
|
1578
|
+
it("throws with disambiguation hint when multiple relations point to the same type", () => {
|
|
1579
|
+
const meta = AmbiguousParent.getModelMetadata();
|
|
1580
|
+
expect(() => resolveParentPredicate(meta, Comment)).toThrow(/multiple relations.*provide "field" to disambiguate/i);
|
|
1581
|
+
});
|
|
1582
|
+
});
|
|
1583
|
+
describe("normalizeParentQuery()", () => {
|
|
1584
|
+
let NpqTag = class NpqTag extends Ad4mModel {
|
|
1585
|
+
};
|
|
1586
|
+
NpqTag = __decorate([
|
|
1587
|
+
Model({ name: "NpqTag" })
|
|
1588
|
+
], NpqTag);
|
|
1589
|
+
let NpqComment = class NpqComment extends Ad4mModel {
|
|
1590
|
+
};
|
|
1591
|
+
NpqComment = __decorate([
|
|
1592
|
+
Model({ name: "NpqComment" })
|
|
1593
|
+
], NpqComment);
|
|
1594
|
+
let NpqBlogPost = class NpqBlogPost extends Ad4mModel {
|
|
1595
|
+
constructor() {
|
|
1596
|
+
super(...arguments);
|
|
1597
|
+
this.tags = [];
|
|
1598
|
+
this.comments = [];
|
|
1599
|
+
this.pinnedComments = [];
|
|
1600
|
+
}
|
|
1601
|
+
};
|
|
1602
|
+
__decorate([
|
|
1603
|
+
HasMany(() => NpqTag, { through: "blog://has_tag" }),
|
|
1604
|
+
__metadata("design:type", Array)
|
|
1605
|
+
], NpqBlogPost.prototype, "tags", void 0);
|
|
1606
|
+
__decorate([
|
|
1607
|
+
HasMany(() => NpqComment, { through: "blog://has_comment" }),
|
|
1608
|
+
__metadata("design:type", Array)
|
|
1609
|
+
], NpqBlogPost.prototype, "comments", void 0);
|
|
1610
|
+
__decorate([
|
|
1611
|
+
HasMany(() => NpqComment, { through: "blog://has_pinned_comment" }),
|
|
1612
|
+
__metadata("design:type", Array)
|
|
1613
|
+
], NpqBlogPost.prototype, "pinnedComments", void 0);
|
|
1614
|
+
NpqBlogPost = __decorate([
|
|
1615
|
+
Model({ name: "NpqBlogPost" })
|
|
1616
|
+
], NpqBlogPost);
|
|
1617
|
+
it("passes through raw { id, predicate } form unchanged", () => {
|
|
1618
|
+
const raw = { id: "some://uri", predicate: "custom://predicate" };
|
|
1619
|
+
expect(normalizeParentQuery(raw)).toEqual(raw);
|
|
1620
|
+
});
|
|
1621
|
+
it("resolves model-backed form with explicit field", () => {
|
|
1622
|
+
const result = normalizeParentQuery({
|
|
1623
|
+
id: "some://uri",
|
|
1624
|
+
model: NpqBlogPost,
|
|
1625
|
+
field: "tags",
|
|
1229
1626
|
});
|
|
1230
|
-
expect(
|
|
1231
|
-
expect(count).toBe(findAllResults.length);
|
|
1627
|
+
expect(result).toEqual({ id: "some://uri", predicate: "blog://has_tag" });
|
|
1232
1628
|
});
|
|
1233
|
-
it("
|
|
1234
|
-
|
|
1235
|
-
|
|
1236
|
-
...Array.from({ length: 3 }, (_, i) => ({
|
|
1237
|
-
source: `node:abc${i + 1}`,
|
|
1238
|
-
source_uri: `literal://recipe${i + 1}`,
|
|
1239
|
-
links: [
|
|
1240
|
-
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1241
|
-
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1242
|
-
]
|
|
1243
|
-
})),
|
|
1244
|
-
...Array.from({ length: 2 }, (_, i) => ({
|
|
1245
|
-
source: `node:def${i + 4}`,
|
|
1246
|
-
source_uri: `literal://recipe${i + 4}`,
|
|
1247
|
-
links: [
|
|
1248
|
-
{ predicate: "recipe://name", target: `Recipe ${i + 4}`, author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
1249
|
-
{ predicate: "recipe://rating", target: "5", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" }
|
|
1250
|
-
]
|
|
1251
|
-
}))
|
|
1252
|
-
];
|
|
1253
|
-
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1254
|
-
// Count recipes by Alice (should match 3 recipes)
|
|
1255
|
-
const count = await Recipe.count(mockPerspective, { where: { author: "did:key:alice" } });
|
|
1256
|
-
// Verify count matches the number of instances that would be returned by findAll
|
|
1257
|
-
const findAllResults = await Recipe.findAll(mockPerspective, { where: { author: "did:key:alice" } });
|
|
1258
|
-
expect(count).toBe(3);
|
|
1259
|
-
expect(count).toBe(findAllResults.length);
|
|
1629
|
+
it("infers predicate from childCtor when field is omitted", () => {
|
|
1630
|
+
const result = normalizeParentQuery({ id: "some://uri", model: NpqBlogPost }, NpqTag);
|
|
1631
|
+
expect(result).toEqual({ id: "some://uri", predicate: "blog://has_tag" });
|
|
1260
1632
|
});
|
|
1261
|
-
it("
|
|
1262
|
-
|
|
1263
|
-
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1264
|
-
source: `node:abc${i + 1}`,
|
|
1265
|
-
source_uri: `literal://recipe${i + 1}`,
|
|
1266
|
-
links: [
|
|
1267
|
-
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1268
|
-
{ predicate: "recipe://rating", target: `${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1269
|
-
]
|
|
1270
|
-
}));
|
|
1271
|
-
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1272
|
-
// Count recipes with rating > 3 using ModelQueryBuilder
|
|
1273
|
-
const count = await Recipe.query(mockPerspective)
|
|
1274
|
-
.where({ rating: { gt: 3 } })
|
|
1275
|
-
.count();
|
|
1276
|
-
// Verify count matches the number of instances that would be returned by get()
|
|
1277
|
-
const getResults = await Recipe.query(mockPerspective)
|
|
1278
|
-
.where({ rating: { gt: 3 } })
|
|
1279
|
-
.get();
|
|
1280
|
-
expect(count).toBe(2);
|
|
1281
|
-
expect(count).toBe(getResults.length);
|
|
1633
|
+
it("throws when model-backed form has no field and multiple relations point to the same child type", () => {
|
|
1634
|
+
expect(() => normalizeParentQuery({ id: "some://uri", model: NpqBlogPost }, NpqComment)).toThrow(/multiple relations.*provide "field" to disambiguate/i);
|
|
1282
1635
|
});
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1305
|
-
|
|
1306
|
-
|
|
1307
|
-
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
const
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1636
|
+
});
|
|
1637
|
+
describe("Ad4mModel.update() — static convenience method", () => {
|
|
1638
|
+
let UpdatableModel = class UpdatableModel extends Ad4mModel {
|
|
1639
|
+
constructor() {
|
|
1640
|
+
super(...arguments);
|
|
1641
|
+
this.title = "";
|
|
1642
|
+
this.body = "";
|
|
1643
|
+
this.count = 0;
|
|
1644
|
+
}
|
|
1645
|
+
};
|
|
1646
|
+
__decorate([
|
|
1647
|
+
Property({ through: "test://title" }),
|
|
1648
|
+
__metadata("design:type", String)
|
|
1649
|
+
], UpdatableModel.prototype, "title", void 0);
|
|
1650
|
+
__decorate([
|
|
1651
|
+
Property({ through: "test://body" }),
|
|
1652
|
+
__metadata("design:type", String)
|
|
1653
|
+
], UpdatableModel.prototype, "body", void 0);
|
|
1654
|
+
__decorate([
|
|
1655
|
+
Property({ through: "test://count" }),
|
|
1656
|
+
__metadata("design:type", Number)
|
|
1657
|
+
], UpdatableModel.prototype, "count", void 0);
|
|
1658
|
+
UpdatableModel = __decorate([
|
|
1659
|
+
Model({ name: "UpdatableModel" })
|
|
1660
|
+
], UpdatableModel);
|
|
1661
|
+
it("calls get() then save() after merging data", async () => {
|
|
1662
|
+
const existingTitle = "Original Title";
|
|
1663
|
+
const existingBody = "Original Body";
|
|
1664
|
+
const mockPerspective = {
|
|
1665
|
+
querySurrealDB: jest.fn().mockResolvedValue([
|
|
1666
|
+
{
|
|
1667
|
+
source: "literal://string:abc123",
|
|
1668
|
+
source_uri: "literal://string:abc123",
|
|
1669
|
+
links: [
|
|
1670
|
+
{ predicate: "test://title", target: `literal://string:${existingTitle}`, author: "did:key:alice", timestamp: "2024-01-01T00:00:00Z" },
|
|
1671
|
+
{ predicate: "test://body", target: `literal://string:${existingBody}`, author: "did:key:alice", timestamp: "2024-01-01T00:00:00Z" },
|
|
1672
|
+
{ predicate: "test://count", target: "literal://number:5", author: "did:key:alice", timestamp: "2024-01-01T00:00:00Z" },
|
|
1673
|
+
],
|
|
1674
|
+
},
|
|
1675
|
+
]),
|
|
1676
|
+
infer: jest.fn(),
|
|
1677
|
+
uuid: "test-uuid",
|
|
1678
|
+
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue("UpdatableModel"),
|
|
1679
|
+
add: jest.fn(),
|
|
1680
|
+
remove: jest.fn(),
|
|
1681
|
+
get: jest.fn().mockResolvedValue([]),
|
|
1682
|
+
removeSubject: jest.fn(),
|
|
1683
|
+
createSubject: jest.fn(),
|
|
1684
|
+
subjectEntities: jest.fn().mockResolvedValue([]),
|
|
1685
|
+
};
|
|
1686
|
+
const saveSpy = jest.spyOn(UpdatableModel.prototype, "save").mockResolvedValue();
|
|
1687
|
+
const getSpy = jest.spyOn(UpdatableModel.prototype, "get").mockImplementation(async function () {
|
|
1688
|
+
this.title = existingTitle;
|
|
1689
|
+
this.body = existingBody;
|
|
1690
|
+
this.count = 5;
|
|
1691
|
+
return this;
|
|
1692
|
+
});
|
|
1693
|
+
const result = await UpdatableModel.update(mockPerspective, "literal://string:abc123", { title: "New Title" });
|
|
1694
|
+
// get() must be called to fetch the pre-existing state
|
|
1695
|
+
expect(getSpy).toHaveBeenCalledTimes(1);
|
|
1696
|
+
// save() must be called after merging
|
|
1697
|
+
expect(saveSpy).toHaveBeenCalledTimes(1);
|
|
1698
|
+
// Only the provided field was overwritten
|
|
1699
|
+
expect(result.title).toBe("New Title");
|
|
1700
|
+
// Other fields are preserved from the fetched state
|
|
1701
|
+
expect(result.body).toBe(existingBody);
|
|
1702
|
+
expect(result.count).toBe(5);
|
|
1703
|
+
getSpy.mockRestore();
|
|
1704
|
+
saveSpy.mockRestore();
|
|
1705
|
+
});
|
|
1706
|
+
it("returns the updated instance", async () => {
|
|
1707
|
+
const saveSpy = jest.spyOn(UpdatableModel.prototype, "save").mockResolvedValue();
|
|
1708
|
+
const getSpy = jest.spyOn(UpdatableModel.prototype, "get").mockImplementation(async function () {
|
|
1709
|
+
this.title = "Old";
|
|
1710
|
+
this.body = "kept";
|
|
1711
|
+
return this;
|
|
1712
|
+
});
|
|
1713
|
+
const mockPerspective = {};
|
|
1714
|
+
const result = await UpdatableModel.update(mockPerspective, "literal://string:xyz", { title: "New" });
|
|
1715
|
+
expect(result).toBeInstanceOf(UpdatableModel);
|
|
1716
|
+
expect(result.id).toBe("literal://string:xyz");
|
|
1717
|
+
getSpy.mockRestore();
|
|
1718
|
+
saveSpy.mockRestore();
|
|
1719
|
+
});
|
|
1720
|
+
it("passes batchId through to save()", async () => {
|
|
1721
|
+
const saveSpy = jest.spyOn(UpdatableModel.prototype, "save").mockResolvedValue();
|
|
1722
|
+
const getSpy = jest.spyOn(UpdatableModel.prototype, "get").mockImplementation(async function () {
|
|
1723
|
+
return this;
|
|
1724
|
+
});
|
|
1725
|
+
await UpdatableModel.update({}, "literal://string:id1", {}, "batch-42");
|
|
1726
|
+
expect(saveSpy).toHaveBeenCalledWith("batch-42");
|
|
1727
|
+
getSpy.mockRestore();
|
|
1728
|
+
saveSpy.mockRestore();
|
|
1314
1729
|
});
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1730
|
+
});
|
|
1731
|
+
describe("Ad4mModel.delete() — static convenience method", () => {
|
|
1732
|
+
let DeletableModel = class DeletableModel extends Ad4mModel {
|
|
1733
|
+
constructor() {
|
|
1734
|
+
super(...arguments);
|
|
1735
|
+
this.label = "";
|
|
1736
|
+
}
|
|
1737
|
+
};
|
|
1738
|
+
__decorate([
|
|
1739
|
+
Property({ through: "test://label" }),
|
|
1740
|
+
__metadata("design:type", String)
|
|
1741
|
+
], DeletableModel.prototype, "label", void 0);
|
|
1742
|
+
DeletableModel = __decorate([
|
|
1743
|
+
Model({ name: "DeletableModel" })
|
|
1744
|
+
], DeletableModel);
|
|
1745
|
+
it("calls instance.delete() with no batchId by default", async () => {
|
|
1746
|
+
const deleteSpy = jest.spyOn(DeletableModel.prototype, "delete").mockResolvedValue();
|
|
1747
|
+
await DeletableModel.delete({}, "literal://string:del1");
|
|
1748
|
+
expect(deleteSpy).toHaveBeenCalledTimes(1);
|
|
1749
|
+
expect(deleteSpy).toHaveBeenCalledWith(undefined);
|
|
1750
|
+
deleteSpy.mockRestore();
|
|
1751
|
+
});
|
|
1752
|
+
it("forwards batchId to instance.delete()", async () => {
|
|
1753
|
+
const deleteSpy = jest.spyOn(DeletableModel.prototype, "delete").mockResolvedValue();
|
|
1754
|
+
await DeletableModel.delete({}, "literal://string:del2", "batch-99");
|
|
1755
|
+
expect(deleteSpy).toHaveBeenCalledWith("batch-99");
|
|
1756
|
+
deleteSpy.mockRestore();
|
|
1757
|
+
});
|
|
1758
|
+
it("constructs an instance with the given id", async () => {
|
|
1759
|
+
const deleteSpy = jest.spyOn(DeletableModel.prototype, "delete").mockResolvedValue();
|
|
1760
|
+
const targetId = "literal://string:target-id";
|
|
1761
|
+
await DeletableModel.delete({}, targetId);
|
|
1762
|
+
// The static method must target the correct id
|
|
1763
|
+
expect(deleteSpy.mock.instances[0].id).toBe(targetId);
|
|
1764
|
+
deleteSpy.mockRestore();
|
|
1324
1765
|
});
|
|
1325
1766
|
});
|