@coasys/ad4m 0.11.2-dev-sdna-only-prolog → 0.11.2-dev.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,6 +3,7 @@ import { Link } from "../links/Links";
3
3
  import { PerspectiveProxy } from "../perspectives/PerspectiveProxy";
4
4
  import { makeRandomPrologAtom, PropertyOptions, CollectionOptions, ModelOptions } from "./decorators";
5
5
  import { singularToPlural, pluralToSingular, propertyNameToSetterName, collectionToAdderName, collectionToRemoverName, collectionToSetterName } from "./util";
6
+ import { escapeSurrealString } from "../utils";
6
7
 
7
8
  // JSON Schema type definitions
8
9
  interface JSONSchemaProperty {
@@ -638,6 +639,11 @@ export class Ad4mModel {
638
639
  * @private
639
640
  */
640
641
  private generatePropertySetterAction(key: string, metadata: PropertyOptions): any[] {
642
+ // Check if property is read-only
643
+ if (metadata.writable === false) {
644
+ throw new Error(`Property "${key}" is read-only and cannot be written`);
645
+ }
646
+
641
647
  if (metadata.setter) {
642
648
  // Custom setter - throw error for now (Phase 2)
643
649
  throw new Error(
@@ -743,36 +749,21 @@ export class Ad4mModel {
743
749
 
744
750
  private async getData() {
745
751
  // Builds an object with the author, timestamp, all properties, & all collections on the Ad4mModel and saves it to the instance
746
- const subQueries = [buildAuthorAndTimestampQuery(), buildPropertiesQuery(), buildCollectionsQuery()];
747
- const fullQuery = `
748
- Base = "${this.#baseExpression}",
749
- subject_class("${this.#subjectClassName}", SubjectClass),
750
- ${subQueries.join(", ")}
751
- `;
752
-
753
- // Try Prolog first
754
- try {
755
- const result = await this.#perspective.infer(fullQuery);
756
- if (result?.[0] && result[0].Properties && result[0].Properties.length > 0) {
757
- const { Properties, Collections, Timestamp, Author } = result[0];
758
- const values = [...Properties, ...Collections, ["timestamp", Timestamp], ["author", Author]];
759
- await Ad4mModel.assignValuesToInstance(this.#perspective, this, values);
760
- return this;
761
- }
762
- } catch (e) {
763
- console.log(`Prolog getData failed for ${this.#baseExpression}, falling back to SurrealDB`);
764
- }
765
-
766
- // Fallback to SurrealDB (SdnaOnly mode)
752
+ // Use SurrealDB for data queries
767
753
  try {
768
754
  const ctor = this.constructor as typeof Ad4mModel;
769
755
  const metadata = ctor.getModelMetadata();
770
756
 
771
757
  // Query for all links from this specific node (base expression)
758
+ // Using formatSurrealValue to prevent SQL injection by properly escaping the value
759
+ const safeBaseExpression = ctor.formatSurrealValue(this.#baseExpression);
760
+ // Note: We use ORDER BY timestamp ASC because:
761
+ // - For collections: we want chronological order (oldest to newest)
762
+ // - For properties: we select the LAST element to get "latest wins" semantics
772
763
  const linksQuery = `
773
764
  SELECT id, predicate, out.uri AS target, author, timestamp
774
765
  FROM link
775
- WHERE in.uri = '${this.#baseExpression}'
766
+ WHERE in.uri = ${safeBaseExpression}
776
767
  ORDER BY timestamp ASC
777
768
  `;
778
769
  const links = await this.#perspective.querySurrealDB(linksQuery);
@@ -785,7 +776,9 @@ export class Ad4mModel {
785
776
  for (const [propName, propMeta] of Object.entries(metadata.properties)) {
786
777
  const matching = links.filter((l: any) => l.predicate === propMeta.predicate);
787
778
  if (matching.length > 0) {
788
- const link = matching[0]; // Take first/latest
779
+ // "Latest wins" semantics: select the last element since links are ordered ASC.
780
+ // The last element has the most recent timestamp and represents the current property value.
781
+ const link = matching[matching.length - 1];
789
782
  let value = link.target;
790
783
 
791
784
  // Track timestamp/author
@@ -830,7 +823,8 @@ export class Ad4mModel {
830
823
  // Process collections
831
824
  for (const [collName, collMeta] of Object.entries(metadata.collections)) {
832
825
  const matching = links.filter((l: any) => l.predicate === collMeta.predicate);
833
- // Links are already sorted by timestamp ASC from the query, so map preserves order
826
+ // Collections preserve chronological order: links are sorted ASC by timestamp,
827
+ // so the collection reflects the order in which items were added (oldest to newest).
834
828
  (this as any)[collName] = matching.map((l: any) => l.target);
835
829
  }
836
830
 
@@ -934,11 +928,11 @@ export class Ad4mModel {
934
928
  // For flag properties, also filter by the target value
935
929
  if (propMeta.flag && propMeta.initial) {
936
930
  graphTraversalFilters.push(
937
- `count(->link[WHERE perspective = $perspective AND predicate = '${propMeta.predicate}' AND out.uri = '${propMeta.initial}']) > 0`
931
+ `count(->link[WHERE perspective = $perspective AND predicate = '${escapeSurrealString(propMeta.predicate)}' AND out.uri = '${escapeSurrealString(propMeta.initial)}']) > 0`
938
932
  );
939
933
  } else {
940
934
  graphTraversalFilters.push(
941
- `count(->link[WHERE perspective = $perspective AND predicate = '${propMeta.predicate}']) > 0`
935
+ `count(->link[WHERE perspective = $perspective AND predicate = '${escapeSurrealString(propMeta.predicate)}']) > 0`
942
936
  );
943
937
  }
944
938
  }
@@ -952,11 +946,11 @@ export class Ad4mModel {
952
946
  // For flag properties, also filter by the target value
953
947
  if (propMeta.flag) {
954
948
  graphTraversalFilters.push(
955
- `count(->link[WHERE perspective = $perspective AND predicate = '${propMeta.predicate}' AND out.uri = '${propMeta.initial}']) > 0`
949
+ `count(->link[WHERE perspective = $perspective AND predicate = '${escapeSurrealString(propMeta.predicate)}' AND out.uri = '${escapeSurrealString(propMeta.initial)}']) > 0`
956
950
  );
957
951
  } else {
958
952
  graphTraversalFilters.push(
959
- `count(->link[WHERE perspective = $perspective AND predicate = '${propMeta.predicate}']) > 0`
953
+ `count(->link[WHERE perspective = $perspective AND predicate = '${escapeSurrealString(propMeta.predicate)}']) > 0`
960
954
  );
961
955
  }
962
956
  break; // Just need one defining property
@@ -1077,7 +1071,7 @@ WHERE ${whereConditions.join(' AND ')}
1077
1071
  const propMeta = metadata.properties[propertyName];
1078
1072
  if (!propMeta) continue; // Skip if property not found in metadata
1079
1073
 
1080
- const predicate = propMeta.predicate;
1074
+ const predicate = escapeSurrealString(propMeta.predicate);
1081
1075
  // Use fn::parse_literal() for properties with resolveLanguage
1082
1076
  const targetField = propMeta.resolveLanguage === 'literal' ? 'fn::parse_literal(out.uri)' : 'out.uri';
1083
1077
 
@@ -1209,7 +1203,7 @@ WHERE ${whereConditions.join(' AND ')}
1209
1203
  const propMeta = metadata.properties[propertyName];
1210
1204
  if (!propMeta) continue; // Skip if property not found in metadata
1211
1205
 
1212
- const predicate = propMeta.predicate;
1206
+ const predicate = escapeSurrealString(propMeta.predicate);
1213
1207
  // Use fn::parse_literal() for properties with resolveLanguage
1214
1208
  const targetField = propMeta.resolveLanguage === 'literal' ? 'fn::parse_literal(target)' : 'target';
1215
1209
 
@@ -1285,7 +1279,8 @@ WHERE ${whereConditions.join(' AND ')}
1285
1279
  if (!propMeta) continue; // Skip if not found
1286
1280
 
1287
1281
  // Reference source directly since we're selecting from link table
1288
- fields.push(`(SELECT VALUE target FROM link WHERE source = source AND predicate = '${propMeta.predicate}' LIMIT 1) AS ${propName}`);
1282
+ const escapedPredicate = escapeSurrealString(propMeta.predicate);
1283
+ fields.push(`(SELECT VALUE target FROM link WHERE source = source AND predicate = '${escapedPredicate}' LIMIT 1) AS ${propName}`);
1289
1284
  }
1290
1285
 
1291
1286
  // Determine collections to fetch
@@ -1295,7 +1290,8 @@ WHERE ${whereConditions.join(' AND ')}
1295
1290
  if (!collMeta) continue; // Skip if not found
1296
1291
 
1297
1292
  // Reference source directly since we're selecting from link table
1298
- fields.push(`(SELECT VALUE target FROM link WHERE source = source AND predicate = '${collMeta.predicate}') AS ${collName}`);
1293
+ const escapedPredicate = escapeSurrealString(collMeta.predicate);
1294
+ fields.push(`(SELECT VALUE target FROM link WHERE source = source AND predicate = '${escapedPredicate}') AS ${collName}`);
1299
1295
  }
1300
1296
 
1301
1297
  // Always add author and timestamp fields
@@ -1321,7 +1317,8 @@ WHERE ${whereConditions.join(' AND ')}
1321
1317
  if (!propMeta) continue; // Skip if not found
1322
1318
 
1323
1319
  // Use array::first to get the first target value for this predicate
1324
- fields.push(`array::first(target[WHERE predicate = '${propMeta.predicate}']) AS ${propName}`);
1320
+ const escapedPredicate = escapeSurrealString(propMeta.predicate);
1321
+ fields.push(`array::first(target[WHERE predicate = '${escapedPredicate}']) AS ${propName}`);
1325
1322
  }
1326
1323
 
1327
1324
  // Determine collections to fetch
@@ -1331,7 +1328,8 @@ WHERE ${whereConditions.join(' AND ')}
1331
1328
  if (!collMeta) continue; // Skip if not found
1332
1329
 
1333
1330
  // Use array filtering to get all target values for this predicate
1334
- fields.push(`target[WHERE predicate = '${collMeta.predicate}'] AS ${collName}`);
1331
+ const escapedPredicate = escapeSurrealString(collMeta.predicate);
1332
+ fields.push(`target[WHERE predicate = '${escapedPredicate}'] AS ${collName}`);
1335
1333
  }
1336
1334
 
1337
1335
  // Always add author and timestamp fields using array::first
@@ -2013,7 +2011,7 @@ WHERE ${whereConditions.join(' AND ')}
2013
2011
  // Generate actions from metadata (replaces Prolog query)
2014
2012
  const actions = this.generateCollectionAction(key, 'setter');
2015
2013
 
2016
- if (value) {
2014
+ if (value != null) {
2017
2015
  if (Array.isArray(value)) {
2018
2016
  await this.#perspective.executeAction(
2019
2017
  actions,
@@ -2038,7 +2036,7 @@ WHERE ${whereConditions.join(' AND ')}
2038
2036
  // Generate actions from metadata (replaces Prolog query)
2039
2037
  const actions = this.generateCollectionAction(key, 'adder');
2040
2038
 
2041
- if (value) {
2039
+ if (value != null) {
2042
2040
  if (Array.isArray(value)) {
2043
2041
  await Promise.all(
2044
2042
  value.map((v) =>
@@ -2062,7 +2060,7 @@ WHERE ${whereConditions.join(' AND ')}
2062
2060
  // Generate actions from metadata (replaces Prolog query)
2063
2061
  const actions = this.generateCollectionAction(key, 'remover');
2064
2062
 
2065
- if (value) {
2063
+ if (value != null) {
2066
2064
  if (Array.isArray(value)) {
2067
2065
  await Promise.all(
2068
2066
  value.map((v) =>
@@ -2174,11 +2172,8 @@ WHERE ${whereConditions.join(' AND ')}
2174
2172
  break;
2175
2173
  }
2176
2174
  } else if (Array.isArray(value)) {
2177
- // Handle all arrays as collections, even empty ones
2178
- if (value.length > 0) {
2179
- await this.setCollectionSetter(key, value, batchId);
2180
- }
2181
- // Skip empty arrays - don't try to set them as properties
2175
+ // Handle all arrays as collections, including empty ones (which clears the collection)
2176
+ await this.setCollectionSetter(key, value, batchId);
2182
2177
  } else if (value !== undefined && value !== null && value !== "") {
2183
2178
  if (setProperties) {
2184
2179
  // Check if this is a collection property (has collection metadata)
@@ -50,35 +50,7 @@ export class Subject {
50
50
  Object.defineProperty(this, p, {
51
51
  configurable: true,
52
52
  get: async () => {
53
- // Try Prolog first
54
- try {
55
- let results = await this.#perspective.infer(`subject_class("${this.#subjectClassName}", C), property_getter(C, "${this.#baseExpression}", "${p}", Value)`)
56
- if(results && results.length > 0 && results[0].Value !== undefined && results[0].Value !== null && results[0].Value !== '') {
57
- let expressionURI = results[0].Value
58
- if(resolveExpressionURI) {
59
- try {
60
- if (expressionURI) {
61
- const expression = await this.#perspective.getExpression(expressionURI)
62
- try {
63
- return JSON.parse(expression.data)
64
- } catch(e) {
65
- return expression.data
66
- }
67
- } else {
68
- return expressionURI
69
- }
70
- } catch (err) {
71
- return expressionURI
72
- }
73
- } else {
74
- return expressionURI
75
- }
76
- }
77
- } catch (err) {
78
- // Prolog query failed, fall through to SurrealDB
79
- }
80
-
81
- // Fallback to SurrealDB if Prolog returned nothing (SdnaOnly mode)
53
+ // Use SurrealDB for data queries
82
54
  try {
83
55
  return await this.#perspective.getPropertyValueViaSurreal(this.#baseExpression, this.#subjectClassName, p);
84
56
  } catch (err) {
@@ -119,20 +91,7 @@ export class Subject {
119
91
  Object.defineProperty(this, c, {
120
92
  configurable: true,
121
93
  get: async () => {
122
- // Try Prolog first
123
- try {
124
- let results = await this.#perspective.infer(`subject_class("${this.#subjectClassName}", C), collection_getter(C, "${this.#baseExpression}", "${c}", Value)`)
125
- if(results && results.length > 0 && results[0].Value && Array.isArray(results[0].Value) && results[0].Value.length > 0) {
126
- let collectionContent = results[0].Value.filter((v: any) => v !== "" && v !== '')
127
- if (collectionContent.length > 0) {
128
- return collectionContent
129
- }
130
- }
131
- } catch (err) {
132
- // Prolog query failed, fall through to SurrealDB
133
- }
134
-
135
- // Fallback to SurrealDB if Prolog returned nothing (SdnaOnly mode)
94
+ // Use SurrealDB for data queries
136
95
  try {
137
96
  return await this.#perspective.getCollectionValuesViaSurreal(this.#baseExpression, this.#subjectClassName, c);
138
97
  } catch (err) {
@@ -13,6 +13,7 @@ import { AIClient } from "../ai/AIClient";
13
13
  import { PERSPECTIVE_QUERY_SUBSCRIPTION } from "./PerspectiveResolver";
14
14
  import { gql } from "@apollo/client/core";
15
15
  import { AllInstancesResult } from "../model/Ad4mModel";
16
+ import { escapeSurrealString } from "../utils";
16
17
 
17
18
  type QueryCallback = (result: AllInstancesResult) => void;
18
19
 
@@ -412,6 +413,19 @@ export class PerspectiveProxy {
412
413
  this.#client.addPerspectiveSyncStateChangeListener(this.#handle.uuid, this.#perspectiveSyncStateChangeCallbacks)
413
414
  }
414
415
 
416
+ /**
417
+ * Escapes special regex characters in a string to prevent ReDoS attacks
418
+ * and regex injection when building dynamic regular expressions.
419
+ *
420
+ * @param str - The string to escape
421
+ * @returns The escaped string safe for use in RegExp constructor
422
+ *
423
+ * @private
424
+ */
425
+ private escapeRegExp(str: string): string {
426
+ return str.replace(/[.*+?^${}()|[\]\\]/g, '\\$&');
427
+ }
428
+
415
429
  /**
416
430
  * Executes a set of actions on an expression with optional parameters.
417
431
  * Used internally by Social DNA flows and subject class operations.
@@ -1084,16 +1098,24 @@ export class PerspectiveProxy {
1084
1098
  // Get metadata from SDNA using Prolog metaprogramming
1085
1099
  const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1086
1100
  if (!metadata) {
1087
- return false;
1101
+ // Fallback to Prolog check if SDNA metadata isn't available
1102
+ // This handles cases where classes exist in Prolog but not in SDNA
1103
+ try {
1104
+ const escapedClassName = className.replace(/"/g, '\\"');
1105
+ const escapedExpression = expression.replace(/"/g, '\\"');
1106
+ const result = await this.infer(`subject_class("${escapedClassName}", C), instance(C, "${escapedExpression}")`);
1107
+ return result && result.length > 0;
1108
+ } catch (e) {
1109
+ console.warn(`Failed to check instance via Prolog for class ${className}:`, e);
1110
+ return false;
1111
+ }
1088
1112
  }
1089
1113
 
1090
1114
  // If no required triples, any expression with links is an instance
1091
1115
  if (metadata.requiredTriples.length === 0) {
1092
- console.log("no required triples, checking surrealDB");
1093
- const checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${expression}'`;
1094
- console.log("checkQuery", checkQuery);
1116
+ const escapedExpression = escapeSurrealString(expression);
1117
+ const checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${escapedExpression}'`;
1095
1118
  const result = await this.querySurrealDB(checkQuery);
1096
- console.log("result", result);
1097
1119
  const count = result[0]?.count ?? 0;
1098
1120
  const countValue = typeof count === 'object' && count?.Int !== undefined ? count.Int : count;
1099
1121
  return countValue > 0;
@@ -1101,18 +1123,18 @@ export class PerspectiveProxy {
1101
1123
 
1102
1124
  // Check if the expression has all required triples (predicate + optional exact target)
1103
1125
  for (const triple of metadata.requiredTriples) {
1104
- console.log("checking triple", triple);
1126
+ const escapedExpression = escapeSurrealString(expression);
1127
+ const escapedPredicate = escapeSurrealString(triple.predicate);
1105
1128
  let checkQuery: string;
1106
1129
  if (triple.target) {
1107
1130
  // Flag: must match both predicate AND exact target value
1108
- checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${expression}' AND predicate = '${triple.predicate}' AND out.uri = '${triple.target}'`;
1131
+ const escapedTarget = escapeSurrealString(triple.target);
1132
+ checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${escapedExpression}' AND predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}'`;
1109
1133
  } else {
1110
1134
  // Property: just check predicate exists
1111
- checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${expression}' AND predicate = '${triple.predicate}'`;
1135
+ checkQuery = `SELECT count() AS count FROM link WHERE in.uri = '${escapedExpression}' AND predicate = '${escapedPredicate}'`;
1112
1136
  }
1113
- console.log("checkQuery", checkQuery);
1114
1137
  const result = await this.querySurrealDB(checkQuery);
1115
- console.log("result", result);
1116
1138
 
1117
1139
  if (!result || result.length === 0) {
1118
1140
  return false;
@@ -1164,8 +1186,9 @@ export class PerspectiveProxy {
1164
1186
  } | null> {
1165
1187
  try {
1166
1188
  // Get SDNA code from perspective - it's stored as a link
1189
+ // Use canonical Literal.from() to construct the source URL
1167
1190
  const sdnaLinks = await this.get(new LinkQuery({
1168
- source: `literal://string:${className}`,
1191
+ source: Literal.from(className).toUrl(),
1169
1192
  predicate: "ad4m://sdna"
1170
1193
  }));
1171
1194
 
@@ -1191,11 +1214,15 @@ export class PerspectiveProxy {
1191
1214
 
1192
1215
  // Parse the instance rule from the SDNA code
1193
1216
  // Format: instance(c, Base) :- triple(Base, "pred1", _), triple(Base, "pred2", "exact_value").
1194
- const instanceRuleMatch = sdnaCode.match(/instance\([^,]+,\s*\w+\)\s*:-\s*(.+?)\./s);
1195
-
1196
- if (instanceRuleMatch) {
1217
+ // Use a more robust pattern that handles complex rule bodies
1218
+ // Match from "instance(" to the closing "." using non-greedy matching
1219
+ const instanceRulePattern = /instance\([^)]+\)\s*:-\s*([^.]+)\./g;
1220
+ let instanceRuleMatch;
1221
+ let foundInstanceRule = false;
1222
+
1223
+ while ((instanceRuleMatch = instanceRulePattern.exec(sdnaCode)) !== null) {
1224
+ foundInstanceRule = true;
1197
1225
  const ruleBody = instanceRuleMatch[1];
1198
- console.log("instance rule body:", ruleBody);
1199
1226
 
1200
1227
  // Extract all triple(Base, "predicate", Target) patterns
1201
1228
  // Match both: triple(Base, "pred", _) and triple(Base, "pred", "value")
@@ -1206,15 +1233,15 @@ export class PerspectiveProxy {
1206
1233
  const predicate = match[1];
1207
1234
  const target = match[2]; // undefined if matched "_"
1208
1235
  requiredTriples.push({ predicate, target });
1209
- console.log(`Extracted triple: predicate="${predicate}", target="${target}"`);
1210
1236
  }
1211
- } else {
1237
+ }
1238
+
1239
+ if (!foundInstanceRule) {
1212
1240
  console.warn(`No instance rule found in SDNA for ${className}`);
1213
1241
  }
1214
1242
 
1215
1243
  // For backward compatibility, also maintain requiredPredicates array
1216
1244
  const requiredPredicates = requiredTriples.map(t => t.predicate);
1217
- console.log("extracted requiredTriples:", requiredTriples);
1218
1245
 
1219
1246
  // Extract property metadata
1220
1247
  const properties = new Map<string, { predicate: string, resolveLanguage?: string }>();
@@ -1239,7 +1266,9 @@ export class PerspectiveProxy {
1239
1266
  // If no setter, try to extract from SDNA property_getter Prolog code
1240
1267
  if (!predicate) {
1241
1268
  // Parse the SDNA code for property_getter definition
1242
- const getterMatch = sdnaCode.match(new RegExp(`property_getter\\([^,]+,\\s*[^,]+,\\s*"${propName}"[^)]*\\)\\s*:-\\s*triple\\([^,]+,\\s*"([^"]+)"`));
1269
+ // Escape propName to prevent regex injection and ReDoS attacks
1270
+ const escapedPropName = this.escapeRegExp(propName);
1271
+ const getterMatch = sdnaCode.match(new RegExp(`property_getter\\([^,]+,\\s*[^,]+,\\s*"${escapedPropName}"[^)]*\\)\\s*:-\\s*triple\\([^,]+,\\s*"([^"]+)"`));
1243
1272
  if (getterMatch) {
1244
1273
  predicate = getterMatch[1];
1245
1274
  }
@@ -1280,7 +1309,9 @@ export class PerspectiveProxy {
1280
1309
  // Format 1 (findall): collection_getter(c, Base, "comments", List) :- findall(C, triple(Base, "todo://comment", C), List).
1281
1310
  // Format 2 (setof): collection_getter(c, Base, "messages", List) :- setof(Target, (triple(Base, "flux://entry_type", Target), ...), List).
1282
1311
  // Use a line-based match to avoid capturing multiple collections
1283
- const getterLinePattern = new RegExp(`collection_getter\\([^,]+,\\s*[^,]+,\\s*"${collName}"[^)]*\\)\\s*:-[^.]+\\.`);
1312
+ // Escape collName to prevent regex injection and ReDoS attacks
1313
+ const escapedCollName = this.escapeRegExp(collName);
1314
+ const getterLinePattern = new RegExp(`collection_getter\\([^,]+,\\s*[^,]+,\\s*"${escapedCollName}"[^)]*\\)\\s*:-[^.]+\\.`);
1284
1315
  const getterLineMatch = sdnaCode.match(getterLinePattern);
1285
1316
 
1286
1317
  if (getterLineMatch) {
@@ -1296,7 +1327,6 @@ export class PerspectiveProxy {
1296
1327
  if (getterBody.startsWith('(') && getterBody.endsWith(')')) {
1297
1328
  getterBody = getterBody.substring(1, getterBody.length - 1);
1298
1329
  }
1299
- console.log(`Collection "${collName}" getter body:`, getterBody);
1300
1330
 
1301
1331
  // Extract predicate from triple(Base, "predicate", Target)
1302
1332
  if (!predicate) {
@@ -1310,7 +1340,6 @@ export class PerspectiveProxy {
1310
1340
  const instanceMatch = getterBody.match(/subject_class\("([^"]+)"/);
1311
1341
  if (instanceMatch) {
1312
1342
  instanceFilter = instanceMatch[1];
1313
- console.log(`Collection "${collName}" has instanceFilter: ${instanceFilter}`);
1314
1343
  }
1315
1344
  }
1316
1345
  }
@@ -1337,29 +1366,25 @@ export class PerspectiveProxy {
1337
1366
  properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1338
1367
  collections: Map<string, { predicate: string }>
1339
1368
  }): string {
1340
- console.log("generateSurrealInstanceQuery called with requiredTriples:", metadata.requiredTriples);
1341
-
1342
1369
  if (metadata.requiredTriples.length === 0) {
1343
1370
  // No required triples - any node with links is an instance
1344
- const query = `SELECT DISTINCT uri AS base FROM node WHERE count(->link) > 0`;
1345
- console.log("Generated query (no required triples):", query);
1346
- return query;
1371
+ return `SELECT DISTINCT uri AS base FROM node WHERE count(->link) > 0`;
1347
1372
  }
1348
1373
 
1349
1374
  // Generate WHERE conditions for each required triple (predicate + optional exact target)
1350
1375
  const whereConditions = metadata.requiredTriples.map(triple => {
1376
+ const escapedPredicate = escapeSurrealString(triple.predicate);
1351
1377
  if (triple.target) {
1352
1378
  // Flag: must match both predicate AND exact target value
1353
- return `count(->link[WHERE predicate = '${triple.predicate}' AND out.uri = '${triple.target}']) > 0`;
1379
+ const escapedTarget = escapeSurrealString(triple.target);
1380
+ return `count(->link[WHERE predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}']) > 0`;
1354
1381
  } else {
1355
1382
  // Property: just check predicate exists
1356
- return `count(->link[WHERE predicate = '${triple.predicate}']) > 0`;
1383
+ return `count(->link[WHERE predicate = '${escapedPredicate}']) > 0`;
1357
1384
  }
1358
1385
  }).join(' AND ');
1359
1386
 
1360
- const query = `SELECT uri AS base FROM node WHERE ${whereConditions}`;
1361
- console.log("Generated query:", query);
1362
- return query;
1387
+ return `SELECT uri AS base FROM node WHERE ${whereConditions}`;
1363
1388
  }
1364
1389
 
1365
1390
  /**
@@ -1377,7 +1402,9 @@ export class PerspectiveProxy {
1377
1402
  return undefined;
1378
1403
  }
1379
1404
 
1380
- const query = `SELECT out.uri AS value FROM link WHERE in.uri = '${baseExpression}' AND predicate = '${propMeta.predicate}' LIMIT 1`;
1405
+ const escapedBaseExpression = escapeSurrealString(baseExpression);
1406
+ const escapedPredicate = escapeSurrealString(propMeta.predicate);
1407
+ const query = `SELECT out.uri AS value FROM link WHERE in.uri = '${escapedBaseExpression}' AND predicate = '${escapedPredicate}' LIMIT 1`;
1381
1408
  const result = await this.querySurrealDB(query);
1382
1409
 
1383
1410
  if (!result || result.length === 0) {
@@ -1418,7 +1445,9 @@ export class PerspectiveProxy {
1418
1445
  return [];
1419
1446
  }
1420
1447
 
1421
- const query = `SELECT out.uri AS value, timestamp FROM link WHERE in.uri = '${baseExpression}' AND predicate = '${collMeta.predicate}' ORDER BY timestamp ASC`;
1448
+ const escapedBaseExpression = escapeSurrealString(baseExpression);
1449
+ const escapedPredicate = escapeSurrealString(collMeta.predicate);
1450
+ const query = `SELECT out.uri AS value, timestamp FROM link WHERE in.uri = '${escapedBaseExpression}' AND predicate = '${escapedPredicate}' ORDER BY timestamp ASC`;
1422
1451
  const result = await this.querySurrealDB(query);
1423
1452
 
1424
1453
  if (!result || result.length === 0) {
@@ -1427,26 +1456,103 @@ export class PerspectiveProxy {
1427
1456
 
1428
1457
  let values = result.map(r => r.value).filter(v => v !== "" && v !== '');
1429
1458
 
1430
- console.log(`getCollectionValuesViaSurreal: base="${baseExpression}", collection="${collectionName}", predicate="${collMeta.predicate}", found ${values.length} values:`, values);
1431
-
1432
- // Apply instance filter if present
1459
+ // Apply instance filter if present - batch-check all values at once
1433
1460
  if (collMeta.instanceFilter) {
1434
- console.log(` Applying instance filter "${collMeta.instanceFilter}" to collection "${collectionName}"`);
1435
- const filteredValues = [];
1436
- for (const value of values) {
1437
- const isInstance = await this.isSubjectInstance(value, collMeta.instanceFilter);
1438
- console.log(` Checking if "${value}" is instance of "${collMeta.instanceFilter}": ${isInstance}`);
1439
- if (isInstance) {
1440
- filteredValues.push(value);
1461
+ try {
1462
+ const filterMetadata = await this.getSubjectClassMetadataFromSDNA(collMeta.instanceFilter);
1463
+ if (!filterMetadata) {
1464
+ // Fallback to sequential checks if metadata isn't available
1465
+ return this.filterInstancesSequential(values, collMeta.instanceFilter);
1441
1466
  }
1467
+
1468
+ return await this.batchCheckSubjectInstances(values, filterMetadata);
1469
+ } catch (err) {
1470
+ // Fallback to sequential checks on error
1471
+ return this.filterInstancesSequential(values, collMeta.instanceFilter);
1442
1472
  }
1443
- console.log(` After filtering: ${filteredValues.length} values`);
1444
- return filteredValues;
1445
1473
  }
1446
1474
 
1447
1475
  return values;
1448
1476
  }
1449
1477
 
1478
+ /**
1479
+ * Batch-checks multiple expressions against subject class metadata using a single or limited SurrealDB queries.
1480
+ * This avoids N+1 query problems by checking all values at once.
1481
+ */
1482
+ private async batchCheckSubjectInstances(
1483
+ expressions: string[],
1484
+ metadata: {
1485
+ requiredPredicates: string[],
1486
+ requiredTriples: Array<{predicate: string, target?: string}>,
1487
+ properties: Map<string, { predicate: string, resolveLanguage?: string }>,
1488
+ collections: Map<string, { predicate: string, instanceFilter?: string }>
1489
+ }
1490
+ ): Promise<string[]> {
1491
+ if (expressions.length === 0) {
1492
+ return [];
1493
+ }
1494
+
1495
+ // If no required triples, check which expressions have any links
1496
+ if (metadata.requiredTriples.length === 0) {
1497
+ const escapedExpressions = expressions.map(e => `'${escapeSurrealString(e)}'`).join(', ');
1498
+ const checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] GROUP BY in.uri HAVING count() > 0`;
1499
+ const result = await this.querySurrealDB(checkQuery);
1500
+ return result.map(r => r.uri);
1501
+ }
1502
+
1503
+ // For each required triple, build a query that finds matching expressions
1504
+ const validExpressionSets: Set<string>[] = [];
1505
+
1506
+ for (const triple of metadata.requiredTriples) {
1507
+ const escapedExpressions = expressions.map(e => `'${escapeSurrealString(e)}'`).join(', ');
1508
+ const escapedPredicate = escapeSurrealString(triple.predicate);
1509
+
1510
+ let checkQuery: string;
1511
+ if (triple.target) {
1512
+ // Flag: must match both predicate AND exact target value
1513
+ const escapedTarget = escapeSurrealString(triple.target);
1514
+ checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' AND out.uri = '${escapedTarget}' GROUP BY in.uri`;
1515
+ } else {
1516
+ // Property: just check predicate exists
1517
+ checkQuery = `SELECT in.uri AS uri FROM link WHERE in.uri IN [${escapedExpressions}] AND predicate = '${escapedPredicate}' GROUP BY in.uri`;
1518
+ }
1519
+
1520
+ const result = await this.querySurrealDB(checkQuery);
1521
+ validExpressionSets.push(new Set(result.map(r => r.uri)));
1522
+ }
1523
+
1524
+ // Find intersection: expressions that passed ALL required triple checks
1525
+ if (validExpressionSets.length === 0) {
1526
+ return expressions;
1527
+ }
1528
+
1529
+ const firstSet = validExpressionSets[0];
1530
+ const validExpressions = expressions.filter(expr => {
1531
+ return validExpressionSets.every(set => set.has(expr));
1532
+ });
1533
+
1534
+ return validExpressions;
1535
+ }
1536
+
1537
+ /**
1538
+ * Fallback sequential instance checking when batch checking isn't available.
1539
+ */
1540
+ private async filterInstancesSequential(values: string[], instanceFilter: string): Promise<string[]> {
1541
+ const filteredValues = [];
1542
+ for (const value of values) {
1543
+ try {
1544
+ const isInstance = await this.isSubjectInstance(value, instanceFilter);
1545
+ if (isInstance) {
1546
+ filteredValues.push(value);
1547
+ }
1548
+ } catch (err) {
1549
+ // Skip values that fail instance check
1550
+ continue;
1551
+ }
1552
+ }
1553
+ return filteredValues;
1554
+ }
1555
+
1450
1556
  /** Returns all subject instances of the given subject class as proxy objects.
1451
1557
  * @param subjectClass Either a string with the name of the subject class, or an object
1452
1558
  * with the properties of the subject class. In the latter case, all subject classes
@@ -1490,7 +1596,7 @@ export class PerspectiveProxy {
1490
1596
  return instances
1491
1597
  }
1492
1598
 
1493
- /** Returns all subject proxies of the given subject class.
1599
+ /** Returns all subject proxies of the given subject class as proxy objects.
1494
1600
  * @param subjectClass Either a string with the name of the subject class, or an object
1495
1601
  * with the properties of the subject class. In the latter case, all subject classes
1496
1602
  * that match the given properties will be used.
@@ -1505,23 +1611,23 @@ export class PerspectiveProxy {
1505
1611
 
1506
1612
  let instances = []
1507
1613
  for(let className of classes) {
1508
- //console.log(`getAllSubjectProxies: Processing class ${className}`);
1509
1614
  // Query SDNA for metadata, then query SurrealDB for instances
1510
1615
  const metadata = await this.getSubjectClassMetadataFromSDNA(className);
1511
- //console.log(`getAllSubjectProxies: Got metadata for ${className}`);
1512
1616
  if (metadata) {
1513
1617
  const surrealQuery = this.generateSurrealInstanceQuery(metadata);
1514
- console.log("surrealQuery", surrealQuery);
1515
1618
  const results = await this.querySurrealDB(surrealQuery);
1516
- console.log(`getAllSubjectProxies: SurrealDB returned ${results?.length || 0} results`);
1517
- const newInstance = results.map(r => ({ X: r.base }))
1518
- console.log("newInstance", newInstance);
1519
- instances = instances.concat(newInstance);
1520
- } else {
1521
- console.warn(`getAllSubjectProxies: No metadata found for ${className}`);
1619
+
1620
+ for (const result of results || []) {
1621
+ try {
1622
+ let subject = new Subject(this, result.base, className);
1623
+ await subject.init();
1624
+ instances.push(subject as unknown as T);
1625
+ } catch (e) {
1626
+ // Skip subjects that fail to initialize
1627
+ }
1628
+ }
1522
1629
  }
1523
1630
  }
1524
- //console.log(`getAllSubjectProxies: Returning ${instances.length} proxies`);
1525
1631
  return instances
1526
1632
  }
1527
1633