@coasys/ad4m 0.10.1-release-candidate-5 → 0.11.0
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/lib/index.cjs +1716 -87
- package/lib/index.js +1716 -87
- package/lib/src/model/Ad4mModel.d.ts +423 -9
- package/lib/src/model/Ad4mModel.js +1499 -61
- package/lib/src/model/Ad4mModel.test.d.ts +1 -0
- package/lib/src/model/Ad4mModel.test.js +1325 -0
- package/lib/src/perspectives/PerspectiveClient.d.ts +17 -0
- package/lib/src/perspectives/PerspectiveClient.js +61 -0
- package/lib/src/perspectives/PerspectiveProxy.d.ts +52 -1
- package/lib/src/perspectives/PerspectiveProxy.js +157 -28
- package/lib/src/perspectives/PerspectiveResolver.d.ts +1 -0
- package/lib/src/perspectives/PerspectiveResolver.js +11 -0
- package/lib/src/schema.gql +1 -0
- package/lib/src/typeDefs.js +1 -0
- package/package.json +1 -1
- package/src/model/Ad4mModel.test.ts +1476 -0
- package/src/model/Ad4mModel.ts +1758 -87
- package/src/perspectives/PerspectiveClient.ts +67 -0
- package/src/perspectives/PerspectiveProxy.ts +175 -35
- package/src/perspectives/PerspectiveResolver.ts +8 -0
|
@@ -0,0 +1,1325 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
var __metadata = (this && this.__metadata) || function (k, v) {
|
|
8
|
+
if (typeof Reflect === "object" && typeof Reflect.metadata === "function") return Reflect.metadata(k, v);
|
|
9
|
+
};
|
|
10
|
+
import { Ad4mModel } from "./Ad4mModel";
|
|
11
|
+
import { ModelOptions, Property, Optional, ReadOnly, Collection, Flag } from "./decorators";
|
|
12
|
+
describe("Ad4mModel.getModelMetadata()", () => {
|
|
13
|
+
it("should extract basic model metadata with className", () => {
|
|
14
|
+
let SimpleModel = class SimpleModel extends Ad4mModel {
|
|
15
|
+
};
|
|
16
|
+
SimpleModel = __decorate([
|
|
17
|
+
ModelOptions({ name: "SimpleModel" })
|
|
18
|
+
], SimpleModel);
|
|
19
|
+
const metadata = SimpleModel.getModelMetadata();
|
|
20
|
+
expect(metadata.className).toBe("SimpleModel");
|
|
21
|
+
expect(metadata.properties).toEqual({});
|
|
22
|
+
expect(metadata.collections).toEqual({});
|
|
23
|
+
});
|
|
24
|
+
it("should extract property metadata with all fields", () => {
|
|
25
|
+
let PropertyModel = class PropertyModel extends Ad4mModel {
|
|
26
|
+
constructor() {
|
|
27
|
+
super(...arguments);
|
|
28
|
+
this.name = "";
|
|
29
|
+
this.optional = "";
|
|
30
|
+
this.readonly = "";
|
|
31
|
+
this.type = "";
|
|
32
|
+
}
|
|
33
|
+
};
|
|
34
|
+
__decorate([
|
|
35
|
+
Property({ through: "test://name", resolveLanguage: "literal" }),
|
|
36
|
+
__metadata("design:type", String)
|
|
37
|
+
], PropertyModel.prototype, "name", void 0);
|
|
38
|
+
__decorate([
|
|
39
|
+
Optional({ through: "test://optional", writable: true }),
|
|
40
|
+
__metadata("design:type", String)
|
|
41
|
+
], PropertyModel.prototype, "optional", void 0);
|
|
42
|
+
__decorate([
|
|
43
|
+
ReadOnly({ through: "test://readonly", getter: "custom_getter" }),
|
|
44
|
+
__metadata("design:type", String)
|
|
45
|
+
], PropertyModel.prototype, "readonly", void 0);
|
|
46
|
+
__decorate([
|
|
47
|
+
Flag({ through: "test://type", value: "test://flag" }),
|
|
48
|
+
__metadata("design:type", String)
|
|
49
|
+
], PropertyModel.prototype, "type", void 0);
|
|
50
|
+
PropertyModel = __decorate([
|
|
51
|
+
ModelOptions({ name: "PropertyModel" })
|
|
52
|
+
], PropertyModel);
|
|
53
|
+
const metadata = PropertyModel.getModelMetadata();
|
|
54
|
+
// Should have 4 properties
|
|
55
|
+
expect(Object.keys(metadata.properties)).toHaveLength(4);
|
|
56
|
+
// Verify "name" property
|
|
57
|
+
expect(metadata.properties.name.predicate).toBe("test://name");
|
|
58
|
+
expect(metadata.properties.name.required).toBe(true);
|
|
59
|
+
expect(metadata.properties.name.writable).toBe(true);
|
|
60
|
+
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
61
|
+
// Verify "optional" property
|
|
62
|
+
expect(metadata.properties.optional.predicate).toBe("test://optional");
|
|
63
|
+
expect(metadata.properties.optional.writable).toBe(true);
|
|
64
|
+
// Verify "readonly" property
|
|
65
|
+
expect(metadata.properties.readonly.predicate).toBe("test://readonly");
|
|
66
|
+
expect(metadata.properties.readonly.writable).toBe(false);
|
|
67
|
+
expect(metadata.properties.readonly.getter).toBe("custom_getter");
|
|
68
|
+
// Verify "type" property (flag)
|
|
69
|
+
expect(metadata.properties.type.predicate).toBe("test://type");
|
|
70
|
+
expect(metadata.properties.type.flag).toBe(true);
|
|
71
|
+
expect(metadata.properties.type.initial).toBe("test://flag");
|
|
72
|
+
});
|
|
73
|
+
it("should extract collection metadata with where clauses", () => {
|
|
74
|
+
let CollectionModel = class CollectionModel extends Ad4mModel {
|
|
75
|
+
constructor() {
|
|
76
|
+
super(...arguments);
|
|
77
|
+
this.items = [];
|
|
78
|
+
this.filtered = [];
|
|
79
|
+
this.local = [];
|
|
80
|
+
}
|
|
81
|
+
};
|
|
82
|
+
__decorate([
|
|
83
|
+
Collection({ through: "test://items" }),
|
|
84
|
+
__metadata("design:type", Array)
|
|
85
|
+
], CollectionModel.prototype, "items", void 0);
|
|
86
|
+
__decorate([
|
|
87
|
+
Collection({
|
|
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 }),
|
|
95
|
+
__metadata("design:type", Array)
|
|
96
|
+
], CollectionModel.prototype, "local", void 0);
|
|
97
|
+
CollectionModel = __decorate([
|
|
98
|
+
ModelOptions({ name: "CollectionModel" })
|
|
99
|
+
], CollectionModel);
|
|
100
|
+
const metadata = CollectionModel.getModelMetadata();
|
|
101
|
+
// Should have 3 collections
|
|
102
|
+
expect(Object.keys(metadata.collections)).toHaveLength(3);
|
|
103
|
+
// Verify "items" collection
|
|
104
|
+
expect(metadata.collections.items.predicate).toBe("test://items");
|
|
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')");
|
|
109
|
+
// Verify "local" collection
|
|
110
|
+
expect(metadata.collections.local.predicate).toBe("test://local");
|
|
111
|
+
expect(metadata.collections.local.local).toBe(true);
|
|
112
|
+
});
|
|
113
|
+
it("should extract transform function from property metadata", () => {
|
|
114
|
+
let TransformModel = class TransformModel extends Ad4mModel {
|
|
115
|
+
constructor() {
|
|
116
|
+
super(...arguments);
|
|
117
|
+
this.data = "";
|
|
118
|
+
}
|
|
119
|
+
};
|
|
120
|
+
__decorate([
|
|
121
|
+
Optional({
|
|
122
|
+
through: "test://data",
|
|
123
|
+
transform: (value) => value.toUpperCase()
|
|
124
|
+
}),
|
|
125
|
+
__metadata("design:type", String)
|
|
126
|
+
], TransformModel.prototype, "data", void 0);
|
|
127
|
+
TransformModel = __decorate([
|
|
128
|
+
ModelOptions({ name: "TransformModel" })
|
|
129
|
+
], TransformModel);
|
|
130
|
+
const metadata = TransformModel.getModelMetadata();
|
|
131
|
+
// Assert transform is a function
|
|
132
|
+
expect(typeof metadata.properties.data.transform).toBe("function");
|
|
133
|
+
// Test the transform function
|
|
134
|
+
const transformed = metadata.properties.data.transform("test");
|
|
135
|
+
expect(transformed).toBe("TEST");
|
|
136
|
+
});
|
|
137
|
+
it("should extract custom getter and setter from property metadata", () => {
|
|
138
|
+
let CustomModel = class CustomModel extends Ad4mModel {
|
|
139
|
+
constructor() {
|
|
140
|
+
super(...arguments);
|
|
141
|
+
this.computed = 0;
|
|
142
|
+
}
|
|
143
|
+
};
|
|
144
|
+
__decorate([
|
|
145
|
+
Optional({
|
|
146
|
+
through: "test://computed",
|
|
147
|
+
getter: "triple(Base, 'test://value', V), Value is V * 2",
|
|
148
|
+
setter: "Value is V / 2, Actions = [{action: 'setSingleTarget', source: 'this', predicate: 'test://value', target: Value}]"
|
|
149
|
+
}),
|
|
150
|
+
__metadata("design:type", Number)
|
|
151
|
+
], CustomModel.prototype, "computed", void 0);
|
|
152
|
+
CustomModel = __decorate([
|
|
153
|
+
ModelOptions({ name: "CustomModel" })
|
|
154
|
+
], CustomModel);
|
|
155
|
+
const metadata = CustomModel.getModelMetadata();
|
|
156
|
+
// Assert getter and setter contain the custom code
|
|
157
|
+
expect(metadata.properties.computed.getter).toContain("triple(Base, 'test://value', V), Value is V * 2");
|
|
158
|
+
expect(metadata.properties.computed.setter).toContain("Value is V / 2");
|
|
159
|
+
expect(metadata.properties.computed.setter).toContain("setSingleTarget");
|
|
160
|
+
});
|
|
161
|
+
it("should handle collection with isInstance where clause", () => {
|
|
162
|
+
let Comment = class Comment extends Ad4mModel {
|
|
163
|
+
};
|
|
164
|
+
Comment = __decorate([
|
|
165
|
+
ModelOptions({ name: "Comment" })
|
|
166
|
+
], Comment);
|
|
167
|
+
let Post = class Post extends Ad4mModel {
|
|
168
|
+
constructor() {
|
|
169
|
+
super(...arguments);
|
|
170
|
+
this.comments = [];
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
__decorate([
|
|
174
|
+
Collection({
|
|
175
|
+
through: "post://comment",
|
|
176
|
+
where: { isInstance: Comment }
|
|
177
|
+
}),
|
|
178
|
+
__metadata("design:type", Array)
|
|
179
|
+
], Post.prototype, "comments", void 0);
|
|
180
|
+
Post = __decorate([
|
|
181
|
+
ModelOptions({ name: "Post" })
|
|
182
|
+
], Post);
|
|
183
|
+
const metadata = Post.getModelMetadata();
|
|
184
|
+
// Assert isInstance is defined
|
|
185
|
+
expect(metadata.collections.comments.where?.isInstance).toBeDefined();
|
|
186
|
+
});
|
|
187
|
+
it("should throw error for class without @ModelOptions decorator", () => {
|
|
188
|
+
class NoDecoratorModel extends Ad4mModel {
|
|
189
|
+
}
|
|
190
|
+
// Assert that calling getModelMetadata throws an error
|
|
191
|
+
expect(() => NoDecoratorModel.getModelMetadata()).toThrow("Model class must be decorated with @ModelOptions");
|
|
192
|
+
});
|
|
193
|
+
it("should handle complex model with mixed property and collection types", () => {
|
|
194
|
+
let Recipe = class Recipe extends Ad4mModel {
|
|
195
|
+
constructor() {
|
|
196
|
+
super(...arguments);
|
|
197
|
+
this.name = "";
|
|
198
|
+
this.description = "";
|
|
199
|
+
this.rating = 0;
|
|
200
|
+
this.ingredients = [];
|
|
201
|
+
this.steps = [];
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
__decorate([
|
|
205
|
+
Property({ through: "recipe://name", resolveLanguage: "literal" }),
|
|
206
|
+
__metadata("design:type", String)
|
|
207
|
+
], Recipe.prototype, "name", void 0);
|
|
208
|
+
__decorate([
|
|
209
|
+
Optional({ through: "recipe://description" }),
|
|
210
|
+
__metadata("design:type", String)
|
|
211
|
+
], Recipe.prototype, "description", void 0);
|
|
212
|
+
__decorate([
|
|
213
|
+
ReadOnly({ through: "recipe://rating", getter: "avg_rating(Base, Value)" }),
|
|
214
|
+
__metadata("design:type", Number)
|
|
215
|
+
], Recipe.prototype, "rating", void 0);
|
|
216
|
+
__decorate([
|
|
217
|
+
Collection({ through: "recipe://ingredient" }),
|
|
218
|
+
__metadata("design:type", Array)
|
|
219
|
+
], Recipe.prototype, "ingredients", void 0);
|
|
220
|
+
__decorate([
|
|
221
|
+
Collection({ through: "recipe://step", local: true }),
|
|
222
|
+
__metadata("design:type", Array)
|
|
223
|
+
], Recipe.prototype, "steps", void 0);
|
|
224
|
+
Recipe = __decorate([
|
|
225
|
+
ModelOptions({ name: "Recipe" })
|
|
226
|
+
], Recipe);
|
|
227
|
+
const metadata = Recipe.getModelMetadata();
|
|
228
|
+
// Assert className
|
|
229
|
+
expect(metadata.className).toBe("Recipe");
|
|
230
|
+
// Assert properties has 3 entries
|
|
231
|
+
expect(Object.keys(metadata.properties)).toHaveLength(3);
|
|
232
|
+
expect(metadata.properties.name).toBeDefined();
|
|
233
|
+
expect(metadata.properties.description).toBeDefined();
|
|
234
|
+
expect(metadata.properties.rating).toBeDefined();
|
|
235
|
+
// Assert collections has 2 entries
|
|
236
|
+
expect(Object.keys(metadata.collections)).toHaveLength(2);
|
|
237
|
+
expect(metadata.collections.ingredients).toBeDefined();
|
|
238
|
+
expect(metadata.collections.steps).toBeDefined();
|
|
239
|
+
// Verify all metadata fields are correctly extracted
|
|
240
|
+
expect(metadata.properties.name.predicate).toBe("recipe://name");
|
|
241
|
+
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
242
|
+
expect(metadata.properties.description.predicate).toBe("recipe://description");
|
|
243
|
+
expect(metadata.properties.rating.predicate).toBe("recipe://rating");
|
|
244
|
+
expect(metadata.properties.rating.getter).toBe("avg_rating(Base, Value)");
|
|
245
|
+
expect(metadata.collections.ingredients.predicate).toBe("recipe://ingredient");
|
|
246
|
+
expect(metadata.collections.steps.predicate).toBe("recipe://step");
|
|
247
|
+
expect(metadata.collections.steps.local).toBe(true);
|
|
248
|
+
});
|
|
249
|
+
});
|
|
250
|
+
describe("Ad4mModel.fromJSONSchema() with getModelMetadata()", () => {
|
|
251
|
+
it("should extract metadata from a model created via fromJSONSchema with basic properties", () => {
|
|
252
|
+
const schema = {
|
|
253
|
+
title: "Product",
|
|
254
|
+
type: "object",
|
|
255
|
+
properties: {
|
|
256
|
+
name: { type: "string" },
|
|
257
|
+
price: { type: "number" },
|
|
258
|
+
description: { type: "string" }
|
|
259
|
+
},
|
|
260
|
+
required: ["name", "price"]
|
|
261
|
+
};
|
|
262
|
+
const ProductClass = Ad4mModel.fromJSONSchema(schema, {
|
|
263
|
+
name: "Product",
|
|
264
|
+
namespace: "product://",
|
|
265
|
+
resolveLanguage: "literal"
|
|
266
|
+
});
|
|
267
|
+
const metadata = ProductClass.getModelMetadata();
|
|
268
|
+
// Verify className
|
|
269
|
+
expect(metadata.className).toBe("Product");
|
|
270
|
+
// Verify properties are extracted
|
|
271
|
+
expect(Object.keys(metadata.properties).length).toBeGreaterThan(0);
|
|
272
|
+
expect(metadata.properties.name).toBeDefined();
|
|
273
|
+
expect(metadata.properties.name.predicate).toBe("product://name");
|
|
274
|
+
expect(metadata.properties.name.required).toBe(true);
|
|
275
|
+
expect(metadata.properties.name.writable).toBe(true);
|
|
276
|
+
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
277
|
+
expect(metadata.properties.price).toBeDefined();
|
|
278
|
+
expect(metadata.properties.price.predicate).toBe("product://price");
|
|
279
|
+
expect(metadata.properties.price.required).toBe(true);
|
|
280
|
+
expect(metadata.properties.price.resolveLanguage).toBe("literal");
|
|
281
|
+
expect(metadata.properties.description).toBeDefined();
|
|
282
|
+
expect(metadata.properties.description.predicate).toBe("product://description");
|
|
283
|
+
expect(metadata.properties.description.required).toBe(false);
|
|
284
|
+
});
|
|
285
|
+
it("should extract collections from a model created via fromJSONSchema with arrays", () => {
|
|
286
|
+
const schema = {
|
|
287
|
+
title: "Post",
|
|
288
|
+
type: "object",
|
|
289
|
+
properties: {
|
|
290
|
+
title: { type: "string" },
|
|
291
|
+
tags: {
|
|
292
|
+
type: "array",
|
|
293
|
+
items: { type: "string" }
|
|
294
|
+
},
|
|
295
|
+
comments: {
|
|
296
|
+
type: "array",
|
|
297
|
+
items: { type: "string" }
|
|
298
|
+
}
|
|
299
|
+
},
|
|
300
|
+
required: ["title"]
|
|
301
|
+
};
|
|
302
|
+
const PostClass = Ad4mModel.fromJSONSchema(schema, {
|
|
303
|
+
name: "Post",
|
|
304
|
+
namespace: "post://"
|
|
305
|
+
});
|
|
306
|
+
const metadata = PostClass.getModelMetadata();
|
|
307
|
+
// Verify className
|
|
308
|
+
expect(metadata.className).toBe("Post");
|
|
309
|
+
// Verify collections are extracted
|
|
310
|
+
expect(Object.keys(metadata.collections).length).toBeGreaterThan(0);
|
|
311
|
+
expect(metadata.collections.tags).toBeDefined();
|
|
312
|
+
expect(metadata.collections.tags.predicate).toBe("post://tags");
|
|
313
|
+
expect(metadata.collections.comments).toBeDefined();
|
|
314
|
+
expect(metadata.collections.comments.predicate).toBe("post://comments");
|
|
315
|
+
// Verify properties (should include at least title)
|
|
316
|
+
expect(metadata.properties.title).toBeDefined();
|
|
317
|
+
expect(metadata.properties.title.predicate).toBe("post://title");
|
|
318
|
+
expect(metadata.properties.title.required).toBe(true);
|
|
319
|
+
});
|
|
320
|
+
it("should handle x-ad4m metadata in JSON schema for property options", () => {
|
|
321
|
+
const schema = {
|
|
322
|
+
title: "Contact",
|
|
323
|
+
"x-ad4m": {
|
|
324
|
+
namespace: "contact://"
|
|
325
|
+
},
|
|
326
|
+
type: "object",
|
|
327
|
+
properties: {
|
|
328
|
+
name: {
|
|
329
|
+
type: "string",
|
|
330
|
+
"x-ad4m": {
|
|
331
|
+
through: "foaf://name",
|
|
332
|
+
resolveLanguage: "literal",
|
|
333
|
+
writable: true
|
|
334
|
+
}
|
|
335
|
+
},
|
|
336
|
+
email: {
|
|
337
|
+
type: "string",
|
|
338
|
+
"x-ad4m": {
|
|
339
|
+
through: "foaf://mbox",
|
|
340
|
+
local: true
|
|
341
|
+
}
|
|
342
|
+
}
|
|
343
|
+
},
|
|
344
|
+
required: ["name"]
|
|
345
|
+
};
|
|
346
|
+
const ContactClass = Ad4mModel.fromJSONSchema(schema, {
|
|
347
|
+
name: "Contact"
|
|
348
|
+
});
|
|
349
|
+
const metadata = ContactClass.getModelMetadata();
|
|
350
|
+
// Verify x-ad4m metadata is respected
|
|
351
|
+
expect(metadata.properties.name.predicate).toBe("foaf://name");
|
|
352
|
+
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
353
|
+
expect(metadata.properties.name.writable).toBe(true);
|
|
354
|
+
expect(metadata.properties.name.required).toBe(true);
|
|
355
|
+
expect(metadata.properties.email.predicate).toBe("foaf://mbox");
|
|
356
|
+
expect(metadata.properties.email.local).toBe(true);
|
|
357
|
+
});
|
|
358
|
+
it("should handle property mapping override in options", () => {
|
|
359
|
+
const schema = {
|
|
360
|
+
title: "User",
|
|
361
|
+
type: "object",
|
|
362
|
+
properties: {
|
|
363
|
+
username: { type: "string" },
|
|
364
|
+
fullName: { type: "string" }
|
|
365
|
+
},
|
|
366
|
+
required: ["username"]
|
|
367
|
+
};
|
|
368
|
+
const UserClass = Ad4mModel.fromJSONSchema(schema, {
|
|
369
|
+
name: "User",
|
|
370
|
+
namespace: "user://",
|
|
371
|
+
propertyMapping: {
|
|
372
|
+
username: "custom://identifier",
|
|
373
|
+
fullName: "custom://name"
|
|
374
|
+
}
|
|
375
|
+
});
|
|
376
|
+
const metadata = UserClass.getModelMetadata();
|
|
377
|
+
// Verify property mappings are applied
|
|
378
|
+
expect(metadata.properties.username.predicate).toBe("custom://identifier");
|
|
379
|
+
expect(metadata.properties.fullName.predicate).toBe("custom://name");
|
|
380
|
+
});
|
|
381
|
+
it("should extract metadata from dynamically generated models with mixed types", () => {
|
|
382
|
+
const schema = {
|
|
383
|
+
title: "Article",
|
|
384
|
+
type: "object",
|
|
385
|
+
properties: {
|
|
386
|
+
title: { type: "string" },
|
|
387
|
+
views: { type: "number" },
|
|
388
|
+
published: { type: "boolean" },
|
|
389
|
+
authors: {
|
|
390
|
+
type: "array",
|
|
391
|
+
items: { type: "string" }
|
|
392
|
+
},
|
|
393
|
+
tags: {
|
|
394
|
+
type: "array",
|
|
395
|
+
items: { type: "string" },
|
|
396
|
+
"x-ad4m": {
|
|
397
|
+
local: true
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
},
|
|
401
|
+
required: ["title", "published"]
|
|
402
|
+
};
|
|
403
|
+
const ArticleClass = Ad4mModel.fromJSONSchema(schema, {
|
|
404
|
+
name: "Article",
|
|
405
|
+
namespace: "article://",
|
|
406
|
+
resolveLanguage: "literal"
|
|
407
|
+
});
|
|
408
|
+
const metadata = ArticleClass.getModelMetadata();
|
|
409
|
+
// Verify className
|
|
410
|
+
expect(metadata.className).toBe("Article");
|
|
411
|
+
// Verify properties
|
|
412
|
+
expect(metadata.properties.title).toBeDefined();
|
|
413
|
+
expect(metadata.properties.title.predicate).toBe("article://title");
|
|
414
|
+
expect(metadata.properties.title.required).toBe(true);
|
|
415
|
+
expect(metadata.properties.title.resolveLanguage).toBe("literal");
|
|
416
|
+
expect(metadata.properties.views).toBeDefined();
|
|
417
|
+
expect(metadata.properties.views.predicate).toBe("article://views");
|
|
418
|
+
expect(metadata.properties.views.resolveLanguage).toBe("literal");
|
|
419
|
+
expect(metadata.properties.published).toBeDefined();
|
|
420
|
+
expect(metadata.properties.published.predicate).toBe("article://published");
|
|
421
|
+
expect(metadata.properties.published.required).toBe(true);
|
|
422
|
+
// Verify collections
|
|
423
|
+
expect(metadata.collections.authors).toBeDefined();
|
|
424
|
+
expect(metadata.collections.authors.predicate).toBe("article://authors");
|
|
425
|
+
expect(metadata.collections.tags).toBeDefined();
|
|
426
|
+
expect(metadata.collections.tags.predicate).toBe("article://tags");
|
|
427
|
+
expect(metadata.collections.tags.local).toBe(true);
|
|
428
|
+
});
|
|
429
|
+
it("should handle models with only an auto-generated type flag", () => {
|
|
430
|
+
const schema = {
|
|
431
|
+
title: "EmptyModel",
|
|
432
|
+
type: "object",
|
|
433
|
+
properties: {}
|
|
434
|
+
};
|
|
435
|
+
const EmptyModelClass = Ad4mModel.fromJSONSchema(schema, {
|
|
436
|
+
name: "EmptyModel",
|
|
437
|
+
namespace: "empty://"
|
|
438
|
+
});
|
|
439
|
+
const metadata = EmptyModelClass.getModelMetadata();
|
|
440
|
+
// Should have className
|
|
441
|
+
expect(metadata.className).toBe("EmptyModel");
|
|
442
|
+
// Should have the auto-generated __ad4m_type property
|
|
443
|
+
expect(metadata.properties.__ad4m_type).toBeDefined();
|
|
444
|
+
expect(metadata.properties.__ad4m_type.predicate).toBe("ad4m://type");
|
|
445
|
+
expect(metadata.properties.__ad4m_type.initial).toBe("empty://instance");
|
|
446
|
+
expect(metadata.properties.__ad4m_type.flag).toBe(true);
|
|
447
|
+
});
|
|
448
|
+
});
|
|
449
|
+
describe("Ad4mModel.queryToSurrealQL()", () => {
|
|
450
|
+
// Mock perspective proxy (minimal since queryToSurrealQL doesn't actually call it)
|
|
451
|
+
const mockPerspective = {};
|
|
452
|
+
// Helper function to normalize whitespace in queries for easier comparison
|
|
453
|
+
function normalizeQuery(query) {
|
|
454
|
+
return query.replace(/\s+/g, ' ').trim();
|
|
455
|
+
}
|
|
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
|
+
it("should generate basic query with no filters", async () => {
|
|
481
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {});
|
|
482
|
+
expect(query).toContain("id AS source");
|
|
483
|
+
expect(query).toContain("uri AS source_uri");
|
|
484
|
+
expect(query).toContain("FROM node");
|
|
485
|
+
expect(query).toContain("->link[WHERE perspective = $perspective] AS links");
|
|
486
|
+
expect(query).toContain("WHERE");
|
|
487
|
+
// Should have graph traversal filters for required properties
|
|
488
|
+
expect(query).toContain("count(->link[WHERE");
|
|
489
|
+
});
|
|
490
|
+
it("should generate query with simple property filter", async () => {
|
|
491
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: "Pasta" } });
|
|
492
|
+
// Should have graph traversal filters for required properties and user filter
|
|
493
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name']) > 0");
|
|
494
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://rating']) > 0");
|
|
495
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri = 'Pasta']) > 0");
|
|
496
|
+
});
|
|
497
|
+
it("should generate query with multiple property filters", async () => {
|
|
498
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: "Pasta", rating: 5 } });
|
|
499
|
+
expect(query).toContain("WHERE");
|
|
500
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri = 'Pasta']) > 0");
|
|
501
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://rating' AND out.uri = 5]) > 0");
|
|
502
|
+
expect(query).toContain("AND");
|
|
503
|
+
});
|
|
504
|
+
it("should handle gt operator (filtered in JavaScript, not SQL)", async () => {
|
|
505
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { gt: 4 } } });
|
|
506
|
+
// Comparison operators for regular properties are now filtered in JavaScript post-query
|
|
507
|
+
// The SQL just filters on the predicate existing
|
|
508
|
+
expect(query).toContain("FROM node");
|
|
509
|
+
// Should NOT contain comparison operators in SQL for regular properties
|
|
510
|
+
expect(query).not.toContain("target > 4");
|
|
511
|
+
});
|
|
512
|
+
it("should handle lt operator (filtered in JavaScript, not SQL)", async () => {
|
|
513
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { lt: 3 } } });
|
|
514
|
+
// Comparison operators are filtered in JavaScript post-query
|
|
515
|
+
expect(query).toContain("FROM node");
|
|
516
|
+
expect(query).not.toContain("target < 3");
|
|
517
|
+
});
|
|
518
|
+
it("should handle gte and lte operators (filtered in JavaScript, not SQL)", async () => {
|
|
519
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { gte: 3, lte: 5 } } });
|
|
520
|
+
// Comparison operators are filtered in JavaScript post-query
|
|
521
|
+
expect(query).toContain("FROM node");
|
|
522
|
+
expect(query).not.toContain("target >= 3");
|
|
523
|
+
expect(query).not.toContain("target <= 5");
|
|
524
|
+
});
|
|
525
|
+
it("should handle not operator with single value", async () => {
|
|
526
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { not: "Salad" } } });
|
|
527
|
+
// Not operator uses graph traversal with count = 0
|
|
528
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri = 'Salad']) = 0");
|
|
529
|
+
});
|
|
530
|
+
it("should handle not operator with array (NOT IN)", async () => {
|
|
531
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { not: ["Salad", "Soup"] } } });
|
|
532
|
+
// Not operator with array uses graph traversal with count = 0
|
|
533
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri IN ['Salad', 'Soup']]) = 0");
|
|
534
|
+
});
|
|
535
|
+
it("should handle between operator (filtered in JavaScript, not SQL)", async () => {
|
|
536
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: { between: [3, 5] } } });
|
|
537
|
+
// Between operator for regular properties is now filtered in JavaScript post-query
|
|
538
|
+
expect(query).toContain("FROM node");
|
|
539
|
+
expect(query).not.toContain("target >= 3");
|
|
540
|
+
expect(query).not.toContain("target <= 5");
|
|
541
|
+
});
|
|
542
|
+
it("should handle contains operator on string property (filtered in JavaScript, not SQL)", async () => {
|
|
543
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { contains: "Past" } } });
|
|
544
|
+
// Contains operator for regular properties is now filtered in JavaScript post-query
|
|
545
|
+
expect(query).toContain("FROM node");
|
|
546
|
+
expect(query).not.toContain("target CONTAINS 'Past'");
|
|
547
|
+
});
|
|
548
|
+
it("should handle contains operator on regular property with substring (filtered in JavaScript, not SQL)", async () => {
|
|
549
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: { contains: "Salad" } } });
|
|
550
|
+
// Contains operator is filtered in JavaScript post-query
|
|
551
|
+
expect(query).toContain("FROM node");
|
|
552
|
+
expect(query).not.toContain("target CONTAINS 'Salad'");
|
|
553
|
+
});
|
|
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
|
+
it("should handle array values (IN clause)", async () => {
|
|
565
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: ["Pasta", "Pizza"] } });
|
|
566
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri IN ['Pasta', 'Pizza']]) > 0");
|
|
567
|
+
});
|
|
568
|
+
it.skip("should handle special fields (author, timestamp) without subqueries", async () => {
|
|
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/);
|
|
573
|
+
});
|
|
574
|
+
it("should not generate ORDER BY clause in SQL (handled in JavaScript)", async () => {
|
|
575
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { order: { timestamp: "DESC" } });
|
|
576
|
+
// ORDER BY is now handled in JavaScript post-query
|
|
577
|
+
expect(query).toContain("FROM node");
|
|
578
|
+
expect(query).not.toContain("ORDER BY");
|
|
579
|
+
});
|
|
580
|
+
it("should not generate LIMIT clause in SQL (handled in JavaScript)", async () => {
|
|
581
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { limit: 10 });
|
|
582
|
+
// LIMIT is now handled in JavaScript post-query
|
|
583
|
+
expect(query).toContain("FROM node");
|
|
584
|
+
expect(query).not.toContain("LIMIT");
|
|
585
|
+
});
|
|
586
|
+
it("should not generate START clause in SQL (handled in JavaScript)", async () => {
|
|
587
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { offset: 20 });
|
|
588
|
+
// START/offset is now handled in JavaScript post-query
|
|
589
|
+
expect(query).toContain("FROM node");
|
|
590
|
+
expect(query).not.toContain("START");
|
|
591
|
+
});
|
|
592
|
+
it("should generate complete query with all options (ORDER BY, LIMIT, START handled in JS)", async () => {
|
|
593
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
594
|
+
where: { name: "Pasta", rating: { gt: 4 } },
|
|
595
|
+
order: { timestamp: "DESC" },
|
|
596
|
+
limit: 10,
|
|
597
|
+
offset: 20
|
|
598
|
+
});
|
|
599
|
+
// WHERE clause uses graph traversal filters
|
|
600
|
+
expect(query).toContain("WHERE");
|
|
601
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri = 'Pasta']) > 0");
|
|
602
|
+
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
603
|
+
expect(query).not.toContain("out.uri > 4");
|
|
604
|
+
expect(query).not.toContain("target > 4");
|
|
605
|
+
// ORDER BY, LIMIT, START are now handled in JavaScript post-query
|
|
606
|
+
expect(query).not.toContain("ORDER BY");
|
|
607
|
+
expect(query).not.toContain("LIMIT");
|
|
608
|
+
expect(query).not.toContain("START");
|
|
609
|
+
});
|
|
610
|
+
it("should only select requested properties", async () => {
|
|
611
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name"] });
|
|
612
|
+
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
613
|
+
expect(query).toContain("->link[WHERE perspective = $perspective] AS links");
|
|
614
|
+
});
|
|
615
|
+
it("should only select requested collections", async () => {
|
|
616
|
+
let MultiCollectionModel = class MultiCollectionModel extends Ad4mModel {
|
|
617
|
+
constructor() {
|
|
618
|
+
super(...arguments);
|
|
619
|
+
this.coll1 = [];
|
|
620
|
+
this.coll2 = [];
|
|
621
|
+
}
|
|
622
|
+
};
|
|
623
|
+
__decorate([
|
|
624
|
+
Collection({ through: "test://coll1" }),
|
|
625
|
+
__metadata("design:type", Array)
|
|
626
|
+
], MultiCollectionModel.prototype, "coll1", void 0);
|
|
627
|
+
__decorate([
|
|
628
|
+
Collection({ through: "test://coll2" }),
|
|
629
|
+
__metadata("design:type", Array)
|
|
630
|
+
], MultiCollectionModel.prototype, "coll2", void 0);
|
|
631
|
+
MultiCollectionModel = __decorate([
|
|
632
|
+
ModelOptions({ name: "MultiCollectionModel" })
|
|
633
|
+
], MultiCollectionModel);
|
|
634
|
+
const query = await MultiCollectionModel.queryToSurrealQL(mockPerspective, { collections: ["coll1"] });
|
|
635
|
+
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
636
|
+
expect(query).toContain("->link[WHERE perspective = $perspective] AS links");
|
|
637
|
+
});
|
|
638
|
+
it("should escape single quotes in string values", async () => {
|
|
639
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { name: "O'Brien's Recipe" } });
|
|
640
|
+
// Single quotes are escaped with backslash in SurrealDB
|
|
641
|
+
expect(query).toContain("O\\'Brien\\'s Recipe");
|
|
642
|
+
});
|
|
643
|
+
it("should handle numeric values without quotes", async () => {
|
|
644
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: 5 } });
|
|
645
|
+
// Numeric values should not have quotes around them
|
|
646
|
+
expect(query).toContain("out.uri = 5");
|
|
647
|
+
expect(query).not.toContain("out.uri = '5'");
|
|
648
|
+
});
|
|
649
|
+
it("should handle complex nested query (comparisons, ORDER BY, LIMIT handled in JS)", async () => {
|
|
650
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
651
|
+
where: {
|
|
652
|
+
name: "Pasta",
|
|
653
|
+
rating: { gte: 4, lte: 5 },
|
|
654
|
+
author: "did:key:alice"
|
|
655
|
+
},
|
|
656
|
+
order: { rating: "DESC" },
|
|
657
|
+
limit: 5
|
|
658
|
+
});
|
|
659
|
+
const normalized = normalizeQuery(query);
|
|
660
|
+
// Verify graph traversal filters are present
|
|
661
|
+
expect(normalized).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri = 'Pasta']) > 0");
|
|
662
|
+
// Comparison operators (gte, lte) are filtered in JavaScript, not SQL
|
|
663
|
+
expect(normalized).not.toContain("out.uri >= 4");
|
|
664
|
+
expect(normalized).not.toContain("target >= 4");
|
|
665
|
+
expect(normalized).not.toContain("out.uri <= 5");
|
|
666
|
+
expect(normalized).not.toContain("target <= 5");
|
|
667
|
+
// author and timestamp filtering is done in JavaScript post-query
|
|
668
|
+
expect(normalized).not.toContain("author = 'did:key:alice'");
|
|
669
|
+
// ORDER BY and LIMIT are handled in JavaScript post-query
|
|
670
|
+
expect(normalized).not.toContain("ORDER BY");
|
|
671
|
+
expect(normalized).not.toContain("LIMIT");
|
|
672
|
+
// Verify query structure (FROM node, no GROUP BY)
|
|
673
|
+
expect(normalized).toContain("FROM node");
|
|
674
|
+
});
|
|
675
|
+
it("should handle empty query object", async () => {
|
|
676
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {});
|
|
677
|
+
// Should generate valid query with WHERE clause for required properties (name and rating)
|
|
678
|
+
expect(query).toContain("id AS source");
|
|
679
|
+
expect(query).toContain("uri AS source_uri");
|
|
680
|
+
expect(query).toContain("FROM node");
|
|
681
|
+
// Should have WHERE clause filtering for required properties using graph traversal
|
|
682
|
+
expect(query).toContain("WHERE");
|
|
683
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name']) > 0");
|
|
684
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://rating']) > 0");
|
|
685
|
+
expect(query).not.toContain("ORDER BY");
|
|
686
|
+
expect(query).not.toContain("START");
|
|
687
|
+
});
|
|
688
|
+
it("should handle base special field", async () => {
|
|
689
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: "literal://test" } });
|
|
690
|
+
expect(query).toContain("uri = 'literal://test'");
|
|
691
|
+
});
|
|
692
|
+
it("should handle base special field with array (IN clause)", async () => {
|
|
693
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: ["literal://test1", "literal://test2"] } });
|
|
694
|
+
expect(query).toContain("uri IN ['literal://test1', 'literal://test2']");
|
|
695
|
+
});
|
|
696
|
+
it("should handle base special field with not operator", async () => {
|
|
697
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { not: "literal://test" } } });
|
|
698
|
+
expect(query).toContain("uri != 'literal://test'");
|
|
699
|
+
});
|
|
700
|
+
it("should handle base special field with not operator and array (NOT IN)", async () => {
|
|
701
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { not: ["literal://test1", "literal://test2"] } } });
|
|
702
|
+
expect(query).toContain("uri NOT IN ['literal://test1', 'literal://test2']");
|
|
703
|
+
});
|
|
704
|
+
it("should handle base special field with between operator", async () => {
|
|
705
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { between: ["literal://a", "literal://z"] } } });
|
|
706
|
+
const normalized = normalizeQuery(query);
|
|
707
|
+
expect(normalized).toContain("uri >= 'literal://a' AND uri <= 'literal://z'");
|
|
708
|
+
});
|
|
709
|
+
it("should handle base special field with gt operator", async () => {
|
|
710
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { gt: "literal://m" } } });
|
|
711
|
+
expect(query).toContain("uri > 'literal://m'");
|
|
712
|
+
});
|
|
713
|
+
it("should handle base special field with gte and lte operators", async () => {
|
|
714
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { base: { gte: "literal://a", lte: "literal://z" } } });
|
|
715
|
+
const normalized = normalizeQuery(query);
|
|
716
|
+
expect(normalized).toContain("uri >= 'literal://a'");
|
|
717
|
+
expect(normalized).toContain("uri <= 'literal://z'");
|
|
718
|
+
});
|
|
719
|
+
it("should handle timestamp special field with gt operator (filtered in JavaScript)", async () => {
|
|
720
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { timestamp: { gt: 1234567890 } } });
|
|
721
|
+
// timestamp filtering is done in JavaScript post-query
|
|
722
|
+
expect(query).toContain("FROM node");
|
|
723
|
+
expect(query).not.toContain("timestamp > 1234567890");
|
|
724
|
+
});
|
|
725
|
+
it("should handle multiple special fields (filtered in JavaScript)", async () => {
|
|
726
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
727
|
+
where: {
|
|
728
|
+
author: "did:key:alice",
|
|
729
|
+
timestamp: { gt: 1000 }
|
|
730
|
+
}
|
|
731
|
+
});
|
|
732
|
+
// author and timestamp filtering is done in JavaScript post-query
|
|
733
|
+
expect(query).toContain("FROM node");
|
|
734
|
+
expect(query).not.toContain("author = 'did:key:alice'");
|
|
735
|
+
expect(query).not.toContain("timestamp > 1000");
|
|
736
|
+
});
|
|
737
|
+
it("should handle mixed special and regular properties", async () => {
|
|
738
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, {
|
|
739
|
+
where: {
|
|
740
|
+
name: "Pasta",
|
|
741
|
+
author: "did:key:alice",
|
|
742
|
+
rating: { gt: 4 }
|
|
743
|
+
}
|
|
744
|
+
});
|
|
745
|
+
const normalized = normalizeQuery(query);
|
|
746
|
+
// Regular properties use graph traversal filters
|
|
747
|
+
expect(normalized).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://name' AND out.uri = 'Pasta']) > 0");
|
|
748
|
+
// Comparison operators (gt) are filtered in JavaScript, not SQL
|
|
749
|
+
expect(normalized).not.toContain("out.uri > 4");
|
|
750
|
+
expect(normalized).not.toContain("target > 4");
|
|
751
|
+
// author filtering is done in JavaScript post-query
|
|
752
|
+
expect(normalized).not.toContain("author = 'did:key:alice'");
|
|
753
|
+
});
|
|
754
|
+
it("should handle boolean values", async () => {
|
|
755
|
+
let Task = class Task extends Ad4mModel {
|
|
756
|
+
constructor() {
|
|
757
|
+
super(...arguments);
|
|
758
|
+
this.completed = false;
|
|
759
|
+
}
|
|
760
|
+
};
|
|
761
|
+
__decorate([
|
|
762
|
+
Property({ through: "task://completed" }),
|
|
763
|
+
__metadata("design:type", Boolean)
|
|
764
|
+
], Task.prototype, "completed", void 0);
|
|
765
|
+
Task = __decorate([
|
|
766
|
+
ModelOptions({ name: "Task" })
|
|
767
|
+
], Task);
|
|
768
|
+
const query = await Task.queryToSurrealQL(mockPerspective, { where: { completed: true } });
|
|
769
|
+
expect(query).toContain("out.uri = true");
|
|
770
|
+
});
|
|
771
|
+
it("should handle array of numbers", async () => {
|
|
772
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { rating: [4, 5] } });
|
|
773
|
+
expect(query).toContain("count(->link[WHERE perspective = $perspective AND predicate = 'recipe://rating' AND out.uri IN [4, 5]]) > 0");
|
|
774
|
+
});
|
|
775
|
+
it("should skip unknown properties in where clause", async () => {
|
|
776
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { where: { unknownProp: "value" } });
|
|
777
|
+
// Should not throw error, just skip the unknown property
|
|
778
|
+
expect(query).toContain("source");
|
|
779
|
+
// Should not contain any condition for unknownProp
|
|
780
|
+
expect(query).not.toContain("unknownProp");
|
|
781
|
+
});
|
|
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
|
+
it("should handle order by regular property (handled in JavaScript)", async () => {
|
|
790
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { order: { name: "ASC" } });
|
|
791
|
+
// ORDER BY is now handled in JavaScript post-query
|
|
792
|
+
expect(query).toContain("FROM node");
|
|
793
|
+
expect(query).not.toContain("ORDER BY");
|
|
794
|
+
});
|
|
795
|
+
it("should generate query with only properties, no collections", async () => {
|
|
796
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: ["name", "rating"], collections: [] });
|
|
797
|
+
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
798
|
+
expect(query).toContain("->link[WHERE perspective = $perspective] AS links");
|
|
799
|
+
});
|
|
800
|
+
it("should generate query with only collections, no properties", async () => {
|
|
801
|
+
const query = await Recipe.queryToSurrealQL(mockPerspective, { properties: [], collections: ["ingredients"] });
|
|
802
|
+
// With array::group(), all link data is selected, filtering happens in instancesFromSurrealResult
|
|
803
|
+
expect(query).toContain("->link[WHERE perspective = $perspective] AS links");
|
|
804
|
+
});
|
|
805
|
+
});
|
|
806
|
+
describe("Ad4mModel.instancesFromSurrealResult() and SurrealDB integration", () => {
|
|
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);
|
|
831
|
+
// Mock perspective with both querySurrealDB and infer methods
|
|
832
|
+
const mockPerspective = {
|
|
833
|
+
querySurrealDB: jest.fn(),
|
|
834
|
+
infer: jest.fn(),
|
|
835
|
+
uuid: 'test-perspective-uuid',
|
|
836
|
+
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue('Recipe')
|
|
837
|
+
};
|
|
838
|
+
beforeEach(() => {
|
|
839
|
+
jest.clearAllMocks();
|
|
840
|
+
});
|
|
841
|
+
it("should convert empty SurrealDB results correctly", async () => {
|
|
842
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, []);
|
|
843
|
+
expect(result.results).toEqual([]);
|
|
844
|
+
expect(result.totalCount).toBe(0);
|
|
845
|
+
});
|
|
846
|
+
it("should convert SurrealDB results to model instances", async () => {
|
|
847
|
+
const surrealResults = [
|
|
848
|
+
{
|
|
849
|
+
source: "node:abc123",
|
|
850
|
+
source_uri: "literal://recipe1",
|
|
851
|
+
links: [
|
|
852
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
853
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
854
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
855
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
856
|
+
{ predicate: "recipe://ingredient", target: "cheese", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
857
|
+
]
|
|
858
|
+
},
|
|
859
|
+
{
|
|
860
|
+
source: "node:def456",
|
|
861
|
+
source_uri: "literal://recipe2",
|
|
862
|
+
links: [
|
|
863
|
+
{ predicate: "recipe://name", target: "Pizza", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
864
|
+
{ predicate: "recipe://rating", target: "4", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
865
|
+
{ predicate: "recipe://ingredient", target: "dough", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
866
|
+
{ predicate: "recipe://ingredient", target: "cheese", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" },
|
|
867
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:bob", timestamp: "2023-01-02T00:00:00Z" }
|
|
868
|
+
]
|
|
869
|
+
}
|
|
870
|
+
];
|
|
871
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
872
|
+
expect(result.results).toHaveLength(2);
|
|
873
|
+
expect(result.totalCount).toBe(2);
|
|
874
|
+
const recipe1 = result.results[0];
|
|
875
|
+
expect(recipe1).toBeInstanceOf(Recipe);
|
|
876
|
+
expect(recipe1.name).toBe("Pasta");
|
|
877
|
+
expect(recipe1.rating).toBe(5);
|
|
878
|
+
expect(recipe1.ingredients).toEqual(["pasta", "tomato", "cheese"]);
|
|
879
|
+
const recipe2 = result.results[1];
|
|
880
|
+
expect(recipe2).toBeInstanceOf(Recipe);
|
|
881
|
+
expect(recipe2.name).toBe("Pizza");
|
|
882
|
+
expect(recipe2.rating).toBe(4);
|
|
883
|
+
});
|
|
884
|
+
it("should filter properties when query specifies properties", async () => {
|
|
885
|
+
const surrealResults = [
|
|
886
|
+
{
|
|
887
|
+
source: "node:abc123",
|
|
888
|
+
source_uri: "literal://recipe1",
|
|
889
|
+
links: [
|
|
890
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
891
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
892
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
893
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
894
|
+
]
|
|
895
|
+
}
|
|
896
|
+
];
|
|
897
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, { properties: ["name"] }, surrealResults);
|
|
898
|
+
expect(result.results).toHaveLength(1);
|
|
899
|
+
const recipe = result.results[0];
|
|
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());
|
|
908
|
+
});
|
|
909
|
+
it("should filter collections when query specifies collections", async () => {
|
|
910
|
+
const surrealResults = [
|
|
911
|
+
{
|
|
912
|
+
source: "node:abc123",
|
|
913
|
+
source_uri: "literal://recipe1",
|
|
914
|
+
links: [
|
|
915
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
916
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
917
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
918
|
+
{ predicate: "recipe://ingredient", target: "tomato", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
919
|
+
]
|
|
920
|
+
}
|
|
921
|
+
];
|
|
922
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, { collections: ["ingredients"] }, surrealResults);
|
|
923
|
+
expect(result.results).toHaveLength(1);
|
|
924
|
+
const recipe = result.results[0];
|
|
925
|
+
expect(recipe.ingredients).toEqual(["pasta", "tomato"]);
|
|
926
|
+
// name and rating should be removed since only "ingredients" was requested
|
|
927
|
+
expect(recipe.name).toBeUndefined();
|
|
928
|
+
expect(recipe.rating).toBeUndefined();
|
|
929
|
+
});
|
|
930
|
+
it("should handle results missing base field", async () => {
|
|
931
|
+
const surrealResults = [
|
|
932
|
+
{
|
|
933
|
+
links: [
|
|
934
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
935
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
936
|
+
]
|
|
937
|
+
// Missing source field
|
|
938
|
+
}
|
|
939
|
+
];
|
|
940
|
+
const result = await Recipe.instancesFromSurrealResult(mockPerspective, {}, surrealResults);
|
|
941
|
+
// Should filter out the invalid result (or handle gracefully)
|
|
942
|
+
expect(result.results).toHaveLength(0);
|
|
943
|
+
expect(result.totalCount).toBe(0);
|
|
944
|
+
});
|
|
945
|
+
it("should use SurrealDB by default in findAll()", async () => {
|
|
946
|
+
const surrealResults = [
|
|
947
|
+
{
|
|
948
|
+
source: "node:abc123",
|
|
949
|
+
source_uri: "literal://recipe1",
|
|
950
|
+
links: [
|
|
951
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
952
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
953
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
954
|
+
]
|
|
955
|
+
}
|
|
956
|
+
];
|
|
957
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
958
|
+
const results = await Recipe.findAll(mockPerspective, {});
|
|
959
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
960
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
961
|
+
expect(results).toHaveLength(1);
|
|
962
|
+
expect(results[0].name).toBe("Pasta");
|
|
963
|
+
});
|
|
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
|
+
it("should use SurrealDB by default in findAllAndCount()", async () => {
|
|
978
|
+
const surrealResults = [
|
|
979
|
+
{
|
|
980
|
+
source: "node:abc123",
|
|
981
|
+
source_uri: "literal://recipe1",
|
|
982
|
+
links: [
|
|
983
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
984
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
985
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
986
|
+
]
|
|
987
|
+
}
|
|
988
|
+
];
|
|
989
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
990
|
+
const { results, totalCount } = await Recipe.findAllAndCount(mockPerspective, {});
|
|
991
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
992
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
993
|
+
expect(results).toHaveLength(1);
|
|
994
|
+
expect(totalCount).toBe(1);
|
|
995
|
+
});
|
|
996
|
+
it("should use SurrealDB by default in paginate()", async () => {
|
|
997
|
+
const surrealResults = [
|
|
998
|
+
{
|
|
999
|
+
source: "node:abc123",
|
|
1000
|
+
source_uri: "literal://recipe1",
|
|
1001
|
+
links: [
|
|
1002
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1003
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1004
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1005
|
+
]
|
|
1006
|
+
}
|
|
1007
|
+
];
|
|
1008
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1009
|
+
const page = await Recipe.paginate(mockPerspective, 10, 1, {});
|
|
1010
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
1011
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1012
|
+
expect(page.results).toHaveLength(1);
|
|
1013
|
+
expect(page.pageSize).toBe(10);
|
|
1014
|
+
expect(page.pageNumber).toBe(1);
|
|
1015
|
+
});
|
|
1016
|
+
it("should use SurrealDB by default in count()", async () => {
|
|
1017
|
+
// Since count() uses result.length and GROUP BY returns one row per source,
|
|
1018
|
+
// mock 5 recipe sources
|
|
1019
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1020
|
+
source: `node:abc${i + 1}`,
|
|
1021
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1022
|
+
links: [
|
|
1023
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1024
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1025
|
+
]
|
|
1026
|
+
}));
|
|
1027
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1028
|
+
const count = await Recipe.count(mockPerspective, {});
|
|
1029
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
1030
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1031
|
+
expect(count).toBe(5);
|
|
1032
|
+
});
|
|
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
|
+
it("should use SurrealDB by default in ModelQueryBuilder.get()", async () => {
|
|
1042
|
+
const surrealResults = [
|
|
1043
|
+
{
|
|
1044
|
+
source: "node:abc123",
|
|
1045
|
+
source_uri: "literal://recipe1",
|
|
1046
|
+
links: [
|
|
1047
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1048
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1049
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1050
|
+
]
|
|
1051
|
+
}
|
|
1052
|
+
];
|
|
1053
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1054
|
+
const results = await Recipe.query(mockPerspective)
|
|
1055
|
+
.where({ name: "Pasta" })
|
|
1056
|
+
.get();
|
|
1057
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
1058
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1059
|
+
expect(results).toHaveLength(1);
|
|
1060
|
+
expect(results[0].name).toBe("Pasta");
|
|
1061
|
+
});
|
|
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
|
+
it("should use SurrealDB by default in ModelQueryBuilder.count()", async () => {
|
|
1079
|
+
// count() counts the number of rows returned by the query (one row per source)
|
|
1080
|
+
const surrealResults = Array.from({ length: 3 }, (_, i) => ({
|
|
1081
|
+
source: `node:abc${i + 1}`,
|
|
1082
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1083
|
+
links: [
|
|
1084
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1085
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1086
|
+
]
|
|
1087
|
+
}));
|
|
1088
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1089
|
+
const count = await Recipe.query(mockPerspective)
|
|
1090
|
+
.where({ rating: { gt: 4 } })
|
|
1091
|
+
.count();
|
|
1092
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
1093
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1094
|
+
expect(count).toBe(3);
|
|
1095
|
+
});
|
|
1096
|
+
it("should use SurrealDB by default in ModelQueryBuilder.paginate()", async () => {
|
|
1097
|
+
const surrealResults = [
|
|
1098
|
+
{
|
|
1099
|
+
source: "node:abc123",
|
|
1100
|
+
source_uri: "literal://recipe1",
|
|
1101
|
+
links: [
|
|
1102
|
+
{ predicate: "recipe://name", target: "Pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1103
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1104
|
+
{ predicate: "recipe://ingredient", target: "pasta", author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1105
|
+
]
|
|
1106
|
+
}
|
|
1107
|
+
];
|
|
1108
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1109
|
+
const page = await Recipe.query(mockPerspective)
|
|
1110
|
+
.where({ rating: { gt: 3 } })
|
|
1111
|
+
.paginate(10, 1);
|
|
1112
|
+
expect(mockPerspective.querySurrealDB).toHaveBeenCalledTimes(1);
|
|
1113
|
+
expect(mockPerspective.infer).not.toHaveBeenCalled();
|
|
1114
|
+
expect(page.results).toHaveLength(1);
|
|
1115
|
+
expect(page.pageSize).toBe(10);
|
|
1116
|
+
expect(page.pageNumber).toBe(1);
|
|
1117
|
+
});
|
|
1118
|
+
});
|
|
1119
|
+
describe("Ad4mModel.count() with advanced where conditions", () => {
|
|
1120
|
+
// Test Recipe model
|
|
1121
|
+
let Recipe = class Recipe extends Ad4mModel {
|
|
1122
|
+
constructor() {
|
|
1123
|
+
super(...arguments);
|
|
1124
|
+
this.name = "";
|
|
1125
|
+
this.rating = 0;
|
|
1126
|
+
this.ingredients = [];
|
|
1127
|
+
}
|
|
1128
|
+
};
|
|
1129
|
+
__decorate([
|
|
1130
|
+
Property({ through: "recipe://name" }),
|
|
1131
|
+
__metadata("design:type", String)
|
|
1132
|
+
], Recipe.prototype, "name", void 0);
|
|
1133
|
+
__decorate([
|
|
1134
|
+
Property({ through: "recipe://rating" }),
|
|
1135
|
+
__metadata("design:type", Number)
|
|
1136
|
+
], Recipe.prototype, "rating", void 0);
|
|
1137
|
+
__decorate([
|
|
1138
|
+
Collection({ through: "recipe://ingredient" }),
|
|
1139
|
+
__metadata("design:type", Array)
|
|
1140
|
+
], Recipe.prototype, "ingredients", void 0);
|
|
1141
|
+
Recipe = __decorate([
|
|
1142
|
+
ModelOptions({ name: "Recipe" })
|
|
1143
|
+
], Recipe);
|
|
1144
|
+
// Mock perspective
|
|
1145
|
+
const mockPerspective = {
|
|
1146
|
+
querySurrealDB: jest.fn(),
|
|
1147
|
+
infer: jest.fn(),
|
|
1148
|
+
uuid: 'test-perspective-uuid',
|
|
1149
|
+
stringOrTemplateObjectToSubjectClassName: jest.fn().mockResolvedValue('Recipe')
|
|
1150
|
+
};
|
|
1151
|
+
beforeEach(() => {
|
|
1152
|
+
jest.clearAllMocks();
|
|
1153
|
+
});
|
|
1154
|
+
it("should apply JS-level filtering for gt operator on properties in SurrealDB count()", async () => {
|
|
1155
|
+
// Mock SurrealDB results: 5 recipes with ratings 1, 2, 3, 4, 5
|
|
1156
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1157
|
+
source: `node:abc${i + 1}`,
|
|
1158
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1159
|
+
links: [
|
|
1160
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1161
|
+
{ predicate: "recipe://rating", target: `${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1162
|
+
]
|
|
1163
|
+
}));
|
|
1164
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1165
|
+
// Count recipes with rating > 3 (should match 2 recipes: rating 4 and 5)
|
|
1166
|
+
const count = await Recipe.count(mockPerspective, { where: { rating: { gt: 3 } } });
|
|
1167
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1168
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { rating: { gt: 3 } } });
|
|
1169
|
+
expect(count).toBe(2);
|
|
1170
|
+
expect(count).toBe(findAllResults.length);
|
|
1171
|
+
});
|
|
1172
|
+
it("should apply JS-level filtering for between operator on properties in SurrealDB count()", async () => {
|
|
1173
|
+
// Mock SurrealDB results: 5 recipes with ratings 1, 2, 3, 4, 5
|
|
1174
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1175
|
+
source: `node:abc${i + 1}`,
|
|
1176
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1177
|
+
links: [
|
|
1178
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" },
|
|
1179
|
+
{ predicate: "recipe://rating", target: `${i + 1}`, author: "did:key:alice", timestamp: "2023-01-01T00:00:00Z" }
|
|
1180
|
+
]
|
|
1181
|
+
}));
|
|
1182
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1183
|
+
// Count recipes with rating between 2 and 4 (should match 3 recipes: rating 2, 3, 4)
|
|
1184
|
+
const count = await Recipe.count(mockPerspective, { where: { rating: { between: [2, 4] } } });
|
|
1185
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1186
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { rating: { between: [2, 4] } } });
|
|
1187
|
+
expect(count).toBe(3);
|
|
1188
|
+
expect(count).toBe(findAllResults.length);
|
|
1189
|
+
});
|
|
1190
|
+
it("should apply JS-level filtering for timestamp gt operator in SurrealDB count()", async () => {
|
|
1191
|
+
// Mock SurrealDB results: 5 recipes with different timestamps
|
|
1192
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1193
|
+
source: `node:abc${i + 1}`,
|
|
1194
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1195
|
+
links: [
|
|
1196
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` },
|
|
1197
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` }
|
|
1198
|
+
]
|
|
1199
|
+
}));
|
|
1200
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1201
|
+
// Count recipes with timestamp > 2023-01-03 (should match 2 recipes: 2023-01-04 and 2023-01-05)
|
|
1202
|
+
const targetTimestamp = new Date("2023-01-03T00:00:00Z").getTime();
|
|
1203
|
+
const count = await Recipe.count(mockPerspective, { where: { timestamp: { gt: targetTimestamp } } });
|
|
1204
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1205
|
+
const findAllResults = await Recipe.findAll(mockPerspective, { where: { timestamp: { gt: targetTimestamp } } });
|
|
1206
|
+
expect(count).toBe(2);
|
|
1207
|
+
expect(count).toBe(findAllResults.length);
|
|
1208
|
+
});
|
|
1209
|
+
it("should apply JS-level filtering for timestamp between operator in SurrealDB count()", async () => {
|
|
1210
|
+
// Mock SurrealDB results: 5 recipes with different timestamps
|
|
1211
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1212
|
+
source: `node:abc${i + 1}`,
|
|
1213
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1214
|
+
links: [
|
|
1215
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` },
|
|
1216
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` }
|
|
1217
|
+
]
|
|
1218
|
+
}));
|
|
1219
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1220
|
+
// Count recipes with timestamp between 2023-01-02 and 2023-01-04
|
|
1221
|
+
const startTimestamp = new Date("2023-01-02T00:00:00Z").getTime();
|
|
1222
|
+
const endTimestamp = new Date("2023-01-04T00:00:00Z").getTime();
|
|
1223
|
+
const count = await Recipe.count(mockPerspective, {
|
|
1224
|
+
where: { timestamp: { between: [startTimestamp, endTimestamp] } }
|
|
1225
|
+
});
|
|
1226
|
+
// Verify count matches the number of instances that would be returned by findAll
|
|
1227
|
+
const findAllResults = await Recipe.findAll(mockPerspective, {
|
|
1228
|
+
where: { timestamp: { between: [startTimestamp, endTimestamp] } }
|
|
1229
|
+
});
|
|
1230
|
+
expect(count).toBe(3);
|
|
1231
|
+
expect(count).toBe(findAllResults.length);
|
|
1232
|
+
});
|
|
1233
|
+
it("should apply JS-level filtering for author filtering in SurrealDB count()", async () => {
|
|
1234
|
+
// Mock SurrealDB results: 3 recipes by Alice and 2 by Bob
|
|
1235
|
+
const surrealResults = [
|
|
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);
|
|
1260
|
+
});
|
|
1261
|
+
it("should apply JS-level filtering in ModelQueryBuilder.count() with gt operator", async () => {
|
|
1262
|
+
// Mock SurrealDB results: 5 recipes with ratings 1, 2, 3, 4, 5
|
|
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);
|
|
1282
|
+
});
|
|
1283
|
+
it("should apply JS-level filtering in ModelQueryBuilder.count() with timestamp between", async () => {
|
|
1284
|
+
// Mock SurrealDB results: 5 recipes with different timestamps
|
|
1285
|
+
const surrealResults = Array.from({ length: 5 }, (_, i) => ({
|
|
1286
|
+
source: `node:abc${i + 1}`,
|
|
1287
|
+
source_uri: `literal://recipe${i + 1}`,
|
|
1288
|
+
links: [
|
|
1289
|
+
{ predicate: "recipe://name", target: `Recipe ${i + 1}`, author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` },
|
|
1290
|
+
{ predicate: "recipe://rating", target: "5", author: "did:key:alice", timestamp: `2023-01-0${i + 1}T00:00:00Z` }
|
|
1291
|
+
]
|
|
1292
|
+
}));
|
|
1293
|
+
mockPerspective.querySurrealDB.mockResolvedValue(surrealResults);
|
|
1294
|
+
const startTimestamp = new Date("2023-01-02T00:00:00Z").getTime();
|
|
1295
|
+
const endTimestamp = new Date("2023-01-04T00:00:00Z").getTime();
|
|
1296
|
+
// Count using ModelQueryBuilder
|
|
1297
|
+
const count = await Recipe.query(mockPerspective)
|
|
1298
|
+
.where({ timestamp: { between: [startTimestamp, endTimestamp] } })
|
|
1299
|
+
.count();
|
|
1300
|
+
// Verify count matches the number of instances that would be returned by get()
|
|
1301
|
+
const getResults = await Recipe.query(mockPerspective)
|
|
1302
|
+
.where({ timestamp: { between: [startTimestamp, endTimestamp] } })
|
|
1303
|
+
.get();
|
|
1304
|
+
expect(count).toBe(3);
|
|
1305
|
+
expect(count).toBe(getResults.length);
|
|
1306
|
+
});
|
|
1307
|
+
it("should handle count() with Prolog for gt operator (legacy)", async () => {
|
|
1308
|
+
const prologResults = [{ TotalCount: 2 }];
|
|
1309
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1310
|
+
const count = await Recipe.count(mockPerspective, { where: { rating: { gt: 3 } } }, false);
|
|
1311
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1312
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1313
|
+
expect(count).toBe(2);
|
|
1314
|
+
});
|
|
1315
|
+
it("should handle count() with Prolog for timestamp between (legacy)", async () => {
|
|
1316
|
+
const prologResults = [{ TotalCount: 3 }];
|
|
1317
|
+
mockPerspective.infer.mockResolvedValue(prologResults);
|
|
1318
|
+
const startTimestamp = new Date("2023-01-02T00:00:00Z").getTime();
|
|
1319
|
+
const endTimestamp = new Date("2023-01-04T00:00:00Z").getTime();
|
|
1320
|
+
const count = await Recipe.count(mockPerspective, { where: { timestamp: { between: [startTimestamp, endTimestamp] } } }, false);
|
|
1321
|
+
expect(mockPerspective.infer).toHaveBeenCalledTimes(1);
|
|
1322
|
+
expect(mockPerspective.querySurrealDB).not.toHaveBeenCalled();
|
|
1323
|
+
expect(count).toBe(3);
|
|
1324
|
+
});
|
|
1325
|
+
});
|