@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.
@@ -2362,10 +2362,7 @@ function parseSerializedSchema(json) {
2362
2362
  }
2363
2363
  async function loadActiveSchemaWithBootstrap(backend, graphId) {
2364
2364
  try {
2365
- const row = await backend.getActiveSchema(graphId);
2366
- const ensureRuntime = backend.ensureRuntimeContributions ? backend.ensureRuntimeContributions(graphId) : backend.ensureFulltextTable?.(graphId);
2367
- await ensureRuntime;
2368
- return row;
2365
+ return await backend.getActiveSchema(graphId);
2369
2366
  } catch (error) {
2370
2367
  if (backend.bootstrapTables && chunkIUIWCFG3_cjs.isMissingTableError(error)) {
2371
2368
  await backend.bootstrapTables();
@@ -2379,11 +2376,17 @@ async function loadAndMergeGraphExtensionDocument(backend, graph) {
2379
2376
  if (activeRow === void 0) {
2380
2377
  return { graph, activeRow: void 0, storedSchema: void 0 };
2381
2378
  }
2379
+ const { graph: merged, storedSchema } = mergeStoredGraphExtension(
2380
+ graph,
2381
+ activeRow
2382
+ );
2383
+ return { graph: merged, activeRow, storedSchema };
2384
+ }
2385
+ function mergeStoredGraphExtension(graph, activeRow) {
2382
2386
  const storedSchema = parseSerializedSchema(activeRow.schema_doc);
2383
2387
  const merged = storedSchema.extension === void 0 ? graph : mergeGraphExtension(graph, storedSchema.extension);
2384
2388
  return {
2385
2389
  graph: applyDeprecatedKinds(merged, storedSchema.deprecatedKinds),
2386
- activeRow,
2387
2390
  storedSchema
2388
2391
  };
2389
2392
  }
@@ -2466,6 +2469,69 @@ async function ensureSchema(backend, graph, options) {
2466
2469
  }
2467
2470
  return { status: "breaking", diff, actions };
2468
2471
  }
2472
+ async function loadAndVerifyGraph(backend, graph) {
2473
+ const activeRow = await readActiveSchemaPure(backend, graph.id);
2474
+ const { graph: merged, storedSchema } = mergeStoredGraphExtension(
2475
+ graph,
2476
+ activeRow
2477
+ );
2478
+ const storedHash = activeRow.schema_hash;
2479
+ const currentHash = await getSchemaHash(merged, activeRow.version + 1);
2480
+ if (storedHash !== currentHash) {
2481
+ const currentSchema = serializeSchema(merged, activeRow.version + 1);
2482
+ const diff = computeSchemaDiff(storedSchema, currentSchema);
2483
+ if (diff.hasChanges) {
2484
+ throw schemaBehindError(merged.id, activeRow.version, diff);
2485
+ }
2486
+ }
2487
+ await backend.assertRuntimeContributionsInitialized?.(merged.id);
2488
+ return {
2489
+ graph: merged,
2490
+ activeRow,
2491
+ result: { status: "unchanged", version: activeRow.version }
2492
+ };
2493
+ }
2494
+ function schemaBehindError(graphId, fromVersion, diff) {
2495
+ const actions = getMigrationActions(diff);
2496
+ const qualifier = isBackwardsCompatible(diff) ? "safe auto-migration" : "breaking change";
2497
+ return new chunkYEDQLNVE_cjs.MigrationError(
2498
+ `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().`,
2499
+ {
2500
+ graphId,
2501
+ fromVersion,
2502
+ toVersion: fromVersion + 1
2503
+ }
2504
+ );
2505
+ }
2506
+ async function assertSchemaCurrent(backend, graph) {
2507
+ const { result } = await loadAndVerifyGraph(backend, graph);
2508
+ return result;
2509
+ }
2510
+ async function readActiveSchemaPure(backend, graphId) {
2511
+ let activeRow;
2512
+ try {
2513
+ activeRow = await backend.getActiveSchema(graphId);
2514
+ } catch (error) {
2515
+ if (chunkIUIWCFG3_cjs.isMissingTableError(error)) {
2516
+ throw schemaNotInitializedError(graphId, error);
2517
+ }
2518
+ throw error;
2519
+ }
2520
+ if (activeRow === void 0) {
2521
+ throw schemaNotInitializedError(graphId);
2522
+ }
2523
+ return activeRow;
2524
+ }
2525
+ function schemaNotInitializedError(graphId, cause) {
2526
+ return new chunkYEDQLNVE_cjs.ConfigurationError(
2527
+ `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.`,
2528
+ { graphId },
2529
+ {
2530
+ cause,
2531
+ 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."
2532
+ }
2533
+ );
2534
+ }
2469
2535
  async function initializeSchema(backend, graph) {
2470
2536
  const schema = serializeSchema(graph, 1);
2471
2537
  const hash = await computeSchemaHash(schema);
@@ -2603,6 +2669,7 @@ function createValidationError(message, issues, context) {
2603
2669
  exports.KindRegistry = KindRegistry;
2604
2670
  exports.META_EDGE_BRAND = META_EDGE_BRAND;
2605
2671
  exports.applyDeprecatedKinds = applyDeprecatedKinds;
2672
+ exports.assertSchemaCurrent = assertSchemaCurrent;
2606
2673
  exports.broader = broader;
2607
2674
  exports.buildGraphExtensionOntologyKeySet = buildGraphExtensionOntologyKeySet;
2608
2675
  exports.commitNewSchemaVersion = commitNewSchemaVersion;
@@ -2634,6 +2701,7 @@ exports.isMetaEdge = isMetaEdge;
2634
2701
  exports.isSchemaInitialized = isSchemaInitialized;
2635
2702
  exports.loadActiveSchemaWithBootstrap = loadActiveSchemaWithBootstrap;
2636
2703
  exports.loadAndMergeGraphExtensionDocument = loadAndMergeGraphExtensionDocument;
2704
+ exports.loadAndVerifyGraph = loadAndVerifyGraph;
2637
2705
  exports.mergeGraphExtension = mergeGraphExtension;
2638
2706
  exports.migrateSchema = migrateSchema;
2639
2707
  exports.narrower = narrower;
@@ -2649,5 +2717,5 @@ exports.validateEdgeProps = validateEdgeProps;
2649
2717
  exports.validateNodeProps = validateNodeProps;
2650
2718
  exports.validateProps = validateProps;
2651
2719
  exports.wrapZodError = wrapZodError;
2652
- //# sourceMappingURL=chunk-4TJI44WP.cjs.map
2653
- //# sourceMappingURL=chunk-4TJI44WP.cjs.map
2720
+ //# sourceMappingURL=chunk-ZGEMI7MJ.cjs.map
2721
+ //# sourceMappingURL=chunk-ZGEMI7MJ.cjs.map