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