@nicia-ai/typegraph 0.26.0 → 0.27.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2360,10 +2360,7 @@ function parseSerializedSchema(json) {
2360
2360
  }
2361
2361
  async function loadActiveSchemaWithBootstrap(backend, graphId) {
2362
2362
  try {
2363
- const row = await backend.getActiveSchema(graphId);
2364
- const ensureRuntime = backend.ensureRuntimeContributions ? backend.ensureRuntimeContributions(graphId) : backend.ensureFulltextTable?.(graphId);
2365
- await ensureRuntime;
2366
- return row;
2363
+ return await backend.getActiveSchema(graphId);
2367
2364
  } catch (error) {
2368
2365
  if (backend.bootstrapTables && isMissingTableError(error)) {
2369
2366
  await backend.bootstrapTables();
@@ -2377,11 +2374,17 @@ async function loadAndMergeGraphExtensionDocument(backend, graph) {
2377
2374
  if (activeRow === void 0) {
2378
2375
  return { graph, activeRow: void 0, storedSchema: void 0 };
2379
2376
  }
2377
+ const { graph: merged, storedSchema } = mergeStoredGraphExtension(
2378
+ graph,
2379
+ activeRow
2380
+ );
2381
+ return { graph: merged, activeRow, storedSchema };
2382
+ }
2383
+ function mergeStoredGraphExtension(graph, activeRow) {
2380
2384
  const storedSchema = parseSerializedSchema(activeRow.schema_doc);
2381
2385
  const merged = storedSchema.extension === void 0 ? graph : mergeGraphExtension(graph, storedSchema.extension);
2382
2386
  return {
2383
2387
  graph: applyDeprecatedKinds(merged, storedSchema.deprecatedKinds),
2384
- activeRow,
2385
2388
  storedSchema
2386
2389
  };
2387
2390
  }
@@ -2464,6 +2467,69 @@ async function ensureSchema(backend, graph, options) {
2464
2467
  }
2465
2468
  return { status: "breaking", diff, actions };
2466
2469
  }
2470
+ async function loadAndVerifyGraph(backend, graph) {
2471
+ const activeRow = await readActiveSchemaPure(backend, graph.id);
2472
+ const { graph: merged, storedSchema } = mergeStoredGraphExtension(
2473
+ graph,
2474
+ activeRow
2475
+ );
2476
+ const storedHash = activeRow.schema_hash;
2477
+ const currentHash = await getSchemaHash(merged, activeRow.version + 1);
2478
+ if (storedHash !== currentHash) {
2479
+ const currentSchema = serializeSchema(merged, activeRow.version + 1);
2480
+ const diff = computeSchemaDiff(storedSchema, currentSchema);
2481
+ if (diff.hasChanges) {
2482
+ throw schemaBehindError(merged.id, activeRow.version, diff);
2483
+ }
2484
+ }
2485
+ await backend.assertRuntimeContributionsInitialized?.(merged.id);
2486
+ return {
2487
+ graph: merged,
2488
+ activeRow,
2489
+ result: { status: "unchanged", version: activeRow.version }
2490
+ };
2491
+ }
2492
+ function schemaBehindError(graphId, fromVersion, diff) {
2493
+ const actions = getMigrationActions(diff);
2494
+ const qualifier = isBackwardsCompatible(diff) ? "safe auto-migration" : "breaking change";
2495
+ return new MigrationError(
2496
+ `Schema verification failed for graph "${graphId}": ${diff.summary} (${qualifier}). ${actions.length} migration action(s) needed. The least-privilege runtime cannot migrate \u2014 run createStoreWithSchema(graph, adminBackend) under a privileged role (after any generated migration SQL, if you manage DDL externally) before attaching with createStore() / createVerifiedStore().`,
2497
+ {
2498
+ graphId,
2499
+ fromVersion,
2500
+ toVersion: fromVersion + 1
2501
+ }
2502
+ );
2503
+ }
2504
+ async function assertSchemaCurrent(backend, graph) {
2505
+ const { result } = await loadAndVerifyGraph(backend, graph);
2506
+ return result;
2507
+ }
2508
+ async function readActiveSchemaPure(backend, graphId) {
2509
+ let activeRow;
2510
+ try {
2511
+ activeRow = await backend.getActiveSchema(graphId);
2512
+ } catch (error) {
2513
+ if (isMissingTableError(error)) {
2514
+ throw schemaNotInitializedError(graphId, error);
2515
+ }
2516
+ throw error;
2517
+ }
2518
+ if (activeRow === void 0) {
2519
+ throw schemaNotInitializedError(graphId);
2520
+ }
2521
+ return activeRow;
2522
+ }
2523
+ function schemaNotInitializedError(graphId, cause) {
2524
+ return new ConfigurationError(
2525
+ `Cannot verify graph "${graphId}": no schema has been initialized. Run createStoreWithSchema(graph, adminBackend) once under a privileged role (which commits the schema_versions row and materializes contribution markers) before attaching with createStore() / createVerifiedStore(). Generated migration SQL creates the tables but does not initialize the schema row.`,
2526
+ { graphId },
2527
+ {
2528
+ cause,
2529
+ suggestion: "Run createStoreWithSchema(graph, adminBackend) once under a privileged role. If you manage DDL externally with drizzle-kit / generatePostgresMigrationSQL / generateSqliteMigrationSQL, apply that first, then still run createStoreWithSchema to commit the schema row and contribution markers."
2530
+ }
2531
+ );
2532
+ }
2467
2533
  async function initializeSchema(backend, graph) {
2468
2534
  const schema = serializeSchema(graph, 1);
2469
2535
  const hash = await computeSchemaHash(schema);
@@ -2598,6 +2664,6 @@ function createValidationError(message, issues, context) {
2598
2664
  });
2599
2665
  }
2600
2666
 
2601
- export { KindRegistry, META_EDGE_BRAND, applyDeprecatedKinds, broader, buildGraphExtensionOntologyKeySet, commitNewSchemaVersion, compileTimeOntologyKey, computeClosuresFromOntology, computeSchemaDiff, computeSchemaHash, core, createEmptyClosures, createValidationError, defineEdge, defineNode, deserializeWherePredicate, differentFrom, disjointWith, ensureSchema, equivalentTo, extensionKindNames, getActiveSchema, getMigrationActions, getSchemaChanges, getTypeName, hasPart, implies, initializeSchema, inverseOf, isBackwardsCompatible, isMetaEdge, isSchemaInitialized, loadActiveSchemaWithBootstrap, loadAndMergeGraphExtensionDocument, mergeGraphExtension, migrateSchema, narrower, parseSerializedSchema, partOf, relatedTo, rollbackSchema, sameAs, serializeSchema, serializeSchemaProperties, subClassOf, validateEdgeProps, validateNodeProps, validateProps, wrapZodError };
2602
- //# sourceMappingURL=chunk-3W6AIFT2.js.map
2603
- //# sourceMappingURL=chunk-3W6AIFT2.js.map
2667
+ export { KindRegistry, META_EDGE_BRAND, applyDeprecatedKinds, assertSchemaCurrent, broader, buildGraphExtensionOntologyKeySet, commitNewSchemaVersion, compileTimeOntologyKey, computeClosuresFromOntology, computeSchemaDiff, computeSchemaHash, core, createEmptyClosures, createValidationError, defineEdge, defineNode, deserializeWherePredicate, differentFrom, disjointWith, ensureSchema, equivalentTo, extensionKindNames, getActiveSchema, getMigrationActions, getSchemaChanges, getTypeName, hasPart, implies, initializeSchema, inverseOf, isBackwardsCompatible, isMetaEdge, isSchemaInitialized, loadActiveSchemaWithBootstrap, loadAndMergeGraphExtensionDocument, loadAndVerifyGraph, mergeGraphExtension, migrateSchema, narrower, parseSerializedSchema, partOf, relatedTo, rollbackSchema, sameAs, serializeSchema, serializeSchemaProperties, subClassOf, validateEdgeProps, validateNodeProps, validateProps, wrapZodError };
2668
+ //# sourceMappingURL=chunk-X53YKGHR.js.map
2669
+ //# sourceMappingURL=chunk-X53YKGHR.js.map