@naturalcycles/mysql-lib 3.16.1 → 3.16.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/mysql.db.js +13 -2
- package/dist/mysqlKeyValueDB.d.ts +5 -7
- package/dist/mysqlKeyValueDB.js +13 -4
- package/package.json +11 -11
- package/src/mysql.db.ts +16 -5
- package/src/mysqlKeyValueDB.ts +28 -17
package/dist/mysql.db.js
CHANGED
|
@@ -173,8 +173,19 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
173
173
|
}
|
|
174
174
|
if (this.cfg.logSQL)
|
|
175
175
|
this.cfg.logger.log(`stream: ${sql}`);
|
|
176
|
-
//
|
|
177
|
-
return this.pool().query(sql).stream().map(row =>
|
|
176
|
+
// todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
|
|
177
|
+
// return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
178
|
+
// _filterUndefinedValues(row, true),
|
|
179
|
+
// )
|
|
180
|
+
return this.pool()
|
|
181
|
+
.query(sql)
|
|
182
|
+
.stream()
|
|
183
|
+
.pipe(new node_stream_1.Transform({
|
|
184
|
+
objectMode: true,
|
|
185
|
+
transform(row, _encoding, cb) {
|
|
186
|
+
cb(null, (0, js_lib_1._filterUndefinedValues)(row, true));
|
|
187
|
+
},
|
|
188
|
+
}));
|
|
178
189
|
}
|
|
179
190
|
// SAVE
|
|
180
191
|
async saveBatch(table, rowsInput, opt = {}) {
|
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import { CommonDBCreateOptions, CommonKeyValueDB } from '@naturalcycles/db-lib';
|
|
1
|
+
import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib';
|
|
2
2
|
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB';
|
|
3
|
-
import { KeyValueTuple } from '@naturalcycles/js-lib';
|
|
4
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
5
4
|
import { MysqlDB, MysqlDBCfg } from './mysql.db';
|
|
6
5
|
export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
@@ -14,19 +13,18 @@ export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
14
13
|
ping(): Promise<void>;
|
|
15
14
|
close(): Promise<void>;
|
|
16
15
|
createTable(table: string, opt?: CommonDBCreateOptions): Promise<void>;
|
|
17
|
-
getByIds
|
|
16
|
+
getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]>;
|
|
18
17
|
/**
|
|
19
18
|
* Use with caution!
|
|
20
19
|
*/
|
|
21
20
|
dropTable(table: string): Promise<void>;
|
|
22
21
|
deleteByIds(table: string, ids: string[]): Promise<void>;
|
|
23
|
-
saveBatch
|
|
22
|
+
saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void>;
|
|
24
23
|
streamIds(table: string, limit?: number): ReadableTyped<string>;
|
|
25
|
-
streamValues
|
|
26
|
-
streamEntries
|
|
24
|
+
streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
|
|
25
|
+
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
|
|
27
26
|
beginTransaction(): Promise<void>;
|
|
28
27
|
endTransaction(): Promise<void>;
|
|
29
28
|
count(table: string): Promise<number>;
|
|
30
|
-
increment(_table: string, _id: string, _by?: number): Promise<number>;
|
|
31
29
|
incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]>;
|
|
32
30
|
}
|
package/dist/mysqlKeyValueDB.js
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MySQLKeyValueDB = void 0;
|
|
4
|
+
const node_stream_1 = require("node:stream");
|
|
4
5
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
5
6
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
7
|
const mysql_db_1 = require("./mysql.db");
|
|
@@ -67,7 +68,18 @@ class MySQLKeyValueDB {
|
|
|
67
68
|
sql += ` LIMIT ${limit}`;
|
|
68
69
|
if (this.cfg.logSQL)
|
|
69
70
|
this.db.cfg.logger.log(`stream: ${sql}`);
|
|
70
|
-
|
|
71
|
+
// todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
|
|
72
|
+
// return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
|
|
73
|
+
return this.db
|
|
74
|
+
.pool()
|
|
75
|
+
.query(sql)
|
|
76
|
+
.stream()
|
|
77
|
+
.pipe(new node_stream_1.Transform({
|
|
78
|
+
objectMode: true,
|
|
79
|
+
transform(row, _encoding, cb) {
|
|
80
|
+
cb(null, row.id);
|
|
81
|
+
},
|
|
82
|
+
}));
|
|
71
83
|
}
|
|
72
84
|
streamValues(table, limit) {
|
|
73
85
|
let sql = `SELECT v FROM ${table}`;
|
|
@@ -101,9 +113,6 @@ class MySQLKeyValueDB {
|
|
|
101
113
|
const rows = await this.db.runSQL({ sql });
|
|
102
114
|
return rows[0].cnt;
|
|
103
115
|
}
|
|
104
|
-
async increment(_table, _id, _by) {
|
|
105
|
-
throw new js_lib_1.AppError('MySQLKeyValueDB.increment() is not implemented');
|
|
106
|
-
}
|
|
107
116
|
async incrementBatch(_table, _entries) {
|
|
108
117
|
throw new js_lib_1.AppError('MySQLKeyValueDB.incrementBatch() is not implemented');
|
|
109
118
|
}
|
package/package.json
CHANGED
|
@@ -9,17 +9,17 @@
|
|
|
9
9
|
"lbt": "dev-lib lbt"
|
|
10
10
|
},
|
|
11
11
|
"dependencies": {
|
|
12
|
-
"@naturalcycles/db-lib": "^9
|
|
13
|
-
"@naturalcycles/js-lib": "^14
|
|
14
|
-
"@naturalcycles/nodejs-lib": "^13
|
|
15
|
-
"@types/mysql": "^2
|
|
16
|
-
"mysql": "^2
|
|
12
|
+
"@naturalcycles/db-lib": "^9",
|
|
13
|
+
"@naturalcycles/js-lib": "^14",
|
|
14
|
+
"@naturalcycles/nodejs-lib": "^13",
|
|
15
|
+
"@types/mysql": "^2",
|
|
16
|
+
"mysql": "^2"
|
|
17
17
|
},
|
|
18
18
|
"devDependencies": {
|
|
19
|
-
"@naturalcycles/dev-lib": "^15
|
|
20
|
-
"@types/node": "^22
|
|
21
|
-
"dotenv": "^16
|
|
22
|
-
"jest": "^29
|
|
19
|
+
"@naturalcycles/dev-lib": "^15",
|
|
20
|
+
"@types/node": "^22",
|
|
21
|
+
"dotenv": "^16",
|
|
22
|
+
"jest": "^29"
|
|
23
23
|
},
|
|
24
24
|
"files": [
|
|
25
25
|
"dist",
|
|
@@ -39,9 +39,9 @@
|
|
|
39
39
|
"url": "https://github.com/NaturalCycles/mysql-lib"
|
|
40
40
|
},
|
|
41
41
|
"engines": {
|
|
42
|
-
"node": ">=
|
|
42
|
+
"node": ">=22.12.0"
|
|
43
43
|
},
|
|
44
|
-
"version": "3.16.
|
|
44
|
+
"version": "3.16.3",
|
|
45
45
|
"description": "MySQL client implementing CommonDB interface",
|
|
46
46
|
"keywords": [
|
|
47
47
|
"mysql",
|
package/src/mysql.db.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { Readable } from 'node:stream'
|
|
1
|
+
import { Readable, Transform } from 'node:stream'
|
|
2
2
|
import { promisify } from 'node:util'
|
|
3
3
|
import {
|
|
4
4
|
BaseCommonDB,
|
|
@@ -277,10 +277,21 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
277
277
|
|
|
278
278
|
if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)
|
|
279
279
|
|
|
280
|
-
//
|
|
281
|
-
return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
282
|
-
|
|
283
|
-
)
|
|
280
|
+
// todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
|
|
281
|
+
// return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
282
|
+
// _filterUndefinedValues(row, true),
|
|
283
|
+
// )
|
|
284
|
+
return this.pool()
|
|
285
|
+
.query(sql)
|
|
286
|
+
.stream()
|
|
287
|
+
.pipe(
|
|
288
|
+
new Transform({
|
|
289
|
+
objectMode: true,
|
|
290
|
+
transform(row: ROW, _encoding, cb) {
|
|
291
|
+
cb(null, _filterUndefinedValues(row, true))
|
|
292
|
+
},
|
|
293
|
+
}),
|
|
294
|
+
)
|
|
284
295
|
}
|
|
285
296
|
|
|
286
297
|
// SAVE
|
package/src/mysqlKeyValueDB.ts
CHANGED
|
@@ -1,17 +1,19 @@
|
|
|
1
|
+
import { Transform } from 'node:stream'
|
|
1
2
|
import {
|
|
2
3
|
CommonDBCreateOptions,
|
|
3
4
|
CommonKeyValueDB,
|
|
4
5
|
commonKeyValueDBFullSupport,
|
|
6
|
+
KeyValueDBTuple,
|
|
5
7
|
} from '@naturalcycles/db-lib'
|
|
6
8
|
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB'
|
|
7
|
-
import { AppError,
|
|
9
|
+
import { AppError, ObjectWithId, pMap } from '@naturalcycles/js-lib'
|
|
8
10
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
9
11
|
import { QueryOptions } from 'mysql'
|
|
10
12
|
import { MysqlDB, MysqlDBCfg } from './mysql.db'
|
|
11
13
|
|
|
12
|
-
interface KeyValueObject
|
|
14
|
+
interface KeyValueObject {
|
|
13
15
|
id: string
|
|
14
|
-
v:
|
|
16
|
+
v: Buffer
|
|
15
17
|
}
|
|
16
18
|
|
|
17
19
|
export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
@@ -45,12 +47,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
45
47
|
await this.db.runSQL({ sql })
|
|
46
48
|
}
|
|
47
49
|
|
|
48
|
-
async getByIds
|
|
50
|
+
async getByIds(table: string, ids: string[]): Promise<KeyValueDBTuple[]> {
|
|
49
51
|
if (!ids.length) return []
|
|
50
52
|
|
|
51
53
|
const sql = `SELECT id,v FROM ${table} where id in (${ids.map(id => `"${id}"`).join(',')})`
|
|
52
54
|
|
|
53
|
-
const rows = await this.db.runSQL<KeyValueObject
|
|
55
|
+
const rows = await this.db.runSQL<KeyValueObject[]>({ sql })
|
|
54
56
|
|
|
55
57
|
return rows.map(({ id, v }) => [id, v])
|
|
56
58
|
}
|
|
@@ -68,7 +70,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
68
70
|
await this.db.runSQL({ sql })
|
|
69
71
|
}
|
|
70
72
|
|
|
71
|
-
async saveBatch
|
|
73
|
+
async saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void> {
|
|
72
74
|
const statements: QueryOptions[] = entries.map(([id, buf]) => {
|
|
73
75
|
return {
|
|
74
76
|
sql: `INSERT INTO ${table} (id, v) VALUES (?, ?)`,
|
|
@@ -87,23 +89,36 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
87
89
|
if (limit) sql += ` LIMIT ${limit}`
|
|
88
90
|
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
89
91
|
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
92
|
+
// todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
|
|
93
|
+
// return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
|
|
94
|
+
return this.db
|
|
95
|
+
.pool()
|
|
96
|
+
.query(sql)
|
|
97
|
+
.stream()
|
|
98
|
+
.pipe(
|
|
99
|
+
new Transform({
|
|
100
|
+
objectMode: true,
|
|
101
|
+
transform(row: ObjectWithId, _encoding, cb) {
|
|
102
|
+
cb(null, row.id)
|
|
103
|
+
},
|
|
104
|
+
}),
|
|
105
|
+
)
|
|
106
|
+
}
|
|
107
|
+
|
|
108
|
+
streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
|
|
94
109
|
let sql = `SELECT v FROM ${table}`
|
|
95
110
|
if (limit) sql += ` LIMIT ${limit}`
|
|
96
111
|
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
97
112
|
|
|
98
|
-
return (this.db.pool().query(sql).stream() as ReadableTyped<{ v:
|
|
113
|
+
return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: Buffer }>).map(row => row.v)
|
|
99
114
|
}
|
|
100
115
|
|
|
101
|
-
streamEntries
|
|
116
|
+
streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
|
|
102
117
|
let sql = `SELECT id,v FROM ${table}`
|
|
103
118
|
if (limit) sql += ` LIMIT ${limit}`
|
|
104
119
|
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
105
120
|
|
|
106
|
-
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject
|
|
121
|
+
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject>).map(row => [
|
|
107
122
|
row.id,
|
|
108
123
|
row.v,
|
|
109
124
|
])
|
|
@@ -125,10 +140,6 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
125
140
|
return rows[0]!.cnt
|
|
126
141
|
}
|
|
127
142
|
|
|
128
|
-
async increment(_table: string, _id: string, _by?: number): Promise<number> {
|
|
129
|
-
throw new AppError('MySQLKeyValueDB.increment() is not implemented')
|
|
130
|
-
}
|
|
131
|
-
|
|
132
143
|
async incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]> {
|
|
133
144
|
throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented')
|
|
134
145
|
}
|