Skip to content

Commit

Permalink
fix: use iterator helpers instead of pipe
Browse files Browse the repository at this point in the history
  • Loading branch information
kirillgroshkov committed Apr 6, 2024
1 parent 8195757 commit 7d54ffc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 59 deletions.
16 changes: 4 additions & 12 deletions src/mysql.db.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Readable, Transform } from 'node:stream'
import { Readable } from 'node:stream'
import { promisify } from 'node:util'
import {
DBPatch,
Expand Down Expand Up @@ -278,17 +278,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)

// return this.streamSQL(sql, opt)
return this.pool()
.query(sql)
.stream()
.pipe(
new Transform({
objectMode: true,
transform(row, _encoding, cb) {
cb(null, _filterUndefinedValues(row, true))
},
}),
)
return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
_filterUndefinedValues(row, true),
)
}

// SAVE
Expand Down
45 changes: 7 additions & 38 deletions src/mysqlKeyValueDB.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Transform } from 'node:stream'
import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
import { pMap } from '@naturalcycles/js-lib'
import { ObjectWithId, pMap } from '@naturalcycles/js-lib'
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
import { QueryOptions } from 'mysql'
import { MysqlDB, MysqlDBCfg } from './mysql.db'
Expand Down Expand Up @@ -78,56 +77,26 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
if (limit) sql += ` LIMIT ${limit}`
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)

return this.db
.pool()
.query(sql)
.stream()
.pipe(
new Transform({
objectMode: true,
transform(row, _, cb) {
cb(null, row.id)
},
}),
)
return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
}

streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
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()
.pipe(
new Transform({
objectMode: true,
transform(row, _, cb) {
cb(null, row.v)
},
}),
)
return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: Buffer }>).map(row => row.v)
}

streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
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()
.pipe(
new Transform({
objectMode: true,
transform(row, _, cb) {
cb(null, [row.id, row.v])
},
}),
)
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject>).map(row => [
row.id,
row.v,
])
}

async beginTransaction(): Promise<void> {
Expand Down
18 changes: 9 additions & 9 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -814,9 +814,9 @@
typescript "^5.0.2"

"@naturalcycles/db-lib@^9.1.0":
version "9.9.2"
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.9.2.tgz#8b0f16b2e479673f59249e0ac82599b25d2799ff"
integrity sha512-jlDjJLhSt5wNFSeRmbCPsWy3KKnXdn7Lz8L8t4go1o2lJTb3Racb+/jHKxoCB0Ncy7+LZc1XjNtMsb4kslGdQw==
version "9.10.1"
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.10.1.tgz#16c25fed9675be5aa2337340861a88d77d2f9138"
integrity sha512-UKrftHMIMkSsYB07HESsJ6KgjxBZ5dkEtmfW8GHABGofODWDSE9q3M/BqXJcW9iRSlX0Qnh9p/40rzSR6BJcwQ==
dependencies:
"@naturalcycles/js-lib" "^14.116.0"
"@naturalcycles/nodejs-lib" "^13.1.1"
Expand Down Expand Up @@ -865,9 +865,9 @@
zod "^3.20.2"

"@naturalcycles/nodejs-lib@^13.0.1", "@naturalcycles/nodejs-lib@^13.1.0", "@naturalcycles/nodejs-lib@^13.1.1":
version "13.9.1"
resolved "https://registry.yarnpkg.com/@naturalcycles/nodejs-lib/-/nodejs-lib-13.9.1.tgz#98aa9ee69cdbe12c20793f2d7b3375556e7d8269"
integrity sha512-IpUGFlepG1KsOi5srYM3zHvlQt4ZXyNeoGd22ggbM2Gi8prJOxbFC76cNwe2Qj9PS+5KHSayGbAhxnpRTREyVg==
version "13.11.0"
resolved "https://registry.yarnpkg.com/@naturalcycles/nodejs-lib/-/nodejs-lib-13.11.0.tgz#b6835d5eb053029cee4645fd8f4c1e0c84ad4590"
integrity sha512-cjkuW9exFUlBxPRhOR8SCweJmn1UP8HmgB98Fsph4vjKaYOuRZUivuPTFIrHGxpvLrOedMXhMh4Ft8SHAnA+aw==
dependencies:
"@naturalcycles/js-lib" "^14.0.0"
"@types/js-yaml" "^4.0.9"
Expand Down Expand Up @@ -2194,9 +2194,9 @@ eslint-plugin-import@^2.22.1:
tsconfig-paths "^3.15.0"

eslint-plugin-jest@^28.0.0:
version "28.0.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-28.0.0.tgz#b18f22977c3c216de928eeae6643c231c5b47316"
integrity sha512-FHiVI/nMYy48juLJKIt34MWPemvZyl0XT8JC3HTiUu/jgKJzoGgrNTCsyq4DzMlEjPZfmXKc0ogIzfrm6DJEuQ==
version "28.1.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-jest/-/eslint-plugin-jest-28.1.0.tgz#360f389b9404dcbcda4d922da901b0fc439ef905"
integrity sha512-BGcrOFWpccNkTrJyO1/tLk7EOXc33kil4LzwrquD7j1CezdLtIucZwhTR8DLTHCqo6l8/gtUwe7yx8d0IHxsAw==
dependencies:
"@typescript-eslint/utils" "^6.0.0"

Expand Down

0 comments on commit 7d54ffc

Please sign in to comment.