@naturalcycles/mysql-lib 4.5.0 → 4.7.0

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.
@@ -1,9 +1,9 @@
1
1
  import type { CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, RunQueryResult } from '@naturalcycles/db-lib';
2
- import { BaseCommonDB, CommonDBType, DBQuery } from '@naturalcycles/db-lib';
2
+ import { BaseCommonDB, DBQuery } from '@naturalcycles/db-lib';
3
3
  import type { JsonSchemaObject, JsonSchemaRootObject } from '@naturalcycles/js-lib/json-schema';
4
4
  import type { CommonLogger } from '@naturalcycles/js-lib/log';
5
5
  import type { ObjectWithId } from '@naturalcycles/js-lib/types';
6
- import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
6
+ import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
7
7
  import type { Connection, Pool, PoolConfig, PoolConnection, QueryOptions } from 'mysql';
8
8
  export interface MysqlDBOptions extends CommonDBOptions {
9
9
  }
@@ -27,7 +27,7 @@ export interface MysqlDBCfg extends PoolConfig {
27
27
  logger?: CommonLogger;
28
28
  }
29
29
  export declare class MysqlDB extends BaseCommonDB implements CommonDB {
30
- dbType: CommonDBType;
30
+ dbType: any;
31
31
  support: CommonDBSupport;
32
32
  constructor(cfg?: MysqlDBCfg);
33
33
  cfg: MysqlDBCfg & {
@@ -54,7 +54,7 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
54
54
  */
55
55
  runSQLString(s: string): Promise<void>;
56
56
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
57
- streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<ROW>;
57
+ streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Pipeline<ROW>;
58
58
  saveBatch<ROW extends ObjectWithId>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
59
59
  /**
60
60
  * Limitation: always returns [], regardless of which rows are actually deleted
package/dist/mysql.db.js CHANGED
@@ -1,5 +1,4 @@
1
1
  import { __decorate } from "tslib";
2
- import { Readable, Transform } from 'node:stream';
3
2
  import { promisify } from 'node:util';
4
3
  import { BaseCommonDB, commonDBFullSupport, CommonDBType, DBQuery } from '@naturalcycles/db-lib';
5
4
  import { _Memo } from '@naturalcycles/js-lib/decorators/memo.decorator.js';
@@ -7,6 +6,7 @@ import { _assert } from '@naturalcycles/js-lib/error/assert.js';
7
6
  import { commonLoggerPrefix } from '@naturalcycles/js-lib/log';
8
7
  import { _filterUndefinedValues, _mapKeys, _mapValues, _omit } from '@naturalcycles/js-lib/object';
9
8
  import { white } from '@naturalcycles/nodejs-lib/colors';
9
+ import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
10
10
  import * as mysql from 'mysql';
11
11
  import { dbQueryToSQLDelete, dbQueryToSQLSelect, dbQueryToSQLUpdate, insertSQL, } from './query.util.js';
12
12
  import { jsonSchemaToMySQLDDL, mapNameFromMySQL, mysqlTableStatsToJsonSchemaField, } from './schema/mysql.schema.util.js';
@@ -170,7 +170,7 @@ export class MysqlDB extends BaseCommonDB {
170
170
  streamQuery(q, _opt = {}) {
171
171
  const sql = dbQueryToSQLSelect(q);
172
172
  if (!sql) {
173
- return Readable.from([]);
173
+ return Pipeline.fromArray([]);
174
174
  }
175
175
  if (this.cfg.logSQL)
176
176
  this.cfg.logger.log(`stream: ${sql}`);
@@ -178,15 +178,7 @@ export class MysqlDB extends BaseCommonDB {
178
178
  // return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
179
179
  // _filterUndefinedValues(row, true),
180
180
  // )
181
- return this.pool()
182
- .query(sql)
183
- .stream()
184
- .pipe(new Transform({
185
- objectMode: true,
186
- transform(row, _encoding, cb) {
187
- cb(null, _filterUndefinedValues(row, { mutate: true }));
188
- },
189
- }));
181
+ return Pipeline.from(this.pool().query(sql).stream()).mapSync(row => _filterUndefinedValues(row, { mutate: true }));
190
182
  }
191
183
  // SAVE
192
184
  async saveBatch(table, rowsInput, opt = {}) {
@@ -1,16 +1,13 @@
1
1
  import type { CommonDBCreateOptions } from '@naturalcycles/db-lib';
2
2
  import type { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '@naturalcycles/db-lib/kv';
3
- import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream';
3
+ import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
4
4
  import type { MysqlDBCfg } from './mysql.db.js';
5
5
  import { MysqlDB } from './mysql.db.js';
6
6
  export declare class MySQLKeyValueDB implements CommonKeyValueDB {
7
7
  cfg: MysqlDBCfg;
8
8
  constructor(cfg: MysqlDBCfg);
9
9
  db: MysqlDB;
10
- support: {
11
- increment: boolean;
12
- count?: boolean;
13
- };
10
+ support: any;
14
11
  ping(): Promise<void>;
15
12
  close(): Promise<void>;
16
13
  createTable(table: string, opt?: CommonDBCreateOptions): Promise<void>;
@@ -21,9 +18,9 @@ export declare class MySQLKeyValueDB implements CommonKeyValueDB {
21
18
  dropTable(table: string): Promise<void>;
22
19
  deleteByIds(table: string, ids: string[]): Promise<void>;
23
20
  saveBatch(table: string, entries: KeyValueDBTuple[]): Promise<void>;
24
- streamIds(table: string, limit?: number): ReadableTyped<string>;
25
- streamValues(table: string, limit?: number): ReadableTyped<Buffer>;
26
- streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple>;
21
+ streamIds(table: string, limit?: number): Pipeline<string>;
22
+ streamValues(table: string, limit?: number): Pipeline<Buffer>;
23
+ streamEntries(table: string, limit?: number): Pipeline<KeyValueDBTuple>;
27
24
  beginTransaction(): Promise<void>;
28
25
  endTransaction(): Promise<void>;
29
26
  count(table: string): Promise<number>;
@@ -1,7 +1,7 @@
1
- import { Transform } from 'node:stream';
2
1
  import { commonKeyValueDBFullSupport } from '@naturalcycles/db-lib/kv';
3
2
  import { AppError } from '@naturalcycles/js-lib/error/error.util.js';
4
3
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
4
+ import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
5
5
  import { MysqlDB } from './mysql.db.js';
6
6
  export class MySQLKeyValueDB {
7
7
  cfg;
@@ -70,16 +70,7 @@ export class MySQLKeyValueDB {
70
70
  this.db.cfg.logger.log(`stream: ${sql}`);
71
71
  // todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
72
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 Transform({
78
- objectMode: true,
79
- transform(row, _encoding, cb) {
80
- cb(null, row.id);
81
- },
82
- }));
73
+ return Pipeline.from(this.db.pool().query(sql).stream()).mapSync(r => r.id);
83
74
  }
84
75
  streamValues(table, limit) {
85
76
  let sql = `SELECT v FROM ${table}`;
@@ -87,7 +78,7 @@ export class MySQLKeyValueDB {
87
78
  sql += ` LIMIT ${limit}`;
88
79
  if (this.cfg.logSQL)
89
80
  this.db.cfg.logger.log(`stream: ${sql}`);
90
- return this.db.pool().query(sql).stream().map(row => row.v);
81
+ return Pipeline.from(this.db.pool().query(sql).stream()).mapSync(row => row.v);
91
82
  }
92
83
  streamEntries(table, limit) {
93
84
  let sql = `SELECT id,v FROM ${table}`;
@@ -95,10 +86,7 @@ export class MySQLKeyValueDB {
95
86
  sql += ` LIMIT ${limit}`;
96
87
  if (this.cfg.logSQL)
97
88
  this.db.cfg.logger.log(`stream: ${sql}`);
98
- return this.db.pool().query(sql).stream().map(row => [
99
- row.id,
100
- row.v,
101
- ]);
89
+ return Pipeline.from(this.db.pool().query(sql).stream()).mapSync(row => [row.id, row.v]);
102
90
  }
103
91
  async beginTransaction() {
104
92
  await this.db.runSQL({ sql: `BEGIN TRANSACTION` });
@@ -43,7 +43,9 @@ export function insertSQL(table, rows, verb = 'INSERT', logger = console) {
43
43
  // VALUES (value1, value2, value3, ...);
44
44
  // eslint-disable-next-line unicorn/no-array-reduce
45
45
  const fieldSet = rows.reduce((set, row) => {
46
- Object.keys(row).forEach(field => set.add(field));
46
+ for (const field of Object.keys(row)) {
47
+ set.add(field);
48
+ }
47
49
  return set;
48
50
  }, new Set());
49
51
  const fields = [...fieldSet];
@@ -62,7 +64,7 @@ export function insertSQL(table, rows, verb = 'INSERT', logger = console) {
62
64
  return [full];
63
65
  const sqls = [];
64
66
  let sql;
65
- valueRows.forEach(vrow => {
67
+ for (const vrow of valueRows) {
66
68
  if (!sql) {
67
69
  sql = start + vrow;
68
70
  }
@@ -75,7 +77,7 @@ export function insertSQL(table, rows, verb = 'INSERT', logger = console) {
75
77
  sql += ',\n ' + vrow; // add
76
78
  }
77
79
  }
78
- });
80
+ }
79
81
  if (sql) {
80
82
  sqls.push(sql); // last one
81
83
  }
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  "devDependencies": {
13
13
  "@types/node": "^24",
14
14
  "dotenv": "^17",
15
- "@naturalcycles/dev-lib": "19.11.0"
15
+ "@naturalcycles/dev-lib": "19.37.0"
16
16
  },
17
17
  "exports": {
18
18
  ".": "./dist/index.js"
@@ -38,7 +38,7 @@
38
38
  "engines": {
39
39
  "node": ">=22.12.0"
40
40
  },
41
- "version": "4.5.0",
41
+ "version": "4.7.0",
42
42
  "description": "MySQL client implementing CommonDB interface",
43
43
  "keywords": [
44
44
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -1,4 +1,3 @@
1
- import { Readable, Transform } from 'node:stream'
2
1
  import { promisify } from 'node:util'
3
2
  import type {
4
3
  CommonDB,
@@ -17,7 +16,7 @@ import { commonLoggerPrefix } from '@naturalcycles/js-lib/log'
17
16
  import { _filterUndefinedValues, _mapKeys, _mapValues, _omit } from '@naturalcycles/js-lib/object'
18
17
  import type { ObjectWithId } from '@naturalcycles/js-lib/types'
19
18
  import { white } from '@naturalcycles/nodejs-lib/colors'
20
- import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream'
19
+ import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
21
20
  import type {
22
21
  Connection,
23
22
  OkPacket,
@@ -269,10 +268,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
269
268
  override streamQuery<ROW extends ObjectWithId>(
270
269
  q: DBQuery<ROW>,
271
270
  _opt: MysqlDBOptions = {},
272
- ): ReadableTyped<ROW> {
271
+ ): Pipeline<ROW> {
273
272
  const sql = dbQueryToSQLSelect(q)
274
273
  if (!sql) {
275
- return Readable.from([])
274
+ return Pipeline.fromArray([])
276
275
  }
277
276
 
278
277
  if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)
@@ -281,17 +280,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
281
280
  // return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
282
281
  // _filterUndefinedValues(row, true),
283
282
  // )
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, { mutate: true }))
292
- },
293
- }),
294
- )
283
+ return Pipeline.from<ROW>(this.pool().query(sql).stream()).mapSync(row =>
284
+ _filterUndefinedValues(row, { mutate: true }),
285
+ )
295
286
  }
296
287
 
297
288
  // SAVE
@@ -1,11 +1,10 @@
1
- import { Transform } from 'node:stream'
2
1
  import type { CommonDBCreateOptions } from '@naturalcycles/db-lib'
3
2
  import type { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '@naturalcycles/db-lib/kv'
4
3
  import { commonKeyValueDBFullSupport } from '@naturalcycles/db-lib/kv'
5
4
  import { AppError } from '@naturalcycles/js-lib/error/error.util.js'
6
5
  import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
7
6
  import type { ObjectWithId } from '@naturalcycles/js-lib/types'
8
- import type { ReadableTyped } from '@naturalcycles/nodejs-lib/stream'
7
+ import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
9
8
  import type { QueryOptions } from 'mysql'
10
9
  import type { MysqlDBCfg } from './mysql.db.js'
11
10
  import { MysqlDB } from './mysql.db.js'
@@ -83,44 +82,32 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
83
82
  })
84
83
  }
85
84
 
86
- streamIds(table: string, limit?: number): ReadableTyped<string> {
85
+ streamIds(table: string, limit?: number): Pipeline<string> {
87
86
  let sql = `SELECT id FROM ${table}`
88
87
  if (limit) sql += ` LIMIT ${limit}`
89
88
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
90
89
 
91
90
  // todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
92
91
  // return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
93
- return this.db
94
- .pool()
95
- .query(sql)
96
- .stream()
97
- .pipe(
98
- new Transform({
99
- objectMode: true,
100
- transform(row: ObjectWithId, _encoding, cb) {
101
- cb(null, row.id)
102
- },
103
- }),
104
- )
105
- }
106
-
107
- streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
92
+ return Pipeline.from<ObjectWithId>(this.db.pool().query(sql).stream()).mapSync(r => r.id)
93
+ }
94
+
95
+ streamValues(table: string, limit?: number): Pipeline<Buffer> {
108
96
  let sql = `SELECT v FROM ${table}`
109
97
  if (limit) sql += ` LIMIT ${limit}`
110
98
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
111
99
 
112
- return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: Buffer }>).map(row => row.v)
100
+ return Pipeline.from<{ v: Buffer }>(this.db.pool().query(sql).stream()).mapSync(row => row.v)
113
101
  }
114
102
 
115
- streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
103
+ streamEntries(table: string, limit?: number): Pipeline<KeyValueDBTuple> {
116
104
  let sql = `SELECT id,v FROM ${table}`
117
105
  if (limit) sql += ` LIMIT ${limit}`
118
106
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
119
107
 
120
- return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject>).map(row => [
121
- row.id,
122
- row.v,
123
- ])
108
+ return Pipeline.from<{ id: string; v: Buffer }>(this.db.pool().query(sql).stream()).mapSync(
109
+ row => [row.id, row.v],
110
+ )
124
111
  }
125
112
 
126
113
  async beginTransaction(): Promise<void> {
package/src/query.util.ts CHANGED
@@ -62,7 +62,9 @@ export function insertSQL(
62
62
 
63
63
  // eslint-disable-next-line unicorn/no-array-reduce
64
64
  const fieldSet = rows.reduce((set: Set<string>, row) => {
65
- Object.keys(row).forEach(field => set.add(field))
65
+ for (const field of Object.keys(row)) {
66
+ set.add(field)
67
+ }
66
68
  return set
67
69
  }, new Set<string>())
68
70
  const fields = [...fieldSet]
@@ -86,7 +88,7 @@ export function insertSQL(
86
88
  const sqls: string[] = []
87
89
  let sql: string | undefined
88
90
 
89
- valueRows.forEach(vrow => {
91
+ for (const vrow of valueRows) {
90
92
  if (!sql) {
91
93
  sql = start + vrow
92
94
  } else {
@@ -97,7 +99,7 @@ export function insertSQL(
97
99
  sql += ',\n ' + vrow // add
98
100
  }
99
101
  }
100
- })
102
+ }
101
103
 
102
104
  if (sql) {
103
105
  sqls.push(sql) // last one