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