@typicalday/firegraph 0.15.0 → 0.16.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/README.md +39 -17
- package/dist/{backend-CvImIwTY.d.cts → backend-CE3pM9-T.d.ts} +32 -2
- package/dist/{backend-BpYLdwCW.d.cts → backend-DNzv8KSR.d.cts} +33 -19
- package/dist/{backend-BpYLdwCW.d.ts → backend-DNzv8KSR.d.ts} +33 -19
- package/dist/{backend-YH5HtawN.d.ts → backend-EjFfw9yO.d.cts} +32 -2
- package/dist/backend.cjs.map +1 -1
- package/dist/backend.d.cts +2 -2
- package/dist/backend.d.ts +2 -2
- package/dist/backend.js +1 -1
- package/dist/{chunk-FODIMIWY.js → chunk-5JBNLH5W.js} +17 -6
- package/dist/chunk-5JBNLH5W.js.map +1 -0
- package/dist/{chunk-5HIRYV2S.js → chunk-6IO74NKD.js} +12 -10
- package/dist/{chunk-5HIRYV2S.js.map → chunk-6IO74NKD.js.map} +1 -1
- package/dist/{chunk-ULRDQ6HZ.js → chunk-NZVSLWNY.js} +6 -1
- package/dist/chunk-NZVSLWNY.js.map +1 -0
- package/dist/{chunk-N5HFDWQX.js → chunk-PWIO46RT.js} +1 -1
- package/dist/{chunk-N5HFDWQX.js.map → chunk-PWIO46RT.js.map} +1 -1
- package/dist/{client-B5o39X79.d.ts → client-CNAwJayO.d.ts} +1 -1
- package/dist/{client-BGHwxwPg.d.cts → client-CaXH5D5C.d.cts} +1 -1
- package/dist/cloudflare/index.cjs +11 -9
- package/dist/cloudflare/index.cjs.map +1 -1
- package/dist/cloudflare/index.d.cts +3 -3
- package/dist/cloudflare/index.d.ts +3 -3
- package/dist/cloudflare/index.js +3 -3
- package/dist/codegen/index.d.cts +1 -1
- package/dist/codegen/index.d.ts +1 -1
- package/dist/firestore-enterprise/index.cjs +11 -9
- package/dist/firestore-enterprise/index.cjs.map +1 -1
- package/dist/firestore-enterprise/index.d.cts +3 -3
- package/dist/firestore-enterprise/index.d.ts +3 -3
- package/dist/firestore-enterprise/index.js +2 -2
- package/dist/firestore-standard/index.cjs +11 -9
- package/dist/firestore-standard/index.cjs.map +1 -1
- package/dist/firestore-standard/index.d.cts +3 -3
- package/dist/firestore-standard/index.d.ts +3 -3
- package/dist/firestore-standard/index.js +2 -2
- package/dist/index.cjs +11 -9
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +4 -4
- package/dist/index.d.ts +4 -4
- package/dist/index.js +1 -1
- package/dist/{registry-tKTb5Kx1.d.ts → registry-By1i-zge.d.ts} +1 -1
- package/dist/{registry-BGh7Jqpb.d.cts → registry-CNToyEra.d.cts} +1 -1
- package/dist/sqlite/index.cjs +24 -12
- package/dist/sqlite/index.cjs.map +1 -1
- package/dist/sqlite/index.d.cts +4 -4
- package/dist/sqlite/index.d.ts +4 -4
- package/dist/sqlite/index.js +4 -4
- package/dist/sqlite/local.cjs +474 -47
- package/dist/sqlite/local.cjs.map +1 -1
- package/dist/sqlite/local.d.cts +31 -5
- package/dist/sqlite/local.d.ts +31 -5
- package/dist/sqlite/local.js +429 -4
- package/dist/sqlite/local.js.map +1 -1
- package/package.json +1 -1
- package/dist/chunk-FODIMIWY.js.map +0 -1
- package/dist/chunk-ULRDQ6HZ.js.map +0 -1
package/dist/sqlite/index.d.cts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { c as createGraphClient } from '../client-
|
|
2
|
-
export { M as META_EDGE_TYPE, a as META_NODE_TYPE, c as createMergedRegistry, b as createRegistry, g as generateId } from '../registry-
|
|
3
|
-
export { S as SqliteBackendOptions, a as SqliteCapability, c as createSqliteBackend } from '../backend-
|
|
4
|
-
import '../backend-
|
|
1
|
+
export { c as createGraphClient } from '../client-CaXH5D5C.cjs';
|
|
2
|
+
export { M as META_EDGE_TYPE, a as META_NODE_TYPE, c as createMergedRegistry, b as createRegistry, g as generateId } from '../registry-CNToyEra.cjs';
|
|
3
|
+
export { S as SqliteBackendOptions, a as SqliteCapability, c as createSqliteBackend } from '../backend-EjFfw9yO.cjs';
|
|
4
|
+
import '../backend-DNzv8KSR.cjs';
|
|
5
5
|
import '@google-cloud/firestore';
|
package/dist/sqlite/index.d.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
export { c as createGraphClient } from '../client-
|
|
2
|
-
export { M as META_EDGE_TYPE, a as META_NODE_TYPE, c as createMergedRegistry, b as createRegistry, g as generateId } from '../registry-
|
|
3
|
-
export { S as SqliteBackendOptions, a as SqliteCapability, c as createSqliteBackend } from '../backend-
|
|
4
|
-
import '../backend-
|
|
1
|
+
export { c as createGraphClient } from '../client-CNAwJayO.js';
|
|
2
|
+
export { M as META_EDGE_TYPE, a as META_NODE_TYPE, c as createMergedRegistry, b as createRegistry, g as generateId } from '../registry-By1i-zge.js';
|
|
3
|
+
export { S as SqliteBackendOptions, a as SqliteCapability, c as createSqliteBackend } from '../backend-CE3pM9-T.js';
|
|
4
|
+
import '../backend-DNzv8KSR.js';
|
|
5
5
|
import '@google-cloud/firestore';
|
package/dist/sqlite/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import {
|
|
2
2
|
createSqliteBackend
|
|
3
|
-
} from "../chunk-
|
|
4
|
-
import "../chunk-
|
|
3
|
+
} from "../chunk-5JBNLH5W.js";
|
|
4
|
+
import "../chunk-NZVSLWNY.js";
|
|
5
5
|
import "../chunk-2DHMNTV6.js";
|
|
6
|
-
import "../chunk-
|
|
6
|
+
import "../chunk-PWIO46RT.js";
|
|
7
7
|
import {
|
|
8
8
|
META_EDGE_TYPE,
|
|
9
9
|
META_NODE_TYPE,
|
|
@@ -11,7 +11,7 @@ import {
|
|
|
11
11
|
createMergedRegistry,
|
|
12
12
|
createRegistry,
|
|
13
13
|
generateId
|
|
14
|
-
} from "../chunk-
|
|
14
|
+
} from "../chunk-6IO74NKD.js";
|
|
15
15
|
import "../chunk-NGAJCALM.js";
|
|
16
16
|
import "../chunk-SIHE4UY4.js";
|
|
17
17
|
import "../chunk-EQJUUVFG.js";
|
package/dist/sqlite/local.cjs
CHANGED
|
@@ -44,24 +44,6 @@ var FiregraphError = class extends Error {
|
|
|
44
44
|
}
|
|
45
45
|
};
|
|
46
46
|
|
|
47
|
-
// src/docid.ts
|
|
48
|
-
var import_node_crypto = require("crypto");
|
|
49
|
-
|
|
50
|
-
// src/internal/constants.ts
|
|
51
|
-
var NODE_RELATION = "is";
|
|
52
|
-
var SHARD_SEPARATOR = ":";
|
|
53
|
-
|
|
54
|
-
// src/docid.ts
|
|
55
|
-
function computeNodeDocId(uid) {
|
|
56
|
-
return uid;
|
|
57
|
-
}
|
|
58
|
-
function computeEdgeDocId(aUid, axbType, bUid) {
|
|
59
|
-
const composite = `${aUid}${SHARD_SEPARATOR}${axbType}${SHARD_SEPARATOR}${bUid}`;
|
|
60
|
-
const hash = (0, import_node_crypto.createHash)("sha256").update(composite).digest("hex");
|
|
61
|
-
const shard = hash[0];
|
|
62
|
-
return `${shard}${SHARD_SEPARATOR}${aUid}${SHARD_SEPARATOR}${axbType}${SHARD_SEPARATOR}${bUid}`;
|
|
63
|
-
}
|
|
64
|
-
|
|
65
47
|
// src/internal/backend.ts
|
|
66
48
|
function createCapabilities(caps) {
|
|
67
49
|
return {
|
|
@@ -225,31 +207,6 @@ function quoteColumnAlias(label) {
|
|
|
225
207
|
return `"${label.replace(/"/g, '""')}"`;
|
|
226
208
|
}
|
|
227
209
|
|
|
228
|
-
// src/timestamp.ts
|
|
229
|
-
var GraphTimestampImpl = class _GraphTimestampImpl {
|
|
230
|
-
constructor(seconds, nanoseconds) {
|
|
231
|
-
this.seconds = seconds;
|
|
232
|
-
this.nanoseconds = nanoseconds;
|
|
233
|
-
}
|
|
234
|
-
toDate() {
|
|
235
|
-
return new Date(this.toMillis());
|
|
236
|
-
}
|
|
237
|
-
toMillis() {
|
|
238
|
-
return this.seconds * 1e3 + Math.floor(this.nanoseconds / 1e6);
|
|
239
|
-
}
|
|
240
|
-
toJSON() {
|
|
241
|
-
return { seconds: this.seconds, nanoseconds: this.nanoseconds };
|
|
242
|
-
}
|
|
243
|
-
static fromMillis(ms) {
|
|
244
|
-
const seconds = Math.floor(ms / 1e3);
|
|
245
|
-
const nanoseconds = (ms - seconds * 1e3) * 1e6;
|
|
246
|
-
return new _GraphTimestampImpl(seconds, nanoseconds);
|
|
247
|
-
}
|
|
248
|
-
static now() {
|
|
249
|
-
return _GraphTimestampImpl.fromMillis(Date.now());
|
|
250
|
-
}
|
|
251
|
-
};
|
|
252
|
-
|
|
253
210
|
// src/internal/sqlite-data-ops.ts
|
|
254
211
|
var FIRESTORE_TYPE_NAMES = /* @__PURE__ */ new Set([
|
|
255
212
|
"Timestamp",
|
|
@@ -318,6 +275,35 @@ function compileDataOpsExpr(ops, base, params, backendLabel) {
|
|
|
318
275
|
return expr;
|
|
319
276
|
}
|
|
320
277
|
|
|
278
|
+
// src/timestamp.ts
|
|
279
|
+
var GraphTimestampImpl = class _GraphTimestampImpl {
|
|
280
|
+
constructor(seconds, nanoseconds) {
|
|
281
|
+
this.seconds = seconds;
|
|
282
|
+
this.nanoseconds = nanoseconds;
|
|
283
|
+
}
|
|
284
|
+
toDate() {
|
|
285
|
+
return new Date(this.toMillis());
|
|
286
|
+
}
|
|
287
|
+
toMillis() {
|
|
288
|
+
return this.seconds * 1e3 + Math.floor(this.nanoseconds / 1e6);
|
|
289
|
+
}
|
|
290
|
+
toJSON() {
|
|
291
|
+
return { seconds: this.seconds, nanoseconds: this.nanoseconds };
|
|
292
|
+
}
|
|
293
|
+
static fromMillis(ms) {
|
|
294
|
+
const seconds = Math.floor(ms / 1e3);
|
|
295
|
+
const nanoseconds = (ms - seconds * 1e3) * 1e6;
|
|
296
|
+
return new _GraphTimestampImpl(seconds, nanoseconds);
|
|
297
|
+
}
|
|
298
|
+
static now() {
|
|
299
|
+
return _GraphTimestampImpl.fromMillis(Date.now());
|
|
300
|
+
}
|
|
301
|
+
};
|
|
302
|
+
|
|
303
|
+
// src/internal/constants.ts
|
|
304
|
+
var NODE_RELATION = "is";
|
|
305
|
+
var SHARD_SEPARATOR = ":";
|
|
306
|
+
|
|
321
307
|
// src/internal/serialization-tag.ts
|
|
322
308
|
var SERIALIZATION_TAG = "__firegraph_ser__";
|
|
323
309
|
var KNOWN_TYPES = /* @__PURE__ */ new Set(["Timestamp", "GeoPoint", "VectorValue", "DocumentReference"]);
|
|
@@ -622,6 +608,9 @@ function compileFilter(filter, params) {
|
|
|
622
608
|
);
|
|
623
609
|
}
|
|
624
610
|
}
|
|
611
|
+
function compileFilterConditions(filters, params) {
|
|
612
|
+
return filters.map((f) => compileFilter(f, params));
|
|
613
|
+
}
|
|
625
614
|
function asArray(value, op) {
|
|
626
615
|
if (!Array.isArray(value) || value.length === 0) {
|
|
627
616
|
throw new FiregraphError(`Operator "${op}" requires a non-empty array value`, "INVALID_QUERY");
|
|
@@ -1036,6 +1025,311 @@ function rowTimestampToMillis(value) {
|
|
|
1036
1025
|
);
|
|
1037
1026
|
}
|
|
1038
1027
|
|
|
1028
|
+
// src/internal/sqlite-search.ts
|
|
1029
|
+
var VECTOR_DISTANCE_UDF = "firegraph_vector_distance";
|
|
1030
|
+
var DISTANCE_ALIAS = "__fg_distance";
|
|
1031
|
+
var BACKEND_ERR_LABEL2 = "SQLite backend";
|
|
1032
|
+
var ENVELOPE_FIELDS = /* @__PURE__ */ new Set([
|
|
1033
|
+
"aType",
|
|
1034
|
+
"aUid",
|
|
1035
|
+
"axbType",
|
|
1036
|
+
"bType",
|
|
1037
|
+
"bUid",
|
|
1038
|
+
"createdAt",
|
|
1039
|
+
"updatedAt",
|
|
1040
|
+
"v"
|
|
1041
|
+
]);
|
|
1042
|
+
function ftsTableName(table) {
|
|
1043
|
+
return `${table}_fts`;
|
|
1044
|
+
}
|
|
1045
|
+
function ftsMapTableName(table) {
|
|
1046
|
+
return `${table}_fts_map`;
|
|
1047
|
+
}
|
|
1048
|
+
function textExtractionExpr(dataRef) {
|
|
1049
|
+
return `(SELECT coalesce(group_concat("value", ' '), '') FROM json_tree(coalesce(${dataRef}, '{}')) WHERE "type" = 'text')`;
|
|
1050
|
+
}
|
|
1051
|
+
function buildFtsDDL(table) {
|
|
1052
|
+
const t = quoteIdent2(table);
|
|
1053
|
+
const fts = quoteIdent2(ftsTableName(table));
|
|
1054
|
+
const map = quoteIdent2(ftsMapTableName(table));
|
|
1055
|
+
const mappedId = `(SELECT "id" FROM ${map} WHERE "doc_id" = new."doc_id")`;
|
|
1056
|
+
const reindexBody = ` INSERT INTO ${map} ("doc_id") SELECT new."doc_id" WHERE NOT EXISTS (SELECT 1 FROM ${map} WHERE "doc_id" = new."doc_id");
|
|
1057
|
+
DELETE FROM ${fts} WHERE rowid = ${mappedId};
|
|
1058
|
+
INSERT INTO ${fts} (rowid, "text") VALUES (${mappedId}, ${textExtractionExpr('new."data"')});
|
|
1059
|
+
`;
|
|
1060
|
+
return [
|
|
1061
|
+
`CREATE TABLE IF NOT EXISTS ${map} (
|
|
1062
|
+
"id" INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
1063
|
+
"doc_id" TEXT NOT NULL UNIQUE
|
|
1064
|
+
)`,
|
|
1065
|
+
`CREATE VIRTUAL TABLE IF NOT EXISTS ${fts} USING fts5("text")`,
|
|
1066
|
+
`CREATE TRIGGER IF NOT EXISTS ${quoteIdent2(`${table}_fts_ai`)} AFTER INSERT ON ${t} BEGIN
|
|
1067
|
+
${reindexBody}END`,
|
|
1068
|
+
`CREATE TRIGGER IF NOT EXISTS ${quoteIdent2(`${table}_fts_au`)} AFTER UPDATE ON ${t} BEGIN
|
|
1069
|
+
${reindexBody}END`,
|
|
1070
|
+
`CREATE TRIGGER IF NOT EXISTS ${quoteIdent2(`${table}_fts_ad`)} AFTER DELETE ON ${t} BEGIN
|
|
1071
|
+
DELETE FROM ${fts} WHERE rowid = (SELECT "id" FROM ${map} WHERE "doc_id" = old."doc_id");
|
|
1072
|
+
DELETE FROM ${map} WHERE "doc_id" = old."doc_id";
|
|
1073
|
+
END`
|
|
1074
|
+
];
|
|
1075
|
+
}
|
|
1076
|
+
function buildFtsSyncStatements(table) {
|
|
1077
|
+
const t = quoteIdent2(table);
|
|
1078
|
+
const fts = quoteIdent2(ftsTableName(table));
|
|
1079
|
+
const map = quoteIdent2(ftsMapTableName(table));
|
|
1080
|
+
return [
|
|
1081
|
+
`DELETE FROM ${fts} WHERE rowid IN (
|
|
1082
|
+
SELECT m."id" FROM ${map} m LEFT JOIN ${t} t ON t."doc_id" = m."doc_id"
|
|
1083
|
+
WHERE t."doc_id" IS NULL
|
|
1084
|
+
)`,
|
|
1085
|
+
`DELETE FROM ${map} WHERE "doc_id" NOT IN (SELECT "doc_id" FROM ${t})`,
|
|
1086
|
+
`INSERT OR IGNORE INTO ${map} ("doc_id") SELECT "doc_id" FROM ${t}`,
|
|
1087
|
+
`INSERT INTO ${fts} (rowid, "text")
|
|
1088
|
+
SELECT m."id", ${textExtractionExpr('t."data"')}
|
|
1089
|
+
FROM ${t} t JOIN ${map} m ON m."doc_id" = t."doc_id"
|
|
1090
|
+
WHERE m."id" NOT IN (SELECT rowid FROM ${fts})`
|
|
1091
|
+
];
|
|
1092
|
+
}
|
|
1093
|
+
function buildLocalSearchDDL(table) {
|
|
1094
|
+
return [...buildFtsDDL(table), ...buildFtsSyncStatements(table)];
|
|
1095
|
+
}
|
|
1096
|
+
function normalizeVectorFieldPath(label, field) {
|
|
1097
|
+
if (ENVELOPE_FIELDS.has(field)) {
|
|
1098
|
+
throw new FiregraphError(
|
|
1099
|
+
`findNearest(): ${label} '${field}' is a built-in envelope field \u2014 vectors must live under \`data.*\`. Use a path like 'data.${field}' if you really meant a nested data field.`,
|
|
1100
|
+
"INVALID_QUERY"
|
|
1101
|
+
);
|
|
1102
|
+
}
|
|
1103
|
+
if (field === "data" || field.startsWith("data.")) return field;
|
|
1104
|
+
return `data.${field}`;
|
|
1105
|
+
}
|
|
1106
|
+
function normalizeFullTextFieldPath(field) {
|
|
1107
|
+
if (ENVELOPE_FIELDS.has(field)) {
|
|
1108
|
+
throw new FiregraphError(
|
|
1109
|
+
`fullTextSearch(): field '${field}' is a built-in envelope field \u2014 text-indexed fields must live under \`data.*\`. Use a path like 'data.${field}' if you really meant a nested data field.`,
|
|
1110
|
+
"INVALID_QUERY"
|
|
1111
|
+
);
|
|
1112
|
+
}
|
|
1113
|
+
if (field === "data" || field.startsWith("data.")) return field;
|
|
1114
|
+
return `data.${field}`;
|
|
1115
|
+
}
|
|
1116
|
+
function buildSearchFilters(params) {
|
|
1117
|
+
const filters = [];
|
|
1118
|
+
if (params.aType) filters.push({ field: "aType", op: "==", value: params.aType });
|
|
1119
|
+
if (params.axbType) filters.push({ field: "axbType", op: "==", value: params.axbType });
|
|
1120
|
+
if (params.bType) filters.push({ field: "bType", op: "==", value: params.bType });
|
|
1121
|
+
for (const clause of params.where ?? []) {
|
|
1122
|
+
const field = ENVELOPE_FIELDS.has(clause.field) || clause.field.startsWith("data.") ? clause.field : `data.${clause.field}`;
|
|
1123
|
+
filters.push({ field, op: clause.op, value: clause.value });
|
|
1124
|
+
}
|
|
1125
|
+
return filters;
|
|
1126
|
+
}
|
|
1127
|
+
function compileFullTextSearch(table, params) {
|
|
1128
|
+
if (typeof params.query !== "string" || params.query.length === 0) {
|
|
1129
|
+
throw new FiregraphError(
|
|
1130
|
+
"fullTextSearch(): query must be a non-empty string.",
|
|
1131
|
+
"INVALID_QUERY"
|
|
1132
|
+
);
|
|
1133
|
+
}
|
|
1134
|
+
if (!Number.isInteger(params.limit) || params.limit <= 0) {
|
|
1135
|
+
throw new FiregraphError(
|
|
1136
|
+
`fullTextSearch(): limit must be a positive integer (got ${params.limit}).`,
|
|
1137
|
+
"INVALID_QUERY"
|
|
1138
|
+
);
|
|
1139
|
+
}
|
|
1140
|
+
const normalizedFields = params.fields?.map((f) => normalizeFullTextFieldPath(f));
|
|
1141
|
+
if (normalizedFields !== void 0 && normalizedFields.length > 0) {
|
|
1142
|
+
throw new FiregraphError(
|
|
1143
|
+
"fullTextSearch(): the `fields` option is not yet supported \u2014 the local SQLite FTS index stores one combined text column per record. Omit `fields` to search all string values.",
|
|
1144
|
+
"INVALID_QUERY"
|
|
1145
|
+
);
|
|
1146
|
+
}
|
|
1147
|
+
const t = quoteIdent2(table);
|
|
1148
|
+
const fts = quoteIdent2(ftsTableName(table));
|
|
1149
|
+
const map = quoteIdent2(ftsMapTableName(table));
|
|
1150
|
+
const sqlParams = [params.query];
|
|
1151
|
+
const conditions = [`${fts} MATCH ?`];
|
|
1152
|
+
conditions.push(...compileFilterConditions(buildSearchFilters(params), sqlParams));
|
|
1153
|
+
sqlParams.push(params.limit);
|
|
1154
|
+
const sql = `SELECT ${t}.* FROM ${fts} JOIN ${map} ON ${map}."id" = ${fts}.rowid JOIN ${t} ON ${t}."doc_id" = ${map}."doc_id" WHERE ${conditions.join(" AND ")} ORDER BY bm25(${fts}) ASC, ${t}."doc_id" ASC LIMIT ?`;
|
|
1155
|
+
return { sql, params: sqlParams };
|
|
1156
|
+
}
|
|
1157
|
+
var DISTANCE_MEASURES = /* @__PURE__ */ new Set(["EUCLIDEAN", "COSINE", "DOT_PRODUCT"]);
|
|
1158
|
+
function toNumberArray(qv) {
|
|
1159
|
+
if (Array.isArray(qv)) return qv;
|
|
1160
|
+
if (typeof qv.toArray === "function") {
|
|
1161
|
+
return qv.toArray();
|
|
1162
|
+
}
|
|
1163
|
+
throw new FiregraphError(
|
|
1164
|
+
"findNearest(): queryVector must be a number[] or a Firestore VectorValue.",
|
|
1165
|
+
"INVALID_QUERY"
|
|
1166
|
+
);
|
|
1167
|
+
}
|
|
1168
|
+
function compileFindNearest(table, params) {
|
|
1169
|
+
const vec = toNumberArray(params.queryVector);
|
|
1170
|
+
if (vec.length === 0) {
|
|
1171
|
+
throw new FiregraphError(
|
|
1172
|
+
"findNearest(): queryVector is empty \u2014 at least one dimension is required.",
|
|
1173
|
+
"INVALID_QUERY"
|
|
1174
|
+
);
|
|
1175
|
+
}
|
|
1176
|
+
if (!Number.isInteger(params.limit) || params.limit <= 0 || params.limit > 1e3) {
|
|
1177
|
+
throw new FiregraphError(
|
|
1178
|
+
`findNearest(): limit must be a positive integer \u2264 1000 (got ${params.limit}).`,
|
|
1179
|
+
"INVALID_QUERY"
|
|
1180
|
+
);
|
|
1181
|
+
}
|
|
1182
|
+
if (!DISTANCE_MEASURES.has(params.distanceMeasure)) {
|
|
1183
|
+
throw new FiregraphError(
|
|
1184
|
+
`findNearest(): unknown distanceMeasure '${String(params.distanceMeasure)}' \u2014 expected EUCLIDEAN, COSINE, or DOT_PRODUCT.`,
|
|
1185
|
+
"INVALID_QUERY"
|
|
1186
|
+
);
|
|
1187
|
+
}
|
|
1188
|
+
const vectorField = normalizeVectorFieldPath("vectorField", params.vectorField);
|
|
1189
|
+
let vectorExpr;
|
|
1190
|
+
if (vectorField === "data") {
|
|
1191
|
+
vectorExpr = '"data"';
|
|
1192
|
+
} else {
|
|
1193
|
+
const suffix = vectorField.slice("data.".length);
|
|
1194
|
+
for (const part of suffix.split(".")) {
|
|
1195
|
+
validateJsonPathKey(part, BACKEND_ERR_LABEL2);
|
|
1196
|
+
}
|
|
1197
|
+
vectorExpr = `json_extract("data", '$.${suffix}')`;
|
|
1198
|
+
}
|
|
1199
|
+
let distancePath = null;
|
|
1200
|
+
if (params.distanceResultField !== void 0) {
|
|
1201
|
+
const normalized = normalizeVectorFieldPath("distanceResultField", params.distanceResultField);
|
|
1202
|
+
if (normalized === "data") {
|
|
1203
|
+
throw new FiregraphError(
|
|
1204
|
+
`findNearest(): distanceResultField 'data' would replace the entire data payload \u2014 use a nested path like 'data.distance'.`,
|
|
1205
|
+
"INVALID_QUERY"
|
|
1206
|
+
);
|
|
1207
|
+
}
|
|
1208
|
+
distancePath = normalized.slice("data.".length).split(".");
|
|
1209
|
+
for (const part of distancePath) {
|
|
1210
|
+
validateJsonPathKey(part, BACKEND_ERR_LABEL2);
|
|
1211
|
+
}
|
|
1212
|
+
}
|
|
1213
|
+
const sqlParams = [JSON.stringify(vec), params.distanceMeasure];
|
|
1214
|
+
const conditions = compileFilterConditions(buildSearchFilters(params), sqlParams);
|
|
1215
|
+
const innerWhere = conditions.length > 0 ? ` WHERE ${conditions.join(" AND ")}` : "";
|
|
1216
|
+
const dist = quoteIdent2(DISTANCE_ALIAS);
|
|
1217
|
+
const descending = params.distanceMeasure === "DOT_PRODUCT";
|
|
1218
|
+
let sql = `SELECT * FROM (SELECT *, ${VECTOR_DISTANCE_UDF}(${vectorExpr}, ?, ?) AS ${dist} FROM ${quoteIdent2(table)}${innerWhere}) WHERE ${dist} IS NOT NULL`;
|
|
1219
|
+
if (params.distanceThreshold !== void 0) {
|
|
1220
|
+
sql += ` AND ${dist} ${descending ? ">=" : "<="} ?`;
|
|
1221
|
+
sqlParams.push(params.distanceThreshold);
|
|
1222
|
+
}
|
|
1223
|
+
sql += ` ORDER BY ${dist} ${descending ? "DESC" : "ASC"}, "doc_id" ASC LIMIT ?`;
|
|
1224
|
+
sqlParams.push(params.limit);
|
|
1225
|
+
return { stmt: { sql, params: sqlParams }, distancePath };
|
|
1226
|
+
}
|
|
1227
|
+
var memoQueryJson = null;
|
|
1228
|
+
var memoQueryVec = null;
|
|
1229
|
+
function computeVectorDistance(storedJson, queryJson, measure) {
|
|
1230
|
+
if (typeof storedJson !== "string" || typeof queryJson !== "string" || typeof measure !== "string") {
|
|
1231
|
+
return null;
|
|
1232
|
+
}
|
|
1233
|
+
let query;
|
|
1234
|
+
if (memoQueryJson === queryJson && memoQueryVec !== null) {
|
|
1235
|
+
query = memoQueryVec;
|
|
1236
|
+
} else {
|
|
1237
|
+
let parsed;
|
|
1238
|
+
try {
|
|
1239
|
+
parsed = JSON.parse(queryJson);
|
|
1240
|
+
} catch {
|
|
1241
|
+
return null;
|
|
1242
|
+
}
|
|
1243
|
+
if (!Array.isArray(parsed)) return null;
|
|
1244
|
+
query = parsed;
|
|
1245
|
+
memoQueryJson = queryJson;
|
|
1246
|
+
memoQueryVec = query;
|
|
1247
|
+
}
|
|
1248
|
+
let stored;
|
|
1249
|
+
try {
|
|
1250
|
+
stored = JSON.parse(storedJson);
|
|
1251
|
+
} catch {
|
|
1252
|
+
return null;
|
|
1253
|
+
}
|
|
1254
|
+
if (!Array.isArray(stored) || stored.length !== query.length) return null;
|
|
1255
|
+
let dot = 0;
|
|
1256
|
+
let sumSq = 0;
|
|
1257
|
+
let normStored = 0;
|
|
1258
|
+
let normQuery = 0;
|
|
1259
|
+
for (let i = 0; i < query.length; i++) {
|
|
1260
|
+
const a = stored[i];
|
|
1261
|
+
const b = query[i];
|
|
1262
|
+
if (typeof a !== "number" || !Number.isFinite(a)) return null;
|
|
1263
|
+
if (typeof b !== "number" || !Number.isFinite(b)) return null;
|
|
1264
|
+
dot += a * b;
|
|
1265
|
+
const diff = a - b;
|
|
1266
|
+
sumSq += diff * diff;
|
|
1267
|
+
normStored += a * a;
|
|
1268
|
+
normQuery += b * b;
|
|
1269
|
+
}
|
|
1270
|
+
let result;
|
|
1271
|
+
switch (measure) {
|
|
1272
|
+
case "EUCLIDEAN":
|
|
1273
|
+
result = Math.sqrt(sumSq);
|
|
1274
|
+
break;
|
|
1275
|
+
case "COSINE": {
|
|
1276
|
+
const denom = Math.sqrt(normStored) * Math.sqrt(normQuery);
|
|
1277
|
+
if (denom === 0) return null;
|
|
1278
|
+
result = 1 - dot / denom;
|
|
1279
|
+
break;
|
|
1280
|
+
}
|
|
1281
|
+
case "DOT_PRODUCT":
|
|
1282
|
+
result = dot;
|
|
1283
|
+
break;
|
|
1284
|
+
default:
|
|
1285
|
+
return null;
|
|
1286
|
+
}
|
|
1287
|
+
return Number.isFinite(result) ? result : null;
|
|
1288
|
+
}
|
|
1289
|
+
function setDataPath(data, path, value) {
|
|
1290
|
+
let cursor = data;
|
|
1291
|
+
for (let i = 0; i < path.length - 1; i++) {
|
|
1292
|
+
const key = path[i];
|
|
1293
|
+
const next = cursor[key];
|
|
1294
|
+
if (typeof next !== "object" || next === null || Array.isArray(next)) {
|
|
1295
|
+
const created = {};
|
|
1296
|
+
cursor[key] = created;
|
|
1297
|
+
cursor = created;
|
|
1298
|
+
} else {
|
|
1299
|
+
cursor = next;
|
|
1300
|
+
}
|
|
1301
|
+
}
|
|
1302
|
+
cursor[path[path.length - 1]] = value;
|
|
1303
|
+
}
|
|
1304
|
+
function findOrphanedFtsTables(allTables, catalogTables, rootTable) {
|
|
1305
|
+
const names = new Set(allTables);
|
|
1306
|
+
const liveGraphTables = new Set(catalogTables);
|
|
1307
|
+
const subgraphPrefix = `${rootTable}_g_`;
|
|
1308
|
+
const orphans = [];
|
|
1309
|
+
for (const name of names) {
|
|
1310
|
+
let base = null;
|
|
1311
|
+
if (name.endsWith("_fts_map")) base = name.slice(0, -"_fts_map".length);
|
|
1312
|
+
else if (name.endsWith("_fts")) base = name.slice(0, -"_fts".length);
|
|
1313
|
+
if (base === null || !base.startsWith(subgraphPrefix)) continue;
|
|
1314
|
+
if (liveGraphTables.has(name)) continue;
|
|
1315
|
+
if (names.has(base)) continue;
|
|
1316
|
+
orphans.push(name);
|
|
1317
|
+
}
|
|
1318
|
+
return orphans.sort();
|
|
1319
|
+
}
|
|
1320
|
+
|
|
1321
|
+
// src/docid.ts
|
|
1322
|
+
var import_node_crypto = require("crypto");
|
|
1323
|
+
function computeNodeDocId(uid) {
|
|
1324
|
+
return uid;
|
|
1325
|
+
}
|
|
1326
|
+
function computeEdgeDocId(aUid, axbType, bUid) {
|
|
1327
|
+
const composite = `${aUid}${SHARD_SEPARATOR}${axbType}${SHARD_SEPARATOR}${bUid}`;
|
|
1328
|
+
const hash = (0, import_node_crypto.createHash)("sha256").update(composite).digest("hex");
|
|
1329
|
+
const shard = hash[0];
|
|
1330
|
+
return `${shard}${SHARD_SEPARATOR}${aUid}${SHARD_SEPARATOR}${axbType}${SHARD_SEPARATOR}${bUid}`;
|
|
1331
|
+
}
|
|
1332
|
+
|
|
1039
1333
|
// src/sqlite/catalog.ts
|
|
1040
1334
|
function catalogTableName(rootTable) {
|
|
1041
1335
|
validateTableName(rootTable);
|
|
@@ -1196,7 +1490,7 @@ var SQLITE_CORE_CAPS = [
|
|
|
1196
1490
|
"raw.sql"
|
|
1197
1491
|
];
|
|
1198
1492
|
var SqliteBackendImpl = class _SqliteBackendImpl {
|
|
1199
|
-
constructor(executor, rootTable, storageScope, scopePath, registry, coreIndexes) {
|
|
1493
|
+
constructor(executor, rootTable, storageScope, scopePath, registry, coreIndexes, extraTableDDL) {
|
|
1200
1494
|
this.executor = executor;
|
|
1201
1495
|
validateTableName(rootTable);
|
|
1202
1496
|
this.rootTable = rootTable;
|
|
@@ -1205,6 +1499,7 @@ var SqliteBackendImpl = class _SqliteBackendImpl {
|
|
|
1205
1499
|
this.scopePath = scopePath;
|
|
1206
1500
|
this.registry = registry;
|
|
1207
1501
|
this.coreIndexes = coreIndexes;
|
|
1502
|
+
this.extraTableDDL = extraTableDDL;
|
|
1208
1503
|
const caps = new Set(SQLITE_CORE_CAPS);
|
|
1209
1504
|
if (typeof executor.transaction === "function") {
|
|
1210
1505
|
caps.add("core.transactions");
|
|
@@ -1221,6 +1516,7 @@ var SqliteBackendImpl = class _SqliteBackendImpl {
|
|
|
1221
1516
|
rootTable;
|
|
1222
1517
|
registry;
|
|
1223
1518
|
coreIndexes;
|
|
1519
|
+
extraTableDDL;
|
|
1224
1520
|
ensured = null;
|
|
1225
1521
|
/**
|
|
1226
1522
|
* Lazily create this graph's table + indexes + the catalog, and register
|
|
@@ -1237,12 +1533,18 @@ var SqliteBackendImpl = class _SqliteBackendImpl {
|
|
|
1237
1533
|
}
|
|
1238
1534
|
return this.ensured;
|
|
1239
1535
|
}
|
|
1536
|
+
/** @internal See `SqliteStorageBackend.ensureReady`. */
|
|
1537
|
+
async ensureReady(force = false) {
|
|
1538
|
+
if (force) this.ensured = null;
|
|
1539
|
+
await this.ensureSchema();
|
|
1540
|
+
}
|
|
1240
1541
|
async doEnsureSchema() {
|
|
1241
1542
|
const ddl = [
|
|
1242
1543
|
...buildSchemaStatements(this.collectionPath, {
|
|
1243
1544
|
coreIndexes: this.coreIndexes,
|
|
1244
1545
|
registry: this.registry
|
|
1245
1546
|
}),
|
|
1547
|
+
...this.extraTableDDL ? this.extraTableDDL(this.collectionPath) : [],
|
|
1246
1548
|
buildCatalogDDL(this.rootTable)
|
|
1247
1549
|
];
|
|
1248
1550
|
const statements = ddl.map((sql) => ({ sql, params: [] }));
|
|
@@ -1373,7 +1675,8 @@ var SqliteBackendImpl = class _SqliteBackendImpl {
|
|
|
1373
1675
|
newStorageScope,
|
|
1374
1676
|
newScope,
|
|
1375
1677
|
this.registry,
|
|
1376
|
-
this.coreIndexes
|
|
1678
|
+
this.coreIndexes,
|
|
1679
|
+
this.extraTableDDL
|
|
1377
1680
|
);
|
|
1378
1681
|
}
|
|
1379
1682
|
// --- Cascade & bulk ---
|
|
@@ -1719,7 +2022,8 @@ function createSqliteBackend(executor, tableName, options = {}) {
|
|
|
1719
2022
|
storageScope,
|
|
1720
2023
|
scopePath,
|
|
1721
2024
|
options.registry,
|
|
1722
|
-
options.coreIndexes
|
|
2025
|
+
options.coreIndexes,
|
|
2026
|
+
options.extraTableDDL
|
|
1723
2027
|
);
|
|
1724
2028
|
}
|
|
1725
2029
|
|
|
@@ -1779,6 +2083,118 @@ function applyPragmas(db, pragmas) {
|
|
|
1779
2083
|
db.pragma(`${key} = ${value}`);
|
|
1780
2084
|
}
|
|
1781
2085
|
}
|
|
2086
|
+
function registerVectorUdf(db) {
|
|
2087
|
+
try {
|
|
2088
|
+
db.function(
|
|
2089
|
+
VECTOR_DISTANCE_UDF,
|
|
2090
|
+
{ deterministic: true },
|
|
2091
|
+
(stored, query, measure) => computeVectorDistance(stored, query, measure)
|
|
2092
|
+
);
|
|
2093
|
+
} catch {
|
|
2094
|
+
}
|
|
2095
|
+
}
|
|
2096
|
+
async function sweepOrphanedFtsArtifacts(executor, rootTable) {
|
|
2097
|
+
const tableRows = await executor.all(
|
|
2098
|
+
`SELECT "name" FROM sqlite_master WHERE "type" = 'table'`,
|
|
2099
|
+
[]
|
|
2100
|
+
);
|
|
2101
|
+
const allTables = tableRows.map((r) => String(r.name));
|
|
2102
|
+
const catalogRows = await executor.all(
|
|
2103
|
+
`SELECT "table_name" FROM ${quoteIdent2(catalogTableName(rootTable))}`,
|
|
2104
|
+
[]
|
|
2105
|
+
);
|
|
2106
|
+
const catalogTables = catalogRows.map((r) => String(r.table_name));
|
|
2107
|
+
for (const name of findOrphanedFtsTables(allTables, catalogTables, rootTable)) {
|
|
2108
|
+
validateTableName(name);
|
|
2109
|
+
await executor.run(`DROP TABLE IF EXISTS ${quoteIdent2(name)}`, []);
|
|
2110
|
+
}
|
|
2111
|
+
}
|
|
2112
|
+
function wrapLocalSearchBackend(inner, executor, rootTable) {
|
|
2113
|
+
const caps = /* @__PURE__ */ new Set([
|
|
2114
|
+
...inner.capabilities.values(),
|
|
2115
|
+
"search.fullText",
|
|
2116
|
+
"search.vector"
|
|
2117
|
+
]);
|
|
2118
|
+
const healableTables = /* @__PURE__ */ new Set([
|
|
2119
|
+
inner.collectionPath,
|
|
2120
|
+
ftsTableName(inner.collectionPath),
|
|
2121
|
+
ftsMapTableName(inner.collectionPath)
|
|
2122
|
+
]);
|
|
2123
|
+
const runWithSchema = async (op) => {
|
|
2124
|
+
await inner.ensureReady();
|
|
2125
|
+
try {
|
|
2126
|
+
return await op();
|
|
2127
|
+
} catch (err) {
|
|
2128
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
2129
|
+
const missing = /no such table: (\S+)/.exec(message)?.[1];
|
|
2130
|
+
if (missing === void 0 || !healableTables.has(missing)) throw err;
|
|
2131
|
+
await inner.ensureReady(true);
|
|
2132
|
+
return op();
|
|
2133
|
+
}
|
|
2134
|
+
};
|
|
2135
|
+
const wrapper = {
|
|
2136
|
+
capabilities: createCapabilities(caps),
|
|
2137
|
+
collectionPath: inner.collectionPath,
|
|
2138
|
+
scopePath: inner.scopePath,
|
|
2139
|
+
getDoc: (docId) => inner.getDoc(docId),
|
|
2140
|
+
query: (filters, options) => inner.query(filters, options),
|
|
2141
|
+
setDoc: (docId, record, mode) => inner.setDoc(docId, record, mode),
|
|
2142
|
+
updateDoc: (docId, update) => inner.updateDoc(docId, update),
|
|
2143
|
+
deleteDoc: (docId) => inner.deleteDoc(docId),
|
|
2144
|
+
runTransaction: (fn) => inner.runTransaction(fn),
|
|
2145
|
+
createBatch: () => inner.createBatch(),
|
|
2146
|
+
subgraph: (parentNodeUid, name) => wrapLocalSearchBackend(inner.subgraph(parentNodeUid, name), executor, rootTable),
|
|
2147
|
+
removeNodeCascade: async (uid, reader, options) => {
|
|
2148
|
+
const result = await inner.removeNodeCascade(uid, reader, options);
|
|
2149
|
+
if (result.errors.length === 0) {
|
|
2150
|
+
await sweepOrphanedFtsArtifacts(executor, rootTable);
|
|
2151
|
+
}
|
|
2152
|
+
return result;
|
|
2153
|
+
},
|
|
2154
|
+
bulkRemoveEdges: (params, reader, options) => inner.bulkRemoveEdges(params, reader, options),
|
|
2155
|
+
aggregate: (spec, filters) => inner.aggregate(spec, filters),
|
|
2156
|
+
bulkDelete: (filters, options) => inner.bulkDelete(filters, options),
|
|
2157
|
+
bulkUpdate: (filters, patch, options) => inner.bulkUpdate(filters, patch, options),
|
|
2158
|
+
expand: (params) => inner.expand(params),
|
|
2159
|
+
findEdgesProjected: (select, filters, options) => inner.findEdgesProjected(select, filters, options),
|
|
2160
|
+
// `findEdgesGlobal` stays absent, same as the inner backend — each graph
|
|
2161
|
+
// is its own table; there is no cross-table index.
|
|
2162
|
+
async findNearest(params) {
|
|
2163
|
+
const { stmt, distancePath } = compileFindNearest(inner.collectionPath, params);
|
|
2164
|
+
const rows = await runWithSchema(() => executor.all(stmt.sql, stmt.params));
|
|
2165
|
+
return rows.map((row) => {
|
|
2166
|
+
const record = rowToRecord(row);
|
|
2167
|
+
if (distancePath) {
|
|
2168
|
+
const distance = row[DISTANCE_ALIAS];
|
|
2169
|
+
setDataPath(
|
|
2170
|
+
record.data,
|
|
2171
|
+
distancePath,
|
|
2172
|
+
typeof distance === "number" ? distance : Number(distance)
|
|
2173
|
+
);
|
|
2174
|
+
}
|
|
2175
|
+
return record;
|
|
2176
|
+
});
|
|
2177
|
+
},
|
|
2178
|
+
async fullTextSearch(params) {
|
|
2179
|
+
const stmt = compileFullTextSearch(inner.collectionPath, params);
|
|
2180
|
+
let rows;
|
|
2181
|
+
try {
|
|
2182
|
+
rows = await runWithSchema(() => executor.all(stmt.sql, stmt.params));
|
|
2183
|
+
} catch (err) {
|
|
2184
|
+
const message = err instanceof Error ? err.message : String(err);
|
|
2185
|
+
if (message.includes("fts5") || message.includes("unknown special query")) {
|
|
2186
|
+
throw new FiregraphError(
|
|
2187
|
+
`fullTextSearch(): invalid FTS5 query syntax \u2014 ${message}`,
|
|
2188
|
+
"INVALID_QUERY"
|
|
2189
|
+
);
|
|
2190
|
+
}
|
|
2191
|
+
throw err;
|
|
2192
|
+
}
|
|
2193
|
+
return rows.map(rowToRecord);
|
|
2194
|
+
}
|
|
2195
|
+
};
|
|
2196
|
+
return wrapper;
|
|
2197
|
+
}
|
|
1782
2198
|
async function createLocalSqliteBackend(pathOrDb, options = {}) {
|
|
1783
2199
|
const {
|
|
1784
2200
|
tableName = "firegraph",
|
|
@@ -1815,7 +2231,18 @@ async function createLocalSqliteBackend(pathOrDb, options = {}) {
|
|
|
1815
2231
|
if (pragmas) {
|
|
1816
2232
|
applyPragmas(db, pragmas);
|
|
1817
2233
|
}
|
|
1818
|
-
|
|
2234
|
+
registerVectorUdf(db);
|
|
2235
|
+
const userExtraDDL = backendOptions.extraTableDDL;
|
|
2236
|
+
const optionsWithSearch = {
|
|
2237
|
+
...backendOptions,
|
|
2238
|
+
extraTableDDL: (table) => [
|
|
2239
|
+
...userExtraDDL ? userExtraDDL(table) : [],
|
|
2240
|
+
...buildLocalSearchDDL(table)
|
|
2241
|
+
]
|
|
2242
|
+
};
|
|
2243
|
+
const executor = createBetterSqliteExecutor(db);
|
|
2244
|
+
const inner = createSqliteBackend(executor, tableName, optionsWithSearch);
|
|
2245
|
+
const backend = wrapLocalSearchBackend(inner, executor, tableName);
|
|
1819
2246
|
let closed = false;
|
|
1820
2247
|
return {
|
|
1821
2248
|
backend,
|