@mistriwork/search-sqlite 1.2.0 → 1.3.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/sqliteFts.d.ts +6 -2
- package/dist/sqliteFts.js +62 -24
- package/package.json +2 -2
package/dist/sqliteFts.d.ts
CHANGED
|
@@ -61,17 +61,21 @@ export declare class SqliteFtsIndex implements SearchIndex {
|
|
|
61
61
|
readonly collectionPrefix: string;
|
|
62
62
|
/** The fields each collection holds, as `ensureCollection` last left them. */
|
|
63
63
|
private readonly fields;
|
|
64
|
+
private readonly ensuredKeys;
|
|
65
|
+
private readonly stmts;
|
|
64
66
|
constructor(db: DatabaseSync, collectionPrefix: string);
|
|
65
67
|
/** The collection exists and holds every field named; a field added later rebuilds the table to hold it. */
|
|
66
68
|
ensureCollection(name: string, fields: readonly string[]): Promise<void>;
|
|
67
69
|
/** The (tenant, key)-to-rowid map for `table`, created when absent and backfilled once from the rows already there. An untenanted record keys on the empty tenant. */
|
|
68
70
|
private ensureKeys;
|
|
71
|
+
private statementsFor;
|
|
69
72
|
/** One unit of work or none: a delete without its insert would lose a document. */
|
|
70
73
|
private transact;
|
|
74
|
+
private writeOne;
|
|
71
75
|
/** The record's document, replacing the document its tenant and key already had; a document whose collection was never ensured is ensured from its own fields. */
|
|
72
76
|
upsert(collection: string, document: SearchDocument): Promise<void>;
|
|
73
|
-
/**
|
|
74
|
-
|
|
77
|
+
/** Bulk upsert an entire batch of documents inside a single SQLite transaction. */
|
|
78
|
+
upsertBatch(collection: string, documents: readonly SearchDocument[]): Promise<void>;
|
|
75
79
|
/**
|
|
76
80
|
* Gone from the index; a document that was never there is gone already, and a collection never ensured holds
|
|
77
81
|
* nothing — as does a name the index cannot hold. With a tenant scope only that tenant's document goes; without
|
package/dist/sqliteFts.js
CHANGED
|
@@ -101,6 +101,8 @@ export class SqliteFtsIndex {
|
|
|
101
101
|
collectionPrefix;
|
|
102
102
|
/** The fields each collection holds, as `ensureCollection` last left them. */
|
|
103
103
|
fields = new Map();
|
|
104
|
+
ensuredKeys = new Set();
|
|
105
|
+
stmts = new Map();
|
|
104
106
|
constructor(db, collectionPrefix) {
|
|
105
107
|
this.db = db;
|
|
106
108
|
this.collectionPrefix = collectionPrefix;
|
|
@@ -116,6 +118,7 @@ export class SqliteFtsIndex {
|
|
|
116
118
|
if (!have) {
|
|
117
119
|
this.db.exec(`CREATE VIRTUAL TABLE "${table}" USING fts5(${definition(wanted)}, tokenize='unicode61')`);
|
|
118
120
|
this.fields.set(table, [...wanted]);
|
|
121
|
+
this.stmts.delete(table);
|
|
119
122
|
this.ensureKeys(table);
|
|
120
123
|
return;
|
|
121
124
|
}
|
|
@@ -145,15 +148,39 @@ export class SqliteFtsIndex {
|
|
|
145
148
|
throw error;
|
|
146
149
|
}
|
|
147
150
|
this.fields.set(table, next);
|
|
151
|
+
this.stmts.delete(table);
|
|
148
152
|
}
|
|
149
153
|
/** The (tenant, key)-to-rowid map for `table`, created when absent and backfilled once from the rows already there. An untenanted record keys on the empty tenant. */
|
|
150
154
|
ensureKeys(table) {
|
|
155
|
+
if (this.ensuredKeys.has(table))
|
|
156
|
+
return;
|
|
151
157
|
const keys = `${table}__keys`;
|
|
152
158
|
const found = this.db.prepare(`SELECT COUNT(*) AS count FROM sqlite_master WHERE type = 'table' AND name = ?`).get(keys);
|
|
153
|
-
if (found && found.count > 0)
|
|
159
|
+
if (found && found.count > 0) {
|
|
160
|
+
this.ensuredKeys.add(table);
|
|
154
161
|
return;
|
|
162
|
+
}
|
|
155
163
|
this.db.exec(`CREATE TABLE "${keys}"(tenant TEXT NOT NULL, id TEXT NOT NULL, rowid INTEGER NOT NULL, PRIMARY KEY (tenant, id))`);
|
|
156
164
|
this.db.exec(`INSERT INTO "${keys}"(tenant, id, rowid) SELECT coalesce("${TENANT_FIELD}", ''), id, rowid FROM "${table}"`);
|
|
165
|
+
this.ensuredKeys.add(table);
|
|
166
|
+
}
|
|
167
|
+
statementsFor(table, held) {
|
|
168
|
+
let cached = this.stmts.get(table);
|
|
169
|
+
if (!cached) {
|
|
170
|
+
const keys = `${table}__keys`;
|
|
171
|
+
const columns = ['id', ...held, TENANT_FIELD];
|
|
172
|
+
const quotedCols = columns.map(quote).join(', ');
|
|
173
|
+
const placeholders = columns.map(() => '?').join(', ');
|
|
174
|
+
cached = {
|
|
175
|
+
keyLookup: this.db.prepare(`SELECT rowid FROM "${keys}" WHERE tenant = ? AND id = ?`),
|
|
176
|
+
deleteRowid: this.db.prepare(`DELETE FROM "${table}" WHERE rowid = ?`),
|
|
177
|
+
insertKey: this.db.prepare(`INSERT INTO "${keys}"(tenant, id, rowid) VALUES (?, ?, ?)`),
|
|
178
|
+
insertRow: this.db.prepare(`INSERT INTO "${table}"(${quotedCols}) VALUES (${placeholders})`),
|
|
179
|
+
insertRowWithId: this.db.prepare(`INSERT INTO "${table}"("rowid", ${quotedCols}) VALUES (?, ${placeholders})`),
|
|
180
|
+
};
|
|
181
|
+
this.stmts.set(table, cached);
|
|
182
|
+
}
|
|
183
|
+
return cached;
|
|
157
184
|
}
|
|
158
185
|
/** One unit of work or none: a delete without its insert would lose a document. */
|
|
159
186
|
transact(work) {
|
|
@@ -172,39 +199,50 @@ export class SqliteFtsIndex {
|
|
|
172
199
|
throw error;
|
|
173
200
|
}
|
|
174
201
|
}
|
|
202
|
+
writeOne(stmts, columns, document) {
|
|
203
|
+
const tenant = document[TENANT_FIELD] ?? '';
|
|
204
|
+
const values = columns.map((column) => column === 'id' ? document.id : (document[column] ?? null));
|
|
205
|
+
const known = stmts.keyLookup.get(tenant, document.id)?.rowid;
|
|
206
|
+
if (known !== undefined) {
|
|
207
|
+
stmts.deleteRowid.run(known);
|
|
208
|
+
stmts.insertRowWithId.run(known, ...values);
|
|
209
|
+
return;
|
|
210
|
+
}
|
|
211
|
+
const result = stmts.insertRow.run(...values);
|
|
212
|
+
stmts.insertKey.run(tenant, document.id, result.lastInsertRowid);
|
|
213
|
+
}
|
|
175
214
|
/** The record's document, replacing the document its tenant and key already had; a document whose collection was never ensured is ensured from its own fields. */
|
|
176
215
|
async upsert(collection, document) {
|
|
177
216
|
const table = identifier(collection, 'A search collection');
|
|
178
217
|
const fields = Object.keys(document).filter((key) => key !== 'id' && key !== TENANT_FIELD);
|
|
179
218
|
await this.ensureCollection(table, fields);
|
|
180
219
|
const held = this.fields.get(table);
|
|
181
|
-
const keys = `${table}__keys`;
|
|
182
|
-
const tenant = document[TENANT_FIELD] ?? '';
|
|
183
220
|
const columns = ['id', ...held, TENANT_FIELD];
|
|
184
|
-
const
|
|
185
|
-
const insert = (extra, extraValues) => this.db
|
|
186
|
-
.prepare(`INSERT INTO "${table}"(${[...extra, ...columns].map(quote).join(', ')}) VALUES (${[...extra, ...columns].map(() => '?').join(', ')})`)
|
|
187
|
-
.run(...extraValues, ...values);
|
|
188
|
-
/* A known key reuses its rowid: the old row is deleted by rowid and
|
|
189
|
-
the new one takes the same rowid, so the map never moves. A new key
|
|
190
|
-
records the rowid its insert got. The map holds every row (see
|
|
191
|
-
ensureKeys), so a key it lacks has no row to delete — a delete by
|
|
192
|
-
`id` would scan the whole table, since FTS5 indexes no such column. */
|
|
221
|
+
const stmts = this.statementsFor(table, held);
|
|
193
222
|
this.transact(() => {
|
|
194
|
-
|
|
195
|
-
if (known !== undefined) {
|
|
196
|
-
this.db.prepare(`DELETE FROM "${table}" WHERE rowid = ?`).run(known);
|
|
197
|
-
insert(['rowid'], [known]);
|
|
198
|
-
return;
|
|
199
|
-
}
|
|
200
|
-
const result = insert([], []);
|
|
201
|
-
this.db.prepare(`INSERT INTO "${keys}"(tenant, id, rowid) VALUES (?, ?, ?)`).run(tenant, document.id, result.lastInsertRowid);
|
|
223
|
+
this.writeOne(stmts, columns, document);
|
|
202
224
|
});
|
|
203
225
|
}
|
|
204
|
-
/**
|
|
205
|
-
|
|
206
|
-
|
|
207
|
-
|
|
226
|
+
/** Bulk upsert an entire batch of documents inside a single SQLite transaction. */
|
|
227
|
+
async upsertBatch(collection, documents) {
|
|
228
|
+
if (!documents.length)
|
|
229
|
+
return;
|
|
230
|
+
const table = identifier(collection, 'A search collection');
|
|
231
|
+
const fieldSet = new Set();
|
|
232
|
+
for (const document of documents) {
|
|
233
|
+
for (const key of Object.keys(document)) {
|
|
234
|
+
if (key !== 'id' && key !== TENANT_FIELD)
|
|
235
|
+
fieldSet.add(key);
|
|
236
|
+
}
|
|
237
|
+
}
|
|
238
|
+
await this.ensureCollection(table, [...fieldSet]);
|
|
239
|
+
const held = this.fields.get(table);
|
|
240
|
+
const columns = ['id', ...held, TENANT_FIELD];
|
|
241
|
+
const stmts = this.statementsFor(table, held);
|
|
242
|
+
this.transact(() => {
|
|
243
|
+
for (const document of documents)
|
|
244
|
+
this.writeOne(stmts, columns, document);
|
|
245
|
+
});
|
|
208
246
|
}
|
|
209
247
|
/**
|
|
210
248
|
* Gone from the index; a document that was never there is gone already, and a collection never ensured holds
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@mistriwork/search-sqlite",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.3.0",
|
|
4
4
|
"description": "SQLite FTS5 search provider pack for mistri Apps, over Node's built-in node:sqlite with zero extra dependencies.",
|
|
5
5
|
"keywords": [
|
|
6
6
|
"mistri",
|
|
@@ -33,7 +33,7 @@
|
|
|
33
33
|
}
|
|
34
34
|
},
|
|
35
35
|
"peerDependencies": {
|
|
36
|
-
"@mistriwork/mistri": "1.
|
|
36
|
+
"@mistriwork/mistri": "1.3.0"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": ">=24"
|