@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.
Files changed (2) hide show
  1. package/lib/sqlite.js +19 -9
  2. 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
- ].join(', ');
129
- await sql`CREATE VIRTUAL TABLE IF NOT EXISTS ${sql.ref(tableName)} USING fts5(${sql.raw(columns)})`.execute(db);
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
- ].join(', ');
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
- ].map((v)=>sql.lit(v));
146
- await sql`INSERT INTO ${sql.ref(tableName)} (${sql.raw(columns)}) VALUES (${sql.join(values)})`.execute(db);
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}`;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@kubun/db-adapter",
3
- "version": "0.13.0",
3
+ "version": "0.13.1",
4
4
  "keywords": [],
5
5
  "license": "see LICENSE.md",
6
6
  "sideEffects": false,