Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 7d54ffc

Browse files
fix: use iterator helpers instead of pipe
1 parent 8195757 commit 7d54ffc

File tree

3 files changed

+20
-59
lines changed

3 files changed

+20
-59
lines changed

src/mysql.db.ts

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { Readable, Transform } from 'node:stream'
1+
import { Readable } from 'node:stream'
22
import { promisify } from 'node:util'
33
import {
44
DBPatch,
@@ -278,17 +278,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
278278
if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)
279279

280280
// return this.streamSQL(sql, opt)
281-
return this.pool()
282-
.query(sql)
283-
.stream()
284-
.pipe(
285-
new Transform({
286-
objectMode: true,
287-
transform(row, _encoding, cb) {
288-
cb(null, _filterUndefinedValues(row, true))
289-
},
290-
}),
291-
)
281+
return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
282+
_filterUndefinedValues(row, true),
283+
)
292284
}
293285

294286
// SAVE

src/mysqlKeyValueDB.ts

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import { Transform } from 'node:stream'
21
import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
3-
import { pMap } from '@naturalcycles/js-lib'
2+
import { ObjectWithId, pMap } from '@naturalcycles/js-lib'
43
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
54
import { QueryOptions } from 'mysql'
65
import { MysqlDB, MysqlDBCfg } from './mysql.db'
@@ -78,56 +77,26 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
7877
if (limit) sql += ` LIMIT ${limit}`
7978
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
8079

81-
return this.db
82-
.pool()
83-
.query(sql)
84-
.stream()
85-
.pipe(
86-
new Transform({
87-
objectMode: true,
88-
transform(row, _, cb) {
89-
cb(null, row.id)
90-
},
91-
}),
92-
)
80+
return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
9381
}
9482

9583
streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
9684
let sql = `SELECT v FROM ${table}`
9785
if (limit) sql += ` LIMIT ${limit}`
9886
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
9987

100-
return this.db
101-
.pool()
102-
.query(sql)
103-
.stream()
104-
.pipe(
105-
new Transform({
106-
objectMode: true,
107-
transform(row, _, cb) {
108-
cb(null, row.v)
109-
},
110-
}),
111-
)
88+
return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: Buffer }>).map(row => row.v)
11289
}
11390

11491
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
11592
let sql = `SELECT id,v FROM ${table}`
11693
if (limit) sql += ` LIMIT ${limit}`
11794
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
11895

119-
return this.db
120-
.pool()
121-
.query(sql)
122-
.stream()
123-
.pipe(
124-
new Transform({
125-
objectMode: true,
126-
transform(row, _, cb) {
127-
cb(null, [row.id, row.v])
128-
},
129-
}),
130-
)
96+
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject>).map(row => [
97+
row.id,
98+
row.v,
99+
])
131100
}
132101

133102
async beginTransaction(): Promise<void> {

yarn.lock

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -814,9 +814,9 @@
814814
typescript "^5.0.2"
815815

816816
"@naturalcycles/db-lib@^9.1.0":
817-
version "9.9.2"
818-
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.9.2.tgz#8b0f16b2e479673f59249e0ac82599b25d2799ff"
819-
integrity sha512-jlDjJLhSt5wNFSeRmbCPsWy3KKnXdn7Lz8L8t4go1o2lJTb3Racb+/jHKxoCB0Ncy7+LZc1XjNtMsb4kslGdQw==
817+
version "9.10.1"
818+
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-9.10.1.tgz#16c25fed9675be5aa2337340861a88d77d2f9138"
819+
integrity sha512-UKrftHMIMkSsYB07HESsJ6KgjxBZ5dkEtmfW8GHABGofODWDSE9q3M/BqXJcW9iRSlX0Qnh9p/40rzSR6BJcwQ==
820820
dependencies:
821821
"@naturalcycles/js-lib" "^14.116.0"
822822
"@naturalcycles/nodejs-lib" "^13.1.1"
@@ -865,9 +865,9 @@
865865
zod "^3.20.2"
866866

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

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

0 commit comments

Comments
 (0)