|
| 1 | +import { Transform } from 'stream' |
| 2 | +import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib' |
| 3 | +import { pMap } from '@naturalcycles/js-lib' |
| 4 | +import { ReadableTyped } from '@naturalcycles/nodejs-lib' |
| 5 | +import { QueryOptions } from 'mysql' |
| 6 | +import { MysqlDB, MysqlDBCfg, typeCast } from './mysql.db' |
| 7 | + |
| 8 | +interface KeyValueObject { |
| 9 | + id: string |
| 10 | + v: Buffer |
| 11 | +} |
| 12 | + |
| 13 | +export class MySQLKeyValueDB implements CommonKeyValueDB { |
| 14 | + constructor(cfg: MysqlDBCfg = {}) { |
| 15 | + this.cfg = { |
| 16 | + typeCast, |
| 17 | + ...cfg, |
| 18 | + } |
| 19 | + |
| 20 | + this.db = new MysqlDB(this.cfg) |
| 21 | + } |
| 22 | + |
| 23 | + cfg: MysqlDBCfg |
| 24 | + |
| 25 | + db: MysqlDB |
| 26 | + |
| 27 | + async ping(): Promise<void> { |
| 28 | + await this.db.ping() |
| 29 | + } |
| 30 | + |
| 31 | + async close(): Promise<void> { |
| 32 | + await this.db.close() |
| 33 | + } |
| 34 | + |
| 35 | + async createTable(table: string, opt: CommonDBCreateOptions = {}): Promise<void> { |
| 36 | + if (opt.dropIfExists) await this.dropTable(table) |
| 37 | + |
| 38 | + // On blob sizes: https://tableplus.com/blog/2019/10/tinyblob-blob-mediumblob-longblob.html |
| 39 | + // LONGBLOB supports up to 4gb |
| 40 | + // MEDIUMBLOB up to 16Mb |
| 41 | + const sql = `create table ${table} (id VARCHAR(64) PRIMARY KEY, v LONGBLOB NOT NULL)` |
| 42 | + this.db.cfg.logger.log(sql) |
| 43 | + await this.db.runSQL({ sql }) |
| 44 | + } |
| 45 | + |
| 46 | + async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> { |
| 47 | + if (!ids.length) return [] |
| 48 | + |
| 49 | + const sql = `SELECT id,v FROM ${table} where id in (${ids.map(id => `"${id}"`).join(',')})` |
| 50 | + |
| 51 | + const rows = await this.db.runSQL<KeyValueObject[]>({ sql }) |
| 52 | + |
| 53 | + return rows.map(({ id, v }) => [id, v]) |
| 54 | + } |
| 55 | + |
| 56 | + /** |
| 57 | + * Use with caution! |
| 58 | + */ |
| 59 | + async dropTable(table: string): Promise<void> { |
| 60 | + await this.db.runSQL({ sql: `DROP TABLE IF EXISTS ${table}` }) |
| 61 | + } |
| 62 | + |
| 63 | + async deleteByIds(table: string, ids: string[]): Promise<void> { |
| 64 | + const sql = `DELETE FROM ${table} WHERE id in (${ids.map(id => `"${id}"`).join(',')})` |
| 65 | + if (this.cfg.logSQL) this.db.cfg.logger.log(sql) |
| 66 | + await this.db.runSQL({ sql }) |
| 67 | + } |
| 68 | + |
| 69 | + async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> { |
| 70 | + const statements: QueryOptions[] = entries.map(([id, buf]) => { |
| 71 | + return { |
| 72 | + sql: `INSERT INTO ${table} (id, v) VALUES (?, ?)`, |
| 73 | + values: [id, buf], |
| 74 | + } |
| 75 | + }) |
| 76 | + |
| 77 | + await pMap(statements, async statement => { |
| 78 | + if (this.cfg.debug) this.db.cfg.logger.log(statement.sql) |
| 79 | + await this.db.runSQL(statement) |
| 80 | + }) |
| 81 | + } |
| 82 | + |
| 83 | + streamIds(table: string, limit?: number): ReadableTyped<string> { |
| 84 | + let sql = `SELECT id FROM ${table}` |
| 85 | + if (limit) sql += ` LIMIT ${limit}` |
| 86 | + if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`) |
| 87 | + |
| 88 | + return this.db |
| 89 | + .pool() |
| 90 | + .query(sql) |
| 91 | + .stream() |
| 92 | + .pipe( |
| 93 | + new Transform({ |
| 94 | + objectMode: true, |
| 95 | + transform(row, _, cb) { |
| 96 | + cb(null, row.id) |
| 97 | + }, |
| 98 | + }), |
| 99 | + ) |
| 100 | + } |
| 101 | + |
| 102 | + streamValues(table: string, limit?: number): ReadableTyped<Buffer> { |
| 103 | + let sql = `SELECT v FROM ${table}` |
| 104 | + if (limit) sql += ` LIMIT ${limit}` |
| 105 | + if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`) |
| 106 | + |
| 107 | + return this.db |
| 108 | + .pool() |
| 109 | + .query(sql) |
| 110 | + .stream() |
| 111 | + .pipe( |
| 112 | + new Transform({ |
| 113 | + objectMode: true, |
| 114 | + transform(row, _, cb) { |
| 115 | + cb(null, row.v) |
| 116 | + }, |
| 117 | + }), |
| 118 | + ) |
| 119 | + } |
| 120 | + |
| 121 | + streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> { |
| 122 | + let sql = `SELECT id,v FROM ${table}` |
| 123 | + if (limit) sql += ` LIMIT ${limit}` |
| 124 | + if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`) |
| 125 | + |
| 126 | + return this.db |
| 127 | + .pool() |
| 128 | + .query(sql) |
| 129 | + .stream() |
| 130 | + .pipe( |
| 131 | + new Transform({ |
| 132 | + objectMode: true, |
| 133 | + transform(row, _, cb) { |
| 134 | + cb(null, [row.id, row.v]) |
| 135 | + }, |
| 136 | + }), |
| 137 | + ) |
| 138 | + } |
| 139 | + |
| 140 | + async beginTransaction(): Promise<void> { |
| 141 | + await this.db.runSQL({ sql: `BEGIN TRANSACTION` }) |
| 142 | + } |
| 143 | + |
| 144 | + async endTransaction(): Promise<void> { |
| 145 | + await this.db.runSQL({ sql: `END TRANSACTION` }) |
| 146 | + } |
| 147 | + |
| 148 | + async count(table: string): Promise<number> { |
| 149 | + const sql = `SELECT count(*) as cnt FROM ${table}` |
| 150 | + if (this.cfg.logSQL) this.db.cfg.logger.log(sql) |
| 151 | + |
| 152 | + const rows = await this.db.runSQL<{ cnt: number }[]>({ sql }) |
| 153 | + return rows[0]!.cnt |
| 154 | + } |
| 155 | +} |
0 commit comments