@coasys/ad4m 0.11.2-dev.5 → 0.11.2-dev.6
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 +137 -61
- package/lib/index.js +137 -61
- package/lib/src/model/Ad4mModel.d.ts +6 -5
- package/lib/src/model/Ad4mModel.js +63 -33
- package/lib/src/model/Ad4mModel.test.js +10 -10
- package/lib/src/model/decorators.d.ts +26 -18
- package/lib/src/model/decorators.js +40 -25
- package/lib/src/perspectives/PerspectiveProxy.d.ts +3 -0
- package/lib/src/perspectives/PerspectiveProxy.js +34 -3
- package/package.json +1 -1
- package/src/model/Ad4mModel.test.ts +10 -10
- package/src/model/Ad4mModel.ts +69 -34
- package/src/model/decorators.ts +48 -34
- package/src/perspectives/PerspectiveProxy.ts +43 -7
|
@@ -373,9 +373,9 @@ export class Ad4mModel {
|
|
|
373
373
|
writable: options.writable || false,
|
|
374
374
|
...(options.initial !== undefined && { initial: options.initial }),
|
|
375
375
|
...(options.resolveLanguage !== undefined && { resolveLanguage: options.resolveLanguage }),
|
|
376
|
+
...(options.prologGetter !== undefined && { prologGetter: options.prologGetter }),
|
|
376
377
|
...(options.getter !== undefined && { getter: options.getter }),
|
|
377
|
-
...(options.
|
|
378
|
-
...(options.setter !== undefined && { setter: options.setter }),
|
|
378
|
+
...(options.prologSetter !== undefined && { prologSetter: options.prologSetter }),
|
|
379
379
|
...(options.local !== undefined && { local: options.local }),
|
|
380
380
|
...(options.transform !== undefined && { transform: options.transform }),
|
|
381
381
|
...(options.flag !== undefined && { flag: options.flag })
|
|
@@ -391,7 +391,7 @@ export class Ad4mModel {
|
|
|
391
391
|
predicate: options.through || "",
|
|
392
392
|
...(options.where !== undefined && { where: options.where }),
|
|
393
393
|
...(options.local !== undefined && { local: options.local }),
|
|
394
|
-
...(options.
|
|
394
|
+
...(options.getter !== undefined && { getter: options.getter })
|
|
395
395
|
};
|
|
396
396
|
}
|
|
397
397
|
// Fallback: If both structures are empty but a JSON schema is attached, derive from it
|
|
@@ -502,9 +502,9 @@ export class Ad4mModel {
|
|
|
502
502
|
if (metadata.writable === false) {
|
|
503
503
|
throw new Error(`Property "${key}" is read-only and cannot be written`);
|
|
504
504
|
}
|
|
505
|
-
if (metadata.
|
|
506
|
-
// Custom setter - throw error for now (Phase 2)
|
|
507
|
-
throw new Error(`Custom setter for property "${key}" not yet supported without Prolog. ` +
|
|
505
|
+
if (metadata.prologSetter) {
|
|
506
|
+
// Custom Prolog setter - throw error for now (Phase 2)
|
|
507
|
+
throw new Error(`Custom Prolog setter for property "${key}" not yet supported without Prolog. ` +
|
|
508
508
|
`Use standard @Property decorator or enable Prolog for custom setters.`);
|
|
509
509
|
}
|
|
510
510
|
if (!metadata.through) {
|
|
@@ -634,10 +634,10 @@ export class Ad4mModel {
|
|
|
634
634
|
let maxTimestamp = null;
|
|
635
635
|
let latestAuthor = null;
|
|
636
636
|
let originalAuthor = null;
|
|
637
|
-
// Process properties (skip those with
|
|
637
|
+
// Process properties (skip those with custom getter)
|
|
638
638
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
639
|
-
if (propMeta.
|
|
640
|
-
continue; // Handle via
|
|
639
|
+
if (propMeta.getter)
|
|
640
|
+
continue; // Handle via custom getter evaluation
|
|
641
641
|
const matching = links.filter((l) => l.predicate === propMeta.predicate);
|
|
642
642
|
if (matching.length > 0) {
|
|
643
643
|
// "Latest wins" semantics: select the last element since links are ordered ASC.
|
|
@@ -689,14 +689,44 @@ export class Ad4mModel {
|
|
|
689
689
|
this[propName] = value;
|
|
690
690
|
}
|
|
691
691
|
}
|
|
692
|
-
// Process collections (skip those with
|
|
692
|
+
// Process collections (skip those with custom getter)
|
|
693
693
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
694
|
-
if (collMeta.
|
|
695
|
-
continue; // Handle via
|
|
694
|
+
if (collMeta.getter)
|
|
695
|
+
continue; // Handle via custom getter evaluation
|
|
696
696
|
const matching = links.filter((l) => l.predicate === collMeta.predicate);
|
|
697
697
|
// Collections preserve chronological order: links are sorted ASC by timestamp,
|
|
698
698
|
// so the collection reflects the order in which items were added (oldest to newest).
|
|
699
699
|
let values = matching.map((l) => l.target);
|
|
700
|
+
// Apply where.condition filtering if present
|
|
701
|
+
if (collMeta.where?.condition && values.length > 0) {
|
|
702
|
+
try {
|
|
703
|
+
// Filter values by evaluating condition for each value
|
|
704
|
+
const filteredValues = [];
|
|
705
|
+
for (const value of values) {
|
|
706
|
+
let condition = collMeta.where.condition
|
|
707
|
+
.replace(/\$perspective/g, `'${__classPrivateFieldGet(this, _Ad4mModel_perspective, "f").uuid}'`)
|
|
708
|
+
.replace(/\$base/g, `'${__classPrivateFieldGet(this, _Ad4mModel_baseExpression, "f")}'`)
|
|
709
|
+
.replace(/Target/g, `'${value.replace(/'/g, "\\'")}'`);
|
|
710
|
+
// If condition starts with WHERE, wrap it in array length check pattern
|
|
711
|
+
// Using array::len() to properly count matching links
|
|
712
|
+
if (condition.trim().startsWith('WHERE')) {
|
|
713
|
+
condition = `array::len(SELECT * FROM link ${condition}) > 0`;
|
|
714
|
+
}
|
|
715
|
+
const filterQuery = `RETURN ${condition}`;
|
|
716
|
+
const result = await __classPrivateFieldGet(this, _Ad4mModel_perspective, "f").querySurrealDB(filterQuery);
|
|
717
|
+
// RETURN can return the value directly or in an array
|
|
718
|
+
const isTrue = result === true || (Array.isArray(result) && result.length > 0 && result[0] === true);
|
|
719
|
+
if (isTrue) {
|
|
720
|
+
filteredValues.push(value);
|
|
721
|
+
}
|
|
722
|
+
}
|
|
723
|
+
values = filteredValues;
|
|
724
|
+
}
|
|
725
|
+
catch (error) {
|
|
726
|
+
console.warn(`Failed to apply condition filter for ${collName}:`, error);
|
|
727
|
+
// Keep unfiltered values on error
|
|
728
|
+
}
|
|
729
|
+
}
|
|
700
730
|
// Apply where.isInstance filtering if present
|
|
701
731
|
if (collMeta.where?.isInstance && values.length > 0) {
|
|
702
732
|
try {
|
|
@@ -726,11 +756,11 @@ export class Ad4mModel {
|
|
|
726
756
|
}
|
|
727
757
|
}
|
|
728
758
|
// Evaluate SurrealQL getters
|
|
729
|
-
await ctor.
|
|
730
|
-
// Apply where.isInstance filtering to
|
|
731
|
-
// (non-
|
|
759
|
+
await ctor.evaluateCustomGettersForInstance(this, __classPrivateFieldGet(this, _Ad4mModel_perspective, "f"), metadata);
|
|
760
|
+
// Apply where.isInstance filtering to getter collections
|
|
761
|
+
// (non-getter collections were already filtered above)
|
|
732
762
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
733
|
-
if (collMeta.
|
|
763
|
+
if (collMeta.getter && collMeta.where?.isInstance && this[collName]?.length > 0) {
|
|
734
764
|
try {
|
|
735
765
|
const className = typeof collMeta.where.isInstance === 'string'
|
|
736
766
|
? collMeta.where.isInstance
|
|
@@ -778,14 +808,14 @@ export class Ad4mModel {
|
|
|
778
808
|
* Evaluates custom SurrealQL getters for properties and collections on a specific instance.
|
|
779
809
|
* @private
|
|
780
810
|
*/
|
|
781
|
-
static async
|
|
811
|
+
static async evaluateCustomGettersForInstance(instance, perspective, metadata) {
|
|
782
812
|
const safeBaseExpression = this.formatSurrealValue(instance.baseExpression);
|
|
783
|
-
// Evaluate property
|
|
813
|
+
// Evaluate property getters
|
|
784
814
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
785
|
-
if (propMeta.
|
|
815
|
+
if (propMeta.getter) {
|
|
786
816
|
try {
|
|
787
817
|
// Replace 'Base' placeholder with actual base expression
|
|
788
|
-
const query = propMeta.
|
|
818
|
+
const query = propMeta.getter.replace(/Base/g, safeBaseExpression);
|
|
789
819
|
// Query from node table to have graph traversal context
|
|
790
820
|
const result = await perspective.querySurrealDB(`SELECT (${query}) AS value FROM node WHERE uri = ${safeBaseExpression}`);
|
|
791
821
|
if (result && result.length > 0 && result[0].value !== undefined && result[0].value !== null && result[0].value !== 'None' && result[0].value !== '') {
|
|
@@ -793,16 +823,16 @@ export class Ad4mModel {
|
|
|
793
823
|
}
|
|
794
824
|
}
|
|
795
825
|
catch (error) {
|
|
796
|
-
console.warn(`Failed to evaluate
|
|
826
|
+
console.warn(`Failed to evaluate getter for ${propName}:`, error);
|
|
797
827
|
}
|
|
798
828
|
}
|
|
799
829
|
}
|
|
800
|
-
// Evaluate collection
|
|
830
|
+
// Evaluate collection getters
|
|
801
831
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
802
|
-
if (collMeta.
|
|
832
|
+
if (collMeta.getter) {
|
|
803
833
|
try {
|
|
804
834
|
// Replace 'Base' placeholder with actual base expression
|
|
805
|
-
const query = collMeta.
|
|
835
|
+
const query = collMeta.getter.replace(/Base/g, safeBaseExpression);
|
|
806
836
|
// Query from node table to have graph traversal context
|
|
807
837
|
const result = await perspective.querySurrealDB(`SELECT (${query}) AS value FROM node WHERE uri = ${safeBaseExpression}`);
|
|
808
838
|
if (result && result.length > 0 && result[0].value !== undefined && result[0].value !== null) {
|
|
@@ -814,7 +844,7 @@ export class Ad4mModel {
|
|
|
814
844
|
}
|
|
815
845
|
}
|
|
816
846
|
catch (error) {
|
|
817
|
-
console.warn(`Failed to evaluate
|
|
847
|
+
console.warn(`Failed to evaluate getter for ${collName}:`, error);
|
|
818
848
|
}
|
|
819
849
|
}
|
|
820
850
|
}
|
|
@@ -1380,11 +1410,11 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1380
1410
|
latestAuthor = link.author;
|
|
1381
1411
|
}
|
|
1382
1412
|
}
|
|
1383
|
-
// Find matching property (skip those with
|
|
1413
|
+
// Find matching property (skip those with getter)
|
|
1384
1414
|
let foundProperty = false;
|
|
1385
1415
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
1386
|
-
if (propMeta.
|
|
1387
|
-
continue; // Handle via
|
|
1416
|
+
if (propMeta.getter)
|
|
1417
|
+
continue; // Handle via getter evaluation
|
|
1388
1418
|
if (propMeta.predicate === predicate) {
|
|
1389
1419
|
// For properties, take the first value (or we could use timestamp to get latest)
|
|
1390
1420
|
// Note: Empty objects {} are truthy, so we need to check for them explicitly
|
|
@@ -1454,11 +1484,11 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1454
1484
|
break;
|
|
1455
1485
|
}
|
|
1456
1486
|
}
|
|
1457
|
-
// If not a property, check if it's a collection (skip those with
|
|
1487
|
+
// If not a property, check if it's a collection (skip those with getter)
|
|
1458
1488
|
if (!foundProperty) {
|
|
1459
1489
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
1460
|
-
if (collMeta.
|
|
1461
|
-
continue; // Handle via
|
|
1490
|
+
if (collMeta.getter)
|
|
1491
|
+
continue; // Handle via getter evaluation
|
|
1462
1492
|
if (collMeta.predicate === predicate) {
|
|
1463
1493
|
// For collections, accumulate all values with their timestamps and indices for sorting
|
|
1464
1494
|
if (!instance[collName]) {
|
|
@@ -1561,10 +1591,10 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1561
1591
|
console.error(`Failed to process SurrealDB instance ${base}:`, error);
|
|
1562
1592
|
}
|
|
1563
1593
|
}
|
|
1564
|
-
// Evaluate
|
|
1594
|
+
// Evaluate custom getters for all instances (single pass)
|
|
1565
1595
|
// This populates collection values needed for where.isInstance filtering
|
|
1566
1596
|
for (const instance of instances) {
|
|
1567
|
-
await this.
|
|
1597
|
+
await this.evaluateCustomGettersForInstance(instance, perspective, metadata);
|
|
1568
1598
|
}
|
|
1569
1599
|
// Filter collections by where.isInstance if specified
|
|
1570
1600
|
// Do this after initial evaluation so collection values exist for filtering
|
|
@@ -40,7 +40,7 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
40
40
|
__metadata("design:type", String)
|
|
41
41
|
], PropertyModel.prototype, "optional", void 0);
|
|
42
42
|
__decorate([
|
|
43
|
-
ReadOnly({ through: "test://readonly",
|
|
43
|
+
ReadOnly({ through: "test://readonly", prologGetter: "custom_getter" }),
|
|
44
44
|
__metadata("design:type", String)
|
|
45
45
|
], PropertyModel.prototype, "readonly", void 0);
|
|
46
46
|
__decorate([
|
|
@@ -64,7 +64,7 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
64
64
|
// Verify "readonly" property
|
|
65
65
|
expect(metadata.properties.readonly.predicate).toBe("test://readonly");
|
|
66
66
|
expect(metadata.properties.readonly.writable).toBe(false);
|
|
67
|
-
expect(metadata.properties.readonly.
|
|
67
|
+
expect(metadata.properties.readonly.prologGetter).toBe("custom_getter");
|
|
68
68
|
// Verify "type" property (flag)
|
|
69
69
|
expect(metadata.properties.type.predicate).toBe("test://type");
|
|
70
70
|
expect(metadata.properties.type.flag).toBe(true);
|
|
@@ -144,8 +144,8 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
144
144
|
__decorate([
|
|
145
145
|
Optional({
|
|
146
146
|
through: "test://computed",
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
prologGetter: "triple(Base, 'test://value', V), Value is V * 2",
|
|
148
|
+
prologSetter: "Value is V / 2, Actions = [{action: 'setSingleTarget', source: 'this', predicate: 'test://value', target: Value}]"
|
|
149
149
|
}),
|
|
150
150
|
__metadata("design:type", Number)
|
|
151
151
|
], CustomModel.prototype, "computed", void 0);
|
|
@@ -153,10 +153,10 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
153
153
|
ModelOptions({ name: "CustomModel" })
|
|
154
154
|
], CustomModel);
|
|
155
155
|
const metadata = CustomModel.getModelMetadata();
|
|
156
|
-
// Assert
|
|
157
|
-
expect(metadata.properties.computed.
|
|
158
|
-
expect(metadata.properties.computed.
|
|
159
|
-
expect(metadata.properties.computed.
|
|
156
|
+
// Assert prologGetter and prologSetter contain the custom code
|
|
157
|
+
expect(metadata.properties.computed.prologGetter).toContain("triple(Base, 'test://value', V), Value is V * 2");
|
|
158
|
+
expect(metadata.properties.computed.prologSetter).toContain("Value is V / 2");
|
|
159
|
+
expect(metadata.properties.computed.prologSetter).toContain("setSingleTarget");
|
|
160
160
|
});
|
|
161
161
|
it("should handle collection with isInstance where clause", () => {
|
|
162
162
|
let Comment = class Comment extends Ad4mModel {
|
|
@@ -210,7 +210,7 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
210
210
|
__metadata("design:type", String)
|
|
211
211
|
], Recipe.prototype, "description", void 0);
|
|
212
212
|
__decorate([
|
|
213
|
-
ReadOnly({ through: "recipe://rating",
|
|
213
|
+
ReadOnly({ through: "recipe://rating", prologGetter: "avg_rating(Base, Value)" }),
|
|
214
214
|
__metadata("design:type", Number)
|
|
215
215
|
], Recipe.prototype, "rating", void 0);
|
|
216
216
|
__decorate([
|
|
@@ -241,7 +241,7 @@ describe("Ad4mModel.getModelMetadata()", () => {
|
|
|
241
241
|
expect(metadata.properties.name.resolveLanguage).toBe("literal");
|
|
242
242
|
expect(metadata.properties.description.predicate).toBe("recipe://description");
|
|
243
243
|
expect(metadata.properties.rating.predicate).toBe("recipe://rating");
|
|
244
|
-
expect(metadata.properties.rating.
|
|
244
|
+
expect(metadata.properties.rating.prologGetter).toBe("avg_rating(Base, Value)");
|
|
245
245
|
expect(metadata.collections.ingredients.predicate).toBe("recipe://ingredient");
|
|
246
246
|
expect(metadata.collections.steps.predicate).toBe("recipe://step");
|
|
247
247
|
expect(metadata.collections.steps.local).toBe(true);
|
|
@@ -12,9 +12,9 @@ export interface InstanceQueryParams {
|
|
|
12
12
|
*/
|
|
13
13
|
where?: object;
|
|
14
14
|
/**
|
|
15
|
-
* A string representing the condition clause of the query.
|
|
15
|
+
* A string representing the Prolog condition clause of the query.
|
|
16
16
|
*/
|
|
17
|
-
|
|
17
|
+
prologCondition?: string;
|
|
18
18
|
}
|
|
19
19
|
/**
|
|
20
20
|
* Decorator for querying instances of a model class.
|
|
@@ -47,14 +47,14 @@ export interface InstanceQueryParams {
|
|
|
47
47
|
* static async findByName(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
48
48
|
*
|
|
49
49
|
* // Get highly rated recipes using a custom condition
|
|
50
|
-
* @InstanceQuery({
|
|
50
|
+
* @InstanceQuery({ prologCondition: "triple(Instance, 'recipe://rating', Rating), Rating > 4" })
|
|
51
51
|
* static async topRated(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
52
52
|
* }
|
|
53
53
|
* ```
|
|
54
54
|
*
|
|
55
55
|
* @param {Object} [options] - Query options
|
|
56
56
|
* @param {object} [options.where] - Object with property-value pairs to match
|
|
57
|
-
* @param {string} [options.
|
|
57
|
+
* @param {string} [options.prologCondition] - Custom Prolog condition for more complex queries
|
|
58
58
|
*/
|
|
59
59
|
export declare function InstanceQuery(options?: InstanceQueryParams): <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) => void;
|
|
60
60
|
export interface PropertyOptions {
|
|
@@ -79,19 +79,19 @@ export interface PropertyOptions {
|
|
|
79
79
|
*/
|
|
80
80
|
resolveLanguage?: string;
|
|
81
81
|
/**
|
|
82
|
-
* Custom getter to get the value of the property
|
|
82
|
+
* Custom Prolog getter to get the value of the property. If not provided, the default getter will be used.
|
|
83
83
|
*/
|
|
84
|
-
|
|
84
|
+
prologGetter?: string;
|
|
85
85
|
/**
|
|
86
|
-
* Custom setter to set the value of the property
|
|
86
|
+
* Custom Prolog setter to set the value of the property. Only available if the property is writable.
|
|
87
87
|
*/
|
|
88
|
-
|
|
88
|
+
prologSetter?: string;
|
|
89
89
|
/**
|
|
90
90
|
* Custom SurrealQL getter to resolve the property value. Use this for custom graph traversals.
|
|
91
91
|
* The expression can reference 'Base' which will be replaced with the instance's base expression.
|
|
92
92
|
* Example: "(<-link[WHERE predicate = 'flux://has_reply'].in.uri)[0]"
|
|
93
93
|
*/
|
|
94
|
-
|
|
94
|
+
getter?: string;
|
|
95
95
|
/**
|
|
96
96
|
* Indicates whether the property is stored locally in the perspective and not in the network. Useful for properties that are not meant to be shared with the network.
|
|
97
97
|
*/
|
|
@@ -178,8 +178,8 @@ export interface PropertyOptions {
|
|
|
178
178
|
* @param {boolean} [opts.required] - Whether the property must have a value
|
|
179
179
|
* @param {boolean} [opts.writable=true] - Whether the property can be modified
|
|
180
180
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
181
|
-
* @param {string} [opts.
|
|
182
|
-
* @param {string} [opts.
|
|
181
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
182
|
+
* @param {string} [opts.prologSetter] - Custom Prolog code for setting the property value
|
|
183
183
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
184
184
|
*/
|
|
185
185
|
export declare function Optional(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
|
|
@@ -249,6 +249,7 @@ export interface FlagOptions {
|
|
|
249
249
|
export declare function Flag(opts: FlagOptions): <T>(target: T, key: keyof T) => void;
|
|
250
250
|
interface WhereOptions {
|
|
251
251
|
isInstance?: any;
|
|
252
|
+
prologCondition?: string;
|
|
252
253
|
condition?: string;
|
|
253
254
|
}
|
|
254
255
|
export interface CollectionOptions {
|
|
@@ -265,7 +266,7 @@ export interface CollectionOptions {
|
|
|
265
266
|
* The expression can reference 'Base' which will be replaced with the instance's base expression.
|
|
266
267
|
* Example: "(<-link[WHERE predicate = 'flux://has_reply'].in.uri)"
|
|
267
268
|
*/
|
|
268
|
-
|
|
269
|
+
getter?: string;
|
|
269
270
|
/**
|
|
270
271
|
* Indicates whether the property is stored locally in the perspective and not in the network. Useful for properties that are not meant to be shared with the network.
|
|
271
272
|
*/
|
|
@@ -307,13 +308,20 @@ export interface CollectionOptions {
|
|
|
307
308
|
* })
|
|
308
309
|
* comments: string[] = [];
|
|
309
310
|
*
|
|
310
|
-
* // Collection with custom filter condition
|
|
311
|
+
* // Collection with custom Prolog filter condition
|
|
311
312
|
* @Collection({
|
|
312
313
|
* through: "recipe://step",
|
|
313
|
-
* where: {
|
|
314
|
+
* where: { prologCondition: `triple(Target, "step://order", Order), Order < 3` }
|
|
314
315
|
* })
|
|
315
316
|
* firstSteps: string[] = [];
|
|
316
317
|
*
|
|
318
|
+
* // Collection with custom SurrealDB filter condition
|
|
319
|
+
* @Collection({
|
|
320
|
+
* through: "recipe://entries",
|
|
321
|
+
* where: { condition: `WHERE in.uri = Target AND predicate = 'recipe://has_ingredient' AND out.uri = 'recipe://test')`
|
|
322
|
+
* })
|
|
323
|
+
* ingredients: string[] = [];
|
|
324
|
+
*
|
|
317
325
|
* // Local-only collection not shared with network
|
|
318
326
|
* @Collection({
|
|
319
327
|
* through: "recipe://note",
|
|
@@ -333,7 +341,7 @@ export interface CollectionOptions {
|
|
|
333
341
|
* @param {string} opts.through - The predicate URI for collection links
|
|
334
342
|
* @param {WhereOptions} [opts.where] - Filter conditions for collection values
|
|
335
343
|
* @param {any} [opts.where.isInstance] - Model class to filter instances by
|
|
336
|
-
* @param {string} [opts.where.
|
|
344
|
+
* @param {string} [opts.where.prologCondition] - Custom Prolog condition for filtering
|
|
337
345
|
* @param {boolean} [opts.local] - Whether collection links are stored locally only
|
|
338
346
|
*/
|
|
339
347
|
export declare function Collection(opts: CollectionOptions): <T>(target: T, key: keyof T) => void;
|
|
@@ -450,8 +458,8 @@ export declare function ModelOptions(opts: ModelOptionsOptions): (target: any) =
|
|
|
450
458
|
* @param {string} opts.through - The predicate URI for the property
|
|
451
459
|
* @param {string} [opts.initial] - Initial value (defaults to "literal://string:uninitialized")
|
|
452
460
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
453
|
-
* @param {string} [opts.
|
|
454
|
-
* @param {string} [opts.
|
|
461
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
462
|
+
* @param {string} [opts.prologSetter] - Custom Prolog code for setting the property value
|
|
455
463
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
456
464
|
*/
|
|
457
465
|
export declare function Property(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
|
|
@@ -507,7 +515,7 @@ export declare function Property(opts: PropertyOptions): <T>(target: T, key: key
|
|
|
507
515
|
* @param {string} opts.through - The predicate URI for the property
|
|
508
516
|
* @param {string} [opts.initial] - Initial value (if property should have one)
|
|
509
517
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
510
|
-
* @param {string} [opts.
|
|
518
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
511
519
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
512
520
|
*/
|
|
513
521
|
export declare function ReadOnly(opts: PropertyOptions): <T>(target: T, key: keyof T) => void;
|
|
@@ -44,14 +44,14 @@ export function hasLink(predicate) {
|
|
|
44
44
|
* static async findByName(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
45
45
|
*
|
|
46
46
|
* // Get highly rated recipes using a custom condition
|
|
47
|
-
* @InstanceQuery({
|
|
47
|
+
* @InstanceQuery({ prologCondition: "triple(Instance, 'recipe://rating', Rating), Rating > 4" })
|
|
48
48
|
* static async topRated(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
49
49
|
* }
|
|
50
50
|
* ```
|
|
51
51
|
*
|
|
52
52
|
* @param {Object} [options] - Query options
|
|
53
53
|
* @param {object} [options.where] - Object with property-value pairs to match
|
|
54
|
-
* @param {string} [options.
|
|
54
|
+
* @param {string} [options.prologCondition] - Custom Prolog condition for more complex queries
|
|
55
55
|
*/
|
|
56
56
|
export function InstanceQuery(options) {
|
|
57
57
|
return function (target, key, descriptor) {
|
|
@@ -70,8 +70,8 @@ export function InstanceQuery(options) {
|
|
|
70
70
|
query += `, property_getter(C, Instance, "${prop}", "${value}")`;
|
|
71
71
|
}
|
|
72
72
|
}
|
|
73
|
-
if (options && options.
|
|
74
|
-
query += ', ' + options.
|
|
73
|
+
if (options && options.prologCondition) {
|
|
74
|
+
query += ', ' + options.prologCondition;
|
|
75
75
|
}
|
|
76
76
|
// Try Prolog first
|
|
77
77
|
try {
|
|
@@ -191,8 +191,8 @@ export function InstanceQuery(options) {
|
|
|
191
191
|
* @param {boolean} [opts.required] - Whether the property must have a value
|
|
192
192
|
* @param {boolean} [opts.writable=true] - Whether the property can be modified
|
|
193
193
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
194
|
-
* @param {string} [opts.
|
|
195
|
-
* @param {string} [opts.
|
|
194
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
195
|
+
* @param {string} [opts.prologSetter] - Custom Prolog code for setting the property value
|
|
196
196
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
197
197
|
*/
|
|
198
198
|
export function Optional(opts) {
|
|
@@ -203,8 +203,8 @@ export function Optional(opts) {
|
|
|
203
203
|
if (opts.required && !opts.initial) {
|
|
204
204
|
throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
|
|
205
205
|
}
|
|
206
|
-
if (!opts.through && !opts.
|
|
207
|
-
throw new Error("SubjectProperty requires either 'through' or '
|
|
206
|
+
if (!opts.through && !opts.prologGetter) {
|
|
207
|
+
throw new Error("SubjectProperty requires either 'through' or 'prologGetter' option");
|
|
208
208
|
}
|
|
209
209
|
target["__properties"] = target["__properties"] || {};
|
|
210
210
|
target["__properties"][key] = target["__properties"][key] || {};
|
|
@@ -330,13 +330,20 @@ export function Flag(opts) {
|
|
|
330
330
|
* })
|
|
331
331
|
* comments: string[] = [];
|
|
332
332
|
*
|
|
333
|
-
* // Collection with custom filter condition
|
|
333
|
+
* // Collection with custom Prolog filter condition
|
|
334
334
|
* @Collection({
|
|
335
335
|
* through: "recipe://step",
|
|
336
|
-
* where: {
|
|
336
|
+
* where: { prologCondition: `triple(Target, "step://order", Order), Order < 3` }
|
|
337
337
|
* })
|
|
338
338
|
* firstSteps: string[] = [];
|
|
339
339
|
*
|
|
340
|
+
* // Collection with custom SurrealDB filter condition
|
|
341
|
+
* @Collection({
|
|
342
|
+
* through: "recipe://entries",
|
|
343
|
+
* where: { condition: `WHERE in.uri = Target AND predicate = 'recipe://has_ingredient' AND out.uri = 'recipe://test')`
|
|
344
|
+
* })
|
|
345
|
+
* ingredients: string[] = [];
|
|
346
|
+
*
|
|
340
347
|
* // Local-only collection not shared with network
|
|
341
348
|
* @Collection({
|
|
342
349
|
* through: "recipe://note",
|
|
@@ -356,7 +363,7 @@ export function Flag(opts) {
|
|
|
356
363
|
* @param {string} opts.through - The predicate URI for collection links
|
|
357
364
|
* @param {WhereOptions} [opts.where] - Filter conditions for collection values
|
|
358
365
|
* @param {any} [opts.where.isInstance] - Model class to filter instances by
|
|
359
|
-
* @param {string} [opts.where.
|
|
366
|
+
* @param {string} [opts.where.prologCondition] - Custom Prolog condition for filtering
|
|
360
367
|
* @param {boolean} [opts.local] - Whether collection links are stored locally only
|
|
361
368
|
*/
|
|
362
369
|
export function Collection(opts) {
|
|
@@ -456,13 +463,13 @@ export function ModelOptions(opts) {
|
|
|
456
463
|
let properties = obj.__properties || {};
|
|
457
464
|
for (let property in properties) {
|
|
458
465
|
let propertyCode = `property(${uuid}, "${property}").\n`;
|
|
459
|
-
let { through, initial, required, resolveLanguage, writable, flag,
|
|
466
|
+
let { through, initial, required, resolveLanguage, writable, flag, prologGetter, prologSetter, local } = properties[property];
|
|
460
467
|
if (resolveLanguage) {
|
|
461
468
|
propertyCode += `property_resolve(${uuid}, "${property}").\n`;
|
|
462
469
|
propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`;
|
|
463
470
|
}
|
|
464
|
-
if (
|
|
465
|
-
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${
|
|
471
|
+
if (prologGetter) {
|
|
472
|
+
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${prologGetter}.\n`;
|
|
466
473
|
}
|
|
467
474
|
else if (through) {
|
|
468
475
|
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`;
|
|
@@ -475,8 +482,8 @@ export function ModelOptions(opts) {
|
|
|
475
482
|
}
|
|
476
483
|
}
|
|
477
484
|
}
|
|
478
|
-
if (
|
|
479
|
-
propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${
|
|
485
|
+
if (prologSetter) {
|
|
486
|
+
propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${prologSetter}.\n`;
|
|
480
487
|
}
|
|
481
488
|
else if (writable && through) {
|
|
482
489
|
let setter = obj[propertyNameToSetterName(property)];
|
|
@@ -514,8 +521,8 @@ export function ModelOptions(opts) {
|
|
|
514
521
|
let { through, where, local } = collections[collection];
|
|
515
522
|
if (through) {
|
|
516
523
|
if (where) {
|
|
517
|
-
if (!where.isInstance && !where.condition) {
|
|
518
|
-
throw "'where' needs one of 'isInstance' or 'condition'";
|
|
524
|
+
if (!where.isInstance && !where.prologCondition && !where.condition) {
|
|
525
|
+
throw "'where' needs one of 'isInstance', 'prologCondition', or 'condition'";
|
|
519
526
|
}
|
|
520
527
|
let conditions = [];
|
|
521
528
|
if (where.isInstance) {
|
|
@@ -528,11 +535,19 @@ export function ModelOptions(opts) {
|
|
|
528
535
|
}
|
|
529
536
|
conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`);
|
|
530
537
|
}
|
|
531
|
-
if (where.
|
|
532
|
-
conditions.push(where.
|
|
538
|
+
if (where.prologCondition) {
|
|
539
|
+
conditions.push(where.prologCondition);
|
|
540
|
+
}
|
|
541
|
+
// If there are Prolog conditions (isInstance or prologCondition), use setof with conditions
|
|
542
|
+
// If only condition is present, use simple findall (SurrealDB will filter later)
|
|
543
|
+
if (conditions.length > 0) {
|
|
544
|
+
const conditionString = conditions.join(", ");
|
|
545
|
+
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`;
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
// Only SurrealDB condition present (no Prolog filtering)
|
|
549
|
+
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`;
|
|
533
550
|
}
|
|
534
|
-
const conditionString = conditions.join(", ");
|
|
535
|
-
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`;
|
|
536
551
|
}
|
|
537
552
|
else {
|
|
538
553
|
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`;
|
|
@@ -634,8 +649,8 @@ export function ModelOptions(opts) {
|
|
|
634
649
|
* @param {string} opts.through - The predicate URI for the property
|
|
635
650
|
* @param {string} [opts.initial] - Initial value (defaults to "literal://string:uninitialized")
|
|
636
651
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
637
|
-
* @param {string} [opts.
|
|
638
|
-
* @param {string} [opts.
|
|
652
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
653
|
+
* @param {string} [opts.prologSetter] - Custom Prolog code for setting the property value
|
|
639
654
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
640
655
|
*/
|
|
641
656
|
export function Property(opts) {
|
|
@@ -698,7 +713,7 @@ export function Property(opts) {
|
|
|
698
713
|
* @param {string} opts.through - The predicate URI for the property
|
|
699
714
|
* @param {string} [opts.initial] - Initial value (if property should have one)
|
|
700
715
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
701
|
-
* @param {string} [opts.
|
|
716
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
702
717
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
703
718
|
*/
|
|
704
719
|
export function ReadOnly(opts) {
|
|
@@ -595,6 +595,7 @@ export declare class PerspectiveProxy {
|
|
|
595
595
|
collections: Map<string, {
|
|
596
596
|
predicate: string;
|
|
597
597
|
instanceFilter?: string;
|
|
598
|
+
condition?: string;
|
|
598
599
|
}>;
|
|
599
600
|
} | null>;
|
|
600
601
|
/**
|
|
@@ -609,6 +610,7 @@ export declare class PerspectiveProxy {
|
|
|
609
610
|
/**
|
|
610
611
|
* Gets collection values using SurrealDB when Prolog fails.
|
|
611
612
|
* This is used as a fallback in SdnaOnly mode where link data isn't in Prolog.
|
|
613
|
+
* Note: This is used by Subject.ts (legacy pattern). Ad4mModel.ts uses getModelMetadata() instead.
|
|
612
614
|
*/
|
|
613
615
|
getCollectionValuesViaSurreal(baseExpression: string, className: string, collectionName: string): Promise<any[]>;
|
|
614
616
|
/**
|
|
@@ -628,6 +630,7 @@ export declare class PerspectiveProxy {
|
|
|
628
630
|
collections: Map<string, {
|
|
629
631
|
predicate: string;
|
|
630
632
|
instanceFilter?: string;
|
|
633
|
+
condition?: string;
|
|
631
634
|
}>;
|
|
632
635
|
}): Promise<string[]>;
|
|
633
636
|
/**
|