@naturalcycles/mysql-lib 3.12.4 → 3.13.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,3 +1,4 @@
1
+ import { DBPatch } from '@naturalcycles/db-lib';
1
2
  import { BaseCommonDB, CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
2
3
  import { AnyObjectWithId, CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
3
4
  import { ReadableTyped } from '@naturalcycles/nodejs-lib';
@@ -66,4 +67,5 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
66
67
  createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
67
68
  getTables(): Promise<string[]>;
68
69
  getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
70
+ updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
69
71
  }
package/dist/mysql.db.js CHANGED
@@ -191,7 +191,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
191
191
  return v;
192
192
  }));
193
193
  const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
194
- if (opt.assignGeneratedIds) {
194
+ if (opt.assignGeneratedIds && opt.saveMethod !== 'update') {
195
195
  // Insert rows one-by-one, to get their auto-generated id
196
196
  let i = -1;
197
197
  for await (const row of rows) {
@@ -204,9 +204,18 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
204
204
  return;
205
205
  }
206
206
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
207
- const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
208
- for await (const sql of sqls) {
209
- await this.runSQL({ sql });
207
+ if (opt.saveMethod === 'update') {
208
+ for await (const row of rows) {
209
+ (0, js_lib_1._assert)(row.id, 'Cannot update without providing an id');
210
+ const query = new db_lib_1.DBQuery(table).filterEq('id', row.id);
211
+ await this.updateByQuery(query, (0, js_lib_1._omit)(row, ['id']));
212
+ }
213
+ }
214
+ else {
215
+ const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
216
+ for await (const sql of sqls) {
217
+ await this.runSQL({ sql });
218
+ }
210
219
  }
211
220
  }
212
221
  // DELETE
@@ -255,6 +264,13 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
255
264
  });
256
265
  return (0, mysql_schema_util_1.mysqlTableStatsToJsonSchemaField)(table, stats, this.cfg.logger);
257
266
  }
267
+ async updateByQuery(q, patch) {
268
+ const sql = (0, query_util_1.dbQueryToSQLUpdate)(q, patch);
269
+ if (!sql)
270
+ return 0;
271
+ const { affectedRows } = await this.runSQL({ sql });
272
+ return affectedRows;
273
+ }
258
274
  }
259
275
  tslib_1.__decorate([
260
276
  (0, js_lib_1._Memo)()
package/package.json CHANGED
@@ -36,7 +36,7 @@
36
36
  "engines": {
37
37
  "node": ">=16.15.0"
38
38
  },
39
- "version": "3.12.4",
39
+ "version": "3.13.0",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { Readable, Transform } from 'node:stream'
2
2
  import { promisify } from 'node:util'
3
+ import { DBPatch } from '@naturalcycles/db-lib'
3
4
  import {
4
5
  BaseCommonDB,
5
6
  CommonDB,
@@ -15,6 +16,7 @@ import {
15
16
  _mapKeys,
16
17
  _mapValues,
17
18
  _Memo,
19
+ _omit,
18
20
  AnyObjectWithId,
19
21
  CommonLogger,
20
22
  commonLoggerPrefix,
@@ -34,7 +36,7 @@ import {
34
36
  TypeCast,
35
37
  } from 'mysql'
36
38
  import * as mysql from 'mysql'
37
- import { dbQueryToSQLDelete, dbQueryToSQLSelect, insertSQL } from './query.util'
39
+ import { dbQueryToSQLDelete, dbQueryToSQLSelect, dbQueryToSQLUpdate, insertSQL } from './query.util'
38
40
  import {
39
41
  jsonSchemaToMySQLDDL,
40
42
  mapNameFromMySQL,
@@ -306,7 +308,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
306
308
 
307
309
  const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
308
310
 
309
- if (opt.assignGeneratedIds) {
311
+ if (opt.assignGeneratedIds && opt.saveMethod !== 'update') {
310
312
  // Insert rows one-by-one, to get their auto-generated id
311
313
 
312
314
  let i = -1
@@ -323,10 +325,18 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
323
325
  }
324
326
 
325
327
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
326
- const sqls = insertSQL(table, rows, verb, this.cfg.logger)
328
+ if (opt.saveMethod === 'update') {
329
+ for await (const row of rows) {
330
+ _assert(row.id, 'Cannot update without providing an id')
331
+ const query = new DBQuery(table).filterEq('id', row.id)
332
+ await this.updateByQuery(query, _omit(row, ['id']))
333
+ }
334
+ } else {
335
+ const sqls = insertSQL(table, rows, verb, this.cfg.logger)
327
336
 
328
- for await (const sql of sqls) {
329
- await this.runSQL({ sql })
337
+ for await (const sql of sqls) {
338
+ await this.runSQL({ sql })
339
+ }
330
340
  }
331
341
  }
332
342
 
@@ -393,4 +403,15 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
393
403
 
394
404
  return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
395
405
  }
406
+
407
+ override async updateByQuery<ROW extends ObjectWithId>(
408
+ q: DBQuery<ROW>,
409
+ patch: DBPatch<ROW>,
410
+ ): Promise<number> {
411
+ const sql = dbQueryToSQLUpdate(q, patch)
412
+ if (!sql) return 0
413
+
414
+ const { affectedRows } = await this.runSQL<OkPacket>({ sql })
415
+ return affectedRows
416
+ }
396
417
  }