@nicia-ai/typegraph 0.28.0 → 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.
- package/dist/backend/postgres/index.cjs +23 -761
- package/dist/backend/postgres/index.cjs.map +1 -1
- package/dist/backend/postgres/index.d.cts +24 -5
- package/dist/backend/postgres/index.d.ts +24 -5
- package/dist/backend/postgres/index.js +10 -752
- package/dist/backend/postgres/index.js.map +1 -1
- package/dist/backend/postgres/pglite.cjs +52 -0
- package/dist/backend/postgres/pglite.cjs.map +1 -0
- package/dist/backend/postgres/pglite.d.cts +116 -0
- package/dist/backend/postgres/pglite.d.ts +116 -0
- package/dist/backend/postgres/pglite.js +50 -0
- package/dist/backend/postgres/pglite.js.map +1 -0
- package/dist/backend/sqlite/index.cjs +22 -26
- package/dist/backend/sqlite/index.d.cts +17 -15
- package/dist/backend/sqlite/index.d.ts +17 -15
- package/dist/backend/sqlite/index.js +9 -9
- package/dist/backend/sqlite/libsql.cjs +17 -13
- package/dist/backend/sqlite/libsql.cjs.map +1 -1
- package/dist/backend/sqlite/libsql.d.cts +3 -3
- package/dist/backend/sqlite/libsql.d.ts +3 -3
- package/dist/backend/sqlite/libsql.js +16 -12
- package/dist/backend/sqlite/libsql.js.map +1 -1
- package/dist/backend/sqlite/local.cjs +17 -16
- package/dist/backend/sqlite/local.cjs.map +1 -1
- package/dist/backend/sqlite/local.d.cts +3 -3
- package/dist/backend/sqlite/local.d.ts +3 -3
- package/dist/backend/sqlite/local.js +15 -14
- package/dist/backend/sqlite/local.js.map +1 -1
- package/dist/{chunk-DFHLG75V.js → chunk-3DTLDGIM.js} +386 -1093
- package/dist/chunk-3DTLDGIM.js.map +1 -0
- package/dist/{chunk-WVQXAFP3.js → chunk-454I6GDF.js} +46 -5
- package/dist/chunk-454I6GDF.js.map +1 -0
- package/dist/chunk-4K6IHUW6.cjs +183 -0
- package/dist/chunk-4K6IHUW6.cjs.map +1 -0
- package/dist/{chunk-ZTREJOGB.js → chunk-7KLVFWXR.js} +113 -68
- package/dist/chunk-7KLVFWXR.js.map +1 -0
- package/dist/{chunk-UKG7XHZ3.js → chunk-A4H6QMV4.js} +110 -102
- package/dist/chunk-A4H6QMV4.js.map +1 -0
- package/dist/{chunk-E5ROA3MN.js → chunk-BMDF2S7S.js} +3 -3
- package/dist/{chunk-E5ROA3MN.js.map → chunk-BMDF2S7S.js.map} +1 -1
- package/dist/{chunk-PN37HVAV.cjs → chunk-C2RWFIYP.cjs} +121 -67
- package/dist/chunk-C2RWFIYP.cjs.map +1 -0
- package/dist/chunk-CUZGBWFT.js +206 -0
- package/dist/chunk-CUZGBWFT.js.map +1 -0
- package/dist/chunk-F2Q76CQ4.cjs +266 -0
- package/dist/chunk-F2Q76CQ4.cjs.map +1 -0
- package/dist/{chunk-YEDQLNVE.cjs → chunk-G6P3OVDY.cjs} +14 -2
- package/dist/chunk-G6P3OVDY.cjs.map +1 -0
- package/dist/chunk-GPUNAJFH.js +181 -0
- package/dist/chunk-GPUNAJFH.js.map +1 -0
- package/dist/chunk-HKN622O4.js +254 -0
- package/dist/chunk-HKN622O4.js.map +1 -0
- package/dist/{chunk-PYJOBZR3.cjs → chunk-HM6XMR3E.cjs} +52 -52
- package/dist/chunk-HM6XMR3E.cjs.map +1 -0
- package/dist/chunk-IE2TPFBT.js +305 -0
- package/dist/chunk-IE2TPFBT.js.map +1 -0
- package/dist/chunk-KGAJ5HFO.cjs +319 -0
- package/dist/chunk-KGAJ5HFO.cjs.map +1 -0
- package/dist/chunk-M6OC455F.js +65 -0
- package/dist/chunk-M6OC455F.js.map +1 -0
- package/dist/{chunk-U5ROKVRF.js → chunk-MB363HT5.js} +14 -3
- package/dist/chunk-MB363HT5.js.map +1 -0
- package/dist/{chunk-X53YKGHR.js → chunk-MRMD7S7E.js} +9 -9
- package/dist/{chunk-X53YKGHR.js.map → chunk-MRMD7S7E.js.map} +1 -1
- package/dist/{chunk-OOS2RKRH.js → chunk-N3VVQDMA.js} +3 -3
- package/dist/{chunk-OOS2RKRH.js.map → chunk-N3VVQDMA.js.map} +1 -1
- package/dist/{chunk-EJCDOA37.js → chunk-OGUH56BB.js} +4 -4
- package/dist/{chunk-EJCDOA37.js.map → chunk-OGUH56BB.js.map} +1 -1
- package/dist/chunk-Q3ZZLU3O.cjs +12 -0
- package/dist/{chunk-4FJVSF3K.cjs.map → chunk-Q3ZZLU3O.cjs.map} +1 -1
- package/dist/chunk-Q5ASTPM4.cjs +208 -0
- package/dist/chunk-Q5ASTPM4.cjs.map +1 -0
- package/dist/{chunk-WFUN6DCM.cjs → chunk-R2LA6MKR.cjs} +11 -11
- package/dist/chunk-R2LA6MKR.cjs.map +1 -0
- package/dist/chunk-RP7YITHX.cjs +70 -0
- package/dist/chunk-RP7YITHX.cjs.map +1 -0
- package/dist/{chunk-33K3ZYFG.cjs → chunk-RU5BBUU2.cjs} +132 -124
- package/dist/chunk-RU5BBUU2.cjs.map +1 -0
- package/dist/{chunk-TSGBRPNE.js → chunk-SFNUHFON.js} +45 -45
- package/dist/chunk-SFNUHFON.js.map +1 -0
- package/dist/chunk-UP4QAJND.cjs +740 -0
- package/dist/chunk-UP4QAJND.cjs.map +1 -0
- package/dist/{chunk-CTHNVU6K.cjs → chunk-UPBXUOW7.cjs} +8 -8
- package/dist/{chunk-CTHNVU6K.cjs.map → chunk-UPBXUOW7.cjs.map} +1 -1
- package/dist/{chunk-EK6RJ373.cjs → chunk-VGU26WIU.cjs} +5 -5
- package/dist/{chunk-EK6RJ373.cjs.map → chunk-VGU26WIU.cjs.map} +1 -1
- package/dist/{chunk-ZGEMI7MJ.cjs → chunk-XTQHSOIC.cjs} +101 -101
- package/dist/{chunk-ZGEMI7MJ.cjs.map → chunk-XTQHSOIC.cjs.map} +1 -1
- package/dist/chunk-Y52UNT66.js +738 -0
- package/dist/chunk-Y52UNT66.js.map +1 -0
- package/dist/{chunk-OYLF3IC7.js → chunk-Z36W5WUM.js} +4 -4
- package/dist/chunk-Z36W5WUM.js.map +1 -0
- package/dist/{chunk-X3244EKP.cjs → chunk-ZFWNO3GE.cjs} +48 -4
- package/dist/chunk-ZFWNO3GE.cjs.map +1 -0
- package/dist/{chunk-2E4VTD2E.cjs → chunk-ZYQEXGYC.cjs} +423 -1139
- package/dist/chunk-ZYQEXGYC.cjs.map +1 -0
- package/dist/ddl-BwH00BB6.d.ts +38 -0
- package/dist/ddl-D3o-RmsV.d.cts +38 -0
- package/dist/{extension-types-B78CZfA5.d.ts → extension-types-CwHV-Uk4.d.cts} +4 -4
- package/dist/{extension-types-B78CZfA5.d.cts → extension-types-CwHV-Uk4.d.ts} +4 -4
- package/dist/graph-extension/index.cjs +18 -18
- package/dist/graph-extension/index.d.cts +4 -4
- package/dist/graph-extension/index.d.ts +4 -4
- package/dist/graph-extension/index.js +3 -3
- package/dist/{index-CWdBFWbI.d.ts → index-CapHwi1a.d.ts} +22 -3
- package/dist/{index-Bilw0pYm.d.cts → index-jPPexxM5.d.cts} +22 -3
- package/dist/index.cjs +1211 -945
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +142 -12
- package/dist/index.d.ts +142 -12
- package/dist/index.js +778 -544
- package/dist/index.js.map +1 -1
- package/dist/indexes/index.cjs +18 -18
- package/dist/indexes/index.d.cts +9 -9
- package/dist/indexes/index.d.ts +9 -9
- package/dist/indexes/index.js +4 -4
- package/dist/interchange/index.cjs +8 -14
- package/dist/interchange/index.cjs.map +1 -1
- package/dist/interchange/index.d.cts +5 -16
- package/dist/interchange/index.d.ts +5 -16
- package/dist/interchange/index.js +3 -9
- package/dist/interchange/index.js.map +1 -1
- package/dist/{manager-ehLTTNq0.d.ts → manager-BUugwCYz.d.ts} +2 -2
- package/dist/{manager-c1MGaVVR.d.cts → manager-DpIpn1HQ.d.cts} +2 -2
- package/dist/{materialize-indexes-DFavFQah.d.ts → materialize-indexes-lkawaDor.d.ts} +1 -1
- package/dist/{materialize-indexes-BQfaAZKA.d.cts → materialize-indexes-oanE3RU0.d.cts} +1 -1
- package/dist/{ddl-0Ooc2dpL.d.ts → postgres-CQL9UIn2.d.cts} +2 -471
- package/dist/{ddl-Dza3kDrI.d.cts → postgres-CcRLaxz0.d.ts} +2 -471
- package/dist/profiler/index.cjs +4 -4
- package/dist/profiler/index.d.cts +7 -7
- package/dist/profiler/index.d.ts +7 -7
- package/dist/profiler/index.js +2 -2
- package/dist/schema/index.cjs +32 -32
- package/dist/schema/index.d.cts +7 -7
- package/dist/schema/index.d.ts +7 -7
- package/dist/schema/index.js +9 -9
- package/dist/{sqlite-BxZUPbyD.d.ts → sqlite-CZDFUfHS.d.ts} +2 -468
- package/dist/{sqlite-mvpXjEeB.d.cts → sqlite-Rj3xu4It.d.cts} +2 -468
- package/dist/{store-Ufc1Eoey.d.ts → store-B43FpOVX.d.cts} +84 -5
- package/dist/{store-D7YEMg2v.d.cts → store-De0Y33wP.d.ts} +84 -5
- package/dist/{types-B-sJJ4GQ.d.ts → types-B5GSeE3t.d.ts} +2 -2
- package/dist/{types-CmUA9sal.d.cts → types-Bwlp_yvu.d.cts} +2 -2
- package/dist/{types-CMQymcwk.d.cts → types-CmAT-Z47.d.ts} +250 -46
- package/dist/{types-CQ7xXJMP.d.ts → types-DXCSyxac.d.cts} +250 -46
- package/dist/{where-5zf15aFZ.d.cts → where-2dsyYt3d.d.cts} +2 -2
- package/dist/{where-CtrLu1Zf.d.ts → where-CP4qGIHd.d.ts} +2 -2
- package/package.json +20 -5
- package/dist/chunk-2E4VTD2E.cjs.map +0 -1
- package/dist/chunk-33K3ZYFG.cjs.map +0 -1
- package/dist/chunk-3EUKJEQ3.js +0 -77
- package/dist/chunk-3EUKJEQ3.js.map +0 -1
- package/dist/chunk-4FJVSF3K.cjs +0 -12
- package/dist/chunk-5H3BAMTB.js +0 -32
- package/dist/chunk-5H3BAMTB.js.map +0 -1
- package/dist/chunk-DFHLG75V.js.map +0 -1
- package/dist/chunk-HTGDWIDE.cjs +0 -89
- package/dist/chunk-HTGDWIDE.cjs.map +0 -1
- package/dist/chunk-IUIWCFG3.cjs +0 -36
- package/dist/chunk-IUIWCFG3.cjs.map +0 -1
- package/dist/chunk-LYRUNG6E.cjs +0 -52
- package/dist/chunk-LYRUNG6E.cjs.map +0 -1
- package/dist/chunk-OYLF3IC7.js.map +0 -1
- package/dist/chunk-PCHTTG42.js +0 -47
- package/dist/chunk-PCHTTG42.js.map +0 -1
- package/dist/chunk-PN37HVAV.cjs.map +0 -1
- package/dist/chunk-PYJOBZR3.cjs.map +0 -1
- package/dist/chunk-TSGBRPNE.js.map +0 -1
- package/dist/chunk-U5ROKVRF.js.map +0 -1
- package/dist/chunk-UKG7XHZ3.js.map +0 -1
- package/dist/chunk-WFUN6DCM.cjs.map +0 -1
- package/dist/chunk-WVQXAFP3.js.map +0 -1
- package/dist/chunk-X3244EKP.cjs.map +0 -1
- package/dist/chunk-YEDQLNVE.cjs.map +0 -1
- package/dist/chunk-ZTREJOGB.js.map +0 -1
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
1
|
+
import { tables } from './chunk-HKN622O4.js';
|
|
2
|
+
import { parseDimensionMismatch, isMissingTableError, sortedReplacer } from './chunk-M6OC455F.js';
|
|
3
|
+
import { buildPostgresNodeIndexBuilders, buildPostgresEdgeIndexBuilders, tsvectorStrategy, fts5Strategy, BASE_CONTRIBUTION_OWNER, quotedTableName, quotedColumn, edgeColumnList, sqlNull, nodeColumnList } from './chunk-7KLVFWXR.js';
|
|
4
|
+
import { sha256Hex } from './chunk-OGUH56BB.js';
|
|
5
|
+
import { EmbeddingDimensionChangedError, DatabaseOperationError, ConfigurationError, MigrationError, SchemaContentConflictError, UniquenessError, StoreNotInitializedError, StaleVersionError } from './chunk-MB363HT5.js';
|
|
5
6
|
import { sql, is, getTableName } from 'drizzle-orm';
|
|
6
7
|
import { customType, pgTable, timestamp, integer, jsonb, text, primaryKey, index, boolean, uniqueIndex, getTableConfig as getTableConfig$1, PgTable } from 'drizzle-orm/pg-core';
|
|
7
|
-
import {
|
|
8
|
+
import { getTableConfig, SQLiteTable } from 'drizzle-orm/sqlite-core';
|
|
8
9
|
|
|
9
10
|
var regconfig = customType({
|
|
10
11
|
dataType: () => "regconfig"
|
|
@@ -12,24 +13,6 @@ var regconfig = customType({
|
|
|
12
13
|
var tsvector = customType({
|
|
13
14
|
dataType: () => "tsvector"
|
|
14
15
|
});
|
|
15
|
-
var vector = customType({
|
|
16
|
-
dataType(config) {
|
|
17
|
-
return config?.dimensions ? `vector(${config.dimensions})` : "vector";
|
|
18
|
-
},
|
|
19
|
-
toDriver(value) {
|
|
20
|
-
return `[${value.join(",")}]`;
|
|
21
|
-
},
|
|
22
|
-
fromDriver(value) {
|
|
23
|
-
if (Array.isArray(value)) {
|
|
24
|
-
return value;
|
|
25
|
-
}
|
|
26
|
-
const content = value.slice(1, -1);
|
|
27
|
-
if (content === "") {
|
|
28
|
-
return [];
|
|
29
|
-
}
|
|
30
|
-
return content.split(",").map((s) => Number.parseFloat(s.trim()));
|
|
31
|
-
}
|
|
32
|
-
});
|
|
33
16
|
|
|
34
17
|
// src/backend/drizzle/schema/postgres.ts
|
|
35
18
|
var DEFAULT_TABLE_NAMES = {
|
|
@@ -37,7 +20,6 @@ var DEFAULT_TABLE_NAMES = {
|
|
|
37
20
|
edges: "typegraph_edges",
|
|
38
21
|
uniques: "typegraph_node_uniques",
|
|
39
22
|
schemaVersions: "typegraph_schema_versions",
|
|
40
|
-
embeddings: "typegraph_node_embeddings",
|
|
41
23
|
fulltext: "typegraph_node_fulltext",
|
|
42
24
|
indexMaterializations: "typegraph_index_materializations",
|
|
43
25
|
contributionMaterializations: "typegraph_contribution_materializations",
|
|
@@ -47,7 +29,7 @@ var DEFAULT_TABLE_NAMES = {
|
|
|
47
29
|
function createPostgresTables(names = {}, options = {}) {
|
|
48
30
|
const n = { ...DEFAULT_TABLE_NAMES, ...names };
|
|
49
31
|
const indexes = options.indexes ?? [];
|
|
50
|
-
const
|
|
32
|
+
const nodes2 = pgTable(
|
|
51
33
|
n.nodes,
|
|
52
34
|
{
|
|
53
35
|
graphId: text("graph_id").notNull(),
|
|
@@ -75,7 +57,7 @@ function createPostgresTables(names = {}, options = {}) {
|
|
|
75
57
|
...buildPostgresNodeIndexBuilders(t, indexes)
|
|
76
58
|
]
|
|
77
59
|
);
|
|
78
|
-
const
|
|
60
|
+
const edges2 = pgTable(
|
|
79
61
|
n.edges,
|
|
80
62
|
{
|
|
81
63
|
graphId: text("graph_id").notNull(),
|
|
@@ -134,7 +116,7 @@ function createPostgresTables(names = {}, options = {}) {
|
|
|
134
116
|
...buildPostgresEdgeIndexBuilders(t, indexes)
|
|
135
117
|
]
|
|
136
118
|
);
|
|
137
|
-
const
|
|
119
|
+
const uniques2 = pgTable(
|
|
138
120
|
n.uniques,
|
|
139
121
|
{
|
|
140
122
|
graphId: text("graph_id").notNull(),
|
|
@@ -152,7 +134,7 @@ function createPostgresTables(names = {}, options = {}) {
|
|
|
152
134
|
index(`${n.uniques}_node_idx`).on(t.graphId, t.concreteKind, t.nodeId)
|
|
153
135
|
]
|
|
154
136
|
);
|
|
155
|
-
const
|
|
137
|
+
const schemaVersions2 = pgTable(
|
|
156
138
|
n.schemaVersions,
|
|
157
139
|
{
|
|
158
140
|
graphId: text("graph_id").notNull(),
|
|
@@ -173,38 +155,6 @@ function createPostgresTables(names = {}, options = {}) {
|
|
|
173
155
|
uniqueIndex(`${n.schemaVersions}_one_active_per_graph_idx`).on(t.graphId).where(sql`${t.isActive} = TRUE`)
|
|
174
156
|
]
|
|
175
157
|
);
|
|
176
|
-
const embeddings3 = pgTable(
|
|
177
|
-
n.embeddings,
|
|
178
|
-
{
|
|
179
|
-
graphId: text("graph_id").notNull(),
|
|
180
|
-
nodeKind: text("node_kind").notNull(),
|
|
181
|
-
nodeId: text("node_id").notNull(),
|
|
182
|
-
fieldPath: text("field_path").notNull(),
|
|
183
|
-
/** Embedding vector stored as native pgvector type */
|
|
184
|
-
embedding: vector("embedding").notNull(),
|
|
185
|
-
/** Number of dimensions (for validation) */
|
|
186
|
-
dimensions: integer("dimensions").notNull(),
|
|
187
|
-
createdAt: timestamp("created_at", { withTimezone: true }).notNull(),
|
|
188
|
-
updatedAt: timestamp("updated_at", { withTimezone: true }).notNull()
|
|
189
|
-
},
|
|
190
|
-
(t) => [
|
|
191
|
-
primaryKey({
|
|
192
|
-
columns: [t.graphId, t.nodeKind, t.nodeId, t.fieldPath]
|
|
193
|
-
}),
|
|
194
|
-
// Index for looking up embeddings by node
|
|
195
|
-
index(`${n.embeddings}_node_idx`).on(
|
|
196
|
-
t.graphId,
|
|
197
|
-
t.nodeKind,
|
|
198
|
-
t.nodeId
|
|
199
|
-
),
|
|
200
|
-
// Index for filtering by kind and field (used in vector search)
|
|
201
|
-
index(`${n.embeddings}_kind_field_idx`).on(
|
|
202
|
-
t.graphId,
|
|
203
|
-
t.nodeKind,
|
|
204
|
-
t.fieldPath
|
|
205
|
-
)
|
|
206
|
-
]
|
|
207
|
-
);
|
|
208
158
|
const indexMaterializations = pgTable(
|
|
209
159
|
n.indexMaterializations,
|
|
210
160
|
{
|
|
@@ -287,11 +237,10 @@ function createPostgresTables(names = {}, options = {}) {
|
|
|
287
237
|
]
|
|
288
238
|
);
|
|
289
239
|
return {
|
|
290
|
-
nodes:
|
|
291
|
-
edges:
|
|
292
|
-
uniques:
|
|
293
|
-
schemaVersions:
|
|
294
|
-
embeddings: embeddings3,
|
|
240
|
+
nodes: nodes2,
|
|
241
|
+
edges: edges2,
|
|
242
|
+
uniques: uniques2,
|
|
243
|
+
schemaVersions: schemaVersions2,
|
|
295
244
|
indexMaterializations,
|
|
296
245
|
contributionMaterializations,
|
|
297
246
|
kindRemovals,
|
|
@@ -300,270 +249,8 @@ function createPostgresTables(names = {}, options = {}) {
|
|
|
300
249
|
fulltextTableName: n.fulltext
|
|
301
250
|
};
|
|
302
251
|
}
|
|
303
|
-
var
|
|
304
|
-
var {
|
|
305
|
-
nodes,
|
|
306
|
-
edges,
|
|
307
|
-
uniques,
|
|
308
|
-
schemaVersions,
|
|
309
|
-
embeddings,
|
|
310
|
-
fulltext
|
|
311
|
-
} = tables;
|
|
312
|
-
var DEFAULT_TABLE_NAMES2 = {
|
|
313
|
-
nodes: "typegraph_nodes",
|
|
314
|
-
edges: "typegraph_edges",
|
|
315
|
-
uniques: "typegraph_node_uniques",
|
|
316
|
-
schemaVersions: "typegraph_schema_versions",
|
|
317
|
-
embeddings: "typegraph_node_embeddings",
|
|
318
|
-
fulltext: "typegraph_node_fulltext",
|
|
319
|
-
indexMaterializations: "typegraph_index_materializations",
|
|
320
|
-
contributionMaterializations: "typegraph_contribution_materializations",
|
|
321
|
-
kindRemovals: "typegraph_kind_removals",
|
|
322
|
-
reconciliationMarkers: "typegraph_reconciliation_markers"
|
|
323
|
-
};
|
|
324
|
-
function createSqliteTables(names = {}, options = {}) {
|
|
325
|
-
const n = { ...DEFAULT_TABLE_NAMES2, ...names };
|
|
326
|
-
const indexes = options.indexes ?? [];
|
|
327
|
-
const nodes3 = sqliteTable(
|
|
328
|
-
n.nodes,
|
|
329
|
-
{
|
|
330
|
-
graphId: text$1("graph_id").notNull(),
|
|
331
|
-
kind: text$1("kind").notNull(),
|
|
332
|
-
id: text$1("id").notNull(),
|
|
333
|
-
props: text$1("props").notNull(),
|
|
334
|
-
version: integer$1("version").notNull().default(1),
|
|
335
|
-
validFrom: text$1("valid_from"),
|
|
336
|
-
validTo: text$1("valid_to"),
|
|
337
|
-
createdAt: text$1("created_at").notNull(),
|
|
338
|
-
updatedAt: text$1("updated_at").notNull(),
|
|
339
|
-
deletedAt: text$1("deleted_at")
|
|
340
|
-
},
|
|
341
|
-
(t) => [
|
|
342
|
-
primaryKey$1({ columns: [t.graphId, t.kind, t.id] }),
|
|
343
|
-
index$1(`${n.nodes}_kind_idx`).on(t.graphId, t.kind),
|
|
344
|
-
index$1(`${n.nodes}_kind_created_idx`).on(
|
|
345
|
-
t.graphId,
|
|
346
|
-
t.kind,
|
|
347
|
-
t.deletedAt,
|
|
348
|
-
t.createdAt
|
|
349
|
-
),
|
|
350
|
-
index$1(`${n.nodes}_deleted_idx`).on(t.graphId, t.deletedAt),
|
|
351
|
-
index$1(`${n.nodes}_valid_idx`).on(t.graphId, t.validFrom, t.validTo),
|
|
352
|
-
...buildSqliteNodeIndexBuilders(t, indexes)
|
|
353
|
-
]
|
|
354
|
-
);
|
|
355
|
-
const edges3 = sqliteTable(
|
|
356
|
-
n.edges,
|
|
357
|
-
{
|
|
358
|
-
graphId: text$1("graph_id").notNull(),
|
|
359
|
-
id: text$1("id").notNull(),
|
|
360
|
-
kind: text$1("kind").notNull(),
|
|
361
|
-
fromKind: text$1("from_kind").notNull(),
|
|
362
|
-
fromId: text$1("from_id").notNull(),
|
|
363
|
-
toKind: text$1("to_kind").notNull(),
|
|
364
|
-
toId: text$1("to_id").notNull(),
|
|
365
|
-
props: text$1("props").notNull(),
|
|
366
|
-
validFrom: text$1("valid_from"),
|
|
367
|
-
validTo: text$1("valid_to"),
|
|
368
|
-
createdAt: text$1("created_at").notNull(),
|
|
369
|
-
updatedAt: text$1("updated_at").notNull(),
|
|
370
|
-
deletedAt: text$1("deleted_at")
|
|
371
|
-
},
|
|
372
|
-
(t) => [
|
|
373
|
-
primaryKey$1({ columns: [t.graphId, t.id] }),
|
|
374
|
-
index$1(`${n.edges}_kind_idx`).on(t.graphId, t.kind),
|
|
375
|
-
// Directional traversal index (outgoing): supports endpoint lookups
|
|
376
|
-
// and extra filtering by edge kind / target kind.
|
|
377
|
-
index$1(`${n.edges}_from_idx`).on(
|
|
378
|
-
t.graphId,
|
|
379
|
-
t.fromKind,
|
|
380
|
-
t.fromId,
|
|
381
|
-
t.kind,
|
|
382
|
-
t.toKind,
|
|
383
|
-
t.deletedAt,
|
|
384
|
-
t.validTo
|
|
385
|
-
),
|
|
386
|
-
// Directional traversal index (incoming): mirrors from_idx for reverse traversals.
|
|
387
|
-
index$1(`${n.edges}_to_idx`).on(
|
|
388
|
-
t.graphId,
|
|
389
|
-
t.toKind,
|
|
390
|
-
t.toId,
|
|
391
|
-
t.kind,
|
|
392
|
-
t.fromKind,
|
|
393
|
-
t.deletedAt,
|
|
394
|
-
t.validTo
|
|
395
|
-
),
|
|
396
|
-
index$1(`${n.edges}_kind_created_idx`).on(
|
|
397
|
-
t.graphId,
|
|
398
|
-
t.kind,
|
|
399
|
-
t.deletedAt,
|
|
400
|
-
t.createdAt
|
|
401
|
-
),
|
|
402
|
-
index$1(`${n.edges}_deleted_idx`).on(t.graphId, t.deletedAt),
|
|
403
|
-
index$1(`${n.edges}_valid_idx`).on(t.graphId, t.validFrom, t.validTo),
|
|
404
|
-
index$1(`${n.edges}_cardinality_idx`).on(
|
|
405
|
-
t.graphId,
|
|
406
|
-
t.kind,
|
|
407
|
-
t.fromKind,
|
|
408
|
-
t.fromId,
|
|
409
|
-
t.validTo
|
|
410
|
-
),
|
|
411
|
-
...buildSqliteEdgeIndexBuilders(t, indexes)
|
|
412
|
-
]
|
|
413
|
-
);
|
|
414
|
-
const uniques3 = sqliteTable(
|
|
415
|
-
n.uniques,
|
|
416
|
-
{
|
|
417
|
-
graphId: text$1("graph_id").notNull(),
|
|
418
|
-
nodeKind: text$1("node_kind").notNull(),
|
|
419
|
-
constraintName: text$1("constraint_name").notNull(),
|
|
420
|
-
key: text$1("key").notNull(),
|
|
421
|
-
nodeId: text$1("node_id").notNull(),
|
|
422
|
-
concreteKind: text$1("concrete_kind").notNull(),
|
|
423
|
-
deletedAt: text$1("deleted_at")
|
|
424
|
-
},
|
|
425
|
-
(t) => [
|
|
426
|
-
primaryKey$1({
|
|
427
|
-
columns: [t.graphId, t.nodeKind, t.constraintName, t.key]
|
|
428
|
-
}),
|
|
429
|
-
index$1(`${n.uniques}_node_idx`).on(t.graphId, t.concreteKind, t.nodeId)
|
|
430
|
-
]
|
|
431
|
-
);
|
|
432
|
-
const schemaVersions3 = sqliteTable(
|
|
433
|
-
n.schemaVersions,
|
|
434
|
-
{
|
|
435
|
-
graphId: text$1("graph_id").notNull(),
|
|
436
|
-
version: integer$1("version").notNull(),
|
|
437
|
-
schemaHash: text$1("schema_hash").notNull(),
|
|
438
|
-
schemaDoc: text$1("schema_doc").notNull(),
|
|
439
|
-
createdAt: text$1("created_at").notNull(),
|
|
440
|
-
isActive: integer$1("is_active", { mode: "boolean" }).notNull().default(false)
|
|
441
|
-
},
|
|
442
|
-
(t) => [
|
|
443
|
-
primaryKey$1({ columns: [t.graphId, t.version] }),
|
|
444
|
-
index$1(`${n.schemaVersions}_active_idx`).on(t.graphId, t.isActive),
|
|
445
|
-
// Partial unique index enforcing the "at most one active version
|
|
446
|
-
// per graph" invariant at the storage layer. Defense in depth
|
|
447
|
-
// against buggy backend implementations or out-of-band writes.
|
|
448
|
-
// Forces the deactivate-then-activate ordering used by
|
|
449
|
-
// `commitSchemaVersion` and `setActiveVersion`.
|
|
450
|
-
uniqueIndex$1(`${n.schemaVersions}_one_active_per_graph_idx`).on(t.graphId).where(sql`${t.isActive} = 1`)
|
|
451
|
-
]
|
|
452
|
-
);
|
|
453
|
-
const embeddings3 = sqliteTable(
|
|
454
|
-
n.embeddings,
|
|
455
|
-
{
|
|
456
|
-
graphId: text$1("graph_id").notNull(),
|
|
457
|
-
nodeKind: text$1("node_kind").notNull(),
|
|
458
|
-
nodeId: text$1("node_id").notNull(),
|
|
459
|
-
fieldPath: text$1("field_path").notNull(),
|
|
460
|
-
/**
|
|
461
|
-
* Embedding vector.
|
|
462
|
-
* Stored as BLOB for sqlite-vec binary format, or JSON text for fallback.
|
|
463
|
-
* For sqlite-vec: use vec_f32() to convert JSON array to binary.
|
|
464
|
-
*/
|
|
465
|
-
embedding: blob("embedding", { mode: "buffer" }).notNull(),
|
|
466
|
-
/** Number of dimensions (for validation) */
|
|
467
|
-
dimensions: integer$1("dimensions").notNull(),
|
|
468
|
-
createdAt: text$1("created_at").notNull(),
|
|
469
|
-
updatedAt: text$1("updated_at").notNull()
|
|
470
|
-
},
|
|
471
|
-
(t) => [
|
|
472
|
-
primaryKey$1({
|
|
473
|
-
columns: [t.graphId, t.nodeKind, t.nodeId, t.fieldPath]
|
|
474
|
-
}),
|
|
475
|
-
// Index for looking up embeddings by node
|
|
476
|
-
index$1(`${n.embeddings}_node_idx`).on(
|
|
477
|
-
t.graphId,
|
|
478
|
-
t.nodeKind,
|
|
479
|
-
t.nodeId
|
|
480
|
-
),
|
|
481
|
-
// Index for filtering by kind and field (used in vector search)
|
|
482
|
-
index$1(`${n.embeddings}_kind_field_idx`).on(
|
|
483
|
-
t.graphId,
|
|
484
|
-
t.nodeKind,
|
|
485
|
-
t.fieldPath
|
|
486
|
-
)
|
|
487
|
-
]
|
|
488
|
-
);
|
|
489
|
-
const indexMaterializations = sqliteTable(
|
|
490
|
-
n.indexMaterializations,
|
|
491
|
-
{
|
|
492
|
-
indexName: text$1("index_name").notNull(),
|
|
493
|
-
graphId: text$1("graph_id").notNull(),
|
|
494
|
-
entity: text$1("entity").notNull(),
|
|
495
|
-
kind: text$1("kind").notNull(),
|
|
496
|
-
signature: text$1("signature").notNull(),
|
|
497
|
-
schemaVersion: integer$1("schema_version").notNull(),
|
|
498
|
-
materializedAt: text$1("materialized_at"),
|
|
499
|
-
lastAttemptedAt: text$1("last_attempted_at").notNull(),
|
|
500
|
-
lastError: text$1("last_error")
|
|
501
|
-
},
|
|
502
|
-
(t) => [primaryKey$1({ columns: [t.indexName] })]
|
|
503
|
-
);
|
|
504
|
-
const kindRemovals = sqliteTable(
|
|
505
|
-
n.kindRemovals,
|
|
506
|
-
{
|
|
507
|
-
graphId: text$1("graph_id").notNull(),
|
|
508
|
-
kindName: text$1("kind_name").notNull(),
|
|
509
|
-
entity: text$1("entity").notNull(),
|
|
510
|
-
schemaVersion: integer$1("schema_version").notNull(),
|
|
511
|
-
removedAt: text$1("removed_at"),
|
|
512
|
-
lastAttemptedAt: text$1("last_attempted_at").notNull(),
|
|
513
|
-
lastError: text$1("last_error")
|
|
514
|
-
},
|
|
515
|
-
(t) => [
|
|
516
|
-
primaryKey$1({
|
|
517
|
-
columns: [t.graphId, t.kindName, t.entity, t.schemaVersion]
|
|
518
|
-
})
|
|
519
|
-
]
|
|
520
|
-
);
|
|
521
|
-
const reconciliationMarkers = sqliteTable(
|
|
522
|
-
n.reconciliationMarkers,
|
|
523
|
-
{
|
|
524
|
-
graphId: text$1("graph_id").notNull(),
|
|
525
|
-
reconciledToVersion: integer$1("reconciled_to_version").notNull()
|
|
526
|
-
},
|
|
527
|
-
(t) => [primaryKey$1({ columns: [t.graphId] })]
|
|
528
|
-
);
|
|
529
|
-
const contributionMaterializations = sqliteTable(
|
|
530
|
-
n.contributionMaterializations,
|
|
531
|
-
{
|
|
532
|
-
graphId: text$1("graph_id").notNull(),
|
|
533
|
-
logicalName: text$1("logical_name").notNull(),
|
|
534
|
-
owner: text$1("owner").notNull(),
|
|
535
|
-
tableName: text$1("table_name").notNull(),
|
|
536
|
-
signature: text$1("signature").notNull(),
|
|
537
|
-
materializedAt: text$1("materialized_at"),
|
|
538
|
-
lastAttemptedAt: text$1("last_attempted_at").notNull(),
|
|
539
|
-
lastError: text$1("last_error")
|
|
540
|
-
},
|
|
541
|
-
(t) => [
|
|
542
|
-
primaryKey$1({
|
|
543
|
-
columns: [t.graphId, t.logicalName, t.owner, t.tableName]
|
|
544
|
-
})
|
|
545
|
-
]
|
|
546
|
-
);
|
|
547
|
-
return {
|
|
548
|
-
nodes: nodes3,
|
|
549
|
-
edges: edges3,
|
|
550
|
-
uniques: uniques3,
|
|
551
|
-
schemaVersions: schemaVersions3,
|
|
552
|
-
embeddings: embeddings3,
|
|
553
|
-
indexMaterializations,
|
|
554
|
-
contributionMaterializations,
|
|
555
|
-
kindRemovals,
|
|
556
|
-
reconciliationMarkers,
|
|
557
|
-
/**
|
|
558
|
-
* The fulltext storage is a FTS5 virtual table which Drizzle cannot
|
|
559
|
-
* represent. DDL is emitted as raw SQL and operations query it via
|
|
560
|
-
* `sql.raw()`.
|
|
561
|
-
*/
|
|
562
|
-
fulltextTableName: n.fulltext
|
|
563
|
-
};
|
|
564
|
-
}
|
|
565
|
-
var tables2 = createSqliteTables();
|
|
566
|
-
var { nodes: nodes2, edges: edges2, uniques: uniques2, schemaVersions: schemaVersions2, embeddings: embeddings2 } = tables2;
|
|
252
|
+
var tables2 = createPostgresTables();
|
|
253
|
+
var { nodes, edges, uniques, schemaVersions, fulltext } = tables2;
|
|
567
254
|
function getSqliteColumnType(column) {
|
|
568
255
|
switch (column.columnType) {
|
|
569
256
|
case "SQLiteText": {
|
|
@@ -619,9 +306,9 @@ function renderIndexColumn(col) {
|
|
|
619
306
|
if (col && typeof col === "object" && "name" in col) {
|
|
620
307
|
return `"${col.name}"`;
|
|
621
308
|
}
|
|
622
|
-
const
|
|
623
|
-
if (
|
|
624
|
-
return
|
|
309
|
+
const sql12 = tryInlineSql(col);
|
|
310
|
+
if (sql12 !== void 0) {
|
|
311
|
+
return sql12;
|
|
625
312
|
}
|
|
626
313
|
return "unknown";
|
|
627
314
|
}
|
|
@@ -680,8 +367,8 @@ function inlineSqlOrThrow(value, context) {
|
|
|
680
367
|
function generateSqliteCreateIndexSQL(table) {
|
|
681
368
|
const config = getTableConfig(table);
|
|
682
369
|
const statements = [];
|
|
683
|
-
for (const
|
|
684
|
-
const indexConfig =
|
|
370
|
+
for (const index2 of config.indexes) {
|
|
371
|
+
const indexConfig = index2.config;
|
|
685
372
|
const columns = indexConfig.columns.map((c) => renderIndexColumn(c)).join(", ");
|
|
686
373
|
const unique = indexConfig.unique ? "UNIQUE " : "";
|
|
687
374
|
const where = indexConfig.where ? ` WHERE ${inlineSqlOrThrow(indexConfig.where, `SQLite index "${indexConfig.name}" WHERE clause`)}` : "";
|
|
@@ -697,7 +384,7 @@ function isSqliteTable(value) {
|
|
|
697
384
|
function runtimeStrategyContributions(fulltextStrategy, fulltextTableName) {
|
|
698
385
|
return fulltextStrategy.ownedTables(fulltextTableName).filter((contribution) => contribution.runtimeEnsure);
|
|
699
386
|
}
|
|
700
|
-
function sqliteContributions(tables3 =
|
|
387
|
+
function sqliteContributions(tables3 = tables, fulltextStrategy = fts5Strategy) {
|
|
701
388
|
const contributions = [];
|
|
702
389
|
for (const [key, table] of Object.entries(tables3)) {
|
|
703
390
|
if (!isSqliteTable(table)) continue;
|
|
@@ -719,10 +406,12 @@ function sqliteContributions(tables3 = tables2, fulltextStrategy = fts5Strategy)
|
|
|
719
406
|
}
|
|
720
407
|
return contributions;
|
|
721
408
|
}
|
|
722
|
-
function generateSqliteDDL(tables3 =
|
|
723
|
-
return sqliteContributions(tables3, fulltextStrategy).flatMap(
|
|
409
|
+
function generateSqliteDDL(tables3 = tables, fulltextStrategy = fts5Strategy) {
|
|
410
|
+
return sqliteContributions(tables3, fulltextStrategy).flatMap(
|
|
411
|
+
(contribution) => [...contribution.createDdl]
|
|
412
|
+
);
|
|
724
413
|
}
|
|
725
|
-
function generateSqliteMigrationSQL(tables3 =
|
|
414
|
+
function generateSqliteMigrationSQL(tables3 = tables, fulltextStrategy = fts5Strategy) {
|
|
726
415
|
return generateSqliteDDL(tables3, fulltextStrategy).join("\n\n");
|
|
727
416
|
}
|
|
728
417
|
function getPgColumnType(column) {
|
|
@@ -768,10 +457,7 @@ function generatePgCreateTableSQL(table) {
|
|
|
768
457
|
const config = getTableConfig$1(table);
|
|
769
458
|
const columnDefs = [];
|
|
770
459
|
for (const column of config.columns) {
|
|
771
|
-
const parts = [
|
|
772
|
-
`"${column.name}"`,
|
|
773
|
-
getPgColumnType(column)
|
|
774
|
-
];
|
|
460
|
+
const parts = [`"${column.name}"`, getPgColumnType(column)];
|
|
775
461
|
if (column.notNull) {
|
|
776
462
|
parts.push("NOT NULL");
|
|
777
463
|
}
|
|
@@ -792,8 +478,8 @@ function generatePgCreateTableSQL(table) {
|
|
|
792
478
|
function generatePgCreateIndexSQL(table) {
|
|
793
479
|
const config = getTableConfig$1(table);
|
|
794
480
|
const statements = [];
|
|
795
|
-
for (const
|
|
796
|
-
const indexConfig =
|
|
481
|
+
for (const index2 of config.indexes) {
|
|
482
|
+
const indexConfig = index2.config;
|
|
797
483
|
const columns = indexConfig.columns.map((c) => renderIndexColumn(c)).join(", ");
|
|
798
484
|
const unique = indexConfig.unique ? "UNIQUE " : "";
|
|
799
485
|
const method = indexConfig.method && indexConfig.method !== "btree" ? ` USING ${indexConfig.method}` : "";
|
|
@@ -807,7 +493,7 @@ function generatePgCreateIndexSQL(table) {
|
|
|
807
493
|
function isPgTable(value) {
|
|
808
494
|
return is(value, PgTable);
|
|
809
495
|
}
|
|
810
|
-
function postgresContributions(tables3 =
|
|
496
|
+
function postgresContributions(tables3 = tables2, fulltextStrategy = tsvectorStrategy) {
|
|
811
497
|
const contributions = [];
|
|
812
498
|
for (const [key, table] of Object.entries(tables3)) {
|
|
813
499
|
if (!isPgTable(table)) continue;
|
|
@@ -830,10 +516,12 @@ function postgresContributions(tables3 = tables, fulltextStrategy = tsvectorStra
|
|
|
830
516
|
}
|
|
831
517
|
return contributions;
|
|
832
518
|
}
|
|
833
|
-
function generatePostgresDDL(tables3 =
|
|
834
|
-
return postgresContributions(tables3, fulltextStrategy).flatMap(
|
|
519
|
+
function generatePostgresDDL(tables3 = tables2, fulltextStrategy = tsvectorStrategy) {
|
|
520
|
+
return postgresContributions(tables3, fulltextStrategy).flatMap(
|
|
521
|
+
(contribution) => [...contribution.createDdl]
|
|
522
|
+
);
|
|
835
523
|
}
|
|
836
|
-
function generatePostgresMigrationSQL(tables3 =
|
|
524
|
+
function generatePostgresMigrationSQL(tables3 = tables2, fulltextStrategy = tsvectorStrategy) {
|
|
837
525
|
const extensionSql = "-- Enable pgvector extension for vector similarity search\nCREATE EXTENSION IF NOT EXISTS vector;";
|
|
838
526
|
const ddlSql = generatePostgresDDL(tables3, fulltextStrategy).join("\n\n");
|
|
839
527
|
return `${extensionSql}
|
|
@@ -841,6 +529,83 @@ function generatePostgresMigrationSQL(tables3 = tables, fulltextStrategy = tsvec
|
|
|
841
529
|
${ddlSql}`;
|
|
842
530
|
}
|
|
843
531
|
|
|
532
|
+
// src/backend/drizzle/vector-runtime.ts
|
|
533
|
+
function vectorSlotKey(slot) {
|
|
534
|
+
return JSON.stringify([
|
|
535
|
+
slot.graphId,
|
|
536
|
+
slot.nodeKind,
|
|
537
|
+
slot.fieldPath,
|
|
538
|
+
slot.dimensions,
|
|
539
|
+
slot.metric,
|
|
540
|
+
slot.indexType
|
|
541
|
+
]);
|
|
542
|
+
}
|
|
543
|
+
function createVectorSlotLatch() {
|
|
544
|
+
const inFlight = /* @__PURE__ */ new Map();
|
|
545
|
+
return {
|
|
546
|
+
ensure(strategy, slot, execDdl) {
|
|
547
|
+
const key = vectorSlotKey(slot);
|
|
548
|
+
const existing = inFlight.get(key);
|
|
549
|
+
if (existing !== void 0) return existing;
|
|
550
|
+
const run = (async () => {
|
|
551
|
+
for (const contribution of strategy.ownedTables(slot)) {
|
|
552
|
+
for (const statement of contribution.createDdl) {
|
|
553
|
+
await execDdl(statement);
|
|
554
|
+
}
|
|
555
|
+
}
|
|
556
|
+
})();
|
|
557
|
+
const cached = run.catch((error) => {
|
|
558
|
+
inFlight.delete(key);
|
|
559
|
+
throw error;
|
|
560
|
+
});
|
|
561
|
+
inFlight.set(key, cached);
|
|
562
|
+
return cached;
|
|
563
|
+
}
|
|
564
|
+
};
|
|
565
|
+
}
|
|
566
|
+
var VECTOR_SLOT_PLACEHOLDER_DIMENSIONS = 1;
|
|
567
|
+
function vectorSlotFromParams(params) {
|
|
568
|
+
return {
|
|
569
|
+
graphId: params.graphId,
|
|
570
|
+
nodeKind: params.nodeKind,
|
|
571
|
+
fieldPath: params.fieldPath,
|
|
572
|
+
dimensions: params.dimensions,
|
|
573
|
+
metric: params.metric,
|
|
574
|
+
indexType: params.indexType
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
function vectorSlotFromCreateIndexParams(params) {
|
|
578
|
+
return {
|
|
579
|
+
...vectorSlotFromParams(params),
|
|
580
|
+
...params.indexParams === void 0 ? {} : { indexParams: params.indexParams }
|
|
581
|
+
};
|
|
582
|
+
}
|
|
583
|
+
function vectorSlotFromDropIndexParams(params) {
|
|
584
|
+
return {
|
|
585
|
+
graphId: params.graphId,
|
|
586
|
+
nodeKind: params.nodeKind,
|
|
587
|
+
fieldPath: params.fieldPath,
|
|
588
|
+
dimensions: VECTOR_SLOT_PLACEHOLDER_DIMENSIONS,
|
|
589
|
+
metric: "cosine",
|
|
590
|
+
indexType: "hnsw"
|
|
591
|
+
};
|
|
592
|
+
}
|
|
593
|
+
function mapVectorWriteError(error, params) {
|
|
594
|
+
const mismatch = parseDimensionMismatch(error);
|
|
595
|
+
if (mismatch === void 0) return error;
|
|
596
|
+
const attempted = mismatch.actual ?? "a different number of";
|
|
597
|
+
return new EmbeddingDimensionChangedError(
|
|
598
|
+
`Embedding "${params.nodeKind}.${params.fieldPath}" was written with ${attempted} dimensions, but its per-field storage expects ${mismatch.expected}. The field's embedding dimension changed; existing vectors must be recreated and re-embedded.`,
|
|
599
|
+
{
|
|
600
|
+
kind: params.nodeKind,
|
|
601
|
+
fieldPath: params.fieldPath,
|
|
602
|
+
storedDimensions: mismatch.expected,
|
|
603
|
+
...mismatch.actual === void 0 ? {} : { declaredDimensions: mismatch.actual }
|
|
604
|
+
},
|
|
605
|
+
{ cause: error }
|
|
606
|
+
);
|
|
607
|
+
}
|
|
608
|
+
|
|
844
609
|
// src/backend/drizzle/row-mappers.ts
|
|
845
610
|
function requireTimestamp(value, field) {
|
|
846
611
|
if (value === void 0) {
|
|
@@ -1325,8 +1090,8 @@ function chunkArray(values, size) {
|
|
|
1325
1090
|
return [values];
|
|
1326
1091
|
}
|
|
1327
1092
|
const chunks = [];
|
|
1328
|
-
for (let
|
|
1329
|
-
chunks.push(values.slice(
|
|
1093
|
+
for (let index2 = 0; index2 < values.length; index2 += size) {
|
|
1094
|
+
chunks.push(values.slice(index2, index2 + size));
|
|
1330
1095
|
}
|
|
1331
1096
|
return chunks;
|
|
1332
1097
|
}
|
|
@@ -1414,18 +1179,19 @@ function createCommonOperationBackend(options) {
|
|
|
1414
1179
|
},
|
|
1415
1180
|
// IMPORTANT: This cascade is not atomic. Callers must ensure this runs
|
|
1416
1181
|
// within a transaction to prevent partial deletion on intermediate failure.
|
|
1182
|
+
//
|
|
1183
|
+
// Embeddings are NOT cleaned up here: they live in per-`(nodeKind,
|
|
1184
|
+
// fieldPath)` strategy-owned tables addressable only with the slot
|
|
1185
|
+
// context the graph-agnostic backend lacks. The store's hard-delete
|
|
1186
|
+
// path (`executeNodeHardDelete`) drives `deleteNodeEmbeddings`, which
|
|
1187
|
+
// resolves each embedding field and routes a per-field
|
|
1188
|
+
// `backend.deleteEmbedding` through the active vector strategy.
|
|
1417
1189
|
async hardDeleteNode(params) {
|
|
1418
1190
|
const deleteUniquesQuery = operationStrategy.buildHardDeleteUniquesByNode(
|
|
1419
1191
|
params.graphId,
|
|
1420
1192
|
params.id
|
|
1421
1193
|
);
|
|
1422
1194
|
await execution.execRun(deleteUniquesQuery);
|
|
1423
|
-
const deleteEmbeddingsQuery = operationStrategy.buildHardDeleteEmbeddingsByNode(
|
|
1424
|
-
params.graphId,
|
|
1425
|
-
params.kind,
|
|
1426
|
-
params.id
|
|
1427
|
-
);
|
|
1428
|
-
await execution.execRun(deleteEmbeddingsQuery);
|
|
1429
1195
|
const deleteFulltextStatements = operationStrategy.buildDeleteFulltextByNode(
|
|
1430
1196
|
params.graphId,
|
|
1431
1197
|
params.kind,
|
|
@@ -1695,7 +1461,6 @@ function buildClearGraph(tables3, graphId) {
|
|
|
1695
1461
|
const fulltext2 = quotedTableName(tables3.fulltextTableName);
|
|
1696
1462
|
return [
|
|
1697
1463
|
sql`DELETE FROM ${fulltext2} WHERE "graph_id" = ${graphId}`,
|
|
1698
|
-
sql`DELETE FROM ${tables3.embeddings} WHERE ${tables3.embeddings.graphId} = ${graphId}`,
|
|
1699
1464
|
sql`DELETE FROM ${tables3.uniques} WHERE ${tables3.uniques.graphId} = ${graphId}`,
|
|
1700
1465
|
sql`DELETE FROM ${tables3.edges} WHERE ${tables3.edges.graphId} = ${graphId}`,
|
|
1701
1466
|
sql`DELETE FROM ${tables3.nodes} WHERE ${tables3.nodes.graphId} = ${graphId}`,
|
|
@@ -1726,20 +1491,20 @@ function buildTemporalConditions(table, params) {
|
|
|
1726
1491
|
return conditions;
|
|
1727
1492
|
}
|
|
1728
1493
|
function buildFindNodesByKind(tables3, params) {
|
|
1729
|
-
const { nodes:
|
|
1494
|
+
const { nodes: nodes2 } = tables3;
|
|
1730
1495
|
const conditions = [
|
|
1731
|
-
sql`${
|
|
1732
|
-
sql`${
|
|
1733
|
-
...buildTemporalConditions(
|
|
1496
|
+
sql`${nodes2.graphId} = ${params.graphId}`,
|
|
1497
|
+
sql`${nodes2.kind} = ${params.kind}`,
|
|
1498
|
+
...buildTemporalConditions(nodes2, params)
|
|
1734
1499
|
];
|
|
1735
1500
|
if (params.orderBy === "id" && params.after !== void 0) {
|
|
1736
|
-
conditions.push(sql`${
|
|
1501
|
+
conditions.push(sql`${nodes2.id} > ${params.after}`);
|
|
1737
1502
|
}
|
|
1738
1503
|
const whereClause = sql.join(conditions, sql` AND `);
|
|
1739
|
-
const orderByClause = params.orderBy === "id" ? sql`${
|
|
1504
|
+
const orderByClause = params.orderBy === "id" ? sql`${nodes2.id} ASC` : sql`${nodes2.createdAt} DESC, ${nodes2.id} DESC`;
|
|
1740
1505
|
if (params.limit !== void 0 && params.offset !== void 0) {
|
|
1741
1506
|
return sql`
|
|
1742
|
-
SELECT * FROM ${
|
|
1507
|
+
SELECT * FROM ${nodes2}
|
|
1743
1508
|
WHERE ${whereClause}
|
|
1744
1509
|
ORDER BY ${orderByClause}
|
|
1745
1510
|
LIMIT ${params.limit} OFFSET ${params.offset}
|
|
@@ -1747,104 +1512,104 @@ function buildFindNodesByKind(tables3, params) {
|
|
|
1747
1512
|
}
|
|
1748
1513
|
if (params.limit !== void 0) {
|
|
1749
1514
|
return sql`
|
|
1750
|
-
SELECT * FROM ${
|
|
1515
|
+
SELECT * FROM ${nodes2}
|
|
1751
1516
|
WHERE ${whereClause}
|
|
1752
1517
|
ORDER BY ${orderByClause}
|
|
1753
1518
|
LIMIT ${params.limit}
|
|
1754
1519
|
`;
|
|
1755
1520
|
}
|
|
1756
1521
|
return sql`
|
|
1757
|
-
SELECT * FROM ${
|
|
1522
|
+
SELECT * FROM ${nodes2}
|
|
1758
1523
|
WHERE ${whereClause}
|
|
1759
1524
|
ORDER BY ${orderByClause}
|
|
1760
1525
|
`;
|
|
1761
1526
|
}
|
|
1762
1527
|
function buildCountNodesByKind(tables3, params) {
|
|
1763
|
-
const { nodes:
|
|
1528
|
+
const { nodes: nodes2 } = tables3;
|
|
1764
1529
|
const conditions = [
|
|
1765
|
-
sql`${
|
|
1766
|
-
sql`${
|
|
1767
|
-
...buildTemporalConditions(
|
|
1530
|
+
sql`${nodes2.graphId} = ${params.graphId}`,
|
|
1531
|
+
sql`${nodes2.kind} = ${params.kind}`,
|
|
1532
|
+
...buildTemporalConditions(nodes2, params)
|
|
1768
1533
|
];
|
|
1769
1534
|
const whereClause = sql.join(conditions, sql` AND `);
|
|
1770
1535
|
return sql`
|
|
1771
|
-
SELECT COUNT(*) as count FROM ${
|
|
1536
|
+
SELECT COUNT(*) as count FROM ${nodes2}
|
|
1772
1537
|
WHERE ${whereClause}
|
|
1773
1538
|
`;
|
|
1774
1539
|
}
|
|
1775
1540
|
function buildFindEdgesByKind(tables3, params) {
|
|
1776
|
-
const { edges:
|
|
1541
|
+
const { edges: edges2 } = tables3;
|
|
1777
1542
|
const conditions = [
|
|
1778
|
-
sql`${
|
|
1779
|
-
sql`${
|
|
1780
|
-
...buildTemporalConditions(
|
|
1543
|
+
sql`${edges2.graphId} = ${params.graphId}`,
|
|
1544
|
+
sql`${edges2.kind} = ${params.kind}`,
|
|
1545
|
+
...buildTemporalConditions(edges2, params)
|
|
1781
1546
|
];
|
|
1782
1547
|
if (params.fromKind !== void 0) {
|
|
1783
|
-
conditions.push(sql`${
|
|
1548
|
+
conditions.push(sql`${edges2.fromKind} = ${params.fromKind}`);
|
|
1784
1549
|
}
|
|
1785
1550
|
if (params.fromId !== void 0) {
|
|
1786
|
-
conditions.push(sql`${
|
|
1551
|
+
conditions.push(sql`${edges2.fromId} = ${params.fromId}`);
|
|
1787
1552
|
}
|
|
1788
1553
|
if (params.toKind !== void 0) {
|
|
1789
|
-
conditions.push(sql`${
|
|
1554
|
+
conditions.push(sql`${edges2.toKind} = ${params.toKind}`);
|
|
1790
1555
|
}
|
|
1791
1556
|
if (params.toId !== void 0) {
|
|
1792
|
-
conditions.push(sql`${
|
|
1557
|
+
conditions.push(sql`${edges2.toId} = ${params.toId}`);
|
|
1793
1558
|
}
|
|
1794
1559
|
const whereClause = sql.join(conditions, sql` AND `);
|
|
1795
1560
|
if (params.limit !== void 0 && params.offset !== void 0) {
|
|
1796
1561
|
return sql`
|
|
1797
|
-
SELECT * FROM ${
|
|
1562
|
+
SELECT * FROM ${edges2}
|
|
1798
1563
|
WHERE ${whereClause}
|
|
1799
|
-
ORDER BY ${
|
|
1564
|
+
ORDER BY ${edges2.createdAt} DESC
|
|
1800
1565
|
LIMIT ${params.limit} OFFSET ${params.offset}
|
|
1801
1566
|
`;
|
|
1802
1567
|
}
|
|
1803
1568
|
if (params.limit !== void 0) {
|
|
1804
1569
|
return sql`
|
|
1805
|
-
SELECT * FROM ${
|
|
1570
|
+
SELECT * FROM ${edges2}
|
|
1806
1571
|
WHERE ${whereClause}
|
|
1807
|
-
ORDER BY ${
|
|
1572
|
+
ORDER BY ${edges2.createdAt} DESC
|
|
1808
1573
|
LIMIT ${params.limit}
|
|
1809
1574
|
`;
|
|
1810
1575
|
}
|
|
1811
1576
|
return sql`
|
|
1812
|
-
SELECT * FROM ${
|
|
1577
|
+
SELECT * FROM ${edges2}
|
|
1813
1578
|
WHERE ${whereClause}
|
|
1814
|
-
ORDER BY ${
|
|
1579
|
+
ORDER BY ${edges2.createdAt} DESC
|
|
1815
1580
|
`;
|
|
1816
1581
|
}
|
|
1817
1582
|
function buildCountEdgesByKind(tables3, params) {
|
|
1818
|
-
const { edges:
|
|
1583
|
+
const { edges: edges2 } = tables3;
|
|
1819
1584
|
const conditions = [
|
|
1820
|
-
sql`${
|
|
1821
|
-
sql`${
|
|
1822
|
-
...buildTemporalConditions(
|
|
1585
|
+
sql`${edges2.graphId} = ${params.graphId}`,
|
|
1586
|
+
sql`${edges2.kind} = ${params.kind}`,
|
|
1587
|
+
...buildTemporalConditions(edges2, params)
|
|
1823
1588
|
];
|
|
1824
1589
|
if (params.fromKind !== void 0) {
|
|
1825
|
-
conditions.push(sql`${
|
|
1590
|
+
conditions.push(sql`${edges2.fromKind} = ${params.fromKind}`);
|
|
1826
1591
|
}
|
|
1827
1592
|
if (params.fromId !== void 0) {
|
|
1828
|
-
conditions.push(sql`${
|
|
1593
|
+
conditions.push(sql`${edges2.fromId} = ${params.fromId}`);
|
|
1829
1594
|
}
|
|
1830
1595
|
if (params.toKind !== void 0) {
|
|
1831
|
-
conditions.push(sql`${
|
|
1596
|
+
conditions.push(sql`${edges2.toKind} = ${params.toKind}`);
|
|
1832
1597
|
}
|
|
1833
1598
|
if (params.toId !== void 0) {
|
|
1834
|
-
conditions.push(sql`${
|
|
1599
|
+
conditions.push(sql`${edges2.toId} = ${params.toId}`);
|
|
1835
1600
|
}
|
|
1836
1601
|
const whereClause = sql.join(conditions, sql` AND `);
|
|
1837
1602
|
return sql`
|
|
1838
|
-
SELECT COUNT(*) as count FROM ${
|
|
1603
|
+
SELECT COUNT(*) as count FROM ${edges2}
|
|
1839
1604
|
WHERE ${whereClause}
|
|
1840
1605
|
`;
|
|
1841
1606
|
}
|
|
1842
1607
|
function buildInsertEdge(tables3, params, timestamp2) {
|
|
1843
|
-
const { edges:
|
|
1608
|
+
const { edges: edges2 } = tables3;
|
|
1844
1609
|
const propsJson = JSON.stringify(params.props);
|
|
1845
|
-
const columns = edgeColumnList(
|
|
1610
|
+
const columns = edgeColumnList(edges2);
|
|
1846
1611
|
return sql`
|
|
1847
|
-
INSERT INTO ${
|
|
1612
|
+
INSERT INTO ${edges2} (${columns})
|
|
1848
1613
|
VALUES (
|
|
1849
1614
|
${params.graphId}, ${params.id}, ${params.kind},
|
|
1850
1615
|
${params.fromKind}, ${params.fromId}, ${params.toKind}, ${params.toId},
|
|
@@ -1855,11 +1620,11 @@ function buildInsertEdge(tables3, params, timestamp2) {
|
|
|
1855
1620
|
`;
|
|
1856
1621
|
}
|
|
1857
1622
|
function buildInsertEdgeNoReturn(tables3, params, timestamp2) {
|
|
1858
|
-
const { edges:
|
|
1623
|
+
const { edges: edges2 } = tables3;
|
|
1859
1624
|
const propsJson = JSON.stringify(params.props);
|
|
1860
|
-
const columns = edgeColumnList(
|
|
1625
|
+
const columns = edgeColumnList(edges2);
|
|
1861
1626
|
return sql`
|
|
1862
|
-
INSERT INTO ${
|
|
1627
|
+
INSERT INTO ${edges2} (${columns})
|
|
1863
1628
|
VALUES (
|
|
1864
1629
|
${params.graphId}, ${params.id}, ${params.kind},
|
|
1865
1630
|
${params.fromKind}, ${params.fromId}, ${params.toKind}, ${params.toId},
|
|
@@ -1869,164 +1634,164 @@ function buildInsertEdgeNoReturn(tables3, params, timestamp2) {
|
|
|
1869
1634
|
`;
|
|
1870
1635
|
}
|
|
1871
1636
|
function buildInsertEdgesBatch(tables3, params, timestamp2) {
|
|
1872
|
-
const { edges:
|
|
1873
|
-
const columns = edgeColumnList(
|
|
1637
|
+
const { edges: edges2 } = tables3;
|
|
1638
|
+
const columns = edgeColumnList(edges2);
|
|
1874
1639
|
const values = params.map((edgeParams) => {
|
|
1875
1640
|
const propsJson = JSON.stringify(edgeParams.props);
|
|
1876
1641
|
return sql`(${edgeParams.graphId}, ${edgeParams.id}, ${edgeParams.kind}, ${edgeParams.fromKind}, ${edgeParams.fromId}, ${edgeParams.toKind}, ${edgeParams.toId}, ${propsJson}, ${sqlNull(edgeParams.validFrom)}, ${sqlNull(edgeParams.validTo)}, ${timestamp2}, ${timestamp2})`;
|
|
1877
1642
|
});
|
|
1878
1643
|
return sql`
|
|
1879
|
-
INSERT INTO ${
|
|
1644
|
+
INSERT INTO ${edges2} (${columns})
|
|
1880
1645
|
VALUES ${sql.join(values, sql`, `)}
|
|
1881
1646
|
`;
|
|
1882
1647
|
}
|
|
1883
1648
|
function buildInsertEdgesBatchReturning(tables3, params, timestamp2) {
|
|
1884
|
-
const { edges:
|
|
1885
|
-
const columns = edgeColumnList(
|
|
1649
|
+
const { edges: edges2 } = tables3;
|
|
1650
|
+
const columns = edgeColumnList(edges2);
|
|
1886
1651
|
const values = params.map((edgeParams) => {
|
|
1887
1652
|
const propsJson = JSON.stringify(edgeParams.props);
|
|
1888
1653
|
return sql`(${edgeParams.graphId}, ${edgeParams.id}, ${edgeParams.kind}, ${edgeParams.fromKind}, ${edgeParams.fromId}, ${edgeParams.toKind}, ${edgeParams.toId}, ${propsJson}, ${sqlNull(edgeParams.validFrom)}, ${sqlNull(edgeParams.validTo)}, ${timestamp2}, ${timestamp2})`;
|
|
1889
1654
|
});
|
|
1890
1655
|
return sql`
|
|
1891
|
-
INSERT INTO ${
|
|
1656
|
+
INSERT INTO ${edges2} (${columns})
|
|
1892
1657
|
VALUES ${sql.join(values, sql`, `)}
|
|
1893
1658
|
RETURNING *
|
|
1894
1659
|
`;
|
|
1895
1660
|
}
|
|
1896
1661
|
function buildGetEdge(tables3, graphId, id) {
|
|
1897
|
-
const { edges:
|
|
1662
|
+
const { edges: edges2 } = tables3;
|
|
1898
1663
|
return sql`
|
|
1899
|
-
SELECT * FROM ${
|
|
1900
|
-
WHERE ${
|
|
1901
|
-
AND ${
|
|
1664
|
+
SELECT * FROM ${edges2}
|
|
1665
|
+
WHERE ${edges2.graphId} = ${graphId}
|
|
1666
|
+
AND ${edges2.id} = ${id}
|
|
1902
1667
|
`;
|
|
1903
1668
|
}
|
|
1904
1669
|
function buildGetEdges(tables3, graphId, ids) {
|
|
1905
|
-
const { edges:
|
|
1670
|
+
const { edges: edges2 } = tables3;
|
|
1906
1671
|
const idPlaceholders = sql.join(
|
|
1907
1672
|
ids.map((edgeId) => sql`${edgeId}`),
|
|
1908
1673
|
sql`, `
|
|
1909
1674
|
);
|
|
1910
1675
|
return sql`
|
|
1911
|
-
SELECT * FROM ${
|
|
1912
|
-
WHERE ${
|
|
1913
|
-
AND ${
|
|
1676
|
+
SELECT * FROM ${edges2}
|
|
1677
|
+
WHERE ${edges2.graphId} = ${graphId}
|
|
1678
|
+
AND ${edges2.id} IN (${idPlaceholders})
|
|
1914
1679
|
`;
|
|
1915
1680
|
}
|
|
1916
1681
|
function buildUpdateEdge(tables3, params, timestamp2) {
|
|
1917
|
-
const { edges:
|
|
1682
|
+
const { edges: edges2 } = tables3;
|
|
1918
1683
|
const propsJson = JSON.stringify(params.props);
|
|
1919
1684
|
const setParts = [
|
|
1920
|
-
sql`${quotedColumn(
|
|
1921
|
-
sql`${quotedColumn(
|
|
1685
|
+
sql`${quotedColumn(edges2.props)} = ${propsJson}`,
|
|
1686
|
+
sql`${quotedColumn(edges2.updatedAt)} = ${timestamp2}`
|
|
1922
1687
|
];
|
|
1923
1688
|
if (params.validTo !== void 0) {
|
|
1924
|
-
setParts.push(sql`${quotedColumn(
|
|
1689
|
+
setParts.push(sql`${quotedColumn(edges2.validTo)} = ${params.validTo}`);
|
|
1925
1690
|
}
|
|
1926
1691
|
if (params.clearDeleted) {
|
|
1927
|
-
setParts.push(sql`${quotedColumn(
|
|
1692
|
+
setParts.push(sql`${quotedColumn(edges2.deletedAt)} = NULL`);
|
|
1928
1693
|
}
|
|
1929
1694
|
const setClause = sql.join(setParts, sql`, `);
|
|
1930
1695
|
if (params.clearDeleted) {
|
|
1931
1696
|
return sql`
|
|
1932
|
-
UPDATE ${
|
|
1697
|
+
UPDATE ${edges2}
|
|
1933
1698
|
SET ${setClause}
|
|
1934
|
-
WHERE ${
|
|
1935
|
-
AND ${
|
|
1699
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1700
|
+
AND ${edges2.id} = ${params.id}
|
|
1936
1701
|
RETURNING *
|
|
1937
1702
|
`;
|
|
1938
1703
|
}
|
|
1939
1704
|
return sql`
|
|
1940
|
-
UPDATE ${
|
|
1705
|
+
UPDATE ${edges2}
|
|
1941
1706
|
SET ${setClause}
|
|
1942
|
-
WHERE ${
|
|
1943
|
-
AND ${
|
|
1944
|
-
AND ${
|
|
1707
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1708
|
+
AND ${edges2.id} = ${params.id}
|
|
1709
|
+
AND ${edges2.deletedAt} IS NULL
|
|
1945
1710
|
RETURNING *
|
|
1946
1711
|
`;
|
|
1947
1712
|
}
|
|
1948
1713
|
function buildDeleteEdge(tables3, params, timestamp2) {
|
|
1949
|
-
const { edges:
|
|
1714
|
+
const { edges: edges2 } = tables3;
|
|
1950
1715
|
return sql`
|
|
1951
|
-
UPDATE ${
|
|
1952
|
-
SET ${quotedColumn(
|
|
1953
|
-
WHERE ${
|
|
1954
|
-
AND ${
|
|
1955
|
-
AND ${
|
|
1716
|
+
UPDATE ${edges2}
|
|
1717
|
+
SET ${quotedColumn(edges2.deletedAt)} = ${timestamp2}
|
|
1718
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1719
|
+
AND ${edges2.id} = ${params.id}
|
|
1720
|
+
AND ${edges2.deletedAt} IS NULL
|
|
1956
1721
|
`;
|
|
1957
1722
|
}
|
|
1958
1723
|
function buildHardDeleteEdge(tables3, params) {
|
|
1959
|
-
const { edges:
|
|
1724
|
+
const { edges: edges2 } = tables3;
|
|
1960
1725
|
return sql`
|
|
1961
|
-
DELETE FROM ${
|
|
1962
|
-
WHERE ${
|
|
1963
|
-
AND ${
|
|
1726
|
+
DELETE FROM ${edges2}
|
|
1727
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1728
|
+
AND ${edges2.id} = ${params.id}
|
|
1964
1729
|
`;
|
|
1965
1730
|
}
|
|
1966
1731
|
function buildHardDeleteEdgesByNode(tables3, graphId, nodeKind, nodeId) {
|
|
1967
|
-
const { edges:
|
|
1732
|
+
const { edges: edges2 } = tables3;
|
|
1968
1733
|
return sql`
|
|
1969
|
-
DELETE FROM ${
|
|
1970
|
-
WHERE ${
|
|
1734
|
+
DELETE FROM ${edges2}
|
|
1735
|
+
WHERE ${edges2.graphId} = ${graphId}
|
|
1971
1736
|
AND (
|
|
1972
|
-
(${
|
|
1973
|
-
OR (${
|
|
1737
|
+
(${edges2.fromKind} = ${nodeKind} AND ${edges2.fromId} = ${nodeId})
|
|
1738
|
+
OR (${edges2.toKind} = ${nodeKind} AND ${edges2.toId} = ${nodeId})
|
|
1974
1739
|
)
|
|
1975
1740
|
`;
|
|
1976
1741
|
}
|
|
1977
1742
|
function buildCountEdgesFrom(tables3, params) {
|
|
1978
|
-
const { edges:
|
|
1743
|
+
const { edges: edges2 } = tables3;
|
|
1979
1744
|
if (params.activeOnly) {
|
|
1980
1745
|
return sql`
|
|
1981
|
-
SELECT COUNT(*) as count FROM ${
|
|
1982
|
-
WHERE ${
|
|
1983
|
-
AND ${
|
|
1984
|
-
AND ${
|
|
1985
|
-
AND ${
|
|
1986
|
-
AND ${
|
|
1987
|
-
AND ${
|
|
1746
|
+
SELECT COUNT(*) as count FROM ${edges2}
|
|
1747
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1748
|
+
AND ${edges2.kind} = ${params.edgeKind}
|
|
1749
|
+
AND ${edges2.fromKind} = ${params.fromKind}
|
|
1750
|
+
AND ${edges2.fromId} = ${params.fromId}
|
|
1751
|
+
AND ${edges2.deletedAt} IS NULL
|
|
1752
|
+
AND ${edges2.validTo} IS NULL
|
|
1988
1753
|
`;
|
|
1989
1754
|
}
|
|
1990
1755
|
return sql`
|
|
1991
|
-
SELECT COUNT(*) as count FROM ${
|
|
1992
|
-
WHERE ${
|
|
1993
|
-
AND ${
|
|
1994
|
-
AND ${
|
|
1995
|
-
AND ${
|
|
1996
|
-
AND ${
|
|
1756
|
+
SELECT COUNT(*) as count FROM ${edges2}
|
|
1757
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1758
|
+
AND ${edges2.kind} = ${params.edgeKind}
|
|
1759
|
+
AND ${edges2.fromKind} = ${params.fromKind}
|
|
1760
|
+
AND ${edges2.fromId} = ${params.fromId}
|
|
1761
|
+
AND ${edges2.deletedAt} IS NULL
|
|
1997
1762
|
`;
|
|
1998
1763
|
}
|
|
1999
1764
|
function buildEdgeExistsBetween(tables3, params) {
|
|
2000
|
-
const { edges:
|
|
1765
|
+
const { edges: edges2 } = tables3;
|
|
2001
1766
|
return sql`
|
|
2002
|
-
SELECT 1 FROM ${
|
|
2003
|
-
WHERE ${
|
|
2004
|
-
AND ${
|
|
2005
|
-
AND ${
|
|
2006
|
-
AND ${
|
|
2007
|
-
AND ${
|
|
2008
|
-
AND ${
|
|
2009
|
-
AND ${
|
|
1767
|
+
SELECT 1 FROM ${edges2}
|
|
1768
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1769
|
+
AND ${edges2.kind} = ${params.edgeKind}
|
|
1770
|
+
AND ${edges2.fromKind} = ${params.fromKind}
|
|
1771
|
+
AND ${edges2.fromId} = ${params.fromId}
|
|
1772
|
+
AND ${edges2.toKind} = ${params.toKind}
|
|
1773
|
+
AND ${edges2.toId} = ${params.toId}
|
|
1774
|
+
AND ${edges2.deletedAt} IS NULL
|
|
2010
1775
|
LIMIT 1
|
|
2011
1776
|
`;
|
|
2012
1777
|
}
|
|
2013
1778
|
function buildFindEdgesConnectedTo(tables3, params) {
|
|
2014
|
-
const { edges:
|
|
1779
|
+
const { edges: edges2 } = tables3;
|
|
2015
1780
|
return sql`
|
|
2016
|
-
SELECT * FROM ${
|
|
2017
|
-
WHERE ${
|
|
2018
|
-
AND ${
|
|
2019
|
-
AND ${
|
|
2020
|
-
AND ${
|
|
1781
|
+
SELECT * FROM ${edges2}
|
|
1782
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1783
|
+
AND ${edges2.deletedAt} IS NULL
|
|
1784
|
+
AND ${edges2.fromKind} = ${params.nodeKind}
|
|
1785
|
+
AND ${edges2.fromId} = ${params.nodeId}
|
|
2021
1786
|
UNION ALL
|
|
2022
|
-
SELECT * FROM ${
|
|
2023
|
-
WHERE ${
|
|
2024
|
-
AND ${
|
|
2025
|
-
AND ${
|
|
2026
|
-
AND ${
|
|
1787
|
+
SELECT * FROM ${edges2}
|
|
1788
|
+
WHERE ${edges2.graphId} = ${params.graphId}
|
|
1789
|
+
AND ${edges2.deletedAt} IS NULL
|
|
1790
|
+
AND ${edges2.toKind} = ${params.nodeKind}
|
|
1791
|
+
AND ${edges2.toId} = ${params.nodeId}
|
|
2027
1792
|
AND NOT (
|
|
2028
|
-
${
|
|
2029
|
-
AND ${
|
|
1793
|
+
${edges2.fromKind} = ${params.nodeKind}
|
|
1794
|
+
AND ${edges2.fromId} = ${params.nodeId}
|
|
2030
1795
|
)
|
|
2031
1796
|
`;
|
|
2032
1797
|
}
|
|
@@ -2085,11 +1850,11 @@ function assertValidSearchParams(params) {
|
|
|
2085
1850
|
}
|
|
2086
1851
|
}
|
|
2087
1852
|
function buildInsertNode(tables3, params, timestamp2) {
|
|
2088
|
-
const { nodes:
|
|
1853
|
+
const { nodes: nodes2 } = tables3;
|
|
2089
1854
|
const propsJson = JSON.stringify(params.props);
|
|
2090
|
-
const columns = nodeColumnList(
|
|
1855
|
+
const columns = nodeColumnList(nodes2);
|
|
2091
1856
|
return sql`
|
|
2092
|
-
INSERT INTO ${
|
|
1857
|
+
INSERT INTO ${nodes2} (${columns})
|
|
2093
1858
|
VALUES (
|
|
2094
1859
|
${params.graphId}, ${params.kind}, ${params.id}, ${propsJson},
|
|
2095
1860
|
1, ${sqlNull(params.validFrom)}, ${sqlNull(params.validTo)},
|
|
@@ -2099,11 +1864,11 @@ function buildInsertNode(tables3, params, timestamp2) {
|
|
|
2099
1864
|
`;
|
|
2100
1865
|
}
|
|
2101
1866
|
function buildInsertNodeNoReturn(tables3, params, timestamp2) {
|
|
2102
|
-
const { nodes:
|
|
1867
|
+
const { nodes: nodes2 } = tables3;
|
|
2103
1868
|
const propsJson = JSON.stringify(params.props);
|
|
2104
|
-
const columns = nodeColumnList(
|
|
1869
|
+
const columns = nodeColumnList(nodes2);
|
|
2105
1870
|
return sql`
|
|
2106
|
-
INSERT INTO ${
|
|
1871
|
+
INSERT INTO ${nodes2} (${columns})
|
|
2107
1872
|
VALUES (
|
|
2108
1873
|
${params.graphId}, ${params.kind}, ${params.id}, ${propsJson},
|
|
2109
1874
|
1, ${sqlNull(params.validFrom)}, ${sqlNull(params.validTo)},
|
|
@@ -2112,109 +1877,109 @@ function buildInsertNodeNoReturn(tables3, params, timestamp2) {
|
|
|
2112
1877
|
`;
|
|
2113
1878
|
}
|
|
2114
1879
|
function buildInsertNodesBatch(tables3, params, timestamp2) {
|
|
2115
|
-
const { nodes:
|
|
2116
|
-
const columns = nodeColumnList(
|
|
1880
|
+
const { nodes: nodes2 } = tables3;
|
|
1881
|
+
const columns = nodeColumnList(nodes2);
|
|
2117
1882
|
const values = params.map((nodeParams) => {
|
|
2118
1883
|
const propsJson = JSON.stringify(nodeParams.props);
|
|
2119
1884
|
return sql`(${nodeParams.graphId}, ${nodeParams.kind}, ${nodeParams.id}, ${propsJson}, 1, ${sqlNull(nodeParams.validFrom)}, ${sqlNull(nodeParams.validTo)}, ${timestamp2}, ${timestamp2})`;
|
|
2120
1885
|
});
|
|
2121
1886
|
return sql`
|
|
2122
|
-
INSERT INTO ${
|
|
1887
|
+
INSERT INTO ${nodes2} (${columns})
|
|
2123
1888
|
VALUES ${sql.join(values, sql`, `)}
|
|
2124
1889
|
`;
|
|
2125
1890
|
}
|
|
2126
1891
|
function buildInsertNodesBatchReturning(tables3, params, timestamp2) {
|
|
2127
|
-
const { nodes:
|
|
2128
|
-
const columns = nodeColumnList(
|
|
1892
|
+
const { nodes: nodes2 } = tables3;
|
|
1893
|
+
const columns = nodeColumnList(nodes2);
|
|
2129
1894
|
const values = params.map((nodeParams) => {
|
|
2130
1895
|
const propsJson = JSON.stringify(nodeParams.props);
|
|
2131
1896
|
return sql`(${nodeParams.graphId}, ${nodeParams.kind}, ${nodeParams.id}, ${propsJson}, 1, ${sqlNull(nodeParams.validFrom)}, ${sqlNull(nodeParams.validTo)}, ${timestamp2}, ${timestamp2})`;
|
|
2132
1897
|
});
|
|
2133
1898
|
return sql`
|
|
2134
|
-
INSERT INTO ${
|
|
1899
|
+
INSERT INTO ${nodes2} (${columns})
|
|
2135
1900
|
VALUES ${sql.join(values, sql`, `)}
|
|
2136
1901
|
RETURNING *
|
|
2137
1902
|
`;
|
|
2138
1903
|
}
|
|
2139
1904
|
function buildGetNode(tables3, graphId, kind, id) {
|
|
2140
|
-
const { nodes:
|
|
1905
|
+
const { nodes: nodes2 } = tables3;
|
|
2141
1906
|
return sql`
|
|
2142
|
-
SELECT * FROM ${
|
|
2143
|
-
WHERE ${
|
|
2144
|
-
AND ${
|
|
2145
|
-
AND ${
|
|
1907
|
+
SELECT * FROM ${nodes2}
|
|
1908
|
+
WHERE ${nodes2.graphId} = ${graphId}
|
|
1909
|
+
AND ${nodes2.kind} = ${kind}
|
|
1910
|
+
AND ${nodes2.id} = ${id}
|
|
2146
1911
|
`;
|
|
2147
1912
|
}
|
|
2148
1913
|
function buildGetNodes(tables3, graphId, kind, ids) {
|
|
2149
|
-
const { nodes:
|
|
1914
|
+
const { nodes: nodes2 } = tables3;
|
|
2150
1915
|
const idPlaceholders = sql.join(
|
|
2151
1916
|
ids.map((nodeId) => sql`${nodeId}`),
|
|
2152
1917
|
sql`, `
|
|
2153
1918
|
);
|
|
2154
1919
|
return sql`
|
|
2155
|
-
SELECT * FROM ${
|
|
2156
|
-
WHERE ${
|
|
2157
|
-
AND ${
|
|
2158
|
-
AND ${
|
|
1920
|
+
SELECT * FROM ${nodes2}
|
|
1921
|
+
WHERE ${nodes2.graphId} = ${graphId}
|
|
1922
|
+
AND ${nodes2.kind} = ${kind}
|
|
1923
|
+
AND ${nodes2.id} IN (${idPlaceholders})
|
|
2159
1924
|
`;
|
|
2160
1925
|
}
|
|
2161
1926
|
function buildUpdateNode(tables3, params, timestamp2) {
|
|
2162
|
-
const { nodes:
|
|
1927
|
+
const { nodes: nodes2 } = tables3;
|
|
2163
1928
|
const propsJson = JSON.stringify(params.props);
|
|
2164
1929
|
const setParts = [
|
|
2165
|
-
sql`${quotedColumn(
|
|
2166
|
-
sql`${quotedColumn(
|
|
1930
|
+
sql`${quotedColumn(nodes2.props)} = ${propsJson}`,
|
|
1931
|
+
sql`${quotedColumn(nodes2.updatedAt)} = ${timestamp2}`
|
|
2167
1932
|
];
|
|
2168
1933
|
if (params.incrementVersion) {
|
|
2169
1934
|
setParts.push(
|
|
2170
|
-
sql`${quotedColumn(
|
|
1935
|
+
sql`${quotedColumn(nodes2.version)} = ${quotedColumn(nodes2.version)} + 1`
|
|
2171
1936
|
);
|
|
2172
1937
|
}
|
|
2173
1938
|
if (params.validTo !== void 0) {
|
|
2174
|
-
setParts.push(sql`${quotedColumn(
|
|
1939
|
+
setParts.push(sql`${quotedColumn(nodes2.validTo)} = ${params.validTo}`);
|
|
2175
1940
|
}
|
|
2176
1941
|
if (params.clearDeleted) {
|
|
2177
|
-
setParts.push(sql`${quotedColumn(
|
|
1942
|
+
setParts.push(sql`${quotedColumn(nodes2.deletedAt)} = NULL`);
|
|
2178
1943
|
}
|
|
2179
1944
|
const setClause = sql.join(setParts, sql`, `);
|
|
2180
1945
|
if (params.clearDeleted) {
|
|
2181
1946
|
return sql`
|
|
2182
|
-
UPDATE ${
|
|
1947
|
+
UPDATE ${nodes2}
|
|
2183
1948
|
SET ${setClause}
|
|
2184
|
-
WHERE ${
|
|
2185
|
-
AND ${
|
|
2186
|
-
AND ${
|
|
1949
|
+
WHERE ${nodes2.graphId} = ${params.graphId}
|
|
1950
|
+
AND ${nodes2.kind} = ${params.kind}
|
|
1951
|
+
AND ${nodes2.id} = ${params.id}
|
|
2187
1952
|
RETURNING *
|
|
2188
1953
|
`;
|
|
2189
1954
|
}
|
|
2190
1955
|
return sql`
|
|
2191
|
-
UPDATE ${
|
|
1956
|
+
UPDATE ${nodes2}
|
|
2192
1957
|
SET ${setClause}
|
|
2193
|
-
WHERE ${
|
|
2194
|
-
AND ${
|
|
2195
|
-
AND ${
|
|
2196
|
-
AND ${
|
|
1958
|
+
WHERE ${nodes2.graphId} = ${params.graphId}
|
|
1959
|
+
AND ${nodes2.kind} = ${params.kind}
|
|
1960
|
+
AND ${nodes2.id} = ${params.id}
|
|
1961
|
+
AND ${nodes2.deletedAt} IS NULL
|
|
2197
1962
|
RETURNING *
|
|
2198
1963
|
`;
|
|
2199
1964
|
}
|
|
2200
1965
|
function buildDeleteNode(tables3, params, timestamp2) {
|
|
2201
|
-
const { nodes:
|
|
1966
|
+
const { nodes: nodes2 } = tables3;
|
|
2202
1967
|
return sql`
|
|
2203
|
-
UPDATE ${
|
|
2204
|
-
SET ${quotedColumn(
|
|
2205
|
-
WHERE ${
|
|
2206
|
-
AND ${
|
|
2207
|
-
AND ${
|
|
2208
|
-
AND ${
|
|
1968
|
+
UPDATE ${nodes2}
|
|
1969
|
+
SET ${quotedColumn(nodes2.deletedAt)} = ${timestamp2}
|
|
1970
|
+
WHERE ${nodes2.graphId} = ${params.graphId}
|
|
1971
|
+
AND ${nodes2.kind} = ${params.kind}
|
|
1972
|
+
AND ${nodes2.id} = ${params.id}
|
|
1973
|
+
AND ${nodes2.deletedAt} IS NULL
|
|
2209
1974
|
`;
|
|
2210
1975
|
}
|
|
2211
1976
|
function buildHardDeleteNode(tables3, params) {
|
|
2212
|
-
const { nodes:
|
|
1977
|
+
const { nodes: nodes2 } = tables3;
|
|
2213
1978
|
return sql`
|
|
2214
|
-
DELETE FROM ${
|
|
2215
|
-
WHERE ${
|
|
2216
|
-
AND ${
|
|
2217
|
-
AND ${
|
|
1979
|
+
DELETE FROM ${nodes2}
|
|
1980
|
+
WHERE ${nodes2.graphId} = ${params.graphId}
|
|
1981
|
+
AND ${nodes2.kind} = ${params.kind}
|
|
1982
|
+
AND ${nodes2.id} = ${params.id}
|
|
2218
1983
|
`;
|
|
2219
1984
|
}
|
|
2220
1985
|
function createSchemaDialectStrategy(dialect) {
|
|
@@ -2231,13 +1996,13 @@ var SCHEMA_DIALECT_STRATEGIES = {
|
|
|
2231
1996
|
sqlite: createSchemaDialectStrategy("sqlite")
|
|
2232
1997
|
};
|
|
2233
1998
|
function buildInsertSchema(tables3, params, timestamp2, dialect = "sqlite") {
|
|
2234
|
-
const { schemaVersions:
|
|
1999
|
+
const { schemaVersions: schemaVersions2 } = tables3;
|
|
2235
2000
|
const strategy = SCHEMA_DIALECT_STRATEGIES[dialect];
|
|
2236
2001
|
const schemaDocumentJson = JSON.stringify(params.schemaDoc);
|
|
2237
2002
|
const isActiveValue = strategy.booleanLiteral(params.isActive);
|
|
2238
|
-
const columns = sql.raw(`"${
|
|
2003
|
+
const columns = sql.raw(`"${schemaVersions2.graphId.name}", "${schemaVersions2.version.name}", "${schemaVersions2.schemaHash.name}", "${schemaVersions2.schemaDoc.name}", "${schemaVersions2.createdAt.name}", "${schemaVersions2.isActive.name}"`);
|
|
2239
2004
|
return sql`
|
|
2240
|
-
INSERT INTO ${
|
|
2005
|
+
INSERT INTO ${schemaVersions2} (${columns})
|
|
2241
2006
|
VALUES (
|
|
2242
2007
|
${params.graphId}, ${params.version},
|
|
2243
2008
|
${params.schemaHash}, ${schemaDocumentJson},
|
|
@@ -2247,98 +2012,98 @@ function buildInsertSchema(tables3, params, timestamp2, dialect = "sqlite") {
|
|
|
2247
2012
|
`;
|
|
2248
2013
|
}
|
|
2249
2014
|
function buildGetActiveSchema(tables3, graphId, dialect = "sqlite") {
|
|
2250
|
-
const { schemaVersions:
|
|
2015
|
+
const { schemaVersions: schemaVersions2 } = tables3;
|
|
2251
2016
|
const strategy = SCHEMA_DIALECT_STRATEGIES[dialect];
|
|
2252
2017
|
return sql`
|
|
2253
|
-
SELECT * FROM ${
|
|
2254
|
-
WHERE ${
|
|
2255
|
-
AND ${
|
|
2018
|
+
SELECT * FROM ${schemaVersions2}
|
|
2019
|
+
WHERE ${schemaVersions2.graphId} = ${graphId}
|
|
2020
|
+
AND ${schemaVersions2.isActive} = ${strategy.booleanLiteral(true)}
|
|
2256
2021
|
`;
|
|
2257
2022
|
}
|
|
2258
2023
|
function buildGetSchemaVersion(tables3, graphId, version) {
|
|
2259
|
-
const { schemaVersions:
|
|
2024
|
+
const { schemaVersions: schemaVersions2 } = tables3;
|
|
2260
2025
|
return sql`
|
|
2261
|
-
SELECT * FROM ${
|
|
2262
|
-
WHERE ${
|
|
2263
|
-
AND ${
|
|
2026
|
+
SELECT * FROM ${schemaVersions2}
|
|
2027
|
+
WHERE ${schemaVersions2.graphId} = ${graphId}
|
|
2028
|
+
AND ${schemaVersions2.version} = ${version}
|
|
2264
2029
|
`;
|
|
2265
2030
|
}
|
|
2266
2031
|
function buildSetActiveSchema(tables3, graphId, version, dialect = "sqlite") {
|
|
2267
|
-
const { schemaVersions:
|
|
2032
|
+
const { schemaVersions: schemaVersions2 } = tables3;
|
|
2268
2033
|
const strategy = SCHEMA_DIALECT_STRATEGIES[dialect];
|
|
2269
2034
|
const deactivateAll = sql`
|
|
2270
|
-
UPDATE ${
|
|
2271
|
-
SET ${quotedColumn(
|
|
2272
|
-
WHERE ${
|
|
2035
|
+
UPDATE ${schemaVersions2}
|
|
2036
|
+
SET ${quotedColumn(schemaVersions2.isActive)} = ${strategy.booleanLiteral(false)}
|
|
2037
|
+
WHERE ${schemaVersions2.graphId} = ${graphId}
|
|
2273
2038
|
`;
|
|
2274
2039
|
const activateVersion = sql`
|
|
2275
|
-
UPDATE ${
|
|
2276
|
-
SET ${quotedColumn(
|
|
2277
|
-
WHERE ${
|
|
2278
|
-
AND ${
|
|
2040
|
+
UPDATE ${schemaVersions2}
|
|
2041
|
+
SET ${quotedColumn(schemaVersions2.isActive)} = ${strategy.booleanLiteral(true)}
|
|
2042
|
+
WHERE ${schemaVersions2.graphId} = ${graphId}
|
|
2043
|
+
AND ${schemaVersions2.version} = ${version}
|
|
2279
2044
|
`;
|
|
2280
2045
|
return { deactivateAll, activateVersion };
|
|
2281
2046
|
}
|
|
2282
2047
|
function buildInsertUniqueSqlite(tables3, params) {
|
|
2283
|
-
const { uniques:
|
|
2284
|
-
const columns = sql.raw(`"${
|
|
2285
|
-
const conflictColumns = sql.raw(`"${
|
|
2048
|
+
const { uniques: uniques2 } = tables3;
|
|
2049
|
+
const columns = sql.raw(`"${uniques2.graphId.name}", "${uniques2.nodeKind.name}", "${uniques2.constraintName.name}", "${uniques2.key.name}", "${uniques2.nodeId.name}", "${uniques2.concreteKind.name}", "${uniques2.deletedAt.name}"`);
|
|
2050
|
+
const conflictColumns = sql.raw(`"${uniques2.graphId.name}", "${uniques2.nodeKind.name}", "${uniques2.constraintName.name}", "${uniques2.key.name}"`);
|
|
2286
2051
|
return sql`
|
|
2287
|
-
INSERT INTO ${
|
|
2052
|
+
INSERT INTO ${uniques2} (${columns})
|
|
2288
2053
|
VALUES (
|
|
2289
2054
|
${params.graphId}, ${params.nodeKind}, ${params.constraintName},
|
|
2290
2055
|
${params.key}, ${params.nodeId}, ${params.concreteKind}, ${sql.raw("NULL")}
|
|
2291
2056
|
)
|
|
2292
2057
|
ON CONFLICT (${conflictColumns})
|
|
2293
2058
|
DO UPDATE SET
|
|
2294
|
-
${quotedColumn(
|
|
2295
|
-
WHEN ${quotedColumn(
|
|
2296
|
-
WHEN ${quotedColumn(
|
|
2297
|
-
ELSE ${quotedColumn(
|
|
2059
|
+
${quotedColumn(uniques2.nodeId)} = CASE
|
|
2060
|
+
WHEN ${quotedColumn(uniques2.nodeId)} = ${params.nodeId} THEN ${params.nodeId}
|
|
2061
|
+
WHEN ${quotedColumn(uniques2.deletedAt)} IS NOT NULL THEN ${params.nodeId}
|
|
2062
|
+
ELSE ${quotedColumn(uniques2.nodeId)}
|
|
2298
2063
|
END,
|
|
2299
|
-
${quotedColumn(
|
|
2300
|
-
WHEN ${quotedColumn(
|
|
2301
|
-
WHEN ${quotedColumn(
|
|
2302
|
-
ELSE ${quotedColumn(
|
|
2064
|
+
${quotedColumn(uniques2.concreteKind)} = CASE
|
|
2065
|
+
WHEN ${quotedColumn(uniques2.nodeId)} = ${params.nodeId} THEN ${params.concreteKind}
|
|
2066
|
+
WHEN ${quotedColumn(uniques2.deletedAt)} IS NOT NULL THEN ${params.concreteKind}
|
|
2067
|
+
ELSE ${quotedColumn(uniques2.concreteKind)}
|
|
2303
2068
|
END,
|
|
2304
|
-
${quotedColumn(
|
|
2305
|
-
WHEN ${quotedColumn(
|
|
2306
|
-
WHEN ${quotedColumn(
|
|
2307
|
-
ELSE ${quotedColumn(
|
|
2069
|
+
${quotedColumn(uniques2.deletedAt)} = CASE
|
|
2070
|
+
WHEN ${quotedColumn(uniques2.nodeId)} = ${params.nodeId} THEN NULL
|
|
2071
|
+
WHEN ${quotedColumn(uniques2.deletedAt)} IS NOT NULL THEN NULL
|
|
2072
|
+
ELSE ${quotedColumn(uniques2.deletedAt)}
|
|
2308
2073
|
END
|
|
2309
|
-
RETURNING ${quotedColumn(
|
|
2074
|
+
RETURNING ${quotedColumn(uniques2.nodeId)} as node_id
|
|
2310
2075
|
`;
|
|
2311
2076
|
}
|
|
2312
2077
|
function buildInsertUniquePostgres(tables3, params) {
|
|
2313
|
-
const { uniques:
|
|
2314
|
-
const columns = sql.raw(`"${
|
|
2315
|
-
const conflictColumns = sql.raw(`"${
|
|
2316
|
-
const tableName = getTableName(
|
|
2078
|
+
const { uniques: uniques2 } = tables3;
|
|
2079
|
+
const columns = sql.raw(`"${uniques2.graphId.name}", "${uniques2.nodeKind.name}", "${uniques2.constraintName.name}", "${uniques2.key.name}", "${uniques2.nodeId.name}", "${uniques2.concreteKind.name}", "${uniques2.deletedAt.name}"`);
|
|
2080
|
+
const conflictColumns = sql.raw(`"${uniques2.graphId.name}", "${uniques2.nodeKind.name}", "${uniques2.constraintName.name}", "${uniques2.key.name}"`);
|
|
2081
|
+
const tableName = getTableName(uniques2);
|
|
2317
2082
|
const existingColumn = (column) => sql.raw(`"${tableName}"."${column.name}"`);
|
|
2318
2083
|
return sql`
|
|
2319
|
-
INSERT INTO ${
|
|
2084
|
+
INSERT INTO ${uniques2} (${columns})
|
|
2320
2085
|
VALUES (
|
|
2321
2086
|
${params.graphId}, ${params.nodeKind}, ${params.constraintName},
|
|
2322
2087
|
${params.key}, ${params.nodeId}, ${params.concreteKind}, ${sql.raw("NULL")}
|
|
2323
2088
|
)
|
|
2324
2089
|
ON CONFLICT (${conflictColumns})
|
|
2325
2090
|
DO UPDATE SET
|
|
2326
|
-
${quotedColumn(
|
|
2327
|
-
WHEN ${existingColumn(
|
|
2328
|
-
WHEN ${existingColumn(
|
|
2329
|
-
ELSE ${existingColumn(
|
|
2091
|
+
${quotedColumn(uniques2.nodeId)} = CASE
|
|
2092
|
+
WHEN ${existingColumn(uniques2.nodeId)} = ${params.nodeId} THEN ${params.nodeId}
|
|
2093
|
+
WHEN ${existingColumn(uniques2.deletedAt)} IS NOT NULL THEN ${params.nodeId}
|
|
2094
|
+
ELSE ${existingColumn(uniques2.nodeId)}
|
|
2330
2095
|
END,
|
|
2331
|
-
${quotedColumn(
|
|
2332
|
-
WHEN ${existingColumn(
|
|
2333
|
-
WHEN ${existingColumn(
|
|
2334
|
-
ELSE ${existingColumn(
|
|
2096
|
+
${quotedColumn(uniques2.concreteKind)} = CASE
|
|
2097
|
+
WHEN ${existingColumn(uniques2.nodeId)} = ${params.nodeId} THEN ${params.concreteKind}
|
|
2098
|
+
WHEN ${existingColumn(uniques2.deletedAt)} IS NOT NULL THEN ${params.concreteKind}
|
|
2099
|
+
ELSE ${existingColumn(uniques2.concreteKind)}
|
|
2335
2100
|
END,
|
|
2336
|
-
${quotedColumn(
|
|
2337
|
-
WHEN ${existingColumn(
|
|
2338
|
-
WHEN ${existingColumn(
|
|
2339
|
-
ELSE ${existingColumn(
|
|
2101
|
+
${quotedColumn(uniques2.deletedAt)} = CASE
|
|
2102
|
+
WHEN ${existingColumn(uniques2.nodeId)} = ${params.nodeId} THEN NULL
|
|
2103
|
+
WHEN ${existingColumn(uniques2.deletedAt)} IS NOT NULL THEN NULL
|
|
2104
|
+
ELSE ${existingColumn(uniques2.deletedAt)}
|
|
2340
2105
|
END
|
|
2341
|
-
RETURNING ${quotedColumn(
|
|
2106
|
+
RETURNING ${quotedColumn(uniques2.nodeId)} as node_id
|
|
2342
2107
|
`;
|
|
2343
2108
|
}
|
|
2344
2109
|
var UNIQUE_INSERT_BUILDERS = {
|
|
@@ -2350,327 +2115,67 @@ function buildInsertUnique(tables3, dialect, params) {
|
|
|
2350
2115
|
return builder(tables3, params);
|
|
2351
2116
|
}
|
|
2352
2117
|
function buildDeleteUnique(tables3, params, timestamp2) {
|
|
2353
|
-
const { uniques:
|
|
2118
|
+
const { uniques: uniques2 } = tables3;
|
|
2354
2119
|
return sql`
|
|
2355
|
-
UPDATE ${
|
|
2356
|
-
SET ${quotedColumn(
|
|
2357
|
-
WHERE ${
|
|
2358
|
-
AND ${
|
|
2359
|
-
AND ${
|
|
2360
|
-
AND ${
|
|
2361
|
-
AND ${
|
|
2120
|
+
UPDATE ${uniques2}
|
|
2121
|
+
SET ${quotedColumn(uniques2.deletedAt)} = ${timestamp2}
|
|
2122
|
+
WHERE ${uniques2.graphId} = ${params.graphId}
|
|
2123
|
+
AND ${uniques2.nodeKind} = ${params.nodeKind}
|
|
2124
|
+
AND ${uniques2.constraintName} = ${params.constraintName}
|
|
2125
|
+
AND ${uniques2.key} = ${params.key}
|
|
2126
|
+
AND ${uniques2.deletedAt} IS NULL
|
|
2362
2127
|
`;
|
|
2363
2128
|
}
|
|
2364
2129
|
function buildHardDeleteUniquesByNode(tables3, graphId, nodeId) {
|
|
2365
|
-
const { uniques:
|
|
2130
|
+
const { uniques: uniques2 } = tables3;
|
|
2366
2131
|
return sql`
|
|
2367
|
-
DELETE FROM ${
|
|
2368
|
-
WHERE ${
|
|
2369
|
-
AND ${
|
|
2370
|
-
`;
|
|
2371
|
-
}
|
|
2372
|
-
function buildHardDeleteEmbeddingsByNode(tables3, graphId, nodeKind, nodeId) {
|
|
2373
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2374
|
-
return sql`
|
|
2375
|
-
DELETE FROM ${embeddings3}
|
|
2376
|
-
WHERE ${embeddings3.graphId} = ${graphId}
|
|
2377
|
-
AND ${embeddings3.nodeKind} = ${nodeKind}
|
|
2378
|
-
AND ${embeddings3.nodeId} = ${nodeId}
|
|
2132
|
+
DELETE FROM ${uniques2}
|
|
2133
|
+
WHERE ${uniques2.graphId} = ${graphId}
|
|
2134
|
+
AND ${uniques2.nodeId} = ${nodeId}
|
|
2379
2135
|
`;
|
|
2380
2136
|
}
|
|
2381
2137
|
function buildCheckUnique(tables3, params) {
|
|
2382
|
-
const { uniques:
|
|
2138
|
+
const { uniques: uniques2 } = tables3;
|
|
2383
2139
|
if (params.includeDeleted) {
|
|
2384
2140
|
return sql`
|
|
2385
|
-
SELECT * FROM ${
|
|
2386
|
-
WHERE ${
|
|
2387
|
-
AND ${
|
|
2388
|
-
AND ${
|
|
2389
|
-
AND ${
|
|
2141
|
+
SELECT * FROM ${uniques2}
|
|
2142
|
+
WHERE ${uniques2.graphId} = ${params.graphId}
|
|
2143
|
+
AND ${uniques2.nodeKind} = ${params.nodeKind}
|
|
2144
|
+
AND ${uniques2.constraintName} = ${params.constraintName}
|
|
2145
|
+
AND ${uniques2.key} = ${params.key}
|
|
2390
2146
|
`;
|
|
2391
2147
|
}
|
|
2392
2148
|
return sql`
|
|
2393
|
-
SELECT * FROM ${
|
|
2394
|
-
WHERE ${
|
|
2395
|
-
AND ${
|
|
2396
|
-
AND ${
|
|
2397
|
-
AND ${
|
|
2398
|
-
AND ${
|
|
2149
|
+
SELECT * FROM ${uniques2}
|
|
2150
|
+
WHERE ${uniques2.graphId} = ${params.graphId}
|
|
2151
|
+
AND ${uniques2.nodeKind} = ${params.nodeKind}
|
|
2152
|
+
AND ${uniques2.constraintName} = ${params.constraintName}
|
|
2153
|
+
AND ${uniques2.key} = ${params.key}
|
|
2154
|
+
AND ${uniques2.deletedAt} IS NULL
|
|
2399
2155
|
`;
|
|
2400
2156
|
}
|
|
2401
2157
|
function buildCheckUniqueBatch(tables3, params) {
|
|
2402
|
-
const { uniques:
|
|
2158
|
+
const { uniques: uniques2 } = tables3;
|
|
2403
2159
|
const keyPlaceholders = sql.join(
|
|
2404
2160
|
params.keys.map((key) => sql`${key}`),
|
|
2405
2161
|
sql`, `
|
|
2406
2162
|
);
|
|
2407
2163
|
if (params.includeDeleted) {
|
|
2408
2164
|
return sql`
|
|
2409
|
-
SELECT * FROM ${
|
|
2410
|
-
WHERE ${
|
|
2411
|
-
AND ${
|
|
2412
|
-
AND ${
|
|
2413
|
-
AND ${
|
|
2165
|
+
SELECT * FROM ${uniques2}
|
|
2166
|
+
WHERE ${uniques2.graphId} = ${params.graphId}
|
|
2167
|
+
AND ${uniques2.nodeKind} = ${params.nodeKind}
|
|
2168
|
+
AND ${uniques2.constraintName} = ${params.constraintName}
|
|
2169
|
+
AND ${uniques2.key} IN (${keyPlaceholders})
|
|
2414
2170
|
`;
|
|
2415
2171
|
}
|
|
2416
2172
|
return sql`
|
|
2417
|
-
SELECT * FROM ${
|
|
2418
|
-
WHERE ${
|
|
2419
|
-
AND ${
|
|
2420
|
-
AND ${
|
|
2421
|
-
AND ${
|
|
2422
|
-
AND ${
|
|
2423
|
-
`;
|
|
2424
|
-
}
|
|
2425
|
-
var MAX_HNSW_EF_SEARCH = 1e3;
|
|
2426
|
-
function assertFiniteNumberArray(array, name) {
|
|
2427
|
-
for (const [index3, value] of array.entries()) {
|
|
2428
|
-
if (typeof value !== "number" || !Number.isFinite(value)) {
|
|
2429
|
-
throw new TypeError(
|
|
2430
|
-
`${name}[${index3}] must be a finite number, got: ${value}`
|
|
2431
|
-
);
|
|
2432
|
-
}
|
|
2433
|
-
}
|
|
2434
|
-
}
|
|
2435
|
-
function formatEmbeddingLiteral(embedding) {
|
|
2436
|
-
assertFiniteNumberArray(embedding, "embedding");
|
|
2437
|
-
return `[${embedding.join(",")}]`;
|
|
2438
|
-
}
|
|
2439
|
-
function buildUpsertEmbeddingPostgres(tables3, params, timestamp2) {
|
|
2440
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2441
|
-
const embeddingLiteral = formatEmbeddingLiteral(params.embedding);
|
|
2442
|
-
const columns = sql.raw(
|
|
2443
|
-
`"${embeddings3.graphId.name}", "${embeddings3.nodeKind.name}", "${embeddings3.nodeId.name}", "${embeddings3.fieldPath.name}", "${embeddings3.embedding.name}", "${embeddings3.dimensions.name}", "${embeddings3.createdAt.name}", "${embeddings3.updatedAt.name}"`
|
|
2444
|
-
);
|
|
2445
|
-
const conflictColumns = sql.raw(
|
|
2446
|
-
`"${embeddings3.graphId.name}", "${embeddings3.nodeKind.name}", "${embeddings3.nodeId.name}", "${embeddings3.fieldPath.name}"`
|
|
2447
|
-
);
|
|
2448
|
-
const column = (target) => sql.raw(`"${target.name}"`);
|
|
2449
|
-
return sql`
|
|
2450
|
-
INSERT INTO ${embeddings3} (${columns})
|
|
2451
|
-
VALUES (
|
|
2452
|
-
${params.graphId}, ${params.nodeKind}, ${params.nodeId}, ${params.fieldPath},
|
|
2453
|
-
${embeddingLiteral}::vector, ${params.dimensions}, ${timestamp2}, ${timestamp2}
|
|
2454
|
-
)
|
|
2455
|
-
ON CONFLICT (${conflictColumns})
|
|
2456
|
-
DO UPDATE SET
|
|
2457
|
-
${column(embeddings3.embedding)} = ${embeddingLiteral}::vector,
|
|
2458
|
-
${column(embeddings3.dimensions)} = ${params.dimensions},
|
|
2459
|
-
${column(embeddings3.updatedAt)} = ${timestamp2}
|
|
2460
|
-
`;
|
|
2461
|
-
}
|
|
2462
|
-
function buildUpsertEmbeddingSqlite(tables3, params, timestamp2) {
|
|
2463
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2464
|
-
const embeddingJson = JSON.stringify(params.embedding);
|
|
2465
|
-
assertFiniteNumberArray(params.embedding, "embedding");
|
|
2466
|
-
const columns = sql.raw(
|
|
2467
|
-
`"${embeddings3.graphId.name}", "${embeddings3.nodeKind.name}", "${embeddings3.nodeId.name}", "${embeddings3.fieldPath.name}", "${embeddings3.embedding.name}", "${embeddings3.dimensions.name}", "${embeddings3.createdAt.name}", "${embeddings3.updatedAt.name}"`
|
|
2468
|
-
);
|
|
2469
|
-
const conflictColumns = sql.raw(
|
|
2470
|
-
`"${embeddings3.graphId.name}", "${embeddings3.nodeKind.name}", "${embeddings3.nodeId.name}", "${embeddings3.fieldPath.name}"`
|
|
2471
|
-
);
|
|
2472
|
-
const column = (target) => sql.raw(`"${target.name}"`);
|
|
2473
|
-
return sql`
|
|
2474
|
-
INSERT INTO ${embeddings3} (${columns})
|
|
2475
|
-
VALUES (
|
|
2476
|
-
${params.graphId}, ${params.nodeKind}, ${params.nodeId}, ${params.fieldPath},
|
|
2477
|
-
vec_f32(${embeddingJson}), ${params.dimensions}, ${timestamp2}, ${timestamp2}
|
|
2478
|
-
)
|
|
2479
|
-
ON CONFLICT (${conflictColumns})
|
|
2480
|
-
DO UPDATE SET
|
|
2481
|
-
${column(embeddings3.embedding)} = vec_f32(${embeddingJson}),
|
|
2482
|
-
${column(embeddings3.dimensions)} = ${params.dimensions},
|
|
2483
|
-
${column(embeddings3.updatedAt)} = ${timestamp2}
|
|
2484
|
-
`;
|
|
2485
|
-
}
|
|
2486
|
-
function buildDeleteEmbedding(tables3, params) {
|
|
2487
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2488
|
-
return sql`
|
|
2489
|
-
DELETE FROM ${embeddings3}
|
|
2490
|
-
WHERE ${embeddings3.graphId} = ${params.graphId}
|
|
2491
|
-
AND ${embeddings3.nodeKind} = ${params.nodeKind}
|
|
2492
|
-
AND ${embeddings3.nodeId} = ${params.nodeId}
|
|
2493
|
-
AND ${embeddings3.fieldPath} = ${params.fieldPath}
|
|
2494
|
-
`;
|
|
2495
|
-
}
|
|
2496
|
-
function buildGetEmbedding(tables3, graphId, nodeKind, nodeId, fieldPath) {
|
|
2497
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2498
|
-
return sql`
|
|
2499
|
-
SELECT * FROM ${embeddings3}
|
|
2500
|
-
WHERE ${embeddings3.graphId} = ${graphId}
|
|
2501
|
-
AND ${embeddings3.nodeKind} = ${nodeKind}
|
|
2502
|
-
AND ${embeddings3.nodeId} = ${nodeId}
|
|
2503
|
-
AND ${embeddings3.fieldPath} = ${fieldPath}
|
|
2504
|
-
`;
|
|
2505
|
-
}
|
|
2506
|
-
function buildDistanceExpression(embeddingColumn, queryLiteral, metric) {
|
|
2507
|
-
const vectorParameter = sql`${queryLiteral}::vector`;
|
|
2508
|
-
switch (metric) {
|
|
2509
|
-
case "cosine": {
|
|
2510
|
-
return sql`(${embeddingColumn} <=> ${vectorParameter})`;
|
|
2511
|
-
}
|
|
2512
|
-
case "l2": {
|
|
2513
|
-
return sql`(${embeddingColumn} <-> ${vectorParameter})`;
|
|
2514
|
-
}
|
|
2515
|
-
case "inner_product": {
|
|
2516
|
-
return sql`(${embeddingColumn} <#> ${vectorParameter})`;
|
|
2517
|
-
}
|
|
2518
|
-
default: {
|
|
2519
|
-
const _exhaustive = metric;
|
|
2520
|
-
throw new Error(`Unsupported vector metric: ${String(_exhaustive)}`);
|
|
2521
|
-
}
|
|
2522
|
-
}
|
|
2523
|
-
}
|
|
2524
|
-
function buildVectorSearchScoreExpression(distanceExpression, metric) {
|
|
2525
|
-
switch (metric) {
|
|
2526
|
-
case "cosine": {
|
|
2527
|
-
return sql`(1 - (${distanceExpression}))`;
|
|
2528
|
-
}
|
|
2529
|
-
case "l2":
|
|
2530
|
-
case "inner_product": {
|
|
2531
|
-
return distanceExpression;
|
|
2532
|
-
}
|
|
2533
|
-
default: {
|
|
2534
|
-
const _exhaustive = metric;
|
|
2535
|
-
throw new Error(`Unsupported vector metric: ${String(_exhaustive)}`);
|
|
2536
|
-
}
|
|
2537
|
-
}
|
|
2538
|
-
}
|
|
2539
|
-
function buildVectorSearchMinScoreCondition(distanceExpression, metric, minScore) {
|
|
2540
|
-
switch (metric) {
|
|
2541
|
-
case "cosine": {
|
|
2542
|
-
const threshold = 1 - minScore;
|
|
2543
|
-
return sql`${distanceExpression} <= ${threshold}`;
|
|
2544
|
-
}
|
|
2545
|
-
case "l2": {
|
|
2546
|
-
return sql`${distanceExpression} <= ${minScore}`;
|
|
2547
|
-
}
|
|
2548
|
-
case "inner_product": {
|
|
2549
|
-
const negativeThreshold = -minScore;
|
|
2550
|
-
return sql`${distanceExpression} <= ${negativeThreshold}`;
|
|
2551
|
-
}
|
|
2552
|
-
default: {
|
|
2553
|
-
const _exhaustive = metric;
|
|
2554
|
-
throw new Error(`Unsupported vector metric: ${String(_exhaustive)}`);
|
|
2555
|
-
}
|
|
2556
|
-
}
|
|
2557
|
-
}
|
|
2558
|
-
function assertVectorSearchBounds(params) {
|
|
2559
|
-
if (params.minScore !== void 0 && !Number.isFinite(params.minScore)) {
|
|
2560
|
-
throw new TypeError(
|
|
2561
|
-
`minScore must be a finite number, got: ${params.minScore}`
|
|
2562
|
-
);
|
|
2563
|
-
}
|
|
2564
|
-
if (!Number.isInteger(params.limit) || params.limit <= 0) {
|
|
2565
|
-
throw new Error(`limit must be a positive integer, got: ${params.limit}`);
|
|
2566
|
-
}
|
|
2567
|
-
if (params.efSearch !== void 0 && (!Number.isInteger(params.efSearch) || params.efSearch <= 0)) {
|
|
2568
|
-
throw new Error(
|
|
2569
|
-
`efSearch must be a positive integer, got: ${params.efSearch}`
|
|
2570
|
-
);
|
|
2571
|
-
}
|
|
2572
|
-
}
|
|
2573
|
-
function buildVectorSearchPostgres(tables3, params) {
|
|
2574
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2575
|
-
const queryLiteral = formatEmbeddingLiteral(params.queryEmbedding);
|
|
2576
|
-
assertVectorSearchBounds(params);
|
|
2577
|
-
if (params.efSearch !== void 0 && params.efSearch > MAX_HNSW_EF_SEARCH) {
|
|
2578
|
-
throw new RangeError(
|
|
2579
|
-
`efSearch must be \u2264 ${MAX_HNSW_EF_SEARCH} (pgvector's hnsw.ef_search valid range is 1..${MAX_HNSW_EF_SEARCH}), got: ${params.efSearch}`
|
|
2580
|
-
);
|
|
2581
|
-
}
|
|
2582
|
-
const embeddingColumn = sql`${embeddings3.embedding}`;
|
|
2583
|
-
const distanceExpression = buildDistanceExpression(
|
|
2584
|
-
embeddingColumn,
|
|
2585
|
-
queryLiteral,
|
|
2586
|
-
params.metric
|
|
2587
|
-
);
|
|
2588
|
-
const conditions = [
|
|
2589
|
-
sql`${embeddings3.graphId} = ${params.graphId}`,
|
|
2590
|
-
sql`${embeddings3.nodeKind} = ${params.nodeKind}`,
|
|
2591
|
-
sql`${embeddings3.fieldPath} = ${params.fieldPath}`
|
|
2592
|
-
];
|
|
2593
|
-
if (params.minScore !== void 0) {
|
|
2594
|
-
conditions.push(
|
|
2595
|
-
buildVectorSearchMinScoreCondition(
|
|
2596
|
-
distanceExpression,
|
|
2597
|
-
params.metric,
|
|
2598
|
-
params.minScore
|
|
2599
|
-
)
|
|
2600
|
-
);
|
|
2601
|
-
}
|
|
2602
|
-
const whereClause = sql.join(conditions, sql` AND `);
|
|
2603
|
-
const scoreExpression = buildVectorSearchScoreExpression(
|
|
2604
|
-
distanceExpression,
|
|
2605
|
-
params.metric
|
|
2606
|
-
);
|
|
2607
|
-
return sql`
|
|
2608
|
-
SELECT
|
|
2609
|
-
${embeddings3.nodeId} as node_id,
|
|
2610
|
-
${scoreExpression} as score
|
|
2611
|
-
FROM ${embeddings3}
|
|
2612
|
-
WHERE ${whereClause}
|
|
2613
|
-
ORDER BY ${distanceExpression} ASC
|
|
2614
|
-
LIMIT ${params.limit}
|
|
2615
|
-
`;
|
|
2616
|
-
}
|
|
2617
|
-
function buildSqliteDistanceExpression(embeddingColumn, embeddingJson, metric) {
|
|
2618
|
-
switch (metric) {
|
|
2619
|
-
case "cosine": {
|
|
2620
|
-
return sql`vec_distance_cosine(${embeddingColumn}, vec_f32(${embeddingJson}))`;
|
|
2621
|
-
}
|
|
2622
|
-
case "l2": {
|
|
2623
|
-
return sql`vec_distance_l2(${embeddingColumn}, vec_f32(${embeddingJson}))`;
|
|
2624
|
-
}
|
|
2625
|
-
case "inner_product": {
|
|
2626
|
-
throw new Error(
|
|
2627
|
-
"Inner product distance is not supported by sqlite-vec. Use 'cosine' or 'l2' metrics instead."
|
|
2628
|
-
);
|
|
2629
|
-
}
|
|
2630
|
-
default: {
|
|
2631
|
-
const _exhaustive = metric;
|
|
2632
|
-
throw new Error(`Unsupported vector metric: ${String(_exhaustive)}`);
|
|
2633
|
-
}
|
|
2634
|
-
}
|
|
2635
|
-
}
|
|
2636
|
-
function buildVectorSearchSqlite(tables3, params) {
|
|
2637
|
-
const { embeddings: embeddings3 } = tables3;
|
|
2638
|
-
assertFiniteNumberArray(params.queryEmbedding, "queryEmbedding");
|
|
2639
|
-
const embeddingJson = JSON.stringify(params.queryEmbedding);
|
|
2640
|
-
assertVectorSearchBounds(params);
|
|
2641
|
-
const embeddingColumn = sql`${embeddings3.embedding}`;
|
|
2642
|
-
const distanceExpression = buildSqliteDistanceExpression(
|
|
2643
|
-
embeddingColumn,
|
|
2644
|
-
embeddingJson,
|
|
2645
|
-
params.metric
|
|
2646
|
-
);
|
|
2647
|
-
const conditions = [
|
|
2648
|
-
sql`${embeddings3.graphId} = ${params.graphId}`,
|
|
2649
|
-
sql`${embeddings3.nodeKind} = ${params.nodeKind}`,
|
|
2650
|
-
sql`${embeddings3.fieldPath} = ${params.fieldPath}`
|
|
2651
|
-
];
|
|
2652
|
-
if (params.minScore !== void 0) {
|
|
2653
|
-
conditions.push(
|
|
2654
|
-
buildVectorSearchMinScoreCondition(
|
|
2655
|
-
distanceExpression,
|
|
2656
|
-
params.metric,
|
|
2657
|
-
params.minScore
|
|
2658
|
-
)
|
|
2659
|
-
);
|
|
2660
|
-
}
|
|
2661
|
-
const whereClause = sql.join(conditions, sql` AND `);
|
|
2662
|
-
const scoreExpression = buildVectorSearchScoreExpression(
|
|
2663
|
-
distanceExpression,
|
|
2664
|
-
params.metric
|
|
2665
|
-
);
|
|
2666
|
-
return sql`
|
|
2667
|
-
SELECT
|
|
2668
|
-
${embeddings3.nodeId} as node_id,
|
|
2669
|
-
${scoreExpression} as score
|
|
2670
|
-
FROM ${embeddings3}
|
|
2671
|
-
WHERE ${whereClause}
|
|
2672
|
-
ORDER BY ${distanceExpression} ASC
|
|
2673
|
-
LIMIT ${params.limit}
|
|
2173
|
+
SELECT * FROM ${uniques2}
|
|
2174
|
+
WHERE ${uniques2.graphId} = ${params.graphId}
|
|
2175
|
+
AND ${uniques2.nodeKind} = ${params.nodeKind}
|
|
2176
|
+
AND ${uniques2.constraintName} = ${params.constraintName}
|
|
2177
|
+
AND ${uniques2.key} IN (${keyPlaceholders})
|
|
2178
|
+
AND ${uniques2.deletedAt} IS NULL
|
|
2674
2179
|
`;
|
|
2675
2180
|
}
|
|
2676
2181
|
|
|
@@ -2713,7 +2218,6 @@ var COMMON_TABLE_OPERATION_BUILDERS = {
|
|
|
2713
2218
|
buildCountEdgesByKind,
|
|
2714
2219
|
buildDeleteUnique,
|
|
2715
2220
|
buildHardDeleteUniquesByNode,
|
|
2716
|
-
buildHardDeleteEmbeddingsByNode,
|
|
2717
2221
|
buildCheckUnique,
|
|
2718
2222
|
buildCheckUniqueBatch,
|
|
2719
2223
|
buildGetSchemaVersion
|
|
@@ -2757,221 +2261,10 @@ function createCommonOperationStrategy(tables3, dialect, fulltextStrategy) {
|
|
|
2757
2261
|
};
|
|
2758
2262
|
}
|
|
2759
2263
|
function createSqliteOperationStrategy(tables3, fulltextStrategy) {
|
|
2760
|
-
|
|
2761
|
-
tables3,
|
|
2762
|
-
"sqlite",
|
|
2763
|
-
fulltextStrategy
|
|
2764
|
-
);
|
|
2765
|
-
return {
|
|
2766
|
-
...common,
|
|
2767
|
-
buildUpsertEmbedding(params, timestamp2) {
|
|
2768
|
-
return buildUpsertEmbeddingSqlite(tables3, params, timestamp2);
|
|
2769
|
-
},
|
|
2770
|
-
buildDeleteEmbedding(params) {
|
|
2771
|
-
return buildDeleteEmbedding(tables3, params);
|
|
2772
|
-
},
|
|
2773
|
-
buildVectorSearch(params) {
|
|
2774
|
-
return buildVectorSearchSqlite(tables3, params);
|
|
2775
|
-
}
|
|
2776
|
-
};
|
|
2264
|
+
return createCommonOperationStrategy(tables3, "sqlite", fulltextStrategy);
|
|
2777
2265
|
}
|
|
2778
2266
|
function createPostgresOperationStrategy(tables3, fulltextStrategy) {
|
|
2779
|
-
|
|
2780
|
-
tables3,
|
|
2781
|
-
"postgres",
|
|
2782
|
-
fulltextStrategy
|
|
2783
|
-
);
|
|
2784
|
-
return {
|
|
2785
|
-
...common,
|
|
2786
|
-
buildUpsertEmbedding(params, timestamp2) {
|
|
2787
|
-
return buildUpsertEmbeddingPostgres(tables3, params, timestamp2);
|
|
2788
|
-
},
|
|
2789
|
-
buildDeleteEmbedding(params) {
|
|
2790
|
-
return buildDeleteEmbedding(tables3, params);
|
|
2791
|
-
},
|
|
2792
|
-
buildGetEmbedding(graphId, nodeKind, nodeId, fieldPath) {
|
|
2793
|
-
return buildGetEmbedding(tables3, graphId, nodeKind, nodeId, fieldPath);
|
|
2794
|
-
},
|
|
2795
|
-
buildVectorSearch(params) {
|
|
2796
|
-
return buildVectorSearchPostgres(tables3, params);
|
|
2797
|
-
}
|
|
2798
|
-
};
|
|
2799
|
-
}
|
|
2800
|
-
function assertPositiveInteger(value, name) {
|
|
2801
|
-
if (!Number.isInteger(value) || value <= 0) {
|
|
2802
|
-
throw new Error(`${name} must be a positive integer, got: ${value}`);
|
|
2803
|
-
}
|
|
2804
|
-
}
|
|
2805
|
-
function assertSafeIdentifier(value, name) {
|
|
2806
|
-
if (!/^[a-z0-9_]+$/i.test(value)) {
|
|
2807
|
-
throw new Error(
|
|
2808
|
-
`${name} must contain only alphanumeric characters and underscores, got: ${value}`
|
|
2809
|
-
);
|
|
2810
|
-
}
|
|
2811
|
-
}
|
|
2812
|
-
function sanitizeIdentifier(s) {
|
|
2813
|
-
return s.toLowerCase().replaceAll(/[^a-z0-9_]/g, "_");
|
|
2814
|
-
}
|
|
2815
|
-
function quoteIdentifier(value) {
|
|
2816
|
-
return `"${value.replaceAll('"', '""')}"`;
|
|
2817
|
-
}
|
|
2818
|
-
function shortHash(input) {
|
|
2819
|
-
let h1 = 3735928559;
|
|
2820
|
-
let h2 = 1103547991;
|
|
2821
|
-
for (let index3 = 0; index3 < input.length; index3++) {
|
|
2822
|
-
const ch = input.codePointAt(index3);
|
|
2823
|
-
h1 = Math.imul(h1 ^ ch, 2654435761);
|
|
2824
|
-
h2 = Math.imul(h2 ^ ch, 1597334677);
|
|
2825
|
-
}
|
|
2826
|
-
h1 = Math.imul(h1 ^ h1 >>> 16, 2246822507) ^ Math.imul(h2 ^ h2 >>> 13, 3266489909);
|
|
2827
|
-
h2 = Math.imul(h2 ^ h2 >>> 16, 2246822507) ^ Math.imul(h1 ^ h1 >>> 13, 3266489909);
|
|
2828
|
-
const hi = (h2 >>> 0).toString(16).padStart(8, "0");
|
|
2829
|
-
const lo = (h1 >>> 0).toString(16).padStart(8, "0");
|
|
2830
|
-
return `${hi}${lo}`.slice(0, 8);
|
|
2831
|
-
}
|
|
2832
|
-
function generateVectorIndexName(graphId, nodeKind, fieldPath, metric = "cosine") {
|
|
2833
|
-
const parts = [
|
|
2834
|
-
"idx_emb",
|
|
2835
|
-
sanitizeIdentifier(graphId),
|
|
2836
|
-
sanitizeIdentifier(nodeKind),
|
|
2837
|
-
sanitizeIdentifier(fieldPath),
|
|
2838
|
-
metric
|
|
2839
|
-
];
|
|
2840
|
-
const name = parts.join("_");
|
|
2841
|
-
if (name.length <= MAX_PG_IDENTIFIER_LENGTH) {
|
|
2842
|
-
return name;
|
|
2843
|
-
}
|
|
2844
|
-
const hash = shortHash(name);
|
|
2845
|
-
const truncated = name.slice(0, MAX_PG_IDENTIFIER_LENGTH - 1 - hash.length);
|
|
2846
|
-
return `${truncated}_${hash}`;
|
|
2847
|
-
}
|
|
2848
|
-
function getOperatorClass(metric) {
|
|
2849
|
-
switch (metric) {
|
|
2850
|
-
case "cosine": {
|
|
2851
|
-
return "vector_cosine_ops";
|
|
2852
|
-
}
|
|
2853
|
-
case "l2": {
|
|
2854
|
-
return "vector_l2_ops";
|
|
2855
|
-
}
|
|
2856
|
-
case "inner_product": {
|
|
2857
|
-
return "vector_ip_ops";
|
|
2858
|
-
}
|
|
2859
|
-
default: {
|
|
2860
|
-
const _exhaustive = metric;
|
|
2861
|
-
throw new Error("Unsupported vector metric: " + String(_exhaustive));
|
|
2862
|
-
}
|
|
2863
|
-
}
|
|
2864
|
-
}
|
|
2865
|
-
function escapeSqlString(value) {
|
|
2866
|
-
const tag = `tg${crypto.randomUUID().replaceAll("-", "").slice(0, 8)}`;
|
|
2867
|
-
return `$${tag}$${value}$${tag}$`;
|
|
2868
|
-
}
|
|
2869
|
-
async function createPostgresVectorIndex(db, options) {
|
|
2870
|
-
const {
|
|
2871
|
-
graphId,
|
|
2872
|
-
nodeKind,
|
|
2873
|
-
fieldPath,
|
|
2874
|
-
dimensions,
|
|
2875
|
-
embeddingsTableName = "typegraph_node_embeddings",
|
|
2876
|
-
indexType = "hnsw",
|
|
2877
|
-
metric = "cosine",
|
|
2878
|
-
hnswM = 16,
|
|
2879
|
-
hnswEfConstruction = 64,
|
|
2880
|
-
ivfflatLists = 100
|
|
2881
|
-
} = options;
|
|
2882
|
-
assertPositiveInteger(dimensions, "dimensions");
|
|
2883
|
-
assertPositiveInteger(hnswM, "hnswM");
|
|
2884
|
-
assertPositiveInteger(hnswEfConstruction, "hnswEfConstruction");
|
|
2885
|
-
assertPositiveInteger(ivfflatLists, "ivfflatLists");
|
|
2886
|
-
const indexName = generateVectorIndexName(graphId, nodeKind, fieldPath, metric);
|
|
2887
|
-
const operatorClass = getOperatorClass(metric);
|
|
2888
|
-
const quotedEmbeddingsTableName = quoteIdentifier(embeddingsTableName);
|
|
2889
|
-
assertSafeIdentifier(indexName, "indexName");
|
|
2890
|
-
const safeGraphId = escapeSqlString(graphId);
|
|
2891
|
-
const safeNodeKind = escapeSqlString(nodeKind);
|
|
2892
|
-
const safeFieldPath = escapeSqlString(fieldPath);
|
|
2893
|
-
let indexSql;
|
|
2894
|
-
const concurrentClause = options.concurrent === true ? "CONCURRENTLY " : "";
|
|
2895
|
-
if (indexType === "hnsw") {
|
|
2896
|
-
indexSql = sql.raw(`
|
|
2897
|
-
CREATE INDEX ${concurrentClause}IF NOT EXISTS ${indexName}
|
|
2898
|
-
ON ${quotedEmbeddingsTableName}
|
|
2899
|
-
USING hnsw ((embedding::vector(${dimensions})) ${operatorClass})
|
|
2900
|
-
WITH (m = ${hnswM}, ef_construction = ${hnswEfConstruction})
|
|
2901
|
-
WHERE graph_id = ${safeGraphId}
|
|
2902
|
-
AND node_kind = ${safeNodeKind}
|
|
2903
|
-
AND field_path = ${safeFieldPath}
|
|
2904
|
-
`);
|
|
2905
|
-
} else if (indexType === "ivfflat") {
|
|
2906
|
-
indexSql = sql.raw(`
|
|
2907
|
-
CREATE INDEX ${concurrentClause}IF NOT EXISTS ${indexName}
|
|
2908
|
-
ON ${quotedEmbeddingsTableName}
|
|
2909
|
-
USING ivfflat ((embedding::vector(${dimensions})) ${operatorClass})
|
|
2910
|
-
WITH (lists = ${ivfflatLists})
|
|
2911
|
-
WHERE graph_id = ${safeGraphId}
|
|
2912
|
-
AND node_kind = ${safeNodeKind}
|
|
2913
|
-
AND field_path = ${safeFieldPath}
|
|
2914
|
-
`);
|
|
2915
|
-
} else {
|
|
2916
|
-
return {
|
|
2917
|
-
indexName,
|
|
2918
|
-
success: true,
|
|
2919
|
-
message: `Index type "${indexType}" not supported, skipping index creation`
|
|
2920
|
-
};
|
|
2921
|
-
}
|
|
2922
|
-
try {
|
|
2923
|
-
await db.execute(indexSql);
|
|
2924
|
-
return {
|
|
2925
|
-
indexName,
|
|
2926
|
-
success: true,
|
|
2927
|
-
message: `Created ${indexType.toUpperCase()} index "${indexName}" for ${nodeKind}.${fieldPath}`
|
|
2928
|
-
};
|
|
2929
|
-
} catch (error) {
|
|
2930
|
-
return {
|
|
2931
|
-
indexName,
|
|
2932
|
-
success: false,
|
|
2933
|
-
message: `Failed to create index: ${error instanceof Error ? error.message : String(error)}`
|
|
2934
|
-
};
|
|
2935
|
-
}
|
|
2936
|
-
}
|
|
2937
|
-
async function dropPostgresVectorIndex(db, indexName) {
|
|
2938
|
-
assertSafeIdentifier(indexName, "indexName");
|
|
2939
|
-
try {
|
|
2940
|
-
const dropSql = sql.raw(`DROP INDEX IF EXISTS ${indexName}`);
|
|
2941
|
-
await db.execute(dropSql);
|
|
2942
|
-
return {
|
|
2943
|
-
indexName,
|
|
2944
|
-
success: true,
|
|
2945
|
-
message: `Dropped index "${indexName}"`
|
|
2946
|
-
};
|
|
2947
|
-
} catch (error) {
|
|
2948
|
-
return {
|
|
2949
|
-
indexName,
|
|
2950
|
-
success: false,
|
|
2951
|
-
message: `Failed to drop index: ${error instanceof Error ? error.message : String(error)}`
|
|
2952
|
-
};
|
|
2953
|
-
}
|
|
2954
|
-
}
|
|
2955
|
-
function createSqliteVectorIndex(_options) {
|
|
2956
|
-
const indexName = generateVectorIndexName(
|
|
2957
|
-
_options.graphId,
|
|
2958
|
-
_options.nodeKind,
|
|
2959
|
-
_options.fieldPath,
|
|
2960
|
-
_options.metric
|
|
2961
|
-
);
|
|
2962
|
-
return {
|
|
2963
|
-
indexName,
|
|
2964
|
-
success: true,
|
|
2965
|
-
message: "SQLite/sqlite-vec uses optimized internal indexing, no explicit index needed"
|
|
2966
|
-
};
|
|
2967
|
-
}
|
|
2968
|
-
function dropSqliteVectorIndex(graphId, nodeKind, fieldPath, metric = "cosine") {
|
|
2969
|
-
const indexName = generateVectorIndexName(graphId, nodeKind, fieldPath, metric);
|
|
2970
|
-
return {
|
|
2971
|
-
indexName,
|
|
2972
|
-
success: true,
|
|
2973
|
-
message: "SQLite/sqlite-vec does not use explicit indexes"
|
|
2974
|
-
};
|
|
2267
|
+
return createCommonOperationStrategy(tables3, "postgres", fulltextStrategy);
|
|
2975
2268
|
}
|
|
2976
2269
|
|
|
2977
2270
|
// src/backend/drizzle/execution/lru.ts
|
|
@@ -3001,6 +2294,6 @@ function compileQueryWithDialect(db, query, backendName) {
|
|
|
3001
2294
|
return compiler.sqlToQuery(query);
|
|
3002
2295
|
}
|
|
3003
2296
|
|
|
3004
|
-
export { POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS, POSTGRES_INDEX_MAT_TIMESTAMPS, POSTGRES_KIND_REMOVAL_TIMESTAMPS, POSTGRES_ROW_MAPPER_CONFIG, SQLITE_CONTRIBUTION_MAT_TIMESTAMPS, SQLITE_INDEX_MAT_TIMESTAMPS, SQLITE_KIND_REMOVAL_TIMESTAMPS, SQLITE_ROW_MAPPER_CONFIG, assertAdoptedDialect, buildContributionInsertValues, buildContributionOnConflictSet, buildKindRemovalInsertValues, buildKindRemovalOnConflictSet, buildMaterializationInsertValues, buildMaterializationOnConflictSet, coerceNumericScore, compileQueryWithDialect, createCommonOperationBackend, createContributionMaterializer, createEdgeRowMapper, createNodeRowMapper, createPostgresOperationStrategy, createPostgresTables,
|
|
3005
|
-
//# sourceMappingURL=chunk-
|
|
3006
|
-
//# sourceMappingURL=chunk-
|
|
2297
|
+
export { POSTGRES_CONTRIBUTION_MAT_TIMESTAMPS, POSTGRES_INDEX_MAT_TIMESTAMPS, POSTGRES_KIND_REMOVAL_TIMESTAMPS, POSTGRES_ROW_MAPPER_CONFIG, SQLITE_CONTRIBUTION_MAT_TIMESTAMPS, SQLITE_INDEX_MAT_TIMESTAMPS, SQLITE_KIND_REMOVAL_TIMESTAMPS, SQLITE_ROW_MAPPER_CONFIG, assertAdoptedDialect, buildContributionInsertValues, buildContributionOnConflictSet, buildKindRemovalInsertValues, buildKindRemovalOnConflictSet, buildMaterializationInsertValues, buildMaterializationOnConflictSet, coerceNumericScore, compileQueryWithDialect, createCommonOperationBackend, createContributionMaterializer, createEdgeRowMapper, createNodeRowMapper, createPostgresOperationStrategy, createPostgresTables, createSchemaVersionRowMapper, createSqliteOperationStrategy, createUniqueRowMapper, createVectorSlotLatch, edges, fulltext, gateFulltext, gateFulltextMethods, generatePgCreateTableSQL, generatePostgresDDL, generatePostgresMigrationSQL, generateSqliteCreateTableSQL, generateSqliteDDL, generateSqliteMigrationSQL, getOrCreateLru, mapContributionMaterializationRow, mapKindRemovalRow, mapMaterializationRow, mapVectorWriteError, nodes, nowIso, schemaVersions, tables2 as tables, uniques, vectorSlotFromCreateIndexParams, vectorSlotFromDropIndexParams, vectorSlotFromParams };
|
|
2298
|
+
//# sourceMappingURL=chunk-3DTLDGIM.js.map
|
|
2299
|
+
//# sourceMappingURL=chunk-3DTLDGIM.js.map
|