@naturalcycles/mysql-lib 3.14.6 → 3.14.8

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,10 +1,10 @@
1
1
  import { DBPatch, BaseCommonDB, CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBQuery, RunQueryResult, CommonDBSupport, CommonDBType } from '@naturalcycles/db-lib';
2
- import { AnyPartialObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } from '@naturalcycles/js-lib';
2
+ import { CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
3
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
4
4
  import { Connection, Pool, PoolConfig, PoolConnection, QueryOptions } from 'mysql';
5
5
  export interface MysqlDBOptions extends CommonDBOptions {
6
6
  }
7
- export interface MysqlDBSaveOptions<ROW extends PartialObjectWithId = AnyPartialObjectWithId> extends CommonDBSaveOptions<ROW> {
7
+ export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
8
8
  }
9
9
  /**
10
10
  * @default false / undefined
@@ -42,22 +42,22 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
42
42
  * Be careful to manage this connection yourself (open, release, etc).
43
43
  */
44
44
  createSingleConnection(): Promise<Connection>;
45
- getByIds<ROW extends PartialObjectWithId>(table: string, ids: string[], opt?: MysqlDBOptions): Promise<Saved<ROW>[]>;
46
- runQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Promise<RunQueryResult<Saved<ROW>>>;
45
+ getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: MysqlDBOptions): Promise<ROW[]>;
46
+ runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Promise<RunQueryResult<ROW>>;
47
47
  runSQL<RESULT>(q: QueryOptions): Promise<RESULT>;
48
48
  /**
49
49
  * Allows to run semicolon-separated "SQL file".
50
50
  * E.g "ddl reset script".
51
51
  */
52
52
  runSQLString(s: string): Promise<void>;
53
- runQueryCount<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
54
- streamQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<Saved<ROW>>;
55
- saveBatch<ROW extends PartialObjectWithId>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
53
+ runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
54
+ streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<ROW>;
55
+ saveBatch<ROW extends ObjectWithId>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
56
56
  /**
57
57
  * Limitation: always returns [], regardless of which rows are actually deleted
58
58
  */
59
59
  deleteByIds(table: string, ids: string[], _opt?: MysqlDBOptions): Promise<number>;
60
- deleteByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
60
+ deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
61
61
  /**
62
62
  * Use with caution!
63
63
  */
@@ -65,8 +65,8 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
65
65
  /**
66
66
  * dropIfExists=true needed as a safety check
67
67
  */
68
- createTable<ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
68
+ createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
69
69
  getTables(): Promise<string[]>;
70
- getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
71
- updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
70
+ getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
71
+ updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
72
72
  }
package/dist/mysql.db.js CHANGED
@@ -173,15 +173,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
173
173
  if (this.cfg.logSQL)
174
174
  this.cfg.logger.log(`stream: ${sql}`);
175
175
  // return this.streamSQL(sql, opt)
176
- return this.pool()
177
- .query(sql)
178
- .stream()
179
- .pipe(new node_stream_1.Transform({
180
- objectMode: true,
181
- transform(row, _encoding, cb) {
182
- cb(null, (0, js_lib_1._filterUndefinedValues)(row, true));
183
- },
184
- }));
176
+ return this.pool().query(sql).stream().map(row => (0, js_lib_1._filterUndefinedValues)(row, true));
185
177
  }
186
178
  // SAVE
187
179
  async saveBatch(table, rowsInput, opt = {}) {
@@ -1,7 +1,6 @@
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");
5
4
  const js_lib_1 = require("@naturalcycles/js-lib");
6
5
  const mysql_db_1 = require("./mysql.db");
7
6
  class MySQLKeyValueDB {
@@ -63,16 +62,7 @@ class MySQLKeyValueDB {
63
62
  sql += ` LIMIT ${limit}`;
64
63
  if (this.cfg.logSQL)
65
64
  this.db.cfg.logger.log(`stream: ${sql}`);
66
- return this.db
67
- .pool()
68
- .query(sql)
69
- .stream()
70
- .pipe(new node_stream_1.Transform({
71
- objectMode: true,
72
- transform(row, _, cb) {
73
- cb(null, row.id);
74
- },
75
- }));
65
+ return this.db.pool().query(sql).stream().map(row => row.id);
76
66
  }
77
67
  streamValues(table, limit) {
78
68
  let sql = `SELECT v FROM ${table}`;
@@ -80,16 +70,7 @@ class MySQLKeyValueDB {
80
70
  sql += ` LIMIT ${limit}`;
81
71
  if (this.cfg.logSQL)
82
72
  this.db.cfg.logger.log(`stream: ${sql}`);
83
- return this.db
84
- .pool()
85
- .query(sql)
86
- .stream()
87
- .pipe(new node_stream_1.Transform({
88
- objectMode: true,
89
- transform(row, _, cb) {
90
- cb(null, row.v);
91
- },
92
- }));
73
+ return this.db.pool().query(sql).stream().map(row => row.v);
93
74
  }
94
75
  streamEntries(table, limit) {
95
76
  let sql = `SELECT id,v FROM ${table}`;
@@ -97,16 +78,10 @@ class MySQLKeyValueDB {
97
78
  sql += ` LIMIT ${limit}`;
98
79
  if (this.cfg.logSQL)
99
80
  this.db.cfg.logger.log(`stream: ${sql}`);
100
- return this.db
101
- .pool()
102
- .query(sql)
103
- .stream()
104
- .pipe(new node_stream_1.Transform({
105
- objectMode: true,
106
- transform(row, _, cb) {
107
- cb(null, [row.id, row.v]);
108
- },
109
- }));
81
+ return this.db.pool().query(sql).stream().map(row => [
82
+ row.id,
83
+ row.v,
84
+ ]);
110
85
  }
111
86
  async beginTransaction() {
112
87
  await this.db.runSQL({ sql: `BEGIN TRANSACTION` });
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/mysql-lib",
3
3
  "scripts": {
4
- "prepare": "husky install"
4
+ "prepare": "husky"
5
5
  },
6
6
  "dependencies": {
7
7
  "@naturalcycles/db-lib": "^9.1.0",
@@ -34,9 +34,9 @@
34
34
  "url": "https://github.com/NaturalCycles/mysql-lib"
35
35
  },
36
36
  "engines": {
37
- "node": ">=16.15.0"
37
+ "node": ">=18.12.0"
38
38
  },
39
- "version": "3.14.6",
39
+ "version": "3.14.8",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Readable, Transform } from 'node:stream'
1
+ import { Readable } from 'node:stream'
2
2
  import { promisify } from 'node:util'
3
3
  import {
4
4
  DBPatch,
@@ -20,13 +20,11 @@ import {
20
20
  _mapValues,
21
21
  _Memo,
22
22
  _omit,
23
- AnyPartialObjectWithId,
24
23
  CommonLogger,
25
24
  commonLoggerPrefix,
26
25
  JsonSchemaObject,
27
26
  JsonSchemaRootObject,
28
- PartialObjectWithId,
29
- Saved,
27
+ ObjectWithId,
30
28
  } from '@naturalcycles/js-lib'
31
29
  import { ReadableTyped, white } from '@naturalcycles/nodejs-lib'
32
30
  import {
@@ -48,8 +46,7 @@ import {
48
46
  } from './schema/mysql.schema.util'
49
47
 
50
48
  export interface MysqlDBOptions extends CommonDBOptions {}
51
- export interface MysqlDBSaveOptions<ROW extends PartialObjectWithId = AnyPartialObjectWithId>
52
- extends CommonDBSaveOptions<ROW> {}
49
+ export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {}
53
50
 
54
51
  /**
55
52
  * @default false / undefined
@@ -196,11 +193,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
196
193
  }
197
194
 
198
195
  // GET
199
- override async getByIds<ROW extends PartialObjectWithId>(
196
+ override async getByIds<ROW extends ObjectWithId>(
200
197
  table: string,
201
198
  ids: string[],
202
199
  opt: MysqlDBOptions = {},
203
- ): Promise<Saved<ROW>[]> {
200
+ ): Promise<ROW[]> {
204
201
  if (!ids.length) return []
205
202
  const q = new DBQuery<ROW>(table).filterEq('id', ids)
206
203
  const { rows } = await this.runQuery(q, opt)
@@ -208,10 +205,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
208
205
  }
209
206
 
210
207
  // QUERY
211
- override async runQuery<ROW extends PartialObjectWithId>(
208
+ override async runQuery<ROW extends ObjectWithId>(
212
209
  q: DBQuery<ROW>,
213
210
  _opt: MysqlDBOptions = {},
214
- ): Promise<RunQueryResult<Saved<ROW>>> {
211
+ ): Promise<RunQueryResult<ROW>> {
215
212
  const sql = dbQueryToSQLSelect(q)
216
213
  if (!sql) {
217
214
  return {
@@ -261,7 +258,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
261
258
  }
262
259
  }
263
260
 
264
- override async runQueryCount<ROW extends PartialObjectWithId>(
261
+ override async runQueryCount<ROW extends ObjectWithId>(
265
262
  q: DBQuery<ROW>,
266
263
  _opt?: CommonDBOptions,
267
264
  ): Promise<number> {
@@ -269,10 +266,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
269
266
  return (rows[0] as any)._count
270
267
  }
271
268
 
272
- override streamQuery<ROW extends PartialObjectWithId>(
269
+ override streamQuery<ROW extends ObjectWithId>(
273
270
  q: DBQuery<ROW>,
274
271
  _opt: MysqlDBOptions = {},
275
- ): ReadableTyped<Saved<ROW>> {
272
+ ): ReadableTyped<ROW> {
276
273
  const sql = dbQueryToSQLSelect(q)
277
274
  if (!sql) {
278
275
  return Readable.from([])
@@ -281,21 +278,13 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
281
278
  if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)
282
279
 
283
280
  // return this.streamSQL(sql, opt)
284
- return this.pool()
285
- .query(sql)
286
- .stream()
287
- .pipe(
288
- new Transform({
289
- objectMode: true,
290
- transform(row, _encoding, cb) {
291
- cb(null, _filterUndefinedValues(row, true))
292
- },
293
- }),
294
- )
281
+ return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
282
+ _filterUndefinedValues(row, true),
283
+ )
295
284
  }
296
285
 
297
286
  // SAVE
298
- override async saveBatch<ROW extends PartialObjectWithId>(
287
+ override async saveBatch<ROW extends ObjectWithId>(
299
288
  table: string,
300
289
  rowsInput: ROW[],
301
290
  opt: MysqlDBSaveOptions<ROW> = {},
@@ -373,7 +362,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
373
362
  return affectedRows
374
363
  }
375
364
 
376
- override async deleteByQuery<ROW extends PartialObjectWithId>(
365
+ override async deleteByQuery<ROW extends ObjectWithId>(
377
366
  q: DBQuery<ROW>,
378
367
  _opt?: CommonDBOptions,
379
368
  ): Promise<number> {
@@ -393,7 +382,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
393
382
  /**
394
383
  * dropIfExists=true needed as a safety check
395
384
  */
396
- override async createTable<ROW extends PartialObjectWithId>(
385
+ override async createTable<ROW extends ObjectWithId>(
397
386
  table: string,
398
387
  schema: JsonSchemaObject<ROW>,
399
388
  opt: CommonDBCreateOptions = {},
@@ -410,7 +399,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
410
399
  .filter(Boolean)
411
400
  }
412
401
 
413
- override async getTableSchema<ROW extends PartialObjectWithId>(
402
+ override async getTableSchema<ROW extends ObjectWithId>(
414
403
  table: string,
415
404
  ): Promise<JsonSchemaRootObject<ROW>> {
416
405
  const stats = await this.runSQL<MySQLTableStats[]>({
@@ -420,7 +409,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
420
409
  return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
421
410
  }
422
411
 
423
- override async updateByQuery<ROW extends PartialObjectWithId>(
412
+ override async updateByQuery<ROW extends ObjectWithId>(
424
413
  q: DBQuery<ROW>,
425
414
  patch: DBPatch<ROW>,
426
415
  ): Promise<number> {
@@ -1,6 +1,5 @@
1
- import { Transform } from 'node:stream'
2
1
  import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib'
3
- import { pMap } from '@naturalcycles/js-lib'
2
+ import { ObjectWithId, pMap } from '@naturalcycles/js-lib'
4
3
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
5
4
  import { QueryOptions } from 'mysql'
6
5
  import { MysqlDB, MysqlDBCfg } from './mysql.db'
@@ -78,18 +77,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
78
77
  if (limit) sql += ` LIMIT ${limit}`
79
78
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
80
79
 
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)
93
81
  }
94
82
 
95
83
  streamValues(table: string, limit?: number): ReadableTyped<Buffer> {
@@ -97,18 +85,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
97
85
  if (limit) sql += ` LIMIT ${limit}`
98
86
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
99
87
 
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)
112
89
  }
113
90
 
114
91
  streamEntries(table: string, limit?: number): ReadableTyped<KeyValueDBTuple> {
@@ -116,18 +93,10 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
116
93
  if (limit) sql += ` LIMIT ${limit}`
117
94
  if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
118
95
 
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
+ ])
131
100
  }
132
101
 
133
102
  async beginTransaction(): Promise<void> {
package/src/query.util.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
2
- import { _hb, CommonLogger } from '@naturalcycles/js-lib'
2
+ import { _hb, AnyObjectWithId, CommonLogger } from '@naturalcycles/js-lib'
3
3
  import { white, yellow } from '@naturalcycles/nodejs-lib'
4
4
  import { QueryOptions } from 'mysql'
5
5
  import * as mysql from 'mysql'
@@ -160,7 +160,7 @@ export function dbQueryToSQLUpdate(q: DBQuery<any>, record: Record<any, any>): s
160
160
  return mysql.format(tokens.join(' '), Object.values(record))
161
161
  }
162
162
 
163
- function selectTokens(q: DBQuery): string[] {
163
+ function selectTokens(q: DBQuery<any>): string[] {
164
164
  let fields = ['*']
165
165
 
166
166
  if (q._selectedFieldNames) {
@@ -192,7 +192,7 @@ function offsetLimitTokens(q: DBQuery<any>): string[] {
192
192
  return tokens
193
193
  }
194
194
 
195
- function groupOrderTokens(q: DBQuery): string[] {
195
+ function groupOrderTokens(q: DBQuery<AnyObjectWithId>): string[] {
196
196
  const t: string[] = []
197
197
 
198
198
  if (q._groupByFieldNames?.length) {
@@ -216,7 +216,7 @@ const OP_MAP: Partial<Record<DBQueryFilterOperator, string>> = {
216
216
  /**
217
217
  * Returns `null` for "guaranteed 0 rows" cases.
218
218
  */
219
- function getWhereTokens(q: DBQuery): string[] | null {
219
+ function getWhereTokens(q: DBQuery<any>): string[] | null {
220
220
  if (!q._filters.length) return []
221
221
 
222
222
  let returnNull = false