@naturalcycles/mysql-lib 3.14.5 → 3.14.6

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 { AnyObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
2
+ import { AnyPartialObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, PartialObjectWithId, Saved } 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 Partial<ObjectWithId> = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
7
+ export interface MysqlDBSaveOptions<ROW extends PartialObjectWithId = AnyPartialObjectWithId> 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 ObjectWithId>(table: string, ids: ROW['id'][], opt?: MysqlDBOptions): Promise<ROW[]>;
46
- runQuery<ROW extends ObjectWithId, OUT = ROW>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Promise<RunQueryResult<OUT>>;
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>>>;
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 ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
54
- streamQuery<ROW extends ObjectWithId, OUT = ROW>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<OUT>;
55
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): 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>;
56
56
  /**
57
57
  * Limitation: always returns [], regardless of which rows are actually deleted
58
58
  */
59
- deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: MysqlDBOptions): Promise<number>;
60
- deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
59
+ deleteByIds(table: string, ids: string[], _opt?: MysqlDBOptions): Promise<number>;
60
+ deleteByQuery<ROW extends PartialObjectWithId>(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 ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
68
+ createTable<ROW extends PartialObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
69
69
  getTables(): Promise<string[]>;
70
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
71
- updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
70
+ getTableSchema<ROW extends PartialObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
71
+ updateByQuery<ROW extends PartialObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
72
72
  }
package/package.json CHANGED
@@ -36,7 +36,7 @@
36
36
  "engines": {
37
37
  "node": ">=16.15.0"
38
38
  },
39
- "version": "3.14.5",
39
+ "version": "3.14.6",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -20,12 +20,13 @@ import {
20
20
  _mapValues,
21
21
  _Memo,
22
22
  _omit,
23
- AnyObjectWithId,
23
+ AnyPartialObjectWithId,
24
24
  CommonLogger,
25
25
  commonLoggerPrefix,
26
26
  JsonSchemaObject,
27
27
  JsonSchemaRootObject,
28
- ObjectWithId,
28
+ PartialObjectWithId,
29
+ Saved,
29
30
  } from '@naturalcycles/js-lib'
30
31
  import { ReadableTyped, white } from '@naturalcycles/nodejs-lib'
31
32
  import {
@@ -47,7 +48,7 @@ import {
47
48
  } from './schema/mysql.schema.util'
48
49
 
49
50
  export interface MysqlDBOptions extends CommonDBOptions {}
50
- export interface MysqlDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
51
+ export interface MysqlDBSaveOptions<ROW extends PartialObjectWithId = AnyPartialObjectWithId>
51
52
  extends CommonDBSaveOptions<ROW> {}
52
53
 
53
54
  /**
@@ -195,11 +196,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
195
196
  }
196
197
 
197
198
  // GET
198
- override async getByIds<ROW extends ObjectWithId>(
199
+ override async getByIds<ROW extends PartialObjectWithId>(
199
200
  table: string,
200
- ids: ROW['id'][],
201
+ ids: string[],
201
202
  opt: MysqlDBOptions = {},
202
- ): Promise<ROW[]> {
203
+ ): Promise<Saved<ROW>[]> {
203
204
  if (!ids.length) return []
204
205
  const q = new DBQuery<ROW>(table).filterEq('id', ids)
205
206
  const { rows } = await this.runQuery(q, opt)
@@ -207,10 +208,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
207
208
  }
208
209
 
209
210
  // QUERY
210
- override async runQuery<ROW extends ObjectWithId, OUT = ROW>(
211
+ override async runQuery<ROW extends PartialObjectWithId>(
211
212
  q: DBQuery<ROW>,
212
213
  _opt: MysqlDBOptions = {},
213
- ): Promise<RunQueryResult<OUT>> {
214
+ ): Promise<RunQueryResult<Saved<ROW>>> {
214
215
  const sql = dbQueryToSQLSelect(q)
215
216
  if (!sql) {
216
217
  return {
@@ -260,7 +261,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
260
261
  }
261
262
  }
262
263
 
263
- override async runQueryCount<ROW extends ObjectWithId>(
264
+ override async runQueryCount<ROW extends PartialObjectWithId>(
264
265
  q: DBQuery<ROW>,
265
266
  _opt?: CommonDBOptions,
266
267
  ): Promise<number> {
@@ -268,10 +269,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
268
269
  return (rows[0] as any)._count
269
270
  }
270
271
 
271
- override streamQuery<ROW extends ObjectWithId, OUT = ROW>(
272
+ override streamQuery<ROW extends PartialObjectWithId>(
272
273
  q: DBQuery<ROW>,
273
274
  _opt: MysqlDBOptions = {},
274
- ): ReadableTyped<OUT> {
275
+ ): ReadableTyped<Saved<ROW>> {
275
276
  const sql = dbQueryToSQLSelect(q)
276
277
  if (!sql) {
277
278
  return Readable.from([])
@@ -294,7 +295,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
294
295
  }
295
296
 
296
297
  // SAVE
297
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
298
+ override async saveBatch<ROW extends PartialObjectWithId>(
298
299
  table: string,
299
300
  rowsInput: ROW[],
300
301
  opt: MysqlDBSaveOptions<ROW> = {},
@@ -363,11 +364,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
363
364
  /**
364
365
  * Limitation: always returns [], regardless of which rows are actually deleted
365
366
  */
366
- override async deleteByIds<ROW extends ObjectWithId>(
367
- table: string,
368
- ids: ROW['id'][],
369
- _opt?: MysqlDBOptions,
370
- ): Promise<number> {
367
+ override async deleteByIds(table: string, ids: string[], _opt?: MysqlDBOptions): Promise<number> {
371
368
  if (!ids.length) return 0
372
369
  const sql = dbQueryToSQLDelete(new DBQuery(table).filterEq('id', ids))
373
370
  if (!sql) return 0
@@ -376,7 +373,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
376
373
  return affectedRows
377
374
  }
378
375
 
379
- override async deleteByQuery<ROW extends ObjectWithId>(
376
+ override async deleteByQuery<ROW extends PartialObjectWithId>(
380
377
  q: DBQuery<ROW>,
381
378
  _opt?: CommonDBOptions,
382
379
  ): Promise<number> {
@@ -396,7 +393,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
396
393
  /**
397
394
  * dropIfExists=true needed as a safety check
398
395
  */
399
- override async createTable<ROW extends ObjectWithId>(
396
+ override async createTable<ROW extends PartialObjectWithId>(
400
397
  table: string,
401
398
  schema: JsonSchemaObject<ROW>,
402
399
  opt: CommonDBCreateOptions = {},
@@ -413,7 +410,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
413
410
  .filter(Boolean)
414
411
  }
415
412
 
416
- override async getTableSchema<ROW extends ObjectWithId>(
413
+ override async getTableSchema<ROW extends PartialObjectWithId>(
417
414
  table: string,
418
415
  ): Promise<JsonSchemaRootObject<ROW>> {
419
416
  const stats = await this.runSQL<MySQLTableStats[]>({
@@ -423,7 +420,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
423
420
  return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
424
421
  }
425
422
 
426
- override async updateByQuery<ROW extends ObjectWithId>(
423
+ override async updateByQuery<ROW extends PartialObjectWithId>(
427
424
  q: DBQuery<ROW>,
428
425
  patch: DBPatch<ROW>,
429
426
  ): Promise<number> {