@coasys/ad4m 0.11.2-dev.4 → 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 +181 -69
- package/lib/index.js +181 -69
- package/lib/src/model/Ad4mModel.d.ts +6 -5
- package/lib/src/model/Ad4mModel.js +107 -41
- 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 +115 -43
- package/src/model/decorators.ts +48 -34
- package/src/perspectives/PerspectiveProxy.ts +43 -7
package/src/model/Ad4mModel.ts
CHANGED
|
@@ -90,11 +90,11 @@ export interface PropertyMetadata {
|
|
|
90
90
|
/** Language for resolution (e.g., "literal") */
|
|
91
91
|
resolveLanguage?: string;
|
|
92
92
|
/** Custom Prolog getter code */
|
|
93
|
-
|
|
93
|
+
prologGetter?: string;
|
|
94
94
|
/** Custom Prolog setter code */
|
|
95
|
-
|
|
95
|
+
prologSetter?: string;
|
|
96
96
|
/** Custom SurrealQL getter code */
|
|
97
|
-
|
|
97
|
+
getter?: string;
|
|
98
98
|
/** Whether stored locally only */
|
|
99
99
|
local?: boolean;
|
|
100
100
|
/** Transform function */
|
|
@@ -112,9 +112,9 @@ export interface CollectionMetadata {
|
|
|
112
112
|
/** The predicate URI (through value) */
|
|
113
113
|
predicate: string;
|
|
114
114
|
/** Filter conditions */
|
|
115
|
-
where?: { isInstance?: any; condition?: string };
|
|
115
|
+
where?: { isInstance?: any; prologCondition?: string; condition?: string };
|
|
116
116
|
/** Custom SurrealQL getter code */
|
|
117
|
-
|
|
117
|
+
getter?: string;
|
|
118
118
|
/** Whether stored locally only */
|
|
119
119
|
local?: boolean;
|
|
120
120
|
}
|
|
@@ -515,9 +515,9 @@ export class Ad4mModel {
|
|
|
515
515
|
writable: options.writable || false,
|
|
516
516
|
...(options.initial !== undefined && { initial: options.initial }),
|
|
517
517
|
...(options.resolveLanguage !== undefined && { resolveLanguage: options.resolveLanguage }),
|
|
518
|
+
...(options.prologGetter !== undefined && { prologGetter: options.prologGetter }),
|
|
518
519
|
...(options.getter !== undefined && { getter: options.getter }),
|
|
519
|
-
...(options.
|
|
520
|
-
...(options.setter !== undefined && { setter: options.setter }),
|
|
520
|
+
...(options.prologSetter !== undefined && { prologSetter: options.prologSetter }),
|
|
521
521
|
...(options.local !== undefined && { local: options.local }),
|
|
522
522
|
...(options.transform !== undefined && { transform: options.transform }),
|
|
523
523
|
...(options.flag !== undefined && { flag: options.flag })
|
|
@@ -535,7 +535,7 @@ export class Ad4mModel {
|
|
|
535
535
|
predicate: options.through || "",
|
|
536
536
|
...(options.where !== undefined && { where: options.where }),
|
|
537
537
|
...(options.local !== undefined && { local: options.local }),
|
|
538
|
-
...(options.
|
|
538
|
+
...(options.getter !== undefined && { getter: options.getter })
|
|
539
539
|
};
|
|
540
540
|
}
|
|
541
541
|
|
|
@@ -659,10 +659,10 @@ export class Ad4mModel {
|
|
|
659
659
|
throw new Error(`Property "${key}" is read-only and cannot be written`);
|
|
660
660
|
}
|
|
661
661
|
|
|
662
|
-
if (metadata.
|
|
663
|
-
// Custom setter - throw error for now (Phase 2)
|
|
662
|
+
if (metadata.prologSetter) {
|
|
663
|
+
// Custom Prolog setter - throw error for now (Phase 2)
|
|
664
664
|
throw new Error(
|
|
665
|
-
`Custom setter for property "${key}" not yet supported without Prolog. ` +
|
|
665
|
+
`Custom Prolog setter for property "${key}" not yet supported without Prolog. ` +
|
|
666
666
|
`Use standard @Property decorator or enable Prolog for custom setters.`
|
|
667
667
|
);
|
|
668
668
|
}
|
|
@@ -758,8 +758,27 @@ export class Ad4mModel {
|
|
|
758
758
|
})
|
|
759
759
|
)
|
|
760
760
|
);
|
|
761
|
+
// Filter out properties that are read-only (getters without setters)
|
|
762
|
+
const writableProps = Object.fromEntries(
|
|
763
|
+
Object.entries(propsObject).filter(([key]) => {
|
|
764
|
+
const descriptor = Object.getOwnPropertyDescriptor(Object.getPrototypeOf(instance), key);
|
|
765
|
+
if (!descriptor) {
|
|
766
|
+
// No descriptor means it's a regular property on the instance, allow it
|
|
767
|
+
return true;
|
|
768
|
+
}
|
|
769
|
+
// Check if it's an accessor descriptor (has get/set) vs data descriptor (has value/writable)
|
|
770
|
+
const isAccessor = descriptor.get !== undefined || descriptor.set !== undefined;
|
|
771
|
+
if (isAccessor) {
|
|
772
|
+
// Accessor descriptor: only allow if it has a setter
|
|
773
|
+
return descriptor.set !== undefined;
|
|
774
|
+
} else {
|
|
775
|
+
// Data descriptor: only allow if writable is not explicitly false
|
|
776
|
+
return descriptor.writable !== false;
|
|
777
|
+
}
|
|
778
|
+
})
|
|
779
|
+
);
|
|
761
780
|
// Assign properties to instance
|
|
762
|
-
Object.assign(instance,
|
|
781
|
+
Object.assign(instance, writableProps);
|
|
763
782
|
}
|
|
764
783
|
|
|
765
784
|
private async getData() {
|
|
@@ -789,9 +808,9 @@ export class Ad4mModel {
|
|
|
789
808
|
let latestAuthor = null;
|
|
790
809
|
let originalAuthor = null;
|
|
791
810
|
|
|
792
|
-
// Process properties (skip those with
|
|
811
|
+
// Process properties (skip those with custom getter)
|
|
793
812
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
794
|
-
if (propMeta.
|
|
813
|
+
if (propMeta.getter) continue; // Handle via custom getter evaluation
|
|
795
814
|
const matching = links.filter((l: any) => l.predicate === propMeta.predicate);
|
|
796
815
|
if (matching.length > 0) {
|
|
797
816
|
// "Latest wins" semantics: select the last element since links are ordered ASC.
|
|
@@ -844,14 +863,49 @@ export class Ad4mModel {
|
|
|
844
863
|
}
|
|
845
864
|
}
|
|
846
865
|
|
|
847
|
-
// Process collections (skip those with
|
|
866
|
+
// Process collections (skip those with custom getter)
|
|
848
867
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
849
|
-
if (collMeta.
|
|
868
|
+
if (collMeta.getter) continue; // Handle via custom getter evaluation
|
|
850
869
|
const matching = links.filter((l: any) => l.predicate === collMeta.predicate);
|
|
851
870
|
// Collections preserve chronological order: links are sorted ASC by timestamp,
|
|
852
871
|
// so the collection reflects the order in which items were added (oldest to newest).
|
|
853
872
|
let values = matching.map((l: any) => l.target);
|
|
854
873
|
|
|
874
|
+
// Apply where.condition filtering if present
|
|
875
|
+
if (collMeta.where?.condition && values.length > 0) {
|
|
876
|
+
try {
|
|
877
|
+
// Filter values by evaluating condition for each value
|
|
878
|
+
const filteredValues: string[] = [];
|
|
879
|
+
|
|
880
|
+
for (const value of values) {
|
|
881
|
+
let condition = collMeta.where.condition
|
|
882
|
+
.replace(/\$perspective/g, `'${this.#perspective.uuid}'`)
|
|
883
|
+
.replace(/\$base/g, `'${this.#baseExpression}'`)
|
|
884
|
+
.replace(/Target/g, `'${value.replace(/'/g, "\\'")}'`);
|
|
885
|
+
|
|
886
|
+
// If condition starts with WHERE, wrap it in array length check pattern
|
|
887
|
+
// Using array::len() to properly count matching links
|
|
888
|
+
if (condition.trim().startsWith('WHERE')) {
|
|
889
|
+
condition = `array::len(SELECT * FROM link ${condition}) > 0`;
|
|
890
|
+
}
|
|
891
|
+
|
|
892
|
+
const filterQuery = `RETURN ${condition}`;
|
|
893
|
+
const result = await this.#perspective.querySurrealDB(filterQuery);
|
|
894
|
+
|
|
895
|
+
// RETURN can return the value directly or in an array
|
|
896
|
+
const isTrue = result === true || (Array.isArray(result) && result.length > 0 && result[0] === true);
|
|
897
|
+
if (isTrue) {
|
|
898
|
+
filteredValues.push(value);
|
|
899
|
+
}
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
values = filteredValues;
|
|
903
|
+
} catch (error) {
|
|
904
|
+
console.warn(`Failed to apply condition filter for ${collName}:`, error);
|
|
905
|
+
// Keep unfiltered values on error
|
|
906
|
+
}
|
|
907
|
+
}
|
|
908
|
+
|
|
855
909
|
// Apply where.isInstance filtering if present
|
|
856
910
|
if (collMeta.where?.isInstance && values.length > 0) {
|
|
857
911
|
try {
|
|
@@ -884,7 +938,27 @@ export class Ad4mModel {
|
|
|
884
938
|
}
|
|
885
939
|
|
|
886
940
|
// Evaluate SurrealQL getters
|
|
887
|
-
await ctor.
|
|
941
|
+
await ctor.evaluateCustomGettersForInstance(this, this.#perspective, metadata);
|
|
942
|
+
|
|
943
|
+
// Apply where.isInstance filtering to getter collections
|
|
944
|
+
// (non-getter collections were already filtered above)
|
|
945
|
+
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
946
|
+
if (collMeta.getter && collMeta.where?.isInstance && (this as any)[collName]?.length > 0) {
|
|
947
|
+
try {
|
|
948
|
+
const className = typeof collMeta.where.isInstance === 'string'
|
|
949
|
+
? collMeta.where.isInstance
|
|
950
|
+
: collMeta.where.isInstance.name;
|
|
951
|
+
|
|
952
|
+
const filterMetadata = await this.#perspective.getSubjectClassMetadataFromSDNA(className);
|
|
953
|
+
if (filterMetadata) {
|
|
954
|
+
const filtered = await this.#perspective.batchCheckSubjectInstances((this as any)[collName], filterMetadata);
|
|
955
|
+
(this as any)[collName] = filtered;
|
|
956
|
+
}
|
|
957
|
+
} catch (error) {
|
|
958
|
+
// Keep unfiltered values on error
|
|
959
|
+
}
|
|
960
|
+
}
|
|
961
|
+
}
|
|
888
962
|
} catch (e) {
|
|
889
963
|
console.error(`SurrealDB getData also failed for ${this.#baseExpression}:`, e);
|
|
890
964
|
}
|
|
@@ -923,19 +997,19 @@ export class Ad4mModel {
|
|
|
923
997
|
* Evaluates custom SurrealQL getters for properties and collections on a specific instance.
|
|
924
998
|
* @private
|
|
925
999
|
*/
|
|
926
|
-
private static async
|
|
1000
|
+
private static async evaluateCustomGettersForInstance(
|
|
927
1001
|
instance: any,
|
|
928
1002
|
perspective: PerspectiveProxy,
|
|
929
1003
|
metadata: any
|
|
930
1004
|
) {
|
|
931
1005
|
const safeBaseExpression = this.formatSurrealValue(instance.baseExpression);
|
|
932
1006
|
|
|
933
|
-
// Evaluate property
|
|
1007
|
+
// Evaluate property getters
|
|
934
1008
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
935
|
-
if ((propMeta as any).
|
|
1009
|
+
if ((propMeta as any).getter) {
|
|
936
1010
|
try {
|
|
937
1011
|
// Replace 'Base' placeholder with actual base expression
|
|
938
|
-
const query = (propMeta as any).
|
|
1012
|
+
const query = (propMeta as any).getter.replace(/Base/g, safeBaseExpression);
|
|
939
1013
|
// Query from node table to have graph traversal context
|
|
940
1014
|
const result = await perspective.querySurrealDB(
|
|
941
1015
|
`SELECT (${query}) AS value FROM node WHERE uri = ${safeBaseExpression}`
|
|
@@ -944,17 +1018,17 @@ export class Ad4mModel {
|
|
|
944
1018
|
instance[propName] = result[0].value;
|
|
945
1019
|
}
|
|
946
1020
|
} catch (error) {
|
|
947
|
-
console.warn(`Failed to evaluate
|
|
1021
|
+
console.warn(`Failed to evaluate getter for ${propName}:`, error);
|
|
948
1022
|
}
|
|
949
1023
|
}
|
|
950
1024
|
}
|
|
951
1025
|
|
|
952
|
-
// Evaluate collection
|
|
1026
|
+
// Evaluate collection getters
|
|
953
1027
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
954
|
-
if ((collMeta as any).
|
|
1028
|
+
if ((collMeta as any).getter) {
|
|
955
1029
|
try {
|
|
956
1030
|
// Replace 'Base' placeholder with actual base expression
|
|
957
|
-
const query = (collMeta as any).
|
|
1031
|
+
const query = (collMeta as any).getter.replace(/Base/g, safeBaseExpression);
|
|
958
1032
|
// Query from node table to have graph traversal context
|
|
959
1033
|
const result = await perspective.querySurrealDB(
|
|
960
1034
|
`SELECT (${query}) AS value FROM node WHERE uri = ${safeBaseExpression}`
|
|
@@ -963,11 +1037,11 @@ export class Ad4mModel {
|
|
|
963
1037
|
// Filter out 'None' from collection results
|
|
964
1038
|
const value = result[0].value;
|
|
965
1039
|
instance[collName] = Array.isArray(value)
|
|
966
|
-
? value.filter((v: any) => v && v !== '' && v !== 'None')
|
|
1040
|
+
? value.filter((v: any) => v !== undefined && v !== null && v !== '' && v !== 'None')
|
|
967
1041
|
: value;
|
|
968
1042
|
}
|
|
969
1043
|
} catch (error) {
|
|
970
|
-
console.warn(`Failed to evaluate
|
|
1044
|
+
console.warn(`Failed to evaluate getter for ${collName}:`, error);
|
|
971
1045
|
}
|
|
972
1046
|
}
|
|
973
1047
|
}
|
|
@@ -1499,7 +1573,7 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1499
1573
|
});
|
|
1500
1574
|
}
|
|
1501
1575
|
// Collect values to assign to instance
|
|
1502
|
-
const values = [...Properties, ...Collections, ["
|
|
1576
|
+
const values = [...Properties, ...Collections, ["createdAt", Timestamp], ["author", Author]];
|
|
1503
1577
|
await Ad4mModel.assignValuesToInstance(perspective, instance, values);
|
|
1504
1578
|
|
|
1505
1579
|
return instance;
|
|
@@ -1583,10 +1657,10 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1583
1657
|
}
|
|
1584
1658
|
}
|
|
1585
1659
|
|
|
1586
|
-
// Find matching property (skip those with
|
|
1660
|
+
// Find matching property (skip those with getter)
|
|
1587
1661
|
let foundProperty = false;
|
|
1588
1662
|
for (const [propName, propMeta] of Object.entries(metadata.properties)) {
|
|
1589
|
-
if (propMeta.
|
|
1663
|
+
if (propMeta.getter) continue; // Handle via getter evaluation
|
|
1590
1664
|
if (propMeta.predicate === predicate) {
|
|
1591
1665
|
// For properties, take the first value (or we could use timestamp to get latest)
|
|
1592
1666
|
// Note: Empty objects {} are truthy, so we need to check for them explicitly
|
|
@@ -1653,10 +1727,10 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1653
1727
|
}
|
|
1654
1728
|
}
|
|
1655
1729
|
|
|
1656
|
-
// If not a property, check if it's a collection (skip those with
|
|
1730
|
+
// If not a property, check if it's a collection (skip those with getter)
|
|
1657
1731
|
if (!foundProperty) {
|
|
1658
1732
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
1659
|
-
if (collMeta.
|
|
1733
|
+
if (collMeta.getter) continue; // Handle via getter evaluation
|
|
1660
1734
|
if (collMeta.predicate === predicate) {
|
|
1661
1735
|
// For collections, accumulate all values with their timestamps and indices for sorting
|
|
1662
1736
|
if (!instance[collName]) {
|
|
@@ -1735,7 +1809,7 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1735
1809
|
// Replace collection with sorted values, filtering out empty strings and None
|
|
1736
1810
|
instance[collName] = pairs
|
|
1737
1811
|
.map(p => p.value)
|
|
1738
|
-
.filter((v: any) => v && v !== '' && v !== 'None');
|
|
1812
|
+
.filter((v: any) => v !== undefined && v !== null && v !== '' && v !== 'None');
|
|
1739
1813
|
// Clean up temporary arrays
|
|
1740
1814
|
delete instance[timestampsKey];
|
|
1741
1815
|
delete instance[indicesKey];
|
|
@@ -1746,8 +1820,9 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1746
1820
|
if (requestedProperties.length > 0 || requestedCollections.length > 0) {
|
|
1747
1821
|
const requestedAttributes = [...requestedProperties, ...requestedCollections];
|
|
1748
1822
|
Object.keys(instance).forEach((key) => {
|
|
1749
|
-
// Keep only requested attributes, plus always keep
|
|
1750
|
-
|
|
1823
|
+
// Keep only requested attributes, plus always keep createdAt, updatedAt, author, and baseExpression
|
|
1824
|
+
// Note: timestamp is a getter alias for createdAt, so we preserve createdAt instead
|
|
1825
|
+
if (!requestedAttributes.includes(key) && key !== 'createdAt' && key !== 'updatedAt' && key !== 'author' && key !== 'baseExpression') {
|
|
1751
1826
|
delete instance[key];
|
|
1752
1827
|
}
|
|
1753
1828
|
});
|
|
@@ -1759,13 +1834,14 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1759
1834
|
}
|
|
1760
1835
|
}
|
|
1761
1836
|
|
|
1762
|
-
// Evaluate
|
|
1837
|
+
// Evaluate custom getters for all instances (single pass)
|
|
1838
|
+
// This populates collection values needed for where.isInstance filtering
|
|
1763
1839
|
for (const instance of instances) {
|
|
1764
|
-
await this.
|
|
1840
|
+
await this.evaluateCustomGettersForInstance(instance, perspective, metadata);
|
|
1765
1841
|
}
|
|
1766
1842
|
|
|
1767
1843
|
// Filter collections by where.isInstance if specified
|
|
1768
|
-
// Do this
|
|
1844
|
+
// Do this after initial evaluation so collection values exist for filtering
|
|
1769
1845
|
for (const instance of instances) {
|
|
1770
1846
|
for (const [collName, collMeta] of Object.entries(metadata.collections)) {
|
|
1771
1847
|
if (collMeta.where?.isInstance && instance[collName]?.length > 0) {
|
|
@@ -1786,6 +1862,7 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1786
1862
|
// Check which subjects are instances of the target class
|
|
1787
1863
|
const validSubjects = await perspective.batchCheckSubjectInstances(subjects, classMetadata);
|
|
1788
1864
|
|
|
1865
|
+
// Update the collection with filtered instances
|
|
1789
1866
|
instance[collName] = validSubjects;
|
|
1790
1867
|
} catch (error) {
|
|
1791
1868
|
// On error, leave the collection unfiltered rather than breaking everything
|
|
@@ -1794,11 +1871,6 @@ WHERE ${whereConditions.join(' AND ')}
|
|
|
1794
1871
|
}
|
|
1795
1872
|
}
|
|
1796
1873
|
|
|
1797
|
-
// Evaluate SurrealQL getters for all instances
|
|
1798
|
-
for (const instance of instances) {
|
|
1799
|
-
await this.evaluateSurrealGettersForInstance(instance, perspective, metadata);
|
|
1800
|
-
}
|
|
1801
|
-
|
|
1802
1874
|
// Filter by where conditions that couldn't be filtered in SQL
|
|
1803
1875
|
// This includes:
|
|
1804
1876
|
// - author/timestamp (computed from grouped links)
|
package/src/model/decorators.ts
CHANGED
|
@@ -29,9 +29,9 @@ export interface InstanceQueryParams {
|
|
|
29
29
|
where?: object;
|
|
30
30
|
|
|
31
31
|
/**
|
|
32
|
-
* A string representing the condition clause of the query.
|
|
32
|
+
* A string representing the Prolog condition clause of the query.
|
|
33
33
|
*/
|
|
34
|
-
|
|
34
|
+
prologCondition?: string;
|
|
35
35
|
}
|
|
36
36
|
|
|
37
37
|
/**
|
|
@@ -65,14 +65,14 @@ condition?: string;
|
|
|
65
65
|
* static async findByName(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
66
66
|
*
|
|
67
67
|
* // Get highly rated recipes using a custom condition
|
|
68
|
-
* @InstanceQuery({
|
|
68
|
+
* @InstanceQuery({ prologCondition: "triple(Instance, 'recipe://rating', Rating), Rating > 4" })
|
|
69
69
|
* static async topRated(perspective: PerspectiveProxy): Promise<Recipe[]> { return [] }
|
|
70
70
|
* }
|
|
71
71
|
* ```
|
|
72
72
|
*
|
|
73
73
|
* @param {Object} [options] - Query options
|
|
74
74
|
* @param {object} [options.where] - Object with property-value pairs to match
|
|
75
|
-
* @param {string} [options.
|
|
75
|
+
* @param {string} [options.prologCondition] - Custom Prolog condition for more complex queries
|
|
76
76
|
*/
|
|
77
77
|
export function InstanceQuery(options?: InstanceQueryParams) {
|
|
78
78
|
return function <T>(target: T, key: keyof T, descriptor: PropertyDescriptor) {
|
|
@@ -93,8 +93,8 @@ export function InstanceQuery(options?: InstanceQueryParams) {
|
|
|
93
93
|
}
|
|
94
94
|
}
|
|
95
95
|
|
|
96
|
-
if(options && options.
|
|
97
|
-
query += ', ' + options.
|
|
96
|
+
if(options && options.prologCondition) {
|
|
97
|
+
query += ', ' + options.prologCondition
|
|
98
98
|
}
|
|
99
99
|
|
|
100
100
|
// Try Prolog first
|
|
@@ -171,21 +171,21 @@ export interface PropertyOptions {
|
|
|
171
171
|
resolveLanguage?: string;
|
|
172
172
|
|
|
173
173
|
/**
|
|
174
|
-
* Custom getter to get the value of the property
|
|
174
|
+
* Custom Prolog getter to get the value of the property. If not provided, the default getter will be used.
|
|
175
175
|
*/
|
|
176
|
-
|
|
176
|
+
prologGetter?: string;
|
|
177
177
|
|
|
178
178
|
/**
|
|
179
|
-
* Custom setter to set the value of the property
|
|
179
|
+
* Custom Prolog setter to set the value of the property. Only available if the property is writable.
|
|
180
180
|
*/
|
|
181
|
-
|
|
181
|
+
prologSetter?: string;
|
|
182
182
|
|
|
183
183
|
/**
|
|
184
184
|
* Custom SurrealQL getter to resolve the property value. Use this for custom graph traversals.
|
|
185
185
|
* The expression can reference 'Base' which will be replaced with the instance's base expression.
|
|
186
186
|
* Example: "(<-link[WHERE predicate = 'flux://has_reply'].in.uri)[0]"
|
|
187
187
|
*/
|
|
188
|
-
|
|
188
|
+
getter?: string;
|
|
189
189
|
|
|
190
190
|
/**
|
|
191
191
|
* 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.
|
|
@@ -276,8 +276,8 @@ export interface PropertyOptions {
|
|
|
276
276
|
* @param {boolean} [opts.required] - Whether the property must have a value
|
|
277
277
|
* @param {boolean} [opts.writable=true] - Whether the property can be modified
|
|
278
278
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
279
|
-
* @param {string} [opts.
|
|
280
|
-
* @param {string} [opts.
|
|
279
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
280
|
+
* @param {string} [opts.prologSetter] - Custom Prolog code for setting the property value
|
|
281
281
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
282
282
|
*/
|
|
283
283
|
export function Optional(opts: PropertyOptions) {
|
|
@@ -290,8 +290,8 @@ export function Optional(opts: PropertyOptions) {
|
|
|
290
290
|
throw new Error("SubjectProperty requires an 'initial' option if 'required' is true");
|
|
291
291
|
}
|
|
292
292
|
|
|
293
|
-
if (!opts.through && !opts.
|
|
294
|
-
throw new Error("SubjectProperty requires either 'through' or '
|
|
293
|
+
if (!opts.through && !opts.prologGetter) {
|
|
294
|
+
throw new Error("SubjectProperty requires either 'through' or 'prologGetter' option")
|
|
295
295
|
}
|
|
296
296
|
|
|
297
297
|
target["__properties"] = target["__properties"] || {};
|
|
@@ -405,6 +405,7 @@ export function Flag(opts: FlagOptions) {
|
|
|
405
405
|
|
|
406
406
|
interface WhereOptions {
|
|
407
407
|
isInstance?: any
|
|
408
|
+
prologCondition?: string
|
|
408
409
|
condition?: string
|
|
409
410
|
}
|
|
410
411
|
|
|
@@ -424,7 +425,7 @@ export interface CollectionOptions {
|
|
|
424
425
|
* The expression can reference 'Base' which will be replaced with the instance's base expression.
|
|
425
426
|
* Example: "(<-link[WHERE predicate = 'flux://has_reply'].in.uri)"
|
|
426
427
|
*/
|
|
427
|
-
|
|
428
|
+
getter?: string;
|
|
428
429
|
|
|
429
430
|
/**
|
|
430
431
|
* 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.
|
|
@@ -468,13 +469,20 @@ export interface CollectionOptions {
|
|
|
468
469
|
* })
|
|
469
470
|
* comments: string[] = [];
|
|
470
471
|
*
|
|
471
|
-
* // Collection with custom filter condition
|
|
472
|
+
* // Collection with custom Prolog filter condition
|
|
472
473
|
* @Collection({
|
|
473
474
|
* through: "recipe://step",
|
|
474
|
-
* where: {
|
|
475
|
+
* where: { prologCondition: `triple(Target, "step://order", Order), Order < 3` }
|
|
475
476
|
* })
|
|
476
477
|
* firstSteps: string[] = [];
|
|
477
478
|
*
|
|
479
|
+
* // Collection with custom SurrealDB filter condition
|
|
480
|
+
* @Collection({
|
|
481
|
+
* through: "recipe://entries",
|
|
482
|
+
* where: { condition: `WHERE in.uri = Target AND predicate = 'recipe://has_ingredient' AND out.uri = 'recipe://test')`
|
|
483
|
+
* })
|
|
484
|
+
* ingredients: string[] = [];
|
|
485
|
+
*
|
|
478
486
|
* // Local-only collection not shared with network
|
|
479
487
|
* @Collection({
|
|
480
488
|
* through: "recipe://note",
|
|
@@ -494,7 +502,7 @@ export interface CollectionOptions {
|
|
|
494
502
|
* @param {string} opts.through - The predicate URI for collection links
|
|
495
503
|
* @param {WhereOptions} [opts.where] - Filter conditions for collection values
|
|
496
504
|
* @param {any} [opts.where.isInstance] - Model class to filter instances by
|
|
497
|
-
* @param {string} [opts.where.
|
|
505
|
+
* @param {string} [opts.where.prologCondition] - Custom Prolog condition for filtering
|
|
498
506
|
* @param {boolean} [opts.local] - Whether collection links are stored locally only
|
|
499
507
|
*/
|
|
500
508
|
export function Collection(opts: CollectionOptions) {
|
|
@@ -614,15 +622,15 @@ export function ModelOptions(opts: ModelOptionsOptions) {
|
|
|
614
622
|
for(let property in properties) {
|
|
615
623
|
let propertyCode = `property(${uuid}, "${property}").\n`
|
|
616
624
|
|
|
617
|
-
let { through, initial, required, resolveLanguage, writable, flag,
|
|
625
|
+
let { through, initial, required, resolveLanguage, writable, flag, prologGetter, prologSetter, local } = properties[property]
|
|
618
626
|
|
|
619
627
|
if(resolveLanguage) {
|
|
620
628
|
propertyCode += `property_resolve(${uuid}, "${property}").\n`
|
|
621
629
|
propertyCode += `property_resolve_language(${uuid}, "${property}", "${resolveLanguage}").\n`
|
|
622
630
|
}
|
|
623
631
|
|
|
624
|
-
if(
|
|
625
|
-
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${
|
|
632
|
+
if(prologGetter) {
|
|
633
|
+
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- ${prologGetter}.\n`
|
|
626
634
|
} else if(through) {
|
|
627
635
|
propertyCode += `property_getter(${uuid}, Base, "${property}", Value) :- triple(Base, "${through}", Value).\n`
|
|
628
636
|
|
|
@@ -635,8 +643,8 @@ export function ModelOptions(opts: ModelOptionsOptions) {
|
|
|
635
643
|
}
|
|
636
644
|
}
|
|
637
645
|
|
|
638
|
-
if(
|
|
639
|
-
propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${
|
|
646
|
+
if(prologSetter) {
|
|
647
|
+
propertyCode += `property_setter(${uuid}, "${property}", Actions) :- ${prologSetter}.\n`
|
|
640
648
|
} else if (writable && through) {
|
|
641
649
|
let setter = obj[propertyNameToSetterName(property)]
|
|
642
650
|
if(typeof setter === "function") {
|
|
@@ -679,8 +687,8 @@ export function ModelOptions(opts: ModelOptionsOptions) {
|
|
|
679
687
|
|
|
680
688
|
if(through) {
|
|
681
689
|
if(where) {
|
|
682
|
-
if(!where.isInstance && !where.condition) {
|
|
683
|
-
throw "'where' needs one of 'isInstance' or 'condition'"
|
|
690
|
+
if(!where.isInstance && !where.prologCondition && !where.condition) {
|
|
691
|
+
throw "'where' needs one of 'isInstance', 'prologCondition', or 'condition'"
|
|
684
692
|
}
|
|
685
693
|
|
|
686
694
|
let conditions = []
|
|
@@ -695,13 +703,19 @@ export function ModelOptions(opts: ModelOptionsOptions) {
|
|
|
695
703
|
conditions.push(`instance(OtherClass, Target), subject_class("${otherClass}", OtherClass)`)
|
|
696
704
|
}
|
|
697
705
|
|
|
698
|
-
if(where.
|
|
699
|
-
conditions.push(where.
|
|
706
|
+
if(where.prologCondition) {
|
|
707
|
+
conditions.push(where.prologCondition)
|
|
700
708
|
}
|
|
701
709
|
|
|
702
|
-
|
|
703
|
-
|
|
704
|
-
|
|
710
|
+
// If there are Prolog conditions (isInstance or prologCondition), use setof with conditions
|
|
711
|
+
// If only condition is present, use simple findall (SurrealDB will filter later)
|
|
712
|
+
if(conditions.length > 0) {
|
|
713
|
+
const conditionString = conditions.join(", ")
|
|
714
|
+
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- setof(Target, (triple(Base, "${through}", Target), ${conditionString}), List).\n`
|
|
715
|
+
} else {
|
|
716
|
+
// Only SurrealDB condition present (no Prolog filtering)
|
|
717
|
+
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`
|
|
718
|
+
}
|
|
705
719
|
} else {
|
|
706
720
|
collectionCode += `collection_getter(${uuid}, Base, "${collection}", List) :- findall(C, triple(Base, "${through}", C), List).\n`
|
|
707
721
|
}
|
|
@@ -810,8 +824,8 @@ export function ModelOptions(opts: ModelOptionsOptions) {
|
|
|
810
824
|
* @param {string} opts.through - The predicate URI for the property
|
|
811
825
|
* @param {string} [opts.initial] - Initial value (defaults to "literal://string:uninitialized")
|
|
812
826
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
813
|
-
* @param {string} [opts.
|
|
814
|
-
* @param {string} [opts.
|
|
827
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
828
|
+
* @param {string} [opts.prologSetter] - Custom Prolog code for setting the property value
|
|
815
829
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
816
830
|
*/
|
|
817
831
|
export function Property(opts: PropertyOptions) {
|
|
@@ -875,7 +889,7 @@ export function Property(opts: PropertyOptions) {
|
|
|
875
889
|
* @param {string} opts.through - The predicate URI for the property
|
|
876
890
|
* @param {string} [opts.initial] - Initial value (if property should have one)
|
|
877
891
|
* @param {string} [opts.resolveLanguage] - Language to use for value resolution (e.g. "literal")
|
|
878
|
-
* @param {string} [opts.
|
|
892
|
+
* @param {string} [opts.prologGetter] - Custom Prolog code for getting the property value
|
|
879
893
|
* @param {boolean} [opts.local] - Whether the property should only be stored locally
|
|
880
894
|
*/
|
|
881
895
|
export function ReadOnly(opts: PropertyOptions) {
|