@nicia-ai/typegraph 0.1.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.
Files changed (131) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +47 -0
  3. package/dist/ast-BVyihVbP.d.cts +564 -0
  4. package/dist/ast-BVyihVbP.d.ts +564 -0
  5. package/dist/backend/drizzle/index.cjs +41 -0
  6. package/dist/backend/drizzle/index.cjs.map +1 -0
  7. package/dist/backend/drizzle/index.d.cts +12 -0
  8. package/dist/backend/drizzle/index.d.ts +12 -0
  9. package/dist/backend/drizzle/index.js +12 -0
  10. package/dist/backend/drizzle/index.js.map +1 -0
  11. package/dist/backend/drizzle/postgres.cjs +27 -0
  12. package/dist/backend/drizzle/postgres.cjs.map +1 -0
  13. package/dist/backend/drizzle/postgres.d.cts +37 -0
  14. package/dist/backend/drizzle/postgres.d.ts +37 -0
  15. package/dist/backend/drizzle/postgres.js +10 -0
  16. package/dist/backend/drizzle/postgres.js.map +1 -0
  17. package/dist/backend/drizzle/schema/postgres.cjs +40 -0
  18. package/dist/backend/drizzle/schema/postgres.cjs.map +1 -0
  19. package/dist/backend/drizzle/schema/postgres.d.cts +2419 -0
  20. package/dist/backend/drizzle/schema/postgres.d.ts +2419 -0
  21. package/dist/backend/drizzle/schema/postgres.js +7 -0
  22. package/dist/backend/drizzle/schema/postgres.js.map +1 -0
  23. package/dist/backend/drizzle/schema/sqlite.cjs +40 -0
  24. package/dist/backend/drizzle/schema/sqlite.cjs.map +1 -0
  25. package/dist/backend/drizzle/schema/sqlite.d.cts +2647 -0
  26. package/dist/backend/drizzle/schema/sqlite.d.ts +2647 -0
  27. package/dist/backend/drizzle/schema/sqlite.js +7 -0
  28. package/dist/backend/drizzle/schema/sqlite.js.map +1 -0
  29. package/dist/backend/drizzle/sqlite.cjs +27 -0
  30. package/dist/backend/drizzle/sqlite.cjs.map +1 -0
  31. package/dist/backend/drizzle/sqlite.d.cts +36 -0
  32. package/dist/backend/drizzle/sqlite.d.ts +36 -0
  33. package/dist/backend/drizzle/sqlite.js +10 -0
  34. package/dist/backend/drizzle/sqlite.js.map +1 -0
  35. package/dist/backend/postgres/index.cjs +53 -0
  36. package/dist/backend/postgres/index.cjs.map +1 -0
  37. package/dist/backend/postgres/index.d.cts +12 -0
  38. package/dist/backend/postgres/index.d.ts +12 -0
  39. package/dist/backend/postgres/index.js +12 -0
  40. package/dist/backend/postgres/index.js.map +1 -0
  41. package/dist/backend/sqlite/index.cjs +117 -0
  42. package/dist/backend/sqlite/index.cjs.map +1 -0
  43. package/dist/backend/sqlite/index.d.cts +71 -0
  44. package/dist/backend/sqlite/index.d.ts +71 -0
  45. package/dist/backend/sqlite/index.js +78 -0
  46. package/dist/backend/sqlite/index.js.map +1 -0
  47. package/dist/chunk-2QHQ2C4P.js +146 -0
  48. package/dist/chunk-2QHQ2C4P.js.map +1 -0
  49. package/dist/chunk-3A5TKOEJ.js +306 -0
  50. package/dist/chunk-3A5TKOEJ.js.map +1 -0
  51. package/dist/chunk-4PIEL2VO.js +162 -0
  52. package/dist/chunk-4PIEL2VO.js.map +1 -0
  53. package/dist/chunk-536PH5FT.js +342 -0
  54. package/dist/chunk-536PH5FT.js.map +1 -0
  55. package/dist/chunk-DBFCKELK.cjs +156 -0
  56. package/dist/chunk-DBFCKELK.cjs.map +1 -0
  57. package/dist/chunk-DDM2FZRJ.cjs +1143 -0
  58. package/dist/chunk-DDM2FZRJ.cjs.map +1 -0
  59. package/dist/chunk-DGUM43GV.js +10 -0
  60. package/dist/chunk-DGUM43GV.js.map +1 -0
  61. package/dist/chunk-F32HCHYA.cjs +680 -0
  62. package/dist/chunk-F32HCHYA.cjs.map +1 -0
  63. package/dist/chunk-IIAT36MI.js +353 -0
  64. package/dist/chunk-IIAT36MI.js.map +1 -0
  65. package/dist/chunk-JDAET5LO.js +236 -0
  66. package/dist/chunk-JDAET5LO.js.map +1 -0
  67. package/dist/chunk-JEQ2X3Z6.cjs +12 -0
  68. package/dist/chunk-JEQ2X3Z6.cjs.map +1 -0
  69. package/dist/chunk-JKTO7TW3.js +299 -0
  70. package/dist/chunk-JKTO7TW3.js.map +1 -0
  71. package/dist/chunk-K7SQ3SWP.js +497 -0
  72. package/dist/chunk-K7SQ3SWP.js.map +1 -0
  73. package/dist/chunk-L642L24T.js +142 -0
  74. package/dist/chunk-L642L24T.js.map +1 -0
  75. package/dist/chunk-MFVCSNIY.cjs +308 -0
  76. package/dist/chunk-MFVCSNIY.cjs.map +1 -0
  77. package/dist/chunk-MNO33ASC.cjs +240 -0
  78. package/dist/chunk-MNO33ASC.cjs.map +1 -0
  79. package/dist/chunk-N4AOJ3VF.cjs +154 -0
  80. package/dist/chunk-N4AOJ3VF.cjs.map +1 -0
  81. package/dist/chunk-P5CNM325.cjs +508 -0
  82. package/dist/chunk-P5CNM325.cjs.map +1 -0
  83. package/dist/chunk-RYT4H46I.js +646 -0
  84. package/dist/chunk-RYT4H46I.js.map +1 -0
  85. package/dist/chunk-SV5H3XM5.cjs +321 -0
  86. package/dist/chunk-SV5H3XM5.cjs.map +1 -0
  87. package/dist/chunk-TXHKFLWX.cjs +344 -0
  88. package/dist/chunk-TXHKFLWX.cjs.map +1 -0
  89. package/dist/chunk-UJAGXJDG.cjs +170 -0
  90. package/dist/chunk-UJAGXJDG.cjs.map +1 -0
  91. package/dist/chunk-VXRVGFCI.js +1128 -0
  92. package/dist/chunk-VXRVGFCI.js.map +1 -0
  93. package/dist/chunk-YM5AL65Y.cjs +357 -0
  94. package/dist/chunk-YM5AL65Y.cjs.map +1 -0
  95. package/dist/index.cjs +8334 -0
  96. package/dist/index.cjs.map +1 -0
  97. package/dist/index.d.cts +1365 -0
  98. package/dist/index.d.ts +1365 -0
  99. package/dist/index.js +8105 -0
  100. package/dist/index.js.map +1 -0
  101. package/dist/indexes/index.cjs +67 -0
  102. package/dist/indexes/index.cjs.map +1 -0
  103. package/dist/indexes/index.d.cts +62 -0
  104. package/dist/indexes/index.d.ts +62 -0
  105. package/dist/indexes/index.js +6 -0
  106. package/dist/indexes/index.js.map +1 -0
  107. package/dist/interchange/index.cjs +612 -0
  108. package/dist/interchange/index.cjs.map +1 -0
  109. package/dist/interchange/index.d.cts +288 -0
  110. package/dist/interchange/index.d.ts +288 -0
  111. package/dist/interchange/index.js +598 -0
  112. package/dist/interchange/index.js.map +1 -0
  113. package/dist/profiler/index.cjs +793 -0
  114. package/dist/profiler/index.cjs.map +1 -0
  115. package/dist/profiler/index.d.cts +283 -0
  116. package/dist/profiler/index.d.ts +283 -0
  117. package/dist/profiler/index.js +785 -0
  118. package/dist/profiler/index.js.map +1 -0
  119. package/dist/store-60Lcfi0w.d.ts +2263 -0
  120. package/dist/store-Bifii8MZ.d.cts +2263 -0
  121. package/dist/test-helpers-BjyRYJZX.d.ts +22 -0
  122. package/dist/test-helpers-NoQXhleQ.d.cts +22 -0
  123. package/dist/types-BRzHlhKC.d.cts +14 -0
  124. package/dist/types-BRzHlhKC.d.ts +14 -0
  125. package/dist/types-BrSfFSpW.d.cts +158 -0
  126. package/dist/types-CX4cLd7M.d.ts +152 -0
  127. package/dist/types-CjZ7g_7v.d.ts +442 -0
  128. package/dist/types-DDOSfrih.d.cts +442 -0
  129. package/dist/types-D_3mEv2y.d.ts +158 -0
  130. package/dist/types-a5rAxC92.d.cts +152 -0
  131. package/package.json +201 -0
@@ -0,0 +1,598 @@
1
+ import { getNodeTypeNames, getEdgeTypeNames, nowIso } from '../chunk-L642L24T.js';
2
+ import '../chunk-JKTO7TW3.js';
3
+ import '../chunk-DGUM43GV.js';
4
+ import { z } from 'zod';
5
+
6
+ var FORMAT_VERSION = "1.0";
7
+ var InterchangeNodeSchema = z.object({
8
+ kind: z.string().min(1),
9
+ id: z.string().min(1),
10
+ properties: z.record(z.string(), z.unknown()),
11
+ validFrom: z.iso.datetime().optional(),
12
+ validTo: z.iso.datetime().optional(),
13
+ meta: z.object({
14
+ version: z.number().int().positive().optional(),
15
+ createdAt: z.iso.datetime().optional(),
16
+ updatedAt: z.iso.datetime().optional()
17
+ }).optional()
18
+ });
19
+ var InterchangeEdgeSchema = z.object({
20
+ kind: z.string().min(1),
21
+ id: z.string().min(1),
22
+ from: z.object({
23
+ kind: z.string().min(1),
24
+ id: z.string().min(1)
25
+ }),
26
+ to: z.object({
27
+ kind: z.string().min(1),
28
+ id: z.string().min(1)
29
+ }),
30
+ properties: z.record(z.string(), z.unknown()).default({}),
31
+ validFrom: z.iso.datetime().optional(),
32
+ validTo: z.iso.datetime().optional(),
33
+ meta: z.object({
34
+ createdAt: z.iso.datetime().optional(),
35
+ updatedAt: z.iso.datetime().optional()
36
+ }).optional()
37
+ });
38
+ var ConflictStrategySchema = z.enum(["skip", "update", "error"]);
39
+ var UnknownPropertyStrategySchema = z.enum([
40
+ "error",
41
+ "strip",
42
+ "allow"
43
+ ]);
44
+ var ImportOptionsSchema = z.object({
45
+ /** How to handle existing nodes/edges with the same ID */
46
+ onConflict: ConflictStrategySchema,
47
+ /** How to handle properties not in the schema */
48
+ onUnknownProperty: UnknownPropertyStrategySchema.default("error"),
49
+ /** Whether to validate that edge endpoints exist */
50
+ validateReferences: z.boolean().default(true),
51
+ /** Number of items to process in each batch */
52
+ batchSize: z.number().int().positive().default(100)
53
+ });
54
+ var GraphDataSourceSchema = z.discriminatedUnion("type", [
55
+ z.object({
56
+ type: z.literal("typegraph-cloud"),
57
+ extractionId: z.string(),
58
+ schemaId: z.string(),
59
+ schemaVersion: z.number().int().positive().optional()
60
+ }),
61
+ z.object({
62
+ type: z.literal("typegraph-export"),
63
+ graphId: z.string(),
64
+ schemaVersion: z.number().int().positive()
65
+ }),
66
+ z.object({
67
+ type: z.literal("external"),
68
+ description: z.string().optional()
69
+ })
70
+ ]);
71
+ var GraphDataSchema = z.object({
72
+ formatVersion: z.literal(FORMAT_VERSION),
73
+ exportedAt: z.iso.datetime(),
74
+ source: GraphDataSourceSchema,
75
+ nodes: z.array(InterchangeNodeSchema),
76
+ edges: z.array(InterchangeEdgeSchema)
77
+ });
78
+ var ImportErrorSchema = z.object({
79
+ entityType: z.enum(["node", "edge"]),
80
+ kind: z.string(),
81
+ id: z.string(),
82
+ error: z.string()
83
+ });
84
+ var ImportResultSchema = z.object({
85
+ success: z.boolean(),
86
+ nodes: z.object({
87
+ created: z.number().int().nonnegative(),
88
+ updated: z.number().int().nonnegative(),
89
+ skipped: z.number().int().nonnegative()
90
+ }),
91
+ edges: z.object({
92
+ created: z.number().int().nonnegative(),
93
+ updated: z.number().int().nonnegative(),
94
+ skipped: z.number().int().nonnegative()
95
+ }),
96
+ errors: z.array(ImportErrorSchema)
97
+ });
98
+ var ExportOptionsSchema = z.object({
99
+ /** Filter to specific node kinds (undefined = all) */
100
+ nodeKinds: z.array(z.string()).optional(),
101
+ /** Filter to specific edge kinds (undefined = all) */
102
+ edgeKinds: z.array(z.string()).optional(),
103
+ /** Include temporal fields (validFrom, validTo) */
104
+ includeTemporal: z.boolean().default(false),
105
+ /** Include metadata (version, timestamps) */
106
+ includeMeta: z.boolean().default(false),
107
+ /** Include soft-deleted records */
108
+ includeDeleted: z.boolean().default(false)
109
+ });
110
+
111
+ // src/interchange/export.ts
112
+ async function exportGraph(store, options) {
113
+ const options_ = {
114
+ nodeKinds: options?.nodeKinds,
115
+ edgeKinds: options?.edgeKinds,
116
+ includeTemporal: options?.includeTemporal ?? false,
117
+ includeMeta: options?.includeMeta ?? false,
118
+ includeDeleted: options?.includeDeleted ?? false
119
+ };
120
+ const graph = store.graph;
121
+ const graphId = store.graphId;
122
+ const backend = store.backend;
123
+ const nodeKindsToExport = options_.nodeKinds ?? getNodeTypeNames(graph);
124
+ const edgeKindsToExport = options_.edgeKinds ?? getEdgeTypeNames(graph);
125
+ const nodes = [];
126
+ for (const kind of nodeKindsToExport) {
127
+ const kindNodes = await exportNodesOfKind(backend, graphId, kind, options_);
128
+ nodes.push(...kindNodes);
129
+ }
130
+ const edges = [];
131
+ for (const kind of edgeKindsToExport) {
132
+ const kindEdges = await exportEdgesOfKind(backend, graphId, kind, options_);
133
+ edges.push(...kindEdges);
134
+ }
135
+ const schemaVersion = await backend.getActiveSchema(graphId);
136
+ return {
137
+ formatVersion: FORMAT_VERSION,
138
+ exportedAt: nowIso(),
139
+ source: {
140
+ type: "typegraph-export",
141
+ graphId,
142
+ schemaVersion: schemaVersion?.version ?? 1
143
+ },
144
+ nodes,
145
+ edges
146
+ };
147
+ }
148
+ async function exportNodesOfKind(backend, graphId, kind, options) {
149
+ const rows = await backend.findNodesByKind({
150
+ graphId,
151
+ kind,
152
+ excludeDeleted: !options.includeDeleted
153
+ });
154
+ return rows.map((row) => {
155
+ const node = {
156
+ kind: row.kind,
157
+ id: row.id,
158
+ properties: JSON.parse(row.props)
159
+ };
160
+ if (options.includeTemporal) {
161
+ if (row.valid_from) {
162
+ node.validFrom = row.valid_from;
163
+ }
164
+ if (row.valid_to) {
165
+ node.validTo = row.valid_to;
166
+ }
167
+ }
168
+ if (options.includeMeta) {
169
+ node.meta = {
170
+ version: row.version,
171
+ createdAt: row.created_at,
172
+ updatedAt: row.updated_at
173
+ };
174
+ }
175
+ return node;
176
+ });
177
+ }
178
+ async function exportEdgesOfKind(backend, graphId, kind, options) {
179
+ const rows = await backend.findEdgesByKind({
180
+ graphId,
181
+ kind,
182
+ excludeDeleted: !options.includeDeleted
183
+ });
184
+ return rows.map((row) => {
185
+ const edge = {
186
+ kind: row.kind,
187
+ id: row.id,
188
+ from: {
189
+ kind: row.from_kind,
190
+ id: row.from_id
191
+ },
192
+ to: {
193
+ kind: row.to_kind,
194
+ id: row.to_id
195
+ },
196
+ properties: JSON.parse(row.props)
197
+ };
198
+ if (options.includeTemporal) {
199
+ if (row.valid_from) {
200
+ edge.validFrom = row.valid_from;
201
+ }
202
+ if (row.valid_to) {
203
+ edge.validTo = row.valid_to;
204
+ }
205
+ }
206
+ if (options.includeMeta) {
207
+ edge.meta = {
208
+ createdAt: row.created_at,
209
+ updatedAt: row.updated_at
210
+ };
211
+ }
212
+ return edge;
213
+ });
214
+ }
215
+
216
+ // src/interchange/import.ts
217
+ async function importGraph(store, data, options) {
218
+ const result = {
219
+ success: true,
220
+ nodes: { created: 0, updated: 0, skipped: 0 },
221
+ edges: { created: 0, updated: 0, skipped: 0 },
222
+ errors: []
223
+ };
224
+ const errors = [];
225
+ const graph = store.graph;
226
+ const graphId = store.graphId;
227
+ const backend = store.backend;
228
+ const nodeSchemas = buildNodeSchemaMap(graph);
229
+ const edgeSchemas = buildEdgeSchemaMap(graph);
230
+ const importedNodeIds = /* @__PURE__ */ new Set();
231
+ if (backend.capabilities.transactions) {
232
+ await backend.transaction(async (tx) => {
233
+ await processNodes(
234
+ tx,
235
+ graphId,
236
+ data.nodes,
237
+ nodeSchemas,
238
+ options,
239
+ result,
240
+ errors,
241
+ importedNodeIds
242
+ );
243
+ await processEdges(
244
+ tx,
245
+ graphId,
246
+ data.edges,
247
+ edgeSchemas,
248
+ nodeSchemas,
249
+ options,
250
+ result,
251
+ errors,
252
+ importedNodeIds
253
+ );
254
+ });
255
+ } else {
256
+ await processNodes(
257
+ backend,
258
+ graphId,
259
+ data.nodes,
260
+ nodeSchemas,
261
+ options,
262
+ result,
263
+ errors,
264
+ importedNodeIds
265
+ );
266
+ await processEdges(
267
+ backend,
268
+ graphId,
269
+ data.edges,
270
+ edgeSchemas,
271
+ nodeSchemas,
272
+ options,
273
+ result,
274
+ errors,
275
+ importedNodeIds
276
+ );
277
+ }
278
+ return {
279
+ ...result,
280
+ success: errors.length === 0,
281
+ errors
282
+ };
283
+ }
284
+ function buildNodeSchemaMap(graph) {
285
+ const map = /* @__PURE__ */ new Map();
286
+ for (const kindName of getNodeTypeNames(graph)) {
287
+ const registration = graph.nodes[kindName];
288
+ map.set(kindName, {
289
+ registration,
290
+ schema: registration.type.schema
291
+ });
292
+ }
293
+ return map;
294
+ }
295
+ function buildEdgeSchemaMap(graph) {
296
+ const map = /* @__PURE__ */ new Map();
297
+ for (const kindName of getEdgeTypeNames(graph)) {
298
+ const registration = graph.edges[kindName];
299
+ map.set(kindName, {
300
+ registration,
301
+ schema: registration.type.schema,
302
+ fromKinds: new Set(registration.from.map((node) => node.name)),
303
+ toKinds: new Set(registration.to.map((node) => node.name))
304
+ });
305
+ }
306
+ return map;
307
+ }
308
+ async function processNodes(backend, graphId, nodes, schemas, options, result, errors, importedNodeIds) {
309
+ const batchSize = options.batchSize;
310
+ for (let index = 0; index < nodes.length; index += batchSize) {
311
+ const batch = nodes.slice(index, index + batchSize);
312
+ for (const node of batch) {
313
+ const importResult = await processNode(
314
+ backend,
315
+ graphId,
316
+ node,
317
+ schemas,
318
+ options
319
+ );
320
+ switch (importResult.status) {
321
+ case "created": {
322
+ result.nodes.created++;
323
+ importedNodeIds.add(makeNodeKey(node.kind, node.id));
324
+ break;
325
+ }
326
+ case "updated": {
327
+ result.nodes.updated++;
328
+ importedNodeIds.add(makeNodeKey(node.kind, node.id));
329
+ break;
330
+ }
331
+ case "skipped": {
332
+ result.nodes.skipped++;
333
+ importedNodeIds.add(makeNodeKey(node.kind, node.id));
334
+ break;
335
+ }
336
+ case "error": {
337
+ errors.push({
338
+ entityType: "node",
339
+ kind: node.kind,
340
+ id: node.id,
341
+ error: importResult.error
342
+ });
343
+ break;
344
+ }
345
+ }
346
+ }
347
+ }
348
+ }
349
+ async function processNode(backend, graphId, node, schemas, options) {
350
+ const schemaEntry = schemas.get(node.kind);
351
+ if (!schemaEntry) {
352
+ return { status: "error", error: `Unknown node kind: ${node.kind}` };
353
+ }
354
+ const propsResult = validateProperties(
355
+ node.properties,
356
+ schemaEntry.schema,
357
+ options.onUnknownProperty
358
+ );
359
+ if (!propsResult.success) {
360
+ return { status: "error", error: propsResult.error };
361
+ }
362
+ const existing = await backend.getNode(graphId, node.kind, node.id);
363
+ if (existing) {
364
+ switch (options.onConflict) {
365
+ case "skip": {
366
+ return { status: "skipped" };
367
+ }
368
+ case "error": {
369
+ return {
370
+ status: "error",
371
+ error: `Node already exists: ${node.kind}:${node.id}`
372
+ };
373
+ }
374
+ case "update": {
375
+ await backend.updateNode({
376
+ graphId,
377
+ kind: node.kind,
378
+ id: node.id,
379
+ props: propsResult.data,
380
+ incrementVersion: true,
381
+ ...node.validTo !== void 0 && { validTo: node.validTo }
382
+ });
383
+ return { status: "updated" };
384
+ }
385
+ }
386
+ }
387
+ await backend.insertNode({
388
+ graphId,
389
+ kind: node.kind,
390
+ id: node.id,
391
+ props: propsResult.data,
392
+ ...node.validFrom !== void 0 && { validFrom: node.validFrom },
393
+ ...node.validTo !== void 0 && { validTo: node.validTo }
394
+ });
395
+ return { status: "created" };
396
+ }
397
+ async function processEdges(backend, graphId, edges, edgeSchemas, nodeSchemas, options, result, errors, importedNodeIds) {
398
+ const batchSize = options.batchSize;
399
+ for (let index = 0; index < edges.length; index += batchSize) {
400
+ const batch = edges.slice(index, index + batchSize);
401
+ for (const edge of batch) {
402
+ const importResult = await processEdge(
403
+ backend,
404
+ graphId,
405
+ edge,
406
+ edgeSchemas,
407
+ nodeSchemas,
408
+ options,
409
+ importedNodeIds
410
+ );
411
+ switch (importResult.status) {
412
+ case "created": {
413
+ result.edges.created++;
414
+ break;
415
+ }
416
+ case "updated": {
417
+ result.edges.updated++;
418
+ break;
419
+ }
420
+ case "skipped": {
421
+ result.edges.skipped++;
422
+ break;
423
+ }
424
+ case "error": {
425
+ errors.push({
426
+ entityType: "edge",
427
+ kind: edge.kind,
428
+ id: edge.id,
429
+ error: importResult.error
430
+ });
431
+ break;
432
+ }
433
+ }
434
+ }
435
+ }
436
+ }
437
+ async function processEdge(backend, graphId, edge, edgeSchemas, nodeSchemas, options, importedNodeIds) {
438
+ const schemaEntry = edgeSchemas.get(edge.kind);
439
+ if (!schemaEntry) {
440
+ return { status: "error", error: `Unknown edge kind: ${edge.kind}` };
441
+ }
442
+ if (!nodeSchemas.has(edge.from.kind)) {
443
+ return {
444
+ status: "error",
445
+ error: `Unknown from node kind: ${edge.from.kind}`
446
+ };
447
+ }
448
+ if (!nodeSchemas.has(edge.to.kind)) {
449
+ return { status: "error", error: `Unknown to node kind: ${edge.to.kind}` };
450
+ }
451
+ if (!schemaEntry.fromKinds.has(edge.from.kind)) {
452
+ return {
453
+ status: "error",
454
+ error: `Edge ${edge.kind} does not allow from kind: ${edge.from.kind}`
455
+ };
456
+ }
457
+ if (!schemaEntry.toKinds.has(edge.to.kind)) {
458
+ return {
459
+ status: "error",
460
+ error: `Edge ${edge.kind} does not allow to kind: ${edge.to.kind}`
461
+ };
462
+ }
463
+ if (options.validateReferences) {
464
+ const fromKey = makeNodeKey(edge.from.kind, edge.from.id);
465
+ const toKey = makeNodeKey(edge.to.kind, edge.to.id);
466
+ if (!importedNodeIds.has(fromKey)) {
467
+ const fromExists = await backend.getNode(
468
+ graphId,
469
+ edge.from.kind,
470
+ edge.from.id
471
+ );
472
+ if (!fromExists) {
473
+ return {
474
+ status: "error",
475
+ error: `From node not found: ${edge.from.kind}:${edge.from.id}`
476
+ };
477
+ }
478
+ }
479
+ if (!importedNodeIds.has(toKey)) {
480
+ const toExists = await backend.getNode(graphId, edge.to.kind, edge.to.id);
481
+ if (!toExists) {
482
+ return {
483
+ status: "error",
484
+ error: `To node not found: ${edge.to.kind}:${edge.to.id}`
485
+ };
486
+ }
487
+ }
488
+ }
489
+ const propsResult = validateProperties(
490
+ edge.properties,
491
+ schemaEntry.schema,
492
+ options.onUnknownProperty
493
+ );
494
+ if (!propsResult.success) {
495
+ return { status: "error", error: propsResult.error };
496
+ }
497
+ const existing = await backend.getEdge(graphId, edge.id);
498
+ if (existing) {
499
+ switch (options.onConflict) {
500
+ case "skip": {
501
+ return { status: "skipped" };
502
+ }
503
+ case "error": {
504
+ return { status: "error", error: `Edge already exists: ${edge.id}` };
505
+ }
506
+ case "update": {
507
+ await backend.updateEdge({
508
+ graphId,
509
+ id: edge.id,
510
+ props: propsResult.data,
511
+ ...edge.validTo !== void 0 && { validTo: edge.validTo }
512
+ });
513
+ return { status: "updated" };
514
+ }
515
+ }
516
+ }
517
+ await backend.insertEdge({
518
+ graphId,
519
+ id: edge.id,
520
+ kind: edge.kind,
521
+ fromKind: edge.from.kind,
522
+ fromId: edge.from.id,
523
+ toKind: edge.to.kind,
524
+ toId: edge.to.id,
525
+ props: propsResult.data,
526
+ ...edge.validFrom !== void 0 && { validFrom: edge.validFrom },
527
+ ...edge.validTo !== void 0 && { validTo: edge.validTo }
528
+ });
529
+ return { status: "created" };
530
+ }
531
+ function validateProperties(properties, schema, unknownStrategy) {
532
+ try {
533
+ const knownKeys = new Set(Object.keys(schema.shape));
534
+ const unknownKeys = Object.keys(properties).filter(
535
+ (key) => !knownKeys.has(key)
536
+ );
537
+ if (unknownKeys.length > 0) {
538
+ switch (unknownStrategy) {
539
+ case "error": {
540
+ return {
541
+ success: false,
542
+ error: `Unknown properties: ${unknownKeys.join(", ")}`
543
+ };
544
+ }
545
+ case "strip": {
546
+ const stripped = {};
547
+ for (const key of knownKeys) {
548
+ if (key in properties) {
549
+ stripped[key] = properties[key];
550
+ }
551
+ }
552
+ const result2 = schema.safeParse(stripped);
553
+ if (!result2.success) {
554
+ return {
555
+ success: false,
556
+ error: formatZodError(result2.error)
557
+ };
558
+ }
559
+ return {
560
+ success: true,
561
+ data: result2.data
562
+ };
563
+ }
564
+ case "allow": {
565
+ const result2 = schema.safeParse(properties);
566
+ if (!result2.success) {
567
+ return {
568
+ success: false,
569
+ error: formatZodError(result2.error)
570
+ };
571
+ }
572
+ return { success: true, data: properties };
573
+ }
574
+ }
575
+ }
576
+ const result = schema.safeParse(properties);
577
+ if (!result.success) {
578
+ return { success: false, error: formatZodError(result.error) };
579
+ }
580
+ return { success: true, data: result.data };
581
+ } catch (error) {
582
+ const message = error instanceof Error ? error.message : String(error);
583
+ return { success: false, error: `Validation error: ${message}` };
584
+ }
585
+ }
586
+ function formatZodError(error) {
587
+ return error.issues.map((issue) => {
588
+ const path = issue.path.join(".");
589
+ return path ? `${path}: ${issue.message}` : issue.message;
590
+ }).join("; ");
591
+ }
592
+ function makeNodeKey(kind, id) {
593
+ return `${kind}:${id}`;
594
+ }
595
+
596
+ export { ConflictStrategySchema, ExportOptionsSchema, FORMAT_VERSION, GraphDataSchema, GraphDataSourceSchema, ImportErrorSchema, ImportOptionsSchema, ImportResultSchema, InterchangeEdgeSchema, InterchangeNodeSchema, UnknownPropertyStrategySchema, exportGraph, importGraph };
597
+ //# sourceMappingURL=index.js.map
598
+ //# sourceMappingURL=index.js.map