@nicia-ai/typegraph 0.28.1 → 0.29.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 (172) hide show
  1. package/dist/backend/postgres/index.cjs +23 -761
  2. package/dist/backend/postgres/index.cjs.map +1 -1
  3. package/dist/backend/postgres/index.d.cts +24 -5
  4. package/dist/backend/postgres/index.d.ts +24 -5
  5. package/dist/backend/postgres/index.js +10 -752
  6. package/dist/backend/postgres/index.js.map +1 -1
  7. package/dist/backend/postgres/pglite.cjs +52 -0
  8. package/dist/backend/postgres/pglite.cjs.map +1 -0
  9. package/dist/backend/postgres/pglite.d.cts +116 -0
  10. package/dist/backend/postgres/pglite.d.ts +116 -0
  11. package/dist/backend/postgres/pglite.js +50 -0
  12. package/dist/backend/postgres/pglite.js.map +1 -0
  13. package/dist/backend/sqlite/index.cjs +22 -26
  14. package/dist/backend/sqlite/index.d.cts +17 -15
  15. package/dist/backend/sqlite/index.d.ts +17 -15
  16. package/dist/backend/sqlite/index.js +9 -9
  17. package/dist/backend/sqlite/libsql.cjs +17 -13
  18. package/dist/backend/sqlite/libsql.cjs.map +1 -1
  19. package/dist/backend/sqlite/libsql.d.cts +3 -3
  20. package/dist/backend/sqlite/libsql.d.ts +3 -3
  21. package/dist/backend/sqlite/libsql.js +16 -12
  22. package/dist/backend/sqlite/libsql.js.map +1 -1
  23. package/dist/backend/sqlite/local.cjs +17 -16
  24. package/dist/backend/sqlite/local.cjs.map +1 -1
  25. package/dist/backend/sqlite/local.d.cts +3 -3
  26. package/dist/backend/sqlite/local.d.ts +3 -3
  27. package/dist/backend/sqlite/local.js +15 -14
  28. package/dist/backend/sqlite/local.js.map +1 -1
  29. package/dist/{chunk-TKM2WDT4.js → chunk-3DTLDGIM.js} +386 -1093
  30. package/dist/chunk-3DTLDGIM.js.map +1 -0
  31. package/dist/{chunk-WVQXAFP3.js → chunk-454I6GDF.js} +46 -5
  32. package/dist/chunk-454I6GDF.js.map +1 -0
  33. package/dist/chunk-4K6IHUW6.cjs +183 -0
  34. package/dist/chunk-4K6IHUW6.cjs.map +1 -0
  35. package/dist/{chunk-ZTREJOGB.js → chunk-7KLVFWXR.js} +113 -68
  36. package/dist/chunk-7KLVFWXR.js.map +1 -0
  37. package/dist/{chunk-PGU2XKMA.js → chunk-A4H6QMV4.js} +110 -102
  38. package/dist/chunk-A4H6QMV4.js.map +1 -0
  39. package/dist/{chunk-E5ROA3MN.js → chunk-BMDF2S7S.js} +3 -3
  40. package/dist/{chunk-E5ROA3MN.js.map → chunk-BMDF2S7S.js.map} +1 -1
  41. package/dist/{chunk-PN37HVAV.cjs → chunk-C2RWFIYP.cjs} +121 -67
  42. package/dist/chunk-C2RWFIYP.cjs.map +1 -0
  43. package/dist/chunk-CUZGBWFT.js +206 -0
  44. package/dist/chunk-CUZGBWFT.js.map +1 -0
  45. package/dist/chunk-F2Q76CQ4.cjs +266 -0
  46. package/dist/chunk-F2Q76CQ4.cjs.map +1 -0
  47. package/dist/{chunk-YEDQLNVE.cjs → chunk-G6P3OVDY.cjs} +14 -2
  48. package/dist/chunk-G6P3OVDY.cjs.map +1 -0
  49. package/dist/chunk-GPUNAJFH.js +181 -0
  50. package/dist/chunk-GPUNAJFH.js.map +1 -0
  51. package/dist/chunk-HKN622O4.js +254 -0
  52. package/dist/chunk-HKN622O4.js.map +1 -0
  53. package/dist/{chunk-PYJOBZR3.cjs → chunk-HM6XMR3E.cjs} +52 -52
  54. package/dist/chunk-HM6XMR3E.cjs.map +1 -0
  55. package/dist/chunk-IE2TPFBT.js +305 -0
  56. package/dist/chunk-IE2TPFBT.js.map +1 -0
  57. package/dist/chunk-KGAJ5HFO.cjs +319 -0
  58. package/dist/chunk-KGAJ5HFO.cjs.map +1 -0
  59. package/dist/{chunk-O57GYGYK.js → chunk-M6OC455F.js} +32 -18
  60. package/dist/chunk-M6OC455F.js.map +1 -0
  61. package/dist/{chunk-U5ROKVRF.js → chunk-MB363HT5.js} +14 -3
  62. package/dist/chunk-MB363HT5.js.map +1 -0
  63. package/dist/{chunk-5MTOKWDA.js → chunk-MRMD7S7E.js} +9 -9
  64. package/dist/{chunk-5MTOKWDA.js.map → chunk-MRMD7S7E.js.map} +1 -1
  65. package/dist/{chunk-OOS2RKRH.js → chunk-N3VVQDMA.js} +3 -3
  66. package/dist/{chunk-OOS2RKRH.js.map → chunk-N3VVQDMA.js.map} +1 -1
  67. package/dist/{chunk-EJCDOA37.js → chunk-OGUH56BB.js} +4 -4
  68. package/dist/{chunk-EJCDOA37.js.map → chunk-OGUH56BB.js.map} +1 -1
  69. package/dist/chunk-Q3ZZLU3O.cjs +12 -0
  70. package/dist/{chunk-4FJVSF3K.cjs.map → chunk-Q3ZZLU3O.cjs.map} +1 -1
  71. package/dist/chunk-Q5ASTPM4.cjs +208 -0
  72. package/dist/chunk-Q5ASTPM4.cjs.map +1 -0
  73. package/dist/{chunk-WFUN6DCM.cjs → chunk-R2LA6MKR.cjs} +11 -11
  74. package/dist/chunk-R2LA6MKR.cjs.map +1 -0
  75. package/dist/{chunk-FFHW2JEK.cjs → chunk-RP7YITHX.cjs} +32 -17
  76. package/dist/chunk-RP7YITHX.cjs.map +1 -0
  77. package/dist/{chunk-NMOE2ZEU.cjs → chunk-RU5BBUU2.cjs} +132 -124
  78. package/dist/chunk-RU5BBUU2.cjs.map +1 -0
  79. package/dist/{chunk-TSGBRPNE.js → chunk-SFNUHFON.js} +45 -45
  80. package/dist/chunk-SFNUHFON.js.map +1 -0
  81. package/dist/chunk-UP4QAJND.cjs +740 -0
  82. package/dist/chunk-UP4QAJND.cjs.map +1 -0
  83. package/dist/{chunk-CTHNVU6K.cjs → chunk-UPBXUOW7.cjs} +8 -8
  84. package/dist/{chunk-CTHNVU6K.cjs.map → chunk-UPBXUOW7.cjs.map} +1 -1
  85. package/dist/{chunk-EK6RJ373.cjs → chunk-VGU26WIU.cjs} +5 -5
  86. package/dist/{chunk-EK6RJ373.cjs.map → chunk-VGU26WIU.cjs.map} +1 -1
  87. package/dist/{chunk-WZH2MKHW.cjs → chunk-XTQHSOIC.cjs} +101 -101
  88. package/dist/{chunk-WZH2MKHW.cjs.map → chunk-XTQHSOIC.cjs.map} +1 -1
  89. package/dist/chunk-Y52UNT66.js +738 -0
  90. package/dist/chunk-Y52UNT66.js.map +1 -0
  91. package/dist/{chunk-OYLF3IC7.js → chunk-Z36W5WUM.js} +4 -4
  92. package/dist/chunk-Z36W5WUM.js.map +1 -0
  93. package/dist/{chunk-X3244EKP.cjs → chunk-ZFWNO3GE.cjs} +48 -4
  94. package/dist/chunk-ZFWNO3GE.cjs.map +1 -0
  95. package/dist/{chunk-NYQFPGYP.cjs → chunk-ZYQEXGYC.cjs} +423 -1139
  96. package/dist/chunk-ZYQEXGYC.cjs.map +1 -0
  97. package/dist/ddl-BwH00BB6.d.ts +38 -0
  98. package/dist/ddl-D3o-RmsV.d.cts +38 -0
  99. package/dist/{extension-types-B78CZfA5.d.cts → extension-types-CwHV-Uk4.d.cts} +4 -4
  100. package/dist/{extension-types-B78CZfA5.d.ts → extension-types-CwHV-Uk4.d.ts} +4 -4
  101. package/dist/graph-extension/index.cjs +18 -18
  102. package/dist/graph-extension/index.d.cts +4 -4
  103. package/dist/graph-extension/index.d.ts +4 -4
  104. package/dist/graph-extension/index.js +3 -3
  105. package/dist/{index-CWdBFWbI.d.ts → index-CapHwi1a.d.ts} +22 -3
  106. package/dist/{index-Bilw0pYm.d.cts → index-jPPexxM5.d.cts} +22 -3
  107. package/dist/index.cjs +1211 -945
  108. package/dist/index.cjs.map +1 -1
  109. package/dist/index.d.cts +142 -12
  110. package/dist/index.d.ts +142 -12
  111. package/dist/index.js +778 -544
  112. package/dist/index.js.map +1 -1
  113. package/dist/indexes/index.cjs +18 -18
  114. package/dist/indexes/index.d.cts +9 -9
  115. package/dist/indexes/index.d.ts +9 -9
  116. package/dist/indexes/index.js +4 -4
  117. package/dist/interchange/index.cjs +8 -14
  118. package/dist/interchange/index.cjs.map +1 -1
  119. package/dist/interchange/index.d.cts +5 -16
  120. package/dist/interchange/index.d.ts +5 -16
  121. package/dist/interchange/index.js +3 -9
  122. package/dist/interchange/index.js.map +1 -1
  123. package/dist/{manager-ehLTTNq0.d.ts → manager-BUugwCYz.d.ts} +2 -2
  124. package/dist/{manager-c1MGaVVR.d.cts → manager-DpIpn1HQ.d.cts} +2 -2
  125. package/dist/{materialize-indexes-DFavFQah.d.ts → materialize-indexes-lkawaDor.d.ts} +1 -1
  126. package/dist/{materialize-indexes-BQfaAZKA.d.cts → materialize-indexes-oanE3RU0.d.cts} +1 -1
  127. package/dist/{ddl-0Ooc2dpL.d.ts → postgres-CQL9UIn2.d.cts} +2 -471
  128. package/dist/{ddl-Dza3kDrI.d.cts → postgres-CcRLaxz0.d.ts} +2 -471
  129. package/dist/profiler/index.cjs +4 -4
  130. package/dist/profiler/index.d.cts +7 -7
  131. package/dist/profiler/index.d.ts +7 -7
  132. package/dist/profiler/index.js +2 -2
  133. package/dist/schema/index.cjs +32 -32
  134. package/dist/schema/index.d.cts +7 -7
  135. package/dist/schema/index.d.ts +7 -7
  136. package/dist/schema/index.js +9 -9
  137. package/dist/{sqlite-BxZUPbyD.d.ts → sqlite-CZDFUfHS.d.ts} +2 -468
  138. package/dist/{sqlite-mvpXjEeB.d.cts → sqlite-Rj3xu4It.d.cts} +2 -468
  139. package/dist/{store-D7YEMg2v.d.cts → store-B43FpOVX.d.cts} +84 -5
  140. package/dist/{store-Ufc1Eoey.d.ts → store-De0Y33wP.d.ts} +84 -5
  141. package/dist/{types-B-sJJ4GQ.d.ts → types-B5GSeE3t.d.ts} +2 -2
  142. package/dist/{types-CmUA9sal.d.cts → types-Bwlp_yvu.d.cts} +2 -2
  143. package/dist/{types-CMQymcwk.d.cts → types-CmAT-Z47.d.ts} +250 -46
  144. package/dist/{types-CQ7xXJMP.d.ts → types-DXCSyxac.d.cts} +250 -46
  145. package/dist/{where-5zf15aFZ.d.cts → where-2dsyYt3d.d.cts} +2 -2
  146. package/dist/{where-CtrLu1Zf.d.ts → where-CP4qGIHd.d.ts} +2 -2
  147. package/package.json +20 -5
  148. package/dist/chunk-3EUKJEQ3.js +0 -77
  149. package/dist/chunk-3EUKJEQ3.js.map +0 -1
  150. package/dist/chunk-4FJVSF3K.cjs +0 -12
  151. package/dist/chunk-FFHW2JEK.cjs.map +0 -1
  152. package/dist/chunk-HTGDWIDE.cjs +0 -89
  153. package/dist/chunk-HTGDWIDE.cjs.map +0 -1
  154. package/dist/chunk-LYRUNG6E.cjs +0 -52
  155. package/dist/chunk-LYRUNG6E.cjs.map +0 -1
  156. package/dist/chunk-NMOE2ZEU.cjs.map +0 -1
  157. package/dist/chunk-NYQFPGYP.cjs.map +0 -1
  158. package/dist/chunk-O57GYGYK.js.map +0 -1
  159. package/dist/chunk-OYLF3IC7.js.map +0 -1
  160. package/dist/chunk-PCHTTG42.js +0 -47
  161. package/dist/chunk-PCHTTG42.js.map +0 -1
  162. package/dist/chunk-PGU2XKMA.js.map +0 -1
  163. package/dist/chunk-PN37HVAV.cjs.map +0 -1
  164. package/dist/chunk-PYJOBZR3.cjs.map +0 -1
  165. package/dist/chunk-TKM2WDT4.js.map +0 -1
  166. package/dist/chunk-TSGBRPNE.js.map +0 -1
  167. package/dist/chunk-U5ROKVRF.js.map +0 -1
  168. package/dist/chunk-WFUN6DCM.cjs.map +0 -1
  169. package/dist/chunk-WVQXAFP3.js.map +0 -1
  170. package/dist/chunk-X3244EKP.cjs.map +0 -1
  171. package/dist/chunk-YEDQLNVE.cjs.map +0 -1
  172. package/dist/chunk-ZTREJOGB.js.map +0 -1
@@ -0,0 +1,740 @@
1
+ 'use strict';
2
+
3
+ var chunkKGAJ5HFO_cjs = require('./chunk-KGAJ5HFO.cjs');
4
+ var chunkZYQEXGYC_cjs = require('./chunk-ZYQEXGYC.cjs');
5
+ var chunkF2Q76CQ4_cjs = require('./chunk-F2Q76CQ4.cjs');
6
+ var chunkRP7YITHX_cjs = require('./chunk-RP7YITHX.cjs');
7
+ var chunkC2RWFIYP_cjs = require('./chunk-C2RWFIYP.cjs');
8
+ var chunkG6P3OVDY_cjs = require('./chunk-G6P3OVDY.cjs');
9
+ var drizzleOrm = require('drizzle-orm');
10
+ var pgCore = require('drizzle-orm/pg-core');
11
+
12
+ // src/backend/drizzle/execution/postgres-execution.ts
13
+ function hasFunctionProperty(value, property) {
14
+ if (value === void 0 || value === null) {
15
+ return false;
16
+ }
17
+ const candidate = value;
18
+ return typeof candidate[property] === "function";
19
+ }
20
+ function isPgNativeClient(candidate) {
21
+ return typeof candidate === "object" && hasFunctionProperty(candidate, "query");
22
+ }
23
+ function isPgliteClient(candidate) {
24
+ return typeof candidate === "object" && hasFunctionProperty(candidate, "query") && hasFunctionProperty(candidate, "dumpDataDir");
25
+ }
26
+ function isPostgresJsClient(candidate) {
27
+ return typeof candidate === "function" && hasFunctionProperty(candidate, "unsafe") && hasFunctionProperty(candidate, "begin");
28
+ }
29
+ function isNeonHttpClient(db) {
30
+ const candidate = db.$client;
31
+ return typeof candidate === "function" && hasFunctionProperty(candidate, "transaction") && !hasFunctionProperty(candidate, "begin");
32
+ }
33
+ var DEFAULT_POSTGRES_STATEMENT_CACHE_MAX = 256;
34
+ var STATEMENT_NAME_CACHE = /* @__PURE__ */ new Map();
35
+ var nextStatementId = 0;
36
+ function statementNameFor(sqlText, cacheMax) {
37
+ return chunkZYQEXGYC_cjs.getOrCreateLru(STATEMENT_NAME_CACHE, sqlText, cacheMax, () => {
38
+ const name = `tg_${nextStatementId}`;
39
+ nextStatementId += 1;
40
+ return name;
41
+ });
42
+ }
43
+ function normalizeRow(row) {
44
+ if (row === null || typeof row !== "object") return row;
45
+ let mutated;
46
+ for (const key of Object.keys(row)) {
47
+ const value = row[key];
48
+ if (value instanceof Date) {
49
+ mutated ??= { ...row };
50
+ mutated[key] = value.toISOString();
51
+ }
52
+ }
53
+ return mutated ?? row;
54
+ }
55
+ function normalizeRows(rows) {
56
+ let mutated;
57
+ for (let index = 0; index < rows.length; index += 1) {
58
+ const original = rows[index];
59
+ const normalized = normalizeRow(original);
60
+ if (normalized !== original) {
61
+ mutated ??= [...rows];
62
+ mutated[index] = normalized;
63
+ }
64
+ }
65
+ return mutated ?? rows;
66
+ }
67
+ function wrapNodePgClient(client, cacheMax) {
68
+ return {
69
+ async query(sqlText, params) {
70
+ const result = await client.query({
71
+ name: statementNameFor(sqlText, cacheMax),
72
+ text: sqlText,
73
+ values: params
74
+ });
75
+ return { rows: normalizeRows(result.rows) };
76
+ }
77
+ };
78
+ }
79
+ function wrapNodePgClientUnnamed(client) {
80
+ return {
81
+ async query(sqlText, params) {
82
+ const result = await client.query(sqlText, params);
83
+ return { rows: normalizeRows(result.rows) };
84
+ }
85
+ };
86
+ }
87
+ function adaptPostgresJsClient(sql2) {
88
+ return {
89
+ async query(sqlText, params) {
90
+ const rows = await sql2.unsafe(sqlText, params);
91
+ return { rows };
92
+ }
93
+ };
94
+ }
95
+ function resolvePgClient(db, prepareStatements, cacheMax) {
96
+ if (isNeonHttpClient(db)) {
97
+ return void 0;
98
+ }
99
+ const client = db.$client;
100
+ if (isPgliteClient(client)) {
101
+ return wrapNodePgClientUnnamed(client);
102
+ }
103
+ if (isPgNativeClient(client)) {
104
+ return prepareStatements ? wrapNodePgClient(client, cacheMax) : wrapNodePgClientUnnamed(client);
105
+ }
106
+ if (isPostgresJsClient(client)) {
107
+ return adaptPostgresJsClient(client);
108
+ }
109
+ return void 0;
110
+ }
111
+ async function executeDrizzleQuery(db, query) {
112
+ const result = await db.execute(query);
113
+ if (Array.isArray(result)) {
114
+ return result;
115
+ }
116
+ return result.rows;
117
+ }
118
+ function createPgPreparedStatement(pgClient, sqlText) {
119
+ return {
120
+ async execute(params) {
121
+ const result = await pgClient.query(sqlText, params);
122
+ return result.rows;
123
+ }
124
+ };
125
+ }
126
+ function createPostgresExecutionAdapter(db, options = {}) {
127
+ const prepareStatements = options.prepareStatements ?? true;
128
+ const cacheMax = options.preparedStatementCacheMax ?? DEFAULT_POSTGRES_STATEMENT_CACHE_MAX;
129
+ const pgClient = resolvePgClient(db, prepareStatements, cacheMax);
130
+ function compile(query) {
131
+ return chunkZYQEXGYC_cjs.compileQueryWithDialect(db, query, "PostgreSQL");
132
+ }
133
+ if (pgClient === void 0) {
134
+ return {
135
+ compile,
136
+ async execute(query) {
137
+ return executeDrizzleQuery(db, query);
138
+ }
139
+ };
140
+ }
141
+ const pgQueryClient = pgClient;
142
+ async function executeCompiled(compiledQuery) {
143
+ const result = await pgQueryClient.query(
144
+ compiledQuery.sql,
145
+ compiledQuery.params
146
+ );
147
+ return result.rows;
148
+ }
149
+ return {
150
+ compile,
151
+ async execute(query) {
152
+ const compiled = compile(query);
153
+ return executeCompiled(compiled);
154
+ },
155
+ executeCompiled,
156
+ prepare(sqlText) {
157
+ return createPgPreparedStatement(pgQueryClient, sqlText);
158
+ }
159
+ };
160
+ }
161
+
162
+ // src/backend/drizzle/postgres.ts
163
+ var POSTGRES_MAX_BIND_PARAMETERS = 65535;
164
+ var NODE_INSERT_PARAM_COUNT = 9;
165
+ var EDGE_INSERT_PARAM_COUNT = 12;
166
+ var POSTGRES_NODE_INSERT_BATCH_SIZE = Math.max(
167
+ 1,
168
+ Math.floor(POSTGRES_MAX_BIND_PARAMETERS / NODE_INSERT_PARAM_COUNT)
169
+ );
170
+ var POSTGRES_EDGE_INSERT_BATCH_SIZE = Math.max(
171
+ 1,
172
+ Math.floor(POSTGRES_MAX_BIND_PARAMETERS / EDGE_INSERT_PARAM_COUNT)
173
+ );
174
+ var POSTGRES_GET_NODES_ID_CHUNK_SIZE = Math.max(
175
+ 1,
176
+ POSTGRES_MAX_BIND_PARAMETERS - 2
177
+ );
178
+ var POSTGRES_GET_EDGES_ID_CHUNK_SIZE = Math.max(
179
+ 1,
180
+ POSTGRES_MAX_BIND_PARAMETERS - 1
181
+ );
182
+ var CHECK_UNIQUE_BATCH_FIXED_PARAM_COUNT = 3;
183
+ var POSTGRES_CHECK_UNIQUE_BATCH_CHUNK_SIZE = Math.max(
184
+ 1,
185
+ POSTGRES_MAX_BIND_PARAMETERS - CHECK_UNIQUE_BATCH_FIXED_PARAM_COUNT
186
+ );
187
+ var toNodeRow = chunkZYQEXGYC_cjs.createNodeRowMapper(chunkZYQEXGYC_cjs.POSTGRES_ROW_MAPPER_CONFIG);
188
+ var toEdgeRow = chunkZYQEXGYC_cjs.createEdgeRowMapper(chunkZYQEXGYC_cjs.POSTGRES_ROW_MAPPER_CONFIG);
189
+ var toUniqueRow = chunkZYQEXGYC_cjs.createUniqueRowMapper(chunkZYQEXGYC_cjs.POSTGRES_ROW_MAPPER_CONFIG);
190
+ var toSchemaVersionRow = chunkZYQEXGYC_cjs.createSchemaVersionRowMapper(
191
+ chunkZYQEXGYC_cjs.POSTGRES_ROW_MAPPER_CONFIG
192
+ );
193
+ function buildPostgresCapabilities(fulltextStrategy, vectorStrategy) {
194
+ return {
195
+ ...chunkF2Q76CQ4_cjs.POSTGRES_CAPABILITIES,
196
+ ...vectorStrategy === void 0 ? {} : { vector: chunkC2RWFIYP_cjs.buildVectorCapabilities(vectorStrategy) },
197
+ fulltext: chunkC2RWFIYP_cjs.buildFulltextCapabilities(fulltextStrategy)
198
+ };
199
+ }
200
+ function createPostgresBackend(db, options = {}) {
201
+ const tables2 = options.tables ?? chunkZYQEXGYC_cjs.tables;
202
+ const fulltextStrategy = options.fulltext ?? chunkC2RWFIYP_cjs.tsvectorStrategy;
203
+ const vectorStrategy = options.vector === false ? void 0 : options.vector ?? chunkKGAJ5HFO_cjs.pgvectorStrategy;
204
+ const vectorSlotLatch = vectorStrategy === void 0 ? void 0 : chunkZYQEXGYC_cjs.createVectorSlotLatch();
205
+ const baseCapabilities = buildPostgresCapabilities(
206
+ fulltextStrategy,
207
+ vectorStrategy
208
+ );
209
+ const httpOnlyOverrides = isNeonHttpClient(db) ? { transactions: false } : {};
210
+ const capabilities = {
211
+ ...baseCapabilities,
212
+ ...httpOnlyOverrides,
213
+ ...options.capabilities
214
+ };
215
+ const adapterOptions = {
216
+ ...options.prepareStatements === void 0 ? {} : { prepareStatements: options.prepareStatements },
217
+ ...options.preparedStatementCacheMax === void 0 ? {} : { preparedStatementCacheMax: options.preparedStatementCacheMax }
218
+ };
219
+ const executionAdapter = createPostgresExecutionAdapter(db, adapterOptions);
220
+ const tableNames = {
221
+ nodes: drizzleOrm.getTableName(tables2.nodes),
222
+ edges: drizzleOrm.getTableName(tables2.edges),
223
+ fulltext: tables2.fulltextTableName,
224
+ uniques: drizzleOrm.getTableName(tables2.uniques)
225
+ };
226
+ const analyzeStatement = drizzleOrm.sql`ANALYZE ${drizzleOrm.sql.join(
227
+ [
228
+ tableNames.nodes,
229
+ tableNames.edges,
230
+ drizzleOrm.getTableName(tables2.uniques),
231
+ tableNames.fulltext
232
+ ].map((name) => chunkKGAJ5HFO_cjs.quoteIdentifier(name)),
233
+ drizzleOrm.sql`, `
234
+ )}`;
235
+ const operationStrategy = chunkZYQEXGYC_cjs.createPostgresOperationStrategy(
236
+ tables2,
237
+ fulltextStrategy
238
+ );
239
+ const operations = createPostgresOperationBackend({
240
+ db,
241
+ executionAdapter,
242
+ adapterOptions,
243
+ operationStrategy,
244
+ tableNames,
245
+ capabilities,
246
+ fulltextStrategy,
247
+ vectorStrategy,
248
+ vectorSlotLatch
249
+ });
250
+ function runSchemaWriteTransaction(graphId, fn) {
251
+ if (!capabilities.transactions) {
252
+ throw new chunkG6P3OVDY_cjs.ConfigurationError(
253
+ "commitSchemaVersion and setActiveVersion require atomic transactions, but this Postgres backend does not provide them. The drizzle-orm/neon-http driver communicates over HTTP and cannot hold a session across statements; use drizzle-orm/neon-serverless (websocket) for transactional writes.",
254
+ {
255
+ backend: "postgres",
256
+ capability: "transactions",
257
+ supportsTransactions: false
258
+ }
259
+ );
260
+ }
261
+ return db.transaction(async (tx) => {
262
+ await tx.execute(drizzleOrm.sql`SELECT pg_advisory_xact_lock(hashtext(${graphId}))`);
263
+ const txBackend = createTransactionBackend({
264
+ db: tx,
265
+ adapterOptions,
266
+ operationStrategy,
267
+ tableNames,
268
+ capabilities,
269
+ fulltextStrategy,
270
+ vectorStrategy});
271
+ return fn(txBackend);
272
+ });
273
+ }
274
+ const matTable = tables2.contributionMaterializations;
275
+ async function ensureContributionMaterializationsTableImpl() {
276
+ await db.execute(drizzleOrm.sql.raw(chunkZYQEXGYC_cjs.generatePgCreateTableSQL(matTable)));
277
+ }
278
+ async function getContributionMaterializationRow(identity) {
279
+ const rows = await db.select().from(matTable).where(
280
+ drizzleOrm.and(
281
+ drizzleOrm.eq(matTable.graphId, identity.graphId),
282
+ drizzleOrm.eq(matTable.logicalName, identity.logicalName),
283
+ drizzleOrm.eq(matTable.owner, identity.owner),
284
+ drizzleOrm.eq(matTable.tableName, identity.tableName)
285
+ )
286
+ );
287
+ const row = rows[0];
288
+ if (row === void 0) return void 0;
289
+ return chunkZYQEXGYC_cjs.mapContributionMaterializationRow(
290
+ row,
291
+ chunkZYQEXGYC_cjs.POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS.decode
292
+ );
293
+ }
294
+ async function recordContributionMaterializationRow(params) {
295
+ await db.insert(matTable).values(
296
+ chunkZYQEXGYC_cjs.buildContributionInsertValues(
297
+ params,
298
+ chunkZYQEXGYC_cjs.POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS.encode
299
+ )
300
+ ).onConflictDoUpdate({
301
+ target: [
302
+ matTable.graphId,
303
+ matTable.logicalName,
304
+ matTable.owner,
305
+ matTable.tableName
306
+ ],
307
+ set: chunkZYQEXGYC_cjs.buildContributionOnConflictSet(
308
+ matTable.materializedAt,
309
+ params.materializedAt
310
+ )
311
+ });
312
+ }
313
+ const contributionMaterializer = chunkZYQEXGYC_cjs.createContributionMaterializer({
314
+ dialect: "postgres",
315
+ fulltextStrategy,
316
+ fulltextTableName: tables2.fulltextTableName,
317
+ execDdl: async (statement) => {
318
+ await db.execute(drizzleOrm.sql.raw(statement));
319
+ },
320
+ ensureMarkerTable: ensureContributionMaterializationsTableImpl,
321
+ getMarker: getContributionMaterializationRow,
322
+ recordMarker: recordContributionMaterializationRow
323
+ });
324
+ function bindTransactionBackend(tx) {
325
+ const txBackend = createTransactionBackend({
326
+ db: tx,
327
+ adapterOptions,
328
+ operationStrategy,
329
+ tableNames,
330
+ capabilities,
331
+ fulltextStrategy,
332
+ vectorStrategy});
333
+ return chunkZYQEXGYC_cjs.gateFulltext(txBackend, contributionMaterializer.assertInitialized);
334
+ }
335
+ const backend = {
336
+ ...operations,
337
+ async bootstrapTables() {
338
+ const statements = chunkZYQEXGYC_cjs.generatePostgresDDL(tables2, fulltextStrategy);
339
+ for (const statement of statements) {
340
+ await db.execute(drizzleOrm.sql.raw(statement));
341
+ }
342
+ },
343
+ // Every fulltext-touching method asserts the durable marker instead
344
+ // of lazily emitting DDL. Steady state performs zero ensure; an
345
+ // uninitialized database throws `StoreNotInitializedError` loudly
346
+ // rather than self-healing on a read/write path (#135). Shared
347
+ // verbatim with the tx-scoped gate via `gateFulltextMethods`.
348
+ ...chunkZYQEXGYC_cjs.gateFulltextMethods(
349
+ operations,
350
+ contributionMaterializer.assertInitialized
351
+ ),
352
+ async executeDdl(ddl) {
353
+ await db.execute(drizzleOrm.sql.raw(ddl));
354
+ },
355
+ async ensureIndexMaterializationsTable() {
356
+ await db.execute(
357
+ drizzleOrm.sql.raw(chunkZYQEXGYC_cjs.generatePgCreateTableSQL(tables2.indexMaterializations))
358
+ );
359
+ },
360
+ async getIndexMaterialization(indexName) {
361
+ const t = tables2.indexMaterializations;
362
+ const rows = await db.select().from(t).where(drizzleOrm.eq(t.indexName, indexName));
363
+ const row = rows[0];
364
+ if (row === void 0) return void 0;
365
+ return chunkZYQEXGYC_cjs.mapMaterializationRow(row, chunkZYQEXGYC_cjs.POSTGRES_INDEX_MAT_TIMESTAMPS.decode);
366
+ },
367
+ async getIndexMaterializations(statusKeys) {
368
+ if (statusKeys.length === 0) return [];
369
+ const t = tables2.indexMaterializations;
370
+ const rows = await db.select().from(t).where(drizzleOrm.inArray(t.indexName, [...statusKeys]));
371
+ return rows.map(
372
+ (row) => chunkZYQEXGYC_cjs.mapMaterializationRow(row, chunkZYQEXGYC_cjs.POSTGRES_INDEX_MAT_TIMESTAMPS.decode)
373
+ );
374
+ },
375
+ async recordIndexMaterialization(params) {
376
+ const t = tables2.indexMaterializations;
377
+ await db.insert(t).values(
378
+ chunkZYQEXGYC_cjs.buildMaterializationInsertValues(
379
+ params,
380
+ chunkZYQEXGYC_cjs.POSTGRES_INDEX_MAT_TIMESTAMPS.encode
381
+ )
382
+ ).onConflictDoUpdate({
383
+ target: t.indexName,
384
+ set: chunkZYQEXGYC_cjs.buildMaterializationOnConflictSet(
385
+ t.materializedAt,
386
+ params.materializedAt
387
+ )
388
+ });
389
+ },
390
+ async ensureContributionMaterializationsTable() {
391
+ await ensureContributionMaterializationsTableImpl();
392
+ },
393
+ async getContributionMaterialization(identity) {
394
+ return getContributionMaterializationRow(identity);
395
+ },
396
+ async recordContributionMaterialization(params) {
397
+ await recordContributionMaterializationRow(params);
398
+ },
399
+ async assertRuntimeContributionsInitialized(graphId) {
400
+ await contributionMaterializer.assertInitialized(graphId);
401
+ },
402
+ async ensureKindRemovalsTable() {
403
+ await db.execute(drizzleOrm.sql.raw(chunkZYQEXGYC_cjs.generatePgCreateTableSQL(tables2.kindRemovals)));
404
+ },
405
+ async getPendingKindRemovals(graphId) {
406
+ const t = tables2.kindRemovals;
407
+ const rows = await db.select().from(t).where(drizzleOrm.and(drizzleOrm.eq(t.graphId, graphId), drizzleOrm.isNull(t.removedAt)));
408
+ return rows.map(
409
+ (row) => chunkZYQEXGYC_cjs.mapKindRemovalRow(row, chunkZYQEXGYC_cjs.POSTGRES_KIND_REMOVAL_TIMESTAMPS.decode)
410
+ );
411
+ },
412
+ async getAllKindRemovals(graphId) {
413
+ const t = tables2.kindRemovals;
414
+ const rows = await db.select().from(t).where(drizzleOrm.eq(t.graphId, graphId));
415
+ return rows.map(
416
+ (row) => chunkZYQEXGYC_cjs.mapKindRemovalRow(row, chunkZYQEXGYC_cjs.POSTGRES_KIND_REMOVAL_TIMESTAMPS.decode)
417
+ );
418
+ },
419
+ async recordKindRemoval(params) {
420
+ const t = tables2.kindRemovals;
421
+ await db.insert(t).values(
422
+ chunkZYQEXGYC_cjs.buildKindRemovalInsertValues(
423
+ params,
424
+ chunkZYQEXGYC_cjs.POSTGRES_KIND_REMOVAL_TIMESTAMPS.encode
425
+ )
426
+ ).onConflictDoUpdate({
427
+ target: [t.graphId, t.kindName, t.entity, t.schemaVersion],
428
+ set: chunkZYQEXGYC_cjs.buildKindRemovalOnConflictSet(t.removedAt, params.removedAt)
429
+ });
430
+ },
431
+ async ensureReconciliationMarkersTable() {
432
+ await db.execute(
433
+ drizzleOrm.sql.raw(chunkZYQEXGYC_cjs.generatePgCreateTableSQL(tables2.reconciliationMarkers))
434
+ );
435
+ },
436
+ async ensureRuntimeContributions(graphId) {
437
+ await contributionMaterializer.ensureRuntimeContributions(graphId);
438
+ },
439
+ /**
440
+ * Superseded by `ensureRuntimeContributions(graphId)` (#129).
441
+ * Retained as a thin back-compat wrapper for callers predating
442
+ * #129; #135 routed it through the durable-marker writer.
443
+ */
444
+ async ensureFulltextTable(graphId) {
445
+ await contributionMaterializer.ensureRuntimeContributions(graphId);
446
+ },
447
+ async getReconciliationMarker(graphId) {
448
+ const t = tables2.reconciliationMarkers;
449
+ const rows = await db.select().from(t).where(drizzleOrm.eq(t.graphId, graphId));
450
+ return rows[0]?.reconciledToVersion;
451
+ },
452
+ async setReconciliationMarker(graphId, version) {
453
+ const t = tables2.reconciliationMarkers;
454
+ await db.insert(t).values({ graphId, reconciledToVersion: version }).onConflictDoUpdate({
455
+ target: t.graphId,
456
+ set: { reconciledToVersion: version }
457
+ });
458
+ },
459
+ async refreshStatistics() {
460
+ await db.execute(analyzeStatement);
461
+ },
462
+ async commitSchemaVersion(params) {
463
+ return runSchemaWriteTransaction(
464
+ params.graphId,
465
+ (target) => target.commitSchemaVersion(params)
466
+ );
467
+ },
468
+ async setActiveVersion(params) {
469
+ await runSchemaWriteTransaction(
470
+ params.graphId,
471
+ (target) => target.setActiveVersion(params)
472
+ );
473
+ },
474
+ async transaction(fn, options2) {
475
+ const txConfig = options2?.isolationLevel ? {
476
+ isolationLevel: options2.isolationLevel.replace("_", " ")
477
+ } : void 0;
478
+ return db.transaction(
479
+ async (tx) => fn(bindTransactionBackend(tx), tx),
480
+ txConfig
481
+ );
482
+ },
483
+ adoptTransaction(externalTx) {
484
+ if (!capabilities.transactions) {
485
+ throw new chunkG6P3OVDY_cjs.ConfigurationError(
486
+ "Cross-store atomicity is unavailable on this Postgres backend: its driver does not support transactions (drizzle-orm/neon-http, Cloudflare D1). Adopting an external transaction here would let the caller's relational write commit with no way to roll back the graph write. Use a node-postgres or neon-serverless (Pool/WebSocket) connection for cross-store transactions.",
487
+ {
488
+ backend: "postgres",
489
+ capability: "transactions",
490
+ supportsTransactions: false
491
+ }
492
+ );
493
+ }
494
+ chunkZYQEXGYC_cjs.assertAdoptedDialect(externalTx, pgCore.PgDatabase, "postgres");
495
+ return bindTransactionBackend(externalTx);
496
+ },
497
+ async close() {
498
+ }
499
+ };
500
+ return backend;
501
+ }
502
+ function createPostgresOperationBackend(options) {
503
+ const {
504
+ db,
505
+ executionAdapter,
506
+ adapterOptions,
507
+ operationStrategy,
508
+ tableNames,
509
+ capabilities,
510
+ fulltextStrategy,
511
+ vectorStrategy,
512
+ vectorSlotLatch
513
+ } = options;
514
+ async function execAll(query) {
515
+ return executionAdapter.execute(query);
516
+ }
517
+ async function execGet(query) {
518
+ const rows = await executionAdapter.execute(query);
519
+ return rows[0];
520
+ }
521
+ async function execRun(query) {
522
+ await executionAdapter.execute(query);
523
+ }
524
+ let efSearchUnsupportedWarned = false;
525
+ function warnEfSearchUnsupported() {
526
+ if (efSearchUnsupportedWarned) return;
527
+ efSearchUnsupportedWarned = true;
528
+ if (typeof console === "undefined" || typeof console.warn !== "function") {
529
+ return;
530
+ }
531
+ console.warn(
532
+ "[typegraph] efSearch (hnsw.ef_search override) was ignored: this Postgres backend has transactions disabled (e.g. drizzle-orm/neon-http), and SET LOCAL needs a transaction to scope the override. Use a transactional driver (node-postgres / neon-serverless / postgres-js) to apply efSearch."
533
+ );
534
+ }
535
+ async function runVectorSearch(efSearch, query) {
536
+ if (efSearch === void 0) {
537
+ return execAll(query);
538
+ }
539
+ if (!capabilities.transactions) {
540
+ warnEfSearchUnsupported();
541
+ return execAll(query);
542
+ }
543
+ const setEfSearch = drizzleOrm.sql.raw(`SET LOCAL hnsw.ef_search = ${efSearch}`);
544
+ if (db instanceof pgCore.PgTransaction) {
545
+ const [setting] = await execAll(
546
+ drizzleOrm.sql`SELECT current_setting('hnsw.ef_search') AS ef_search`
547
+ );
548
+ await execRun(setEfSearch);
549
+ const rows = await execAll(query);
550
+ if (setting !== void 0) {
551
+ await execRun(
552
+ drizzleOrm.sql`SELECT set_config('hnsw.ef_search', ${setting.ef_search}, true)`
553
+ );
554
+ }
555
+ return rows;
556
+ }
557
+ return db.transaction(async (tx) => {
558
+ const txAdapter = createPostgresExecutionAdapter(tx, adapterOptions);
559
+ await txAdapter.execute(setEfSearch);
560
+ return txAdapter.execute(query);
561
+ });
562
+ }
563
+ async function ensureVectorSlotStorage(slot) {
564
+ if (vectorStrategy === void 0 || vectorSlotLatch === void 0) return;
565
+ await vectorSlotLatch.ensure(vectorStrategy, slot, async (statement) => {
566
+ await execRun(drizzleOrm.sql.raw(statement));
567
+ });
568
+ }
569
+ const commonBackend = chunkZYQEXGYC_cjs.createCommonOperationBackend({
570
+ batchConfig: {
571
+ checkUniqueBatchChunkSize: POSTGRES_CHECK_UNIQUE_BATCH_CHUNK_SIZE,
572
+ edgeInsertBatchSize: POSTGRES_EDGE_INSERT_BATCH_SIZE,
573
+ getEdgesChunkSize: POSTGRES_GET_EDGES_ID_CHUNK_SIZE,
574
+ getNodesChunkSize: POSTGRES_GET_NODES_ID_CHUNK_SIZE,
575
+ nodeInsertBatchSize: POSTGRES_NODE_INSERT_BATCH_SIZE
576
+ },
577
+ execution: {
578
+ execAll,
579
+ execGet,
580
+ execRun
581
+ },
582
+ nowIso: chunkZYQEXGYC_cjs.nowIso,
583
+ operationStrategy,
584
+ rowMappers: {
585
+ toEdgeRow,
586
+ toNodeRow,
587
+ toSchemaVersionRow,
588
+ toUniqueRow
589
+ }
590
+ });
591
+ const executeCompiled = executionAdapter.executeCompiled;
592
+ const executeRawMethod = executeCompiled === void 0 ? {} : {
593
+ async executeRaw(sqlText, params) {
594
+ return executeCompiled({ params, sql: sqlText });
595
+ }
596
+ };
597
+ const vectorEmbeddingMethods = vectorStrategy === void 0 ? {} : {
598
+ async upsertEmbedding(params) {
599
+ const slot = chunkZYQEXGYC_cjs.vectorSlotFromParams(params);
600
+ await ensureVectorSlotStorage(slot);
601
+ const statements = vectorStrategy.buildUpsert(slot, params, chunkZYQEXGYC_cjs.nowIso());
602
+ try {
603
+ for (const statement of statements) {
604
+ await execRun(statement);
605
+ }
606
+ } catch (error) {
607
+ throw chunkZYQEXGYC_cjs.mapVectorWriteError(error, params);
608
+ }
609
+ },
610
+ async deleteEmbedding(params) {
611
+ const slot = chunkZYQEXGYC_cjs.vectorSlotFromParams(params);
612
+ await ensureVectorSlotStorage(slot);
613
+ const statements = vectorStrategy.buildDelete(slot, params);
614
+ for (const statement of statements) {
615
+ await execRun(statement);
616
+ }
617
+ },
618
+ async vectorSearch(params) {
619
+ chunkC2RWFIYP_cjs.assertVectorSearchLimit(params.limit);
620
+ chunkKGAJ5HFO_cjs.assertPgvectorEfSearch(params.efSearch);
621
+ const slot = chunkZYQEXGYC_cjs.vectorSlotFromParams(params);
622
+ await ensureVectorSlotStorage(slot);
623
+ const query = vectorStrategy.buildSearch(slot, params);
624
+ let rows;
625
+ try {
626
+ rows = await runVectorSearch(params.efSearch, query);
627
+ } catch (error) {
628
+ throw chunkZYQEXGYC_cjs.mapVectorWriteError(error, params);
629
+ }
630
+ return rows.map((row) => ({
631
+ nodeId: row.node_id,
632
+ score: row.score
633
+ }));
634
+ }
635
+ };
636
+ const operationBackend = {
637
+ ...commonBackend,
638
+ ...executeRawMethod,
639
+ ...vectorEmbeddingMethods,
640
+ capabilities,
641
+ fulltextStrategy,
642
+ ...vectorStrategy === void 0 ? {} : { vectorStrategy },
643
+ dialect: "postgres",
644
+ tableNames,
645
+ // === Vector Index Operations ===
646
+ async createVectorIndex(params) {
647
+ if (vectorStrategy === void 0) return;
648
+ const slot = chunkZYQEXGYC_cjs.vectorSlotFromCreateIndexParams(params);
649
+ await ensureVectorSlotStorage(slot);
650
+ const indexStatement = vectorStrategy.buildCreateIndex?.(slot, {
651
+ concurrent: params.concurrent === true
652
+ });
653
+ if (indexStatement !== void 0) {
654
+ await execRun(indexStatement);
655
+ }
656
+ },
657
+ // === Fulltext Operations ===
658
+ async upsertFulltext(params) {
659
+ const timestamp = chunkZYQEXGYC_cjs.nowIso();
660
+ const statements = operationStrategy.buildUpsertFulltext(
661
+ params,
662
+ timestamp
663
+ );
664
+ for (const stmt of statements) {
665
+ await execRun(stmt);
666
+ }
667
+ },
668
+ async deleteFulltext(params) {
669
+ const statements = operationStrategy.buildDeleteFulltext(params);
670
+ for (const stmt of statements) {
671
+ await execRun(stmt);
672
+ }
673
+ },
674
+ async upsertFulltextBatch(params) {
675
+ if (params.rows.length === 0) return;
676
+ const timestamp = chunkZYQEXGYC_cjs.nowIso();
677
+ const statements = operationStrategy.buildUpsertFulltextBatch(
678
+ params,
679
+ timestamp
680
+ );
681
+ for (const stmt of statements) {
682
+ await execRun(stmt);
683
+ }
684
+ },
685
+ async deleteFulltextBatch(params) {
686
+ if (params.nodeIds.length === 0) return;
687
+ const statements = operationStrategy.buildDeleteFulltextBatch(params);
688
+ for (const stmt of statements) {
689
+ await execRun(stmt);
690
+ }
691
+ },
692
+ async fulltextSearch(params) {
693
+ const query = operationStrategy.buildFulltextSearch(params);
694
+ const rows = await execAll(query);
695
+ return rows.map((row, index) => ({
696
+ nodeId: row.node_id,
697
+ score: chunkZYQEXGYC_cjs.coerceNumericScore(row.score),
698
+ rank: index + 1,
699
+ ...row.snippet === null ? {} : { snippet: row.snippet }
700
+ }));
701
+ },
702
+ async dropVectorIndex(params) {
703
+ if (vectorStrategy === void 0) return;
704
+ const slot = chunkZYQEXGYC_cjs.vectorSlotFromDropIndexParams(params);
705
+ const dropStatement = vectorStrategy.buildDropIndex?.(slot);
706
+ if (dropStatement === void 0) return;
707
+ try {
708
+ await execRun(dropStatement);
709
+ } catch (error) {
710
+ if (!chunkRP7YITHX_cjs.isMissingTableError(error)) throw error;
711
+ }
712
+ },
713
+ // === Query Execution ===
714
+ async execute(query) {
715
+ return executionAdapter.execute(query);
716
+ },
717
+ compileSql(query) {
718
+ return executionAdapter.compile(query);
719
+ }
720
+ };
721
+ return operationBackend;
722
+ }
723
+ function createTransactionBackend(options) {
724
+ const txExecutionAdapter = options.executionAdapter ?? createPostgresExecutionAdapter(options.db, options.adapterOptions);
725
+ return createPostgresOperationBackend({
726
+ db: options.db,
727
+ executionAdapter: txExecutionAdapter,
728
+ adapterOptions: options.adapterOptions,
729
+ operationStrategy: options.operationStrategy,
730
+ tableNames: options.tableNames,
731
+ capabilities: options.capabilities,
732
+ fulltextStrategy: options.fulltextStrategy,
733
+ vectorStrategy: options.vectorStrategy,
734
+ vectorSlotLatch: options.vectorStrategy === void 0 ? void 0 : chunkZYQEXGYC_cjs.createVectorSlotLatch()
735
+ });
736
+ }
737
+
738
+ exports.createPostgresBackend = createPostgresBackend;
739
+ //# sourceMappingURL=chunk-UP4QAJND.cjs.map
740
+ //# sourceMappingURL=chunk-UP4QAJND.cjs.map