From 27033f1d52b6b93cf9eeaa8f8636432fdb17edf1 Mon Sep 17 00:00:00 2001 From: kirillgroshkov Date: Mon, 14 Oct 2024 18:09:20 +0200 Subject: [PATCH] fix: adapt to db-lib --- src/mysqlKeyValueDB.ts | 27 ++++++++++++--------------- yarn.lock | 34 +++++++++++++++++----------------- 2 files changed, 29 insertions(+), 32 deletions(-) diff --git a/src/mysqlKeyValueDB.ts b/src/mysqlKeyValueDB.ts index d2e4dab..827d001 100644 --- a/src/mysqlKeyValueDB.ts +++ b/src/mysqlKeyValueDB.ts @@ -2,16 +2,16 @@ import { CommonDBCreateOptions, CommonKeyValueDB, commonKeyValueDBFullSupport, - KeyValueDBTuple, } from '@naturalcycles/db-lib' -import { AppError, ObjectWithId, pMap, StringMap } from '@naturalcycles/js-lib' +import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB' +import { AppError, KeyValueTuple, ObjectWithId, pMap } from '@naturalcycles/js-lib' import { ReadableTyped } from '@naturalcycles/nodejs-lib' import { QueryOptions } from 'mysql' import { MysqlDB, MysqlDBCfg } from './mysql.db' -interface KeyValueObject { +interface KeyValueObject { id: string - v: Buffer + v: V } export class MySQLKeyValueDB implements CommonKeyValueDB { @@ -45,12 +45,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB { await this.db.runSQL({ sql }) } - async getByIds(table: string, ids: string[]): Promise { + async getByIds(table: string, ids: string[]): Promise[]> { if (!ids.length) return [] const sql = `SELECT id,v FROM ${table} where id in (${ids.map(id => `"${id}"`).join(',')})` - const rows = await this.db.runSQL({ sql }) + const rows = await this.db.runSQL[]>({ sql }) return rows.map(({ id, v }) => [id, v]) } @@ -68,7 +68,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB { await this.db.runSQL({ sql }) } - async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise { + async saveBatch(table: string, entries: KeyValueTuple[]): Promise { const statements: QueryOptions[] = entries.map(([id, buf]) => { return { sql: `INSERT INTO ${table} (id, v) VALUES (?, ?)`, @@ -90,20 +90,20 @@ export class MySQLKeyValueDB implements CommonKeyValueDB { return (this.db.pool().query(sql).stream() as ReadableTyped).map(row => row.id) } - streamValues(table: string, limit?: number): ReadableTyped { + streamValues(table: string, limit?: number): ReadableTyped { let sql = `SELECT v FROM ${table}` if (limit) sql += ` LIMIT ${limit}` if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`) - return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: Buffer }>).map(row => row.v) + return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: V }>).map(row => row.v) } - streamEntries(table: string, limit?: number): ReadableTyped { + streamEntries(table: string, limit?: number): ReadableTyped> { let sql = `SELECT id,v FROM ${table}` if (limit) sql += ` LIMIT ${limit}` if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`) - return (this.db.pool().query(sql).stream() as ReadableTyped).map(row => [ + return (this.db.pool().query(sql).stream() as ReadableTyped>).map(row => [ row.id, row.v, ]) @@ -129,10 +129,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB { throw new AppError('MySQLKeyValueDB.increment() is not implemented') } - async incrementBatch( - _table: string, - _incrementMap: StringMap, - ): Promise> { + async incrementBatch(_table: string, _entries: IncrementTuple[]): Promise { throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented') } } diff --git a/yarn.lock b/yarn.lock index 528ca66..e168a1c 100644 --- a/yarn.lock +++ b/yarn.lock @@ -507,10 +507,10 @@ dependencies: "@jridgewell/trace-mapping" "0.3.9" -"@es-joy/jsdoccomment@~0.48.0": - version "0.48.0" - resolved "https://registry.yarnpkg.com/@es-joy/jsdoccomment/-/jsdoccomment-0.48.0.tgz#5d9dc1a295cf5d1ed224dffafb4800d5c7206c27" - integrity sha512-G6QUWIcC+KvSwXNsJyDTHvqUdNoAVJPPgkc3+Uk4WBKqZvoXhlvazOgm9aL0HwihJLQf0l+tOE2UFzXBqCqgDw== +"@es-joy/jsdoccomment@~0.49.0": + version "0.49.0" + resolved "https://registry.yarnpkg.com/@es-joy/jsdoccomment/-/jsdoccomment-0.49.0.tgz#e5ec1eda837c802eca67d3b29e577197f14ba1db" + integrity sha512-xjZTSFgECpb9Ohuk5yMX5RhUEbfeQcuOp8IF60e+wyzWEF0M5xeSgqsfLtvPEX8BIyOX9saZqzuGPmZ8oWc+5Q== dependencies: comment-parser "1.4.1" esquery "^1.6.0" @@ -1002,17 +1002,17 @@ typescript "^5.0.2" "@naturalcycles/db-lib@^9.1.0": - version "9.22.0" - resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.22.0.tgz#d01153b033e22155ade705aa049fe089978c5798" - integrity sha512-989fWQqlfMrtoaKxzqWN2Eh7Y7MrJcqoq5wl7Uldm84eUe3OUY87H0BYgGr/1kO309l2EuzhEkkEzcuO9QyKJw== + version "9.22.1" + resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.22.1.tgz#debfd7d5dda951070441cb27e15fdf1aecd825ff" + integrity sha512-Y5AEdc/0pQKiJM7XHmaVrAB6AW+NZu70QbhGWINS+r/TPLuvnLFfi9KViiNmfByIS7WRNu1D2XNiZr3FbLgceg== dependencies: "@naturalcycles/js-lib" "^14.116.0" "@naturalcycles/nodejs-lib" "^13.1.1" "@naturalcycles/dev-lib@^15.18.0": - version "15.22.0" - resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-15.22.0.tgz#f1a31b5e0483e2d5beaadf8e5bce9345789b9fef" - integrity sha512-QZQEvP2yM27hDATOOHoaNDXXphUwt/dDMbVd7RATQMGoSmjV8QYANJSisWLPAVAee1eF+yQ+t/SUDNrtFoLSXg== + version "15.23.0" + resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-15.23.0.tgz#44de477d9f842bc6397a84649809e053b6b9628f" + integrity sha512-m1qbFzgmqDGerZYnDqtqlg6j1wc+MM/f6jKXBNHs4h90bAJtxXVG2pQaGoAaDdrsXHyT3eq6aE9iCZh7DjyIvw== dependencies: "@biomejs/biome" "^1.8.3" "@commitlint/cli" "^19.0.0" @@ -2125,11 +2125,11 @@ eslint-plugin-jest@^28.0.0: "@typescript-eslint/utils" "^6.0.0 || ^7.0.0 || ^8.0.0" eslint-plugin-jsdoc@^50.0.0: - version "50.3.2" - resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.3.2.tgz#f9b8b3d70abe7a84e22a3bbadf22a935c6194b4a" - integrity sha512-TjgZocG53N3a84PdCFGqVMWLWwDitOUuKjlJftwTu/iTiD7N/Q2Q3eEy/Q4GfJqpM4rTJCkzUYWQfol6RZNDcA== + version "50.4.1" + resolved "https://registry.yarnpkg.com/eslint-plugin-jsdoc/-/eslint-plugin-jsdoc-50.4.1.tgz#845ddf4c395891babe135023d97a45c750d94c4c" + integrity sha512-OXIq+JJQPCLAKL473/esioFOwbXyRE5MAQ4HbZjcp3e+K3zdxt2uDpGs3FR+WezUXNStzEtTfgx15T+JFrVwBA== dependencies: - "@es-joy/jsdoccomment" "~0.48.0" + "@es-joy/jsdoccomment" "~0.49.0" are-docs-informative "^0.0.2" comment-parser "1.4.1" debug "^4.3.6" @@ -2491,9 +2491,9 @@ get-caller-file@^2.0.5: integrity sha512-DyFP3BM/3YHTQOCUL/w0OZHR0lpKeGrxotcHWcqNEdnltqFwXVfhEBQ94eIo34AfQpo0rGki4cyIiftY06h2Fg== get-east-asian-width@^1.0.0: - version "1.2.0" - resolved "https://registry.yarnpkg.com/get-east-asian-width/-/get-east-asian-width-1.2.0.tgz#5e6ebd9baee6fb8b7b6bd505221065f0cd91f64e" - integrity sha512-2nk+7SIVb14QrgXFHcm84tD4bKQz0RxPuMT8Ag5KPOq7J5fEmAg0UbXdTOSHqNuHSU28k55qnceesxXRZGzKWA== + version "1.3.0" + resolved "https://registry.yarnpkg.com/get-east-asian-width/-/get-east-asian-width-1.3.0.tgz#21b4071ee58ed04ee0db653371b55b4299875389" + integrity sha512-vpeMIQKxczTD/0s2CdEWHcb0eeJe6TFjxb+J5xgX7hScxqrGuyjmv4c1D4A/gelKfyox0gJJwIHF+fLjeaM8kQ== get-package-type@^0.1.0: version "0.1.0"