@kubun/db-adapter 0.13.0 → 0.13.1
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/lib/sqlite.js +19 -9
- package/package.json +1 -1
package/lib/sqlite.js
CHANGED
|
@@ -1,4 +1,12 @@
|
|
|
1
1
|
import { sql } from 'kysely';
|
|
2
|
+
const SEARCH_FIELD_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/;
|
|
3
|
+
function assertSearchFieldNames(fields) {
|
|
4
|
+
for (const field of fields){
|
|
5
|
+
if (!SEARCH_FIELD_NAME.test(field)) {
|
|
6
|
+
throw new Error(`invalid search field name: ${JSON.stringify(field)}`);
|
|
7
|
+
}
|
|
8
|
+
}
|
|
9
|
+
}
|
|
2
10
|
export class AbstractSQLiteAdapter {
|
|
3
11
|
functions = {
|
|
4
12
|
now: sql`(unixepoch())`
|
|
@@ -121,29 +129,31 @@ export class AbstractSQLiteAdapter {
|
|
|
121
129
|
)`;
|
|
122
130
|
}
|
|
123
131
|
async createSearchIndex(db, config) {
|
|
132
|
+
assertSearchFieldNames(config.fields);
|
|
124
133
|
const tableName = `fts_${config.modelID}`;
|
|
125
134
|
const columns = [
|
|
126
|
-
'document_id UNINDEXED
|
|
127
|
-
...config.fields
|
|
128
|
-
]
|
|
129
|
-
await sql`CREATE VIRTUAL TABLE IF NOT EXISTS ${sql.ref(tableName)} USING fts5(${sql.
|
|
135
|
+
sql`${sql.ref('document_id')} UNINDEXED`,
|
|
136
|
+
...config.fields.map((f)=>sql.ref(f))
|
|
137
|
+
];
|
|
138
|
+
await sql`CREATE VIRTUAL TABLE IF NOT EXISTS ${sql.ref(tableName)} USING fts5(${sql.join(columns)})`.execute(db);
|
|
130
139
|
}
|
|
131
140
|
async dropSearchIndex(db, modelID) {
|
|
132
141
|
const tableName = `fts_${modelID}`;
|
|
133
142
|
await sql`DROP TABLE IF EXISTS ${sql.ref(tableName)}`.execute(db);
|
|
134
143
|
}
|
|
135
144
|
async updateSearchEntry(db, modelID, documentID, fields, fieldNames) {
|
|
145
|
+
assertSearchFieldNames(fieldNames);
|
|
136
146
|
const tableName = `fts_${modelID}`;
|
|
137
147
|
await sql`DELETE FROM ${sql.ref(tableName)} WHERE document_id = ${documentID}`.execute(db);
|
|
138
148
|
const columns = [
|
|
139
|
-
'document_id',
|
|
140
|
-
...fieldNames
|
|
141
|
-
]
|
|
149
|
+
sql.ref('document_id'),
|
|
150
|
+
...fieldNames.map((f)=>sql.ref(f))
|
|
151
|
+
];
|
|
142
152
|
const values = [
|
|
143
153
|
documentID,
|
|
144
154
|
...fieldNames.map((f)=>fields[f] ?? '')
|
|
145
|
-
]
|
|
146
|
-
await sql`INSERT INTO ${sql.ref(tableName)} (${sql.
|
|
155
|
+
];
|
|
156
|
+
await sql`INSERT INTO ${sql.ref(tableName)} (${sql.join(columns)}) VALUES (${sql.join(values.map((v)=>sql`${v}`))})`.execute(db);
|
|
147
157
|
}
|
|
148
158
|
async removeSearchEntry(db, modelID, documentID) {
|
|
149
159
|
const tableName = `fts_${modelID}`;
|