@naturalcycles/mysql-lib 3.14.4 → 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.
package/dist/index.d.ts CHANGED
@@ -1,6 +1,3 @@
1
- import { getDBAdapter } from './dbAdapter';
2
- import { MysqlDB, MysqlDBCfg, MysqlDBOptions, MysqlDBSaveOptions } from './mysql.db';
3
- import { MySQLKeyValueDB } from './mysqlKeyValueDB';
4
- import { jsonSchemaToMySQLDDL } from './schema/mysql.schema.util';
5
- export type { MysqlDBCfg, MysqlDBOptions, MysqlDBSaveOptions };
6
- export { MysqlDB, MySQLKeyValueDB, jsonSchemaToMySQLDDL, getDBAdapter };
1
+ export * from './mysql.db';
2
+ export * from './mysqlKeyValueDB';
3
+ export { jsonSchemaToMySQLDDL } from './schema/mysql.schema.util';
package/dist/index.js CHANGED
@@ -1,11 +1,8 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDBAdapter = exports.jsonSchemaToMySQLDDL = exports.MySQLKeyValueDB = exports.MysqlDB = void 0;
4
- const dbAdapter_1 = require("./dbAdapter");
5
- Object.defineProperty(exports, "getDBAdapter", { enumerable: true, get: function () { return dbAdapter_1.getDBAdapter; } });
6
- const mysql_db_1 = require("./mysql.db");
7
- Object.defineProperty(exports, "MysqlDB", { enumerable: true, get: function () { return mysql_db_1.MysqlDB; } });
8
- const mysqlKeyValueDB_1 = require("./mysqlKeyValueDB");
9
- Object.defineProperty(exports, "MySQLKeyValueDB", { enumerable: true, get: function () { return mysqlKeyValueDB_1.MySQLKeyValueDB; } });
10
- const mysql_schema_util_1 = require("./schema/mysql.schema.util");
3
+ exports.jsonSchemaToMySQLDDL = void 0;
4
+ const tslib_1 = require("tslib");
5
+ tslib_1.__exportStar(require("./mysql.db"), exports);
6
+ tslib_1.__exportStar(require("./mysqlKeyValueDB"), exports);
7
+ var mysql_schema_util_1 = require("./schema/mysql.schema.util");
11
8
  Object.defineProperty(exports, "jsonSchemaToMySQLDDL", { enumerable: true, get: function () { return mysql_schema_util_1.jsonSchemaToMySQLDDL; } });
@@ -1,10 +1,10 @@
1
- import { DBPatch, BaseCommonDB, CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
2
- import { AnyObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
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';
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
@@ -24,6 +24,8 @@ export interface MysqlDBCfg extends PoolConfig {
24
24
  logger?: CommonLogger;
25
25
  }
26
26
  export declare class MysqlDB extends BaseCommonDB implements CommonDB {
27
+ dbType: CommonDBType;
28
+ support: CommonDBSupport;
27
29
  constructor(cfg?: MysqlDBCfg);
28
30
  cfg: MysqlDBCfg & {
29
31
  logger: CommonLogger;
@@ -40,22 +42,22 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
40
42
  * Be careful to manage this connection yourself (open, release, etc).
41
43
  */
42
44
  createSingleConnection(): Promise<Connection>;
43
- getByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], opt?: MysqlDBOptions): Promise<ROW[]>;
44
- 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>>>;
45
47
  runSQL<RESULT>(q: QueryOptions): Promise<RESULT>;
46
48
  /**
47
49
  * Allows to run semicolon-separated "SQL file".
48
50
  * E.g "ddl reset script".
49
51
  */
50
52
  runSQLString(s: string): Promise<void>;
51
- runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
52
- streamQuery<ROW extends ObjectWithId, OUT = ROW>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<OUT>;
53
- 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>;
54
56
  /**
55
57
  * Limitation: always returns [], regardless of which rows are actually deleted
56
58
  */
57
- deleteByIds<ROW extends ObjectWithId>(table: string, ids: ROW['id'][], _opt?: MysqlDBOptions): Promise<number>;
58
- 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>;
59
61
  /**
60
62
  * Use with caution!
61
63
  */
@@ -63,8 +65,8 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
63
65
  /**
64
66
  * dropIfExists=true needed as a safety check
65
67
  */
66
- 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>;
67
69
  getTables(): Promise<string[]>;
68
- getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
69
- 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>;
70
72
  }
package/dist/mysql.db.js CHANGED
@@ -34,6 +34,12 @@ const typeCast = (field, next) => {
34
34
  class MysqlDB extends db_lib_1.BaseCommonDB {
35
35
  constructor(cfg = {}) {
36
36
  super();
37
+ this.dbType = db_lib_1.CommonDBType.relational;
38
+ this.support = {
39
+ ...db_lib_1.commonDBFullSupport,
40
+ updateSaveMethod: false,
41
+ transactions: false,
42
+ };
37
43
  this.cfg = {
38
44
  typeCast,
39
45
  // charset: 'utf8mb4', // for emoji support
package/package.json CHANGED
@@ -4,7 +4,7 @@
4
4
  "prepare": "husky install"
5
5
  },
6
6
  "dependencies": {
7
- "@naturalcycles/db-lib": "^8.16.2",
7
+ "@naturalcycles/db-lib": "^9.1.0",
8
8
  "@naturalcycles/js-lib": "^14.47.0",
9
9
  "@naturalcycles/nodejs-lib": "^13.1.0",
10
10
  "@types/mysql": "^2.15.19",
@@ -36,7 +36,7 @@
36
36
  "engines": {
37
37
  "node": ">=16.15.0"
38
38
  },
39
- "version": "3.14.4",
39
+ "version": "3.14.6",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/index.ts CHANGED
@@ -1,8 +1,3 @@
1
- import { getDBAdapter } from './dbAdapter'
2
- import { MysqlDB, MysqlDBCfg, MysqlDBOptions, MysqlDBSaveOptions } from './mysql.db'
3
- import { MySQLKeyValueDB } from './mysqlKeyValueDB'
4
- import { jsonSchemaToMySQLDDL } from './schema/mysql.schema.util'
5
-
6
- export type { MysqlDBCfg, MysqlDBOptions, MysqlDBSaveOptions }
7
-
8
- export { MysqlDB, MySQLKeyValueDB, jsonSchemaToMySQLDDL, getDBAdapter }
1
+ export * from './mysql.db'
2
+ export * from './mysqlKeyValueDB'
3
+ export { jsonSchemaToMySQLDDL } from './schema/mysql.schema.util'
package/src/mysql.db.ts CHANGED
@@ -9,6 +9,9 @@ import {
9
9
  CommonDBSaveOptions,
10
10
  DBQuery,
11
11
  RunQueryResult,
12
+ CommonDBSupport,
13
+ commonDBFullSupport,
14
+ CommonDBType,
12
15
  } from '@naturalcycles/db-lib'
13
16
  import {
14
17
  _assert,
@@ -17,12 +20,13 @@ import {
17
20
  _mapValues,
18
21
  _Memo,
19
22
  _omit,
20
- AnyObjectWithId,
23
+ AnyPartialObjectWithId,
21
24
  CommonLogger,
22
25
  commonLoggerPrefix,
23
26
  JsonSchemaObject,
24
27
  JsonSchemaRootObject,
25
- ObjectWithId,
28
+ PartialObjectWithId,
29
+ Saved,
26
30
  } from '@naturalcycles/js-lib'
27
31
  import { ReadableTyped, white } from '@naturalcycles/nodejs-lib'
28
32
  import {
@@ -44,7 +48,7 @@ import {
44
48
  } from './schema/mysql.schema.util'
45
49
 
46
50
  export interface MysqlDBOptions extends CommonDBOptions {}
47
- export interface MysqlDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
51
+ export interface MysqlDBSaveOptions<ROW extends PartialObjectWithId = AnyPartialObjectWithId>
48
52
  extends CommonDBSaveOptions<ROW> {}
49
53
 
50
54
  /**
@@ -91,6 +95,14 @@ const typeCast: TypeCast = (field, next) => {
91
95
  }
92
96
 
93
97
  export class MysqlDB extends BaseCommonDB implements CommonDB {
98
+ override dbType = CommonDBType.relational
99
+
100
+ override support: CommonDBSupport = {
101
+ ...commonDBFullSupport,
102
+ updateSaveMethod: false,
103
+ transactions: false,
104
+ }
105
+
94
106
  constructor(cfg: MysqlDBCfg = {}) {
95
107
  super()
96
108
  this.cfg = {
@@ -184,11 +196,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
184
196
  }
185
197
 
186
198
  // GET
187
- override async getByIds<ROW extends ObjectWithId>(
199
+ override async getByIds<ROW extends PartialObjectWithId>(
188
200
  table: string,
189
- ids: ROW['id'][],
201
+ ids: string[],
190
202
  opt: MysqlDBOptions = {},
191
- ): Promise<ROW[]> {
203
+ ): Promise<Saved<ROW>[]> {
192
204
  if (!ids.length) return []
193
205
  const q = new DBQuery<ROW>(table).filterEq('id', ids)
194
206
  const { rows } = await this.runQuery(q, opt)
@@ -196,10 +208,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
196
208
  }
197
209
 
198
210
  // QUERY
199
- override async runQuery<ROW extends ObjectWithId, OUT = ROW>(
211
+ override async runQuery<ROW extends PartialObjectWithId>(
200
212
  q: DBQuery<ROW>,
201
213
  _opt: MysqlDBOptions = {},
202
- ): Promise<RunQueryResult<OUT>> {
214
+ ): Promise<RunQueryResult<Saved<ROW>>> {
203
215
  const sql = dbQueryToSQLSelect(q)
204
216
  if (!sql) {
205
217
  return {
@@ -249,7 +261,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
249
261
  }
250
262
  }
251
263
 
252
- override async runQueryCount<ROW extends ObjectWithId>(
264
+ override async runQueryCount<ROW extends PartialObjectWithId>(
253
265
  q: DBQuery<ROW>,
254
266
  _opt?: CommonDBOptions,
255
267
  ): Promise<number> {
@@ -257,10 +269,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
257
269
  return (rows[0] as any)._count
258
270
  }
259
271
 
260
- override streamQuery<ROW extends ObjectWithId, OUT = ROW>(
272
+ override streamQuery<ROW extends PartialObjectWithId>(
261
273
  q: DBQuery<ROW>,
262
274
  _opt: MysqlDBOptions = {},
263
- ): ReadableTyped<OUT> {
275
+ ): ReadableTyped<Saved<ROW>> {
264
276
  const sql = dbQueryToSQLSelect(q)
265
277
  if (!sql) {
266
278
  return Readable.from([])
@@ -283,7 +295,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
283
295
  }
284
296
 
285
297
  // SAVE
286
- override async saveBatch<ROW extends Partial<ObjectWithId>>(
298
+ override async saveBatch<ROW extends PartialObjectWithId>(
287
299
  table: string,
288
300
  rowsInput: ROW[],
289
301
  opt: MysqlDBSaveOptions<ROW> = {},
@@ -352,11 +364,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
352
364
  /**
353
365
  * Limitation: always returns [], regardless of which rows are actually deleted
354
366
  */
355
- async deleteByIds<ROW extends ObjectWithId>(
356
- table: string,
357
- ids: ROW['id'][],
358
- _opt?: MysqlDBOptions,
359
- ): Promise<number> {
367
+ override async deleteByIds(table: string, ids: string[], _opt?: MysqlDBOptions): Promise<number> {
360
368
  if (!ids.length) return 0
361
369
  const sql = dbQueryToSQLDelete(new DBQuery(table).filterEq('id', ids))
362
370
  if (!sql) return 0
@@ -365,7 +373,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
365
373
  return affectedRows
366
374
  }
367
375
 
368
- override async deleteByQuery<ROW extends ObjectWithId>(
376
+ override async deleteByQuery<ROW extends PartialObjectWithId>(
369
377
  q: DBQuery<ROW>,
370
378
  _opt?: CommonDBOptions,
371
379
  ): Promise<number> {
@@ -385,7 +393,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
385
393
  /**
386
394
  * dropIfExists=true needed as a safety check
387
395
  */
388
- override async createTable<ROW extends ObjectWithId>(
396
+ override async createTable<ROW extends PartialObjectWithId>(
389
397
  table: string,
390
398
  schema: JsonSchemaObject<ROW>,
391
399
  opt: CommonDBCreateOptions = {},
@@ -402,7 +410,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
402
410
  .filter(Boolean)
403
411
  }
404
412
 
405
- override async getTableSchema<ROW extends ObjectWithId>(
413
+ override async getTableSchema<ROW extends PartialObjectWithId>(
406
414
  table: string,
407
415
  ): Promise<JsonSchemaRootObject<ROW>> {
408
416
  const stats = await this.runSQL<MySQLTableStats[]>({
@@ -412,7 +420,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
412
420
  return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
413
421
  }
414
422
 
415
- override async updateByQuery<ROW extends ObjectWithId>(
423
+ override async updateByQuery<ROW extends PartialObjectWithId>(
416
424
  q: DBQuery<ROW>,
417
425
  patch: DBPatch<ROW>,
418
426
  ): Promise<number> {
@@ -1,2 +0,0 @@
1
- import { MysqlDB } from './mysql.db';
2
- export declare function getDBAdapter(cfgStr?: string): MysqlDB;
package/dist/dbAdapter.js DELETED
@@ -1,9 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.getDBAdapter = void 0;
4
- const mysql_db_1 = require("./mysql.db");
5
- function getDBAdapter(cfgStr = '{}') {
6
- const cfg = JSON.parse(cfgStr);
7
- return new mysql_db_1.MysqlDB(cfg);
8
- }
9
- exports.getDBAdapter = getDBAdapter;
package/src/dbAdapter.ts DELETED
@@ -1,6 +0,0 @@
1
- import { MysqlDB } from './mysql.db'
2
-
3
- export function getDBAdapter(cfgStr: string = '{}'): MysqlDB {
4
- const cfg = JSON.parse(cfgStr)
5
- return new MysqlDB(cfg)
6
- }