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