Skip to content

Commit

Permalink
fix: adapt to db-lib
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Oct 14, 2024
1 parent 62ba326 commit 27033f1
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 32 deletions.
27 changes: 12 additions & 15 deletions src/mysqlKeyValueDB.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<V> {
id: string
v: Buffer
v: V
}

export class MySQLKeyValueDB implements CommonKeyValueDB {
Expand Down Expand Up @@ -45,12 +45,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
await this.db.runSQL({ sql })
}

async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
async getByIds<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]> {
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<KeyValueObject[]>({ sql })
const rows = await this.db.runSQL<KeyValueObject<V>[]>({ sql })

return rows.map(({ id, v }) => [id, v])
}
Expand All @@ -68,7 +68,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
await this.db.runSQL({ sql })
}

async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
async saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void> {
const statements: QueryOptions[] = entries.map(([id, buf]) => {
return {
sql: `INSERT INTO ${table} (id, v) VALUES (?, ?)`,
Expand All @@ -90,20 +90,20 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
}

streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
streamValues<V>(table: string, limit?: number): ReadableTyped<V> {
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<KeyValueDBTuple> {
streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>> {
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<KeyValueObject>).map(row => [
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject<V>>).map(row => [
row.id,
row.v,
])
Expand All @@ -129,10 +129,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
throw new AppError('MySQLKeyValueDB.increment() is not implemented')
}

async incrementBatch(
_table: string,
_incrementMap: StringMap<number>,
): Promise<StringMap<number>> {
async incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]> {
throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented')
}
}
34 changes: 17 additions & 17 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit 27033f1

Please sign in to comment.