@naturalcycles/mysql-lib 3.12.4 → 3.14.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
@@ -190,19 +190,27 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
190
190
  }
191
191
  return v;
192
192
  }));
193
- const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
194
193
  if (opt.assignGeneratedIds) {
195
194
  // Insert rows one-by-one, to get their auto-generated id
196
195
  let i = -1;
197
196
  for await (const row of rows) {
198
197
  i++;
199
- const sql = (0, query_util_1.insertSQL)(table, [row], verb, this.cfg.logger)[0];
200
- const { insertId } = await this.runSQL({ sql });
201
- // Mutate the input row with insertIt
202
- rowsInput[i].id = insertId;
198
+ if (row.id) {
199
+ // Update already existing
200
+ const query = new db_lib_1.DBQuery(table).filterEq('id', row.id);
201
+ await this.updateByQuery(query, (0, js_lib_1._omit)(row, ['id']));
202
+ }
203
+ else {
204
+ // Create new
205
+ const sql = (0, query_util_1.insertSQL)(table, [row], 'INSERT', this.cfg.logger)[0];
206
+ const { insertId } = await this.runSQL({ sql });
207
+ // Mutate the input row with insertIt
208
+ rowsInput[i].id = insertId;
209
+ }
203
210
  }
204
211
  return;
205
212
  }
213
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
206
214
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
207
215
  const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
208
216
  for await (const sql of sqls) {
@@ -255,6 +263,13 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
255
263
  });
256
264
  return (0, mysql_schema_util_1.mysqlTableStatsToJsonSchemaField)(table, stats, this.cfg.logger);
257
265
  }
266
+ async updateByQuery(q, patch) {
267
+ const sql = (0, query_util_1.dbQueryToSQLUpdate)(q, patch);
268
+ if (!sql)
269
+ return 0;
270
+ const { affectedRows } = await this.runSQL({ sql });
271
+ return affectedRows;
272
+ }
258
273
  }
259
274
  tslib_1.__decorate([
260
275
  (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.14.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,
@@ -304,24 +306,31 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
304
306
  }),
305
307
  )
306
308
 
307
- const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
308
-
309
309
  if (opt.assignGeneratedIds) {
310
310
  // Insert rows one-by-one, to get their auto-generated id
311
311
 
312
312
  let i = -1
313
313
  for await (const row of rows) {
314
314
  i++
315
- const sql = insertSQL(table, [row], verb, this.cfg.logger)[0]!
316
- const { insertId } = await this.runSQL<OkPacket>({ sql })
317
-
318
- // Mutate the input row with insertIt
319
- rowsInput[i]!.id = insertId
315
+ if (row.id) {
316
+ // Update already existing
317
+ const query = new DBQuery(table).filterEq('id', row.id)
318
+ await this.updateByQuery(query, _omit(row, ['id']))
319
+ } else {
320
+ // Create new
321
+ const sql = insertSQL(table, [row], 'INSERT', this.cfg.logger)[0]!
322
+ const { insertId } = await this.runSQL<OkPacket>({ sql })
323
+
324
+ // Mutate the input row with insertIt
325
+ rowsInput[i]!.id = insertId
326
+ }
320
327
  }
321
328
 
322
329
  return
323
330
  }
324
331
 
332
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
333
+
325
334
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
326
335
  const sqls = insertSQL(table, rows, verb, this.cfg.logger)
327
336
 
@@ -393,4 +402,15 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
393
402
 
394
403
  return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
395
404
  }
405
+
406
+ override async updateByQuery<ROW extends ObjectWithId>(
407
+ q: DBQuery<ROW>,
408
+ patch: DBPatch<ROW>,
409
+ ): Promise<number> {
410
+ const sql = dbQueryToSQLUpdate(q, patch)
411
+ if (!sql) return 0
412
+
413
+ const { affectedRows } = await this.runSQL<OkPacket>({ sql })
414
+ return affectedRows
415
+ }
396
416
  }