@naturalcycles/mysql-lib 3.11.2 → 3.12.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.
@@ -50,7 +50,7 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
50
50
  runSQLString(s: string): Promise<void>;
51
51
  runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
52
52
  streamQuery<ROW extends ObjectWithId, OUT = ROW>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<OUT>;
53
- saveBatch<ROW extends Partial<ObjectWithId>>(table: string, _rows: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
53
+ saveBatch<ROW extends Partial<ObjectWithId>>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
54
54
  /**
55
55
  * Limitation: always returns [], regardless of which rows are actually deleted
56
56
  */
package/dist/mysql.db.js CHANGED
@@ -171,15 +171,28 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
171
171
  }));
172
172
  }
173
173
  // SAVE
174
- async saveBatch(table, _rows, opt = {}) {
175
- if (!_rows.length)
174
+ async saveBatch(table, rowsInput, opt = {}) {
175
+ if (!rowsInput.length)
176
176
  return;
177
177
  // Stringify object values
178
- const rows = _rows.map(row => (0, js_lib_1._mapValues)(row, (_k, v) => {
178
+ const rows = rowsInput.map(row => (0, js_lib_1._mapValues)(row, (_k, v) => {
179
179
  return v && typeof v === 'object' && !Buffer.isBuffer(v) ? JSON.stringify(v) : v;
180
180
  }));
181
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
182
+ if (opt.assignGeneratedIds) {
183
+ // Insert rows one-by-one, to get their auto-generated id
184
+ let i = -1;
185
+ for await (const row of rows) {
186
+ i++;
187
+ const sql = (0, query_util_1.insertSQL)(table, [row], verb, this.cfg.logger)[0];
188
+ const { insertId } = await this.runSQL({ sql });
189
+ // Mutate the input row with insertIt
190
+ rowsInput[i].id = insertId;
191
+ }
192
+ return;
193
+ }
181
194
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
182
- const sqls = (0, query_util_1.insertSQL)(table, rows, opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE', this.cfg.logger);
195
+ const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
183
196
  for await (const sql of sqls) {
184
197
  await this.runSQL({ sql });
185
198
  }
package/package.json CHANGED
@@ -14,7 +14,7 @@
14
14
  "@naturalcycles/dev-lib": "^12.7.5",
15
15
  "@types/node": "^17.0.5",
16
16
  "dotenv": "^16.0.0",
17
- "jest": "^27.2.4"
17
+ "jest": "^28.1.0"
18
18
  },
19
19
  "files": [
20
20
  "dist",
@@ -36,7 +36,7 @@
36
36
  "engines": {
37
37
  "node": ">=14.15.0"
38
38
  },
39
- "version": "3.11.2",
39
+ "version": "3.12.0",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -23,7 +23,15 @@ import {
23
23
  } from '@naturalcycles/js-lib'
24
24
  import { ReadableTyped } from '@naturalcycles/nodejs-lib'
25
25
  import { white } from '@naturalcycles/nodejs-lib/dist/colors'
26
- import { Connection, Pool, PoolConfig, PoolConnection, QueryOptions, TypeCast } from 'mysql'
26
+ import {
27
+ Connection,
28
+ OkPacket,
29
+ Pool,
30
+ PoolConfig,
31
+ PoolConnection,
32
+ QueryOptions,
33
+ TypeCast,
34
+ } from 'mysql'
27
35
  import * as mysql from 'mysql'
28
36
  import { dbQueryToSQLDelete, dbQueryToSQLSelect, insertSQL } from './query.util'
29
37
  import {
@@ -266,25 +274,38 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
266
274
  // SAVE
267
275
  override async saveBatch<ROW extends Partial<ObjectWithId>>(
268
276
  table: string,
269
- _rows: ROW[],
277
+ rowsInput: ROW[],
270
278
  opt: MysqlDBSaveOptions<ROW> = {},
271
279
  ): Promise<void> {
272
- if (!_rows.length) return
280
+ if (!rowsInput.length) return
273
281
 
274
282
  // Stringify object values
275
- const rows = _rows.map(row =>
283
+ const rows = rowsInput.map(row =>
276
284
  _mapValues(row, (_k, v) => {
277
285
  return v && typeof v === 'object' && !Buffer.isBuffer(v) ? JSON.stringify(v) : v
278
286
  }),
279
287
  )
280
288
 
289
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
290
+
291
+ if (opt.assignGeneratedIds) {
292
+ // Insert rows one-by-one, to get their auto-generated id
293
+
294
+ let i = -1
295
+ for await (const row of rows) {
296
+ i++
297
+ const sql = insertSQL(table, [row], verb, this.cfg.logger)[0]!
298
+ const { insertId } = await this.runSQL<OkPacket>({ sql })
299
+
300
+ // Mutate the input row with insertIt
301
+ rowsInput[i]!.id = insertId
302
+ }
303
+
304
+ return
305
+ }
306
+
281
307
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
282
- const sqls = insertSQL(
283
- table,
284
- rows,
285
- opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE',
286
- this.cfg.logger,
287
- )
308
+ const sqls = insertSQL(table, rows, verb, this.cfg.logger)
288
309
 
289
310
  for await (const sql of sqls) {
290
311
  await this.runSQL({ sql })