@anvia/neo4j 1.0.8 → 1.0.11

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.
@@ -227,14 +227,50 @@ function stableObject(value) {
227
227
  );
228
228
  }
229
229
 
230
+ // src/managed-scope.ts
231
+ function managedScopeParameters(scope, parameters) {
232
+ return scope === void 0 ? parameters : { graph: scope.graph, namespace: scope.namespace ?? null, ...parameters };
233
+ }
234
+ function managedScopePredicate(scope, variable, prefix) {
235
+ if (scope === void 0) return "";
236
+ return `${prefix} ${variable}.${quoteIdentifier("__anvia_graph")} = $graph${namespacePredicate(scope, variable, " AND")}`;
237
+ }
238
+ function namespacePredicate(scope, variable, prefix) {
239
+ return scope.namespace === void 0 ? "" : `${prefix} ${variable}.${quoteIdentifier("__anvia_namespace")} = $namespace`;
240
+ }
241
+ function tenantScopePredicate(scope, variable, prefix) {
242
+ return scope.namespace === void 0 ? "" : managedScopePredicate(scope, variable, prefix);
243
+ }
244
+ function managedPathPredicate(scope, relationships) {
245
+ if (scope === void 0) return "";
246
+ const item = `item.${quoteIdentifier("__anvia_graph")} = $graph${namespacePredicate(scope, "item", " AND")}`;
247
+ const nodes = ` AND all(item IN nodes(path) WHERE ${item})`;
248
+ return relationships ? `${nodes}
249
+ AND all(item IN relationships(path) WHERE ${item})` : nodes;
250
+ }
251
+ function namespaceMapEntry(scope) {
252
+ return scope.namespace === void 0 ? "" : `, ${quoteIdentifier("__anvia_namespace")}: $namespace`;
253
+ }
254
+ function tenantMapEntries(scope) {
255
+ return scope.namespace === void 0 ? "" : `, ${quoteIdentifier("__anvia_graph")}: $graph, ${quoteIdentifier("__anvia_namespace")}: $namespace`;
256
+ }
257
+ function tenantRelationshipProperties(scope) {
258
+ return scope.namespace === void 0 ? "" : ` {${quoteIdentifier("__anvia_graph")}: $graph, ${quoteIdentifier("__anvia_namespace")}: $namespace}`;
259
+ }
260
+
230
261
  // src/graph.ts
231
262
  import {
232
263
  isInt as isInt2
233
264
  } from "neo4j-driver";
265
+ var tenantScopeBrand = /* @__PURE__ */ Symbol("Neo4jTenantScope");
266
+ function neo4jTenantScope(namespace) {
267
+ return Object.freeze({ namespace, [tenantScopeBrand]: true });
268
+ }
234
269
  var Neo4jKnowledgeGraphBase = class {
235
- constructor(owner, schema) {
270
+ constructor(owner, schema, namespace) {
236
271
  this.owner = owner;
237
272
  this.schema = schema;
273
+ this.namespace = namespace;
238
274
  if (schema.kind !== "neo4j-graph-schema" && schema.kind !== "graph-schema") {
239
275
  throw new TypeError(
240
276
  "Neo4j knowledge graphs require defineGraphSchema() or defineNeo4jGraphSchema()."
@@ -243,6 +279,7 @@ var Neo4jKnowledgeGraphBase = class {
243
279
  }
244
280
  owner;
245
281
  schema;
282
+ namespace;
246
283
  async validate(options = {}) {
247
284
  throwIfAborted(options.abortSignal);
248
285
  await this.assertSupportedVersion(options.abortSignal);
@@ -273,14 +310,14 @@ var Neo4jKnowledgeGraphBase = class {
273
310
  );
274
311
  }
275
312
  async retrieve(options) {
276
- const { retrieveGraphContext } = await import("./retrieve-QRQHIVRO.js");
313
+ const { retrieveGraphContext } = await import("./retrieve-IJVPFOPP.js");
277
314
  return retrieveGraphContext({
278
315
  ...options,
279
316
  graph: this
280
317
  });
281
318
  }
282
319
  async explore(options) {
283
- const { exploreGraph } = await import("./explore-RA53GFX2.js");
320
+ const { exploreGraph } = await import("./explore-HRISJG7V.js");
284
321
  return exploreGraph(this, options);
285
322
  }
286
323
  seed(name) {
@@ -362,9 +399,11 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
362
399
  seeds;
363
400
  resources;
364
401
  name;
365
- constructor(owner, options) {
366
- super(owner, options.schema);
402
+ constructor(owner, options, tenantScope) {
403
+ const namespace = tenantScope?.namespace;
404
+ super(owner, options.schema, namespace);
367
405
  assertName(options.name, "Managed Neo4j graph name");
406
+ if (namespace !== void 0) assertName(namespace, "Managed Neo4j namespace");
368
407
  this.name = options.name;
369
408
  this.resources = validateManagedResources(options.resources);
370
409
  let chunks = {
@@ -373,6 +412,7 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
373
412
  ...this.resources.indexes.chunks.vector,
374
413
  property: "__anvia_embedding"
375
414
  }),
415
+ vectorFilterProperties: this.namespace === void 0 ? void 0 : Object.freeze(["__anvia_namespace"]),
376
416
  entryRelationshipType: "ANVIA_MENTIONS"
377
417
  };
378
418
  if (this.resources.indexes.chunks.fulltext !== void 0) {
@@ -380,7 +420,10 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
380
420
  ...chunks,
381
421
  fulltextIndex: Object.freeze({
382
422
  ...this.resources.indexes.chunks.fulltext,
383
- properties: Object.freeze(["__anvia_text"])
423
+ properties: Object.freeze([
424
+ "__anvia_text",
425
+ ...this.namespace === void 0 ? [] : ["__anvia_namespace"]
426
+ ])
384
427
  })
385
428
  };
386
429
  }
@@ -389,16 +432,18 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
389
432
  vectorIndex: Object.freeze({
390
433
  ...this.resources.indexes.entities.vector,
391
434
  property: "__anvia_embedding"
392
- })
435
+ }),
436
+ vectorFilterProperties: this.namespace === void 0 ? void 0 : Object.freeze(["__anvia_namespace"])
393
437
  };
394
438
  if (this.resources.indexes.entities.fulltext !== void 0) {
395
439
  entities = {
396
440
  ...entities,
397
441
  fulltextIndex: Object.freeze({
398
442
  ...this.resources.indexes.entities.fulltext,
399
- properties: Object.freeze(
400
- Array.from(this.resources.indexes.entities.fulltext.properties ?? [])
401
- )
443
+ properties: Object.freeze([
444
+ ...this.resources.indexes.entities.fulltext.properties ?? [],
445
+ ...this.namespace === void 0 ? [] : ["__anvia_namespace"]
446
+ ])
402
447
  })
403
448
  };
404
449
  }
@@ -411,30 +456,43 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
411
456
  positiveInteger(options.indexTimeoutMs, "Neo4j index timeout");
412
457
  throwIfAborted(options.abortSignal);
413
458
  const { labels, indexes } = this.resources;
459
+ const identityPrefix = this.namespace === void 0 ? [] : ["__anvia_namespace"];
414
460
  const statements = [
415
- uniquenessConstraint(constraintName(this.name, "document"), labels.document, ["__anvia_id"]),
416
- uniquenessConstraint(constraintName(this.name, "chunk"), labels.chunk, ["__anvia_id"]),
417
- uniquenessConstraint(constraintName(this.name, "entity"), labels.entity, ["__anvia_key"]),
461
+ uniquenessConstraint(constraintName(this.name, "document"), labels.document, [
462
+ ...identityPrefix,
463
+ "__anvia_id"
464
+ ]),
465
+ uniquenessConstraint(constraintName(this.name, "chunk"), labels.chunk, [
466
+ ...identityPrefix,
467
+ "__anvia_id"
468
+ ]),
469
+ uniquenessConstraint(constraintName(this.name, "entity"), labels.entity, [
470
+ ...identityPrefix,
471
+ "__anvia_key"
472
+ ]),
418
473
  ...Object.entries(this.schema.nodes).map(
419
- ([type, definition]) => uniquenessConstraint(
420
- constraintName(this.name, `entity_${type}`),
421
- type,
422
- definition.identity
423
- )
474
+ ([type, definition]) => uniquenessConstraint(constraintName(this.name, `entity_${type}`), type, [
475
+ ...identityPrefix,
476
+ ...definition.identity
477
+ ])
424
478
  ),
425
- vectorIndex(indexes.chunks.vector, labels.chunk),
426
- vectorIndex(indexes.entities.vector, labels.entity)
479
+ vectorIndex(indexes.chunks.vector, labels.chunk, this.namespace !== void 0),
480
+ vectorIndex(indexes.entities.vector, labels.entity, this.namespace !== void 0)
427
481
  ];
428
482
  if (indexes.chunks.fulltext !== void 0) {
429
- statements.push(fulltextIndex(indexes.chunks.fulltext.name, labels.chunk, ["__anvia_text"]));
483
+ statements.push(
484
+ fulltextIndex(indexes.chunks.fulltext.name, labels.chunk, [
485
+ "__anvia_text",
486
+ ...this.namespace === void 0 ? [] : ["__anvia_namespace"]
487
+ ])
488
+ );
430
489
  }
431
490
  if (indexes.entities.fulltext !== void 0) {
432
491
  statements.push(
433
- fulltextIndex(
434
- indexes.entities.fulltext.name,
435
- labels.entity,
436
- indexes.entities.fulltext.properties ?? []
437
- )
492
+ fulltextIndex(indexes.entities.fulltext.name, labels.entity, [
493
+ ...indexes.entities.fulltext.properties ?? [],
494
+ ...this.namespace === void 0 ? [] : ["__anvia_namespace"]
495
+ ])
438
496
  );
439
497
  }
440
498
  for (const statement of statements)
@@ -489,30 +547,52 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
489
547
  const before = await snapshotGraphState(
490
548
  transaction,
491
549
  this.name,
550
+ this.namespace,
492
551
  this.resources.labels,
493
552
  targets
494
553
  );
495
554
  await removeDocuments(
496
555
  transaction,
497
556
  this.name,
557
+ this.namespace,
498
558
  this.resources.labels,
499
559
  documentIds,
500
560
  options.orphanEntities
501
561
  );
502
- await writeDocuments(transaction, this.resources.labels, options.documents);
503
- await writeChunks(transaction, this.resources.labels, options.chunks);
562
+ await writeDocuments(
563
+ transaction,
564
+ this.name,
565
+ this.namespace,
566
+ this.resources.labels,
567
+ options.documents
568
+ );
569
+ await writeChunks(
570
+ transaction,
571
+ this.name,
572
+ this.namespace,
573
+ this.resources.labels,
574
+ options.chunks
575
+ );
504
576
  await writeEntities(
505
577
  transaction,
506
578
  this.name,
579
+ this.namespace,
507
580
  this.resources.labels.entity,
508
581
  options.entities,
509
582
  options.conflict,
510
583
  sourceDocuments
511
584
  );
512
- await writeMentions(transaction, this.resources.labels, options.mentions);
585
+ await writeMentions(
586
+ transaction,
587
+ this.name,
588
+ this.namespace,
589
+ this.resources.labels,
590
+ options.mentions
591
+ );
513
592
  await writeRelationships(
514
593
  transaction,
515
594
  this.name,
595
+ this.namespace,
516
596
  this.resources.labels.entity,
517
597
  options.relationships,
518
598
  options.conflict,
@@ -521,6 +601,7 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
521
601
  const after = await snapshotGraphState(
522
602
  transaction,
523
603
  this.name,
604
+ this.namespace,
524
605
  this.resources.labels,
525
606
  expandSnapshotTargets(targets, before)
526
607
  );
@@ -536,12 +617,14 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
536
617
  const before = await snapshotGraphState(
537
618
  transaction,
538
619
  this.name,
620
+ this.namespace,
539
621
  this.resources.labels,
540
622
  targets
541
623
  );
542
624
  await removeDocuments(
543
625
  transaction,
544
626
  this.name,
627
+ this.namespace,
545
628
  this.resources.labels,
546
629
  ids,
547
630
  options.orphanEntities
@@ -549,6 +632,7 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
549
632
  const after = await snapshotGraphState(
550
633
  transaction,
551
634
  this.name,
635
+ this.namespace,
552
636
  this.resources.labels,
553
637
  expandSnapshotTargets(targets, before)
554
638
  );
@@ -556,26 +640,27 @@ var ManagedNeo4jKnowledgeGraph = class extends Neo4jKnowledgeGraphBase {
556
640
  });
557
641
  }
558
642
  expectedConstraints() {
643
+ const identityPrefix = this.namespace === void 0 ? [] : ["__anvia_namespace"];
559
644
  return [
560
645
  {
561
646
  name: constraintName(this.name, "document"),
562
647
  label: this.resources.labels.document,
563
- properties: ["__anvia_id"]
648
+ properties: [...identityPrefix, "__anvia_id"]
564
649
  },
565
650
  {
566
651
  name: constraintName(this.name, "chunk"),
567
652
  label: this.resources.labels.chunk,
568
- properties: ["__anvia_id"]
653
+ properties: [...identityPrefix, "__anvia_id"]
569
654
  },
570
655
  {
571
656
  name: constraintName(this.name, "entity"),
572
657
  label: this.resources.labels.entity,
573
- properties: ["__anvia_key"]
658
+ properties: [...identityPrefix, "__anvia_key"]
574
659
  },
575
660
  ...Object.entries(this.schema.nodes).map(([type, definition]) => ({
576
661
  name: constraintName(this.name, `entity_${type}`),
577
662
  label: type,
578
- properties: definition.identity
663
+ properties: [...identityPrefix, ...definition.identity]
579
664
  }))
580
665
  ];
581
666
  }
@@ -709,7 +794,7 @@ function expectedIndexes(seeds) {
709
794
  kind: "vector",
710
795
  name: seed.vectorIndex.name,
711
796
  labels: seed.labels,
712
- properties: [seed.vectorIndex.property],
797
+ properties: [seed.vectorIndex.property, ...seed.vectorFilterProperties ?? []],
713
798
  dimensions: seed.vectorIndex.dimensions,
714
799
  similarity: seed.vectorIndex.similarity
715
800
  });
@@ -820,8 +905,9 @@ function uniquenessConstraint(name, label, properties) {
820
905
  const expression = properties.length === 1 ? `n.${quoteIdentifier(properties[0])}` : `(${properties.map((property) => `n.${quoteIdentifier(property)}`).join(", ")})`;
821
906
  return `CREATE CONSTRAINT ${quoteIdentifier(name)} IF NOT EXISTS FOR (n:${quoteIdentifier(label)}) REQUIRE ${expression} IS UNIQUE`;
822
907
  }
823
- function vectorIndex(index, label) {
824
- return `CREATE VECTOR INDEX ${quoteIdentifier(index.name)} IF NOT EXISTS FOR (n:${quoteIdentifier(label)}) ON (n.${quoteIdentifier("__anvia_embedding")}) OPTIONS {indexConfig: {\`vector.dimensions\`: ${index.dimensions}, \`vector.similarity_function\`: '${index.similarity}'}}`;
908
+ function vectorIndex(index, label, namespaced) {
909
+ const filterProperties = namespaced ? ` WITH [n.${quoteIdentifier("__anvia_namespace")}]` : "";
910
+ return `CREATE VECTOR INDEX ${quoteIdentifier(index.name)} IF NOT EXISTS FOR (n:${quoteIdentifier(label)}) ON (n.${quoteIdentifier("__anvia_embedding")})${filterProperties} OPTIONS {indexConfig: {\`vector.dimensions\`: ${index.dimensions}, \`vector.similarity_function\`: '${index.similarity}'}}`;
825
911
  }
826
912
  function fulltextIndex(name, label, properties) {
827
913
  if (properties.length === 0) throw new TypeError(`Full-text index ${name} requires properties.`);
@@ -1089,32 +1175,38 @@ function expandSnapshotTargets(targets, snapshot) {
1089
1175
  mentions: [...mentions.values()]
1090
1176
  };
1091
1177
  }
1092
- async function snapshotGraphState(transaction, graphName, labels, targets) {
1178
+ async function snapshotGraphState(transaction, graphName, namespace, labels, targets) {
1093
1179
  const documentsResult = await transaction.run(
1094
1180
  `MATCH (d:${quoteIdentifier(labels.document)})
1095
- WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds
1181
+ WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds${tenantPredicate("d", namespace, " AND")}
1096
1182
  RETURN d.${quoteIdentifier("__anvia_id")} AS key, properties(d) AS properties`,
1097
- { documentIds: targets.documentIds }
1183
+ managedParameters(graphName, namespace, { documentIds: targets.documentIds })
1098
1184
  );
1099
1185
  const chunksResult = await transaction.run(
1100
1186
  `MATCH (c:${quoteIdentifier(labels.chunk)})
1101
1187
  OPTIONAL MATCH (d:${quoteIdentifier(labels.document)})-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c)
1102
- WITH c, d WHERE c.${quoteIdentifier("__anvia_id")} IN $chunkIds OR d.${quoteIdentifier("__anvia_id")} IN $documentIds
1188
+ WITH c, d WHERE (c.${quoteIdentifier("__anvia_id")} IN $chunkIds OR d.${quoteIdentifier("__anvia_id")} IN $documentIds)${tenantPredicate("c", namespace, " AND")}
1103
1189
  RETURN c.${quoteIdentifier("__anvia_id")} AS key, properties(c) AS properties`,
1104
- { chunkIds: targets.chunkIds, documentIds: targets.documentIds }
1190
+ managedParameters(graphName, namespace, {
1191
+ chunkIds: targets.chunkIds,
1192
+ documentIds: targets.documentIds
1193
+ })
1105
1194
  );
1106
1195
  const entitiesResult = await transaction.run(
1107
1196
  `MATCH (e:${quoteIdentifier(labels.entity)})
1108
- WHERE e.${quoteIdentifier("__anvia_graph")} = $graph AND (
1197
+ WHERE e.${quoteIdentifier("__anvia_graph")} = $graph${namespacePredicate2("e", namespace, " AND")} AND (
1109
1198
  e.${quoteIdentifier("__anvia_key")} IN $entityKeys OR
1110
1199
  any(id IN coalesce(e.${quoteIdentifier("__anvia_source_document_ids")}, []) WHERE id IN $documentIds)
1111
1200
  )
1112
1201
  RETURN e.${quoteIdentifier("__anvia_key")} AS key, labels(e) AS labels, properties(e) AS properties`,
1113
- { graph: graphName, entityKeys: targets.entityKeys, documentIds: targets.documentIds }
1202
+ managedParameters(graphName, namespace, {
1203
+ entityKeys: targets.entityKeys,
1204
+ documentIds: targets.documentIds
1205
+ })
1114
1206
  );
1115
1207
  const relationshipsResult = await transaction.run(
1116
1208
  `MATCH ()-[r]->()
1117
- WHERE r.${quoteIdentifier("__anvia_graph")} = $graph AND (
1209
+ WHERE r.${quoteIdentifier("__anvia_graph")} = $graph${namespacePredicate2("r", namespace, " AND")} AND (
1118
1210
  r.${quoteIdentifier("__anvia_key")} IN $relationshipKeys OR
1119
1211
  any(id IN coalesce(r.${quoteIdentifier("__anvia_source_document_ids")}, []) WHERE id IN $documentIds)
1120
1212
  )
@@ -1123,19 +1215,22 @@ RETURN r.${quoteIdentifier("__anvia_key")} AS key,
1123
1215
  startNode(r).${quoteIdentifier("__anvia_key")} AS source,
1124
1216
  endNode(r).${quoteIdentifier("__anvia_key")} AS target,
1125
1217
  properties(r) AS properties`,
1126
- {
1127
- graph: graphName,
1218
+ managedParameters(graphName, namespace, {
1128
1219
  relationshipKeys: targets.relationshipKeys,
1129
1220
  documentIds: targets.documentIds
1130
- }
1221
+ })
1131
1222
  );
1132
1223
  const mentionsResult = await transaction.run(
1133
1224
  `MATCH (c:${quoteIdentifier(labels.chunk)})-[:${quoteIdentifier("ANVIA_MENTIONS")}]->(e:${quoteIdentifier(labels.entity)})
1134
1225
  OPTIONAL MATCH (d:${quoteIdentifier(labels.document)})-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c)
1135
- WITH c, e, d WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds OR
1226
+ WITH c, e, d WHERE (d.${quoteIdentifier("__anvia_id")} IN $documentIds OR
1136
1227
  any(item IN $mentions WHERE item.chunkId = c.${quoteIdentifier("__anvia_id")} AND item.entityKey = e.${quoteIdentifier("__anvia_key")})
1228
+ )${tenantPredicate("c", namespace, " AND")}
1137
1229
  RETURN c.${quoteIdentifier("__anvia_id")} AS chunkId, e.${quoteIdentifier("__anvia_key")} AS entityKey`,
1138
- { documentIds: targets.documentIds, mentions: targets.mentions }
1230
+ managedParameters(graphName, namespace, {
1231
+ documentIds: targets.documentIds,
1232
+ mentions: targets.mentions
1233
+ })
1139
1234
  );
1140
1235
  return {
1141
1236
  documents: snapshotProperties(documentsResult.records, "document"),
@@ -1262,54 +1357,71 @@ function emptyWriteResult() {
1262
1357
  mentions: empty()
1263
1358
  };
1264
1359
  }
1265
- async function removeDocuments(transaction, graphName, labels, documentIds, orphanEntities) {
1360
+ function managedParameters(graph, namespace, parameters) {
1361
+ return managedScopeParameters({ graph, namespace }, parameters);
1362
+ }
1363
+ function namespacePredicate2(variable, namespace, prefix) {
1364
+ return namespacePredicate({ graph: "", namespace }, variable, prefix);
1365
+ }
1366
+ function tenantPredicate(variable, namespace, prefix) {
1367
+ return tenantScopePredicate({ graph: "", namespace }, variable, prefix);
1368
+ }
1369
+ function namespaceMapEntry2(namespace) {
1370
+ return namespaceMapEntry({ graph: "", namespace });
1371
+ }
1372
+ function tenantMapEntries2(namespace) {
1373
+ return tenantMapEntries({ graph: "", namespace });
1374
+ }
1375
+ function tenantRelationshipProperties2(namespace) {
1376
+ return tenantRelationshipProperties({ graph: "", namespace });
1377
+ }
1378
+ async function removeDocuments(transaction, graphName, namespace, labels, documentIds, orphanEntities) {
1266
1379
  const chunks = await transaction.run(
1267
- `MATCH (d:${quoteIdentifier(labels.document)})-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c:${quoteIdentifier(labels.chunk)}) WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds RETURN collect(c.${quoteIdentifier("__anvia_id")}) AS chunkIds`,
1268
- { documentIds }
1380
+ `MATCH (d:${quoteIdentifier(labels.document)})-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c:${quoteIdentifier(labels.chunk)}) WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds${tenantPredicate("d", namespace, " AND")} RETURN collect(c.${quoteIdentifier("__anvia_id")}) AS chunkIds`,
1381
+ managedParameters(graphName, namespace, { documentIds })
1269
1382
  );
1270
1383
  const chunkIdsValue = chunks.records[0]?.get("chunkIds");
1271
1384
  const chunkIds = requiredStringArray(chunkIdsValue, "Neo4j replacement chunk ids");
1272
1385
  await transaction.run(
1273
- `MATCH ()-[r]->() WHERE r.${quoteIdentifier("__anvia_graph")} = $graph AND any(id IN r.${quoteIdentifier("__anvia_source_document_ids")} WHERE id IN $documentIds)
1386
+ `MATCH ()-[r]->() WHERE r.${quoteIdentifier("__anvia_graph")} = $graph${namespacePredicate2("r", namespace, " AND")} AND any(id IN r.${quoteIdentifier("__anvia_source_document_ids")} WHERE id IN $documentIds)
1274
1387
  SET r.${quoteIdentifier("__anvia_source_document_ids")} = [id IN r.${quoteIdentifier("__anvia_source_document_ids")} WHERE NOT id IN $documentIds]
1275
1388
  SET r.${quoteIdentifier("__anvia_source_chunk_ids")} = [id IN coalesce(r.${quoteIdentifier("__anvia_source_chunk_ids")}, []) WHERE NOT id IN $chunkIds]
1276
1389
  WITH r WHERE size(r.${quoteIdentifier("__anvia_source_document_ids")}) = 0 DELETE r`,
1277
- { graph: graphName, documentIds, chunkIds }
1390
+ managedParameters(graphName, namespace, { documentIds, chunkIds })
1278
1391
  );
1279
1392
  await transaction.run(
1280
- `MATCH (d:${quoteIdentifier(labels.document)}) WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds
1393
+ `MATCH (d:${quoteIdentifier(labels.document)}) WHERE d.${quoteIdentifier("__anvia_id")} IN $documentIds${tenantPredicate("d", namespace, " AND")}
1281
1394
  OPTIONAL MATCH (d)-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c:${quoteIdentifier(labels.chunk)})
1282
1395
  DETACH DELETE c, d`,
1283
- { documentIds }
1396
+ managedParameters(graphName, namespace, { documentIds })
1284
1397
  );
1285
1398
  await transaction.run(
1286
1399
  `MATCH (e:${quoteIdentifier(labels.entity)})
1287
- WHERE e.${quoteIdentifier("__anvia_graph")} = $graph AND
1400
+ WHERE e.${quoteIdentifier("__anvia_graph")} = $graph${namespacePredicate2("e", namespace, " AND")} AND
1288
1401
  any(id IN coalesce(e.${quoteIdentifier("__anvia_source_document_ids")}, []) WHERE id IN $documentIds)
1289
1402
  SET e.${quoteIdentifier("__anvia_source_document_ids")} = [id IN e.${quoteIdentifier("__anvia_source_document_ids")} WHERE NOT id IN $documentIds],
1290
1403
  e.${quoteIdentifier("__anvia_source_chunk_ids")} = [id IN coalesce(e.${quoteIdentifier("__anvia_source_chunk_ids")}, []) WHERE NOT id IN $chunkIds]
1291
1404
  WITH e WHERE $deleteOrphans AND size(e.${quoteIdentifier("__anvia_source_document_ids")}) = 0
1292
1405
  DETACH DELETE e`,
1293
- {
1294
- graph: graphName,
1406
+ managedParameters(graphName, namespace, {
1295
1407
  documentIds,
1296
1408
  chunkIds,
1297
1409
  deleteOrphans: orphanEntities === "delete"
1298
- }
1410
+ })
1299
1411
  );
1300
1412
  }
1301
- async function writeDocuments(transaction, labels, documents) {
1413
+ async function writeDocuments(transaction, graphName, namespace, labels, documents) {
1302
1414
  await transaction.run(
1303
- `UNWIND $rows AS row MERGE (d:${quoteIdentifier(labels.document)} {${quoteIdentifier("__anvia_id")}: row.id}) SET d += row.properties`,
1304
- {
1415
+ `UNWIND $rows AS row MERGE (d:${quoteIdentifier(labels.document)} {${quoteIdentifier("__anvia_id")}: row.id${namespaceMapEntry2(namespace)}}) ${namespace === void 0 ? "SET d += row.properties" : `SET d.${quoteIdentifier("__anvia_graph")} = $graph, d += row.properties`}`,
1416
+ managedParameters(graphName, namespace, {
1305
1417
  rows: documents.map((document) => ({
1306
1418
  id: document.id,
1307
1419
  properties: document.properties ?? {}
1308
1420
  }))
1309
- }
1421
+ })
1310
1422
  );
1311
1423
  }
1312
- async function writeChunks(transaction, labels, chunks) {
1424
+ async function writeChunks(transaction, graphName, namespace, labels, chunks) {
1313
1425
  const rows = chunks.map((chunk) => ({
1314
1426
  id: chunk.id,
1315
1427
  documentId: chunk.document.documentId,
@@ -1321,10 +1433,11 @@ async function writeChunks(transaction, labels, chunks) {
1321
1433
  await transaction.run(
1322
1434
  `UNWIND $rows AS row
1323
1435
  MATCH (d:${quoteIdentifier(labels.document)} {${quoteIdentifier("__anvia_id")}: row.documentId})
1324
- CREATE (c:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.id, ${quoteIdentifier("__anvia_document_id")}: row.documentId, ${quoteIdentifier("__anvia_index")}: row.index, ${quoteIdentifier("__anvia_text")}: row.text, ${quoteIdentifier("__anvia_embedding")}: row.embedding})
1436
+ ${tenantPredicate("d", namespace, " WHERE")}
1437
+ CREATE (c:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.id, ${quoteIdentifier("__anvia_document_id")}: row.documentId, ${quoteIdentifier("__anvia_index")}: row.index, ${quoteIdentifier("__anvia_text")}: row.text, ${quoteIdentifier("__anvia_embedding")}: row.embedding${tenantMapEntries2(namespace)}})
1325
1438
  SET c += row.metadata
1326
- CREATE (d)-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c)`,
1327
- { rows }
1439
+ CREATE (d)-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}${tenantRelationshipProperties2(namespace)}]->(c)`,
1440
+ managedParameters(graphName, namespace, { rows })
1328
1441
  );
1329
1442
  const pairs = [...rows].sort(
1330
1443
  (left, right) => left.documentId.localeCompare(right.documentId) || left.index - right.index
@@ -1334,12 +1447,12 @@ CREATE (d)-[:${quoteIdentifier("ANVIA_HAS_CHUNK")}]->(c)`,
1334
1447
  });
1335
1448
  if (pairs.length > 0) {
1336
1449
  await transaction.run(
1337
- `UNWIND $rows AS row MATCH (a:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.from}), (b:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.to}) CREATE (a)-[:${quoteIdentifier("ANVIA_NEXT_CHUNK")}]->(b)`,
1338
- { rows: pairs }
1450
+ `UNWIND $rows AS row MATCH (a:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.from${namespaceMapEntry2(namespace)}}), (b:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.to${namespaceMapEntry2(namespace)}}) CREATE (a)-[:${quoteIdentifier("ANVIA_NEXT_CHUNK")}${tenantRelationshipProperties2(namespace)}]->(b)`,
1451
+ managedParameters(graphName, namespace, { rows: pairs })
1339
1452
  );
1340
1453
  }
1341
1454
  }
1342
- async function writeEntities(transaction, graphName, entityLabel, entities, conflict, sourceDocuments) {
1455
+ async function writeEntities(transaction, graphName, namespace, entityLabel, entities, conflict, sourceDocuments) {
1343
1456
  const grouped = groupBy(entities, (entity) => entity.document.type);
1344
1457
  for (const [type, values] of grouped) {
1345
1458
  const rows = values.map((entity) => ({
@@ -1349,26 +1462,28 @@ async function writeEntities(transaction, graphName, entityLabel, entities, conf
1349
1462
  sourceDocumentIds: sourceDocuments(entity.document.sourceChunkIds),
1350
1463
  sourceChunkIds: entity.document.sourceChunkIds
1351
1464
  }));
1352
- if (conflict === "error") await assertEntityConflicts(transaction, entityLabel, rows);
1465
+ if (conflict === "error")
1466
+ await assertEntityConflicts(transaction, graphName, namespace, entityLabel, rows);
1353
1467
  const propertySet = conflictPropertySet("e", conflict);
1354
1468
  const embedding = entityEmbeddingExpression(conflict);
1355
1469
  await transaction.run(
1356
1470
  `UNWIND $rows AS row
1357
- MERGE (e:${quoteIdentifier(entityLabel)}:${quoteIdentifier(type)} {${quoteIdentifier("__anvia_key")}: row.key})
1471
+ MERGE (e:${quoteIdentifier(entityLabel)}:${quoteIdentifier(type)} {${quoteIdentifier("__anvia_key")}: row.key${namespaceMapEntry2(namespace)}})
1358
1472
  ${propertySet}
1359
1473
  SET e.${quoteIdentifier("__anvia_key")} = row.key,
1360
1474
  e.${quoteIdentifier("__anvia_graph")} = $graph,
1475
+ e.${quoteIdentifier("__anvia_namespace")} = coalesce($namespace, e.${quoteIdentifier("__anvia_namespace")}),
1361
1476
  e.${quoteIdentifier("__anvia_embedding")} = ${embedding},
1362
1477
  e.${quoteIdentifier("__anvia_source_document_ids")} = reduce(all = existingDocumentIds, id IN row.sourceDocumentIds | CASE WHEN id IN all THEN all ELSE all + id END),
1363
1478
  e.${quoteIdentifier("__anvia_source_chunk_ids")} = reduce(all = existingChunkIds, id IN row.sourceChunkIds | CASE WHEN id IN all THEN all ELSE all + id END)`,
1364
- { rows, graph: graphName }
1479
+ managedParameters(graphName, namespace, { rows })
1365
1480
  );
1366
1481
  }
1367
1482
  }
1368
- async function assertEntityConflicts(transaction, entityLabel, rows) {
1483
+ async function assertEntityConflicts(transaction, graphName, namespace, entityLabel, rows) {
1369
1484
  const result = await transaction.run(
1370
- `UNWIND $keys AS key MATCH (e:${quoteIdentifier(entityLabel)} {${quoteIdentifier("__anvia_key")}: key}) RETURN key, properties(e) AS properties`,
1371
- { keys: rows.map((row) => row.key) }
1485
+ `UNWIND $keys AS key MATCH (e:${quoteIdentifier(entityLabel)} {${quoteIdentifier("__anvia_key")}: key${namespaceMapEntry2(namespace)}}) WHERE e.${quoteIdentifier("__anvia_graph")} = $graph RETURN key, properties(e) AS properties`,
1486
+ managedParameters(graphName, namespace, { keys: rows.map((row) => row.key) })
1372
1487
  );
1373
1488
  const incoming = new Map(rows.map((row) => [row.key, row.properties]));
1374
1489
  for (const record of result.records) {
@@ -1379,16 +1494,17 @@ async function assertEntityConflicts(transaction, entityLabel, rows) {
1379
1494
  }
1380
1495
  }
1381
1496
  }
1382
- async function writeMentions(transaction, labels, mentions) {
1497
+ async function writeMentions(transaction, graphName, namespace, labels, mentions) {
1383
1498
  if (mentions.length === 0) return;
1384
1499
  await transaction.run(
1385
1500
  `UNWIND $rows AS row
1386
- MATCH (c:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.chunkId}), (e:${quoteIdentifier(labels.entity)} {${quoteIdentifier("__anvia_key")}: row.entityKey})
1387
- MERGE (c)-[:${quoteIdentifier("ANVIA_MENTIONS")}]->(e)`,
1388
- { rows: mentions }
1501
+ MATCH (c:${quoteIdentifier(labels.chunk)} {${quoteIdentifier("__anvia_id")}: row.chunkId${namespaceMapEntry2(namespace)}}), (e:${quoteIdentifier(labels.entity)} {${quoteIdentifier("__anvia_key")}: row.entityKey${namespaceMapEntry2(namespace)}})
1502
+ ${namespace === void 0 ? "" : `WHERE c.${quoteIdentifier("__anvia_graph")} = $graph AND e.${quoteIdentifier("__anvia_graph")} = $graph`}
1503
+ MERGE (c)-[:${quoteIdentifier("ANVIA_MENTIONS")}${tenantRelationshipProperties2(namespace)}]->(e)`,
1504
+ managedParameters(graphName, namespace, { rows: mentions })
1389
1505
  );
1390
1506
  }
1391
- async function writeRelationships(transaction, graphName, entityLabel, relationships, conflict, sourceDocuments) {
1507
+ async function writeRelationships(transaction, graphName, namespace, entityLabel, relationships, conflict, sourceDocuments) {
1392
1508
  const grouped = groupBy(relationships, (relationship) => relationship.type);
1393
1509
  for (const [type, values] of grouped) {
1394
1510
  const rows = values.map((relationship) => ({
@@ -1401,8 +1517,8 @@ async function writeRelationships(transaction, graphName, entityLabel, relations
1401
1517
  }));
1402
1518
  if (conflict === "error") {
1403
1519
  const existing = await transaction.run(
1404
- `UNWIND $keys AS key MATCH ()-[r:${quoteIdentifier(type)} {${quoteIdentifier("__anvia_key")}: key}]->() RETURN key, properties(r) AS properties`,
1405
- { keys: rows.map((row) => row.key) }
1520
+ `UNWIND $keys AS key MATCH ()-[r:${quoteIdentifier(type)} {${quoteIdentifier("__anvia_key")}: key${namespaceMapEntry2(namespace)}}]->() WHERE r.${quoteIdentifier("__anvia_graph")} = $graph RETURN key, properties(r) AS properties`,
1521
+ managedParameters(graphName, namespace, { keys: rows.map((row) => row.key) })
1406
1522
  );
1407
1523
  const incoming = new Map(rows.map((row) => [row.key, row.properties]));
1408
1524
  for (const record of existing.records) {
@@ -1415,14 +1531,16 @@ async function writeRelationships(transaction, graphName, entityLabel, relations
1415
1531
  const propertySet = conflictPropertySet("r", conflict);
1416
1532
  await transaction.run(
1417
1533
  `UNWIND $rows AS row
1418
- MATCH (a:${quoteIdentifier(entityLabel)} {${quoteIdentifier("__anvia_key")}: row.from}), (b:${quoteIdentifier(entityLabel)} {${quoteIdentifier("__anvia_key")}: row.to})
1419
- MERGE (a)-[r:${quoteIdentifier(type)} {${quoteIdentifier("__anvia_key")}: row.key}]->(b)
1534
+ MATCH (a:${quoteIdentifier(entityLabel)} {${quoteIdentifier("__anvia_key")}: row.from${namespaceMapEntry2(namespace)}}), (b:${quoteIdentifier(entityLabel)} {${quoteIdentifier("__anvia_key")}: row.to${namespaceMapEntry2(namespace)}})
1535
+ WHERE a.${quoteIdentifier("__anvia_graph")} = $graph AND b.${quoteIdentifier("__anvia_graph")} = $graph
1536
+ MERGE (a)-[r:${quoteIdentifier(type)} {${quoteIdentifier("__anvia_key")}: row.key${namespaceMapEntry2(namespace)}}]->(b)
1420
1537
  ${propertySet}
1421
1538
  SET r.${quoteIdentifier("__anvia_key")} = row.key,
1422
1539
  r.${quoteIdentifier("__anvia_graph")} = $graph,
1540
+ r.${quoteIdentifier("__anvia_namespace")} = coalesce($namespace, r.${quoteIdentifier("__anvia_namespace")}),
1423
1541
  r.${quoteIdentifier("__anvia_source_document_ids")} = reduce(all = existingDocumentIds, id IN row.sourceDocumentIds | CASE WHEN id IN all THEN all ELSE all + id END),
1424
1542
  r.${quoteIdentifier("__anvia_source_chunk_ids")} = reduce(all = existingChunkIds, id IN row.sourceChunkIds | CASE WHEN id IN all THEN all ELSE all + id END)`,
1425
- { rows, graph: graphName }
1543
+ managedParameters(graphName, namespace, { rows })
1426
1544
  );
1427
1545
  }
1428
1546
  }
@@ -1479,8 +1597,13 @@ export {
1479
1597
  positiveInteger,
1480
1598
  finiteScore,
1481
1599
  strictProperties,
1600
+ managedScopeParameters,
1601
+ managedScopePredicate,
1602
+ tenantScopePredicate,
1603
+ managedPathPredicate,
1604
+ neo4jTenantScope,
1482
1605
  Neo4jKnowledgeGraphBase,
1483
1606
  Neo4jKnowledgeGraph,
1484
1607
  ManagedNeo4jKnowledgeGraph
1485
1608
  };
1486
- //# sourceMappingURL=chunk-QNHOT7P2.js.map
1609
+ //# sourceMappingURL=chunk-EUZ5C6OC.js.map