|
| 1 | +import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib' |
| 2 | +import { CommonLogger } from '@naturalcycles/js-lib' |
| 3 | +import { readableCreate, ReadableTyped } from '@naturalcycles/nodejs-lib' |
| 4 | +import { boldWhite } from '@naturalcycles/nodejs-lib/dist/colors' |
| 5 | +import type { Options, Database } from 'better-sqlite3' |
| 6 | +import * as BetterSqlite3 from 'better-sqlite3' |
| 7 | + |
| 8 | +export interface BetterSQLiteKeyValueDBCfg extends Options { |
| 9 | + filename: string |
| 10 | + |
| 11 | + /** |
| 12 | + * Will log all sql queries executed. |
| 13 | + * |
| 14 | + * @default false |
| 15 | + */ |
| 16 | + debug?: boolean |
| 17 | + |
| 18 | + /** |
| 19 | + * Defaults to `console` |
| 20 | + */ |
| 21 | + logger?: CommonLogger |
| 22 | +} |
| 23 | + |
| 24 | +interface KeyValueObject { |
| 25 | + id: string |
| 26 | + v: Buffer |
| 27 | +} |
| 28 | + |
| 29 | +/** |
| 30 | + * @experimental |
| 31 | + */ |
| 32 | +export class BetterSqliteKeyValueDB implements CommonKeyValueDB { |
| 33 | + constructor(cfg: BetterSQLiteKeyValueDBCfg) { |
| 34 | + this.cfg = { |
| 35 | + logger: console, |
| 36 | + ...cfg, |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + cfg: BetterSQLiteKeyValueDBCfg & { logger: CommonLogger } |
| 41 | + |
| 42 | + _db?: Database |
| 43 | + |
| 44 | + get db(): Database { |
| 45 | + if (!this._db) { |
| 46 | + this.open() |
| 47 | + } |
| 48 | + |
| 49 | + return this._db! |
| 50 | + } |
| 51 | + |
| 52 | + open(): void { |
| 53 | + if (this._db) return |
| 54 | + |
| 55 | + this._db = new BetterSqlite3(this.cfg.filename, { |
| 56 | + verbose: this.cfg.debug ? this.cfg.logger.log : undefined, |
| 57 | + ...this.cfg, |
| 58 | + }) |
| 59 | + |
| 60 | + this.cfg.logger.log(`${boldWhite(this.cfg.filename)} opened`) |
| 61 | + } |
| 62 | + |
| 63 | + close(): void { |
| 64 | + if (!this._db) return |
| 65 | + this.db.close() |
| 66 | + this.cfg.logger.log(`${boldWhite(this.cfg.filename)} closed`) |
| 67 | + } |
| 68 | + |
| 69 | + async ping(): Promise<void> { |
| 70 | + this.open() |
| 71 | + } |
| 72 | + |
| 73 | + async createTable(table: string, opt: CommonDBCreateOptions = {}): Promise<void> { |
| 74 | + if (opt.dropIfExists) this.dropTable(table) |
| 75 | + |
| 76 | + const sql = `create table ${table} (id TEXT PRIMARY KEY, v BLOB NOT NULL)` |
| 77 | + this.cfg.logger.log(sql) |
| 78 | + this.db.prepare(sql).run() |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * Use with caution! |
| 83 | + */ |
| 84 | + dropTable(table: string): void { |
| 85 | + this.db.prepare(`DROP TABLE IF EXISTS ${table}`).run() |
| 86 | + } |
| 87 | + |
| 88 | + async deleteByIds(table: string, ids: string[]): Promise<void> { |
| 89 | + const sql = `DELETE FROM ${table} WHERE id in (${ids.map(id => `'${id}'`).join(',')})` |
| 90 | + if (this.cfg.debug) this.cfg.logger.log(sql) |
| 91 | + this.db.prepare(sql).run() |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * API design note: |
| 96 | + * Here in the array of rows we have no way to map row to id (it's an opaque Buffer). |
| 97 | + */ |
| 98 | + async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> { |
| 99 | + const sql = `SELECT id,v FROM ${table} where id in (${ids.map(id => `'${id}'`).join(',')})` |
| 100 | + if (this.cfg.debug) this.cfg.logger.log(sql) |
| 101 | + const rows = this.db.prepare(sql).all() as KeyValueObject[] |
| 102 | + // console.log(rows) |
| 103 | + return rows.map(r => [r.id, r.v]) |
| 104 | + } |
| 105 | + |
| 106 | + async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> { |
| 107 | + const sql = `INSERT INTO ${table} (id, v) VALUES (?, ?)` |
| 108 | + if (this.cfg.debug) this.cfg.logger.log(sql) |
| 109 | + |
| 110 | + const stmt = this.db.prepare(sql) |
| 111 | + |
| 112 | + entries.forEach(([id, buf]) => { |
| 113 | + stmt.run(id, buf) |
| 114 | + }) |
| 115 | + } |
| 116 | + |
| 117 | + async beginTransaction(): Promise<void> { |
| 118 | + this.db.exec(`BEGIN TRANSACTION`) |
| 119 | + } |
| 120 | + |
| 121 | + async endTransaction(): Promise<void> { |
| 122 | + this.db.exec(`END TRANSACTION`) |
| 123 | + } |
| 124 | + |
| 125 | + streamIds(table: string, limit?: number): ReadableTyped<string> { |
| 126 | + const readable = readableCreate<string>() |
| 127 | + |
| 128 | + let sql = `SELECT id FROM ${table}` |
| 129 | + if (limit) { |
| 130 | + sql += ` LIMIT ${limit}` |
| 131 | + } |
| 132 | + |
| 133 | + void (async () => { |
| 134 | + for (const row of this.db.prepare(sql).iterate()) { |
| 135 | + readable.push((row as { id: string }).id) |
| 136 | + } |
| 137 | + |
| 138 | + // Now we're done |
| 139 | + readable.push(null) |
| 140 | + })() |
| 141 | + |
| 142 | + return readable |
| 143 | + } |
| 144 | + |
| 145 | + streamValues(table: string, limit?: number): ReadableTyped<Buffer> { |
| 146 | + const readable = readableCreate<Buffer>() |
| 147 | + |
| 148 | + let sql = `SELECT v FROM ${table}` |
| 149 | + if (limit) { |
| 150 | + sql += ` LIMIT ${limit}` |
| 151 | + } |
| 152 | + |
| 153 | + void (async () => { |
| 154 | + for (const row of this.db.prepare(sql).iterate()) { |
| 155 | + readable.push((row as { v: Buffer }).v) |
| 156 | + } |
| 157 | + |
| 158 | + // Now we're done |
| 159 | + readable.push(null) |
| 160 | + })() |
| 161 | + |
| 162 | + return readable |
| 163 | + } |
| 164 | + |
| 165 | + streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> { |
| 166 | + const readable = readableCreate<KeyValueDBTuple>() |
| 167 | + |
| 168 | + let sql = `SELECT id,v FROM ${table}` |
| 169 | + if (limit) { |
| 170 | + sql += ` LIMIT ${limit}` |
| 171 | + } |
| 172 | + |
| 173 | + void (async () => { |
| 174 | + for (const row of this.db.prepare(sql).iterate()) { |
| 175 | + readable.push([row.id, row.v]) |
| 176 | + } |
| 177 | + |
| 178 | + // Now we're done |
| 179 | + readable.push(null) |
| 180 | + })() |
| 181 | + |
| 182 | + return readable |
| 183 | + } |
| 184 | + |
| 185 | + /** |
| 186 | + * Count rows in the given table. |
| 187 | + */ |
| 188 | + async count(table: string): Promise<number> { |
| 189 | + const sql = `SELECT count(*) as cnt FROM ${table}` |
| 190 | + |
| 191 | + if (this.cfg.debug) this.cfg.logger.log(sql) |
| 192 | + |
| 193 | + const { cnt } = this.db.prepare(sql).get() as { cnt: number } |
| 194 | + return cnt |
| 195 | + } |
| 196 | +} |
0 commit comments