@naturalcycles/mysql-lib 3.13.0 → 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.
package/dist/mysql.db.js CHANGED
@@ -190,32 +190,31 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
190
190
  }
191
191
  return v;
192
192
  }));
193
- const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
194
- if (opt.assignGeneratedIds && opt.saveMethod !== 'update') {
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
- 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
- }
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 });
219
218
  }
220
219
  }
221
220
  // DELETE
package/package.json CHANGED
@@ -36,7 +36,7 @@
36
36
  "engines": {
37
37
  "node": ">=16.15.0"
38
38
  },
39
- "version": "3.13.0",
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
@@ -306,37 +306,36 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
306
306
  }),
307
307
  )
308
308
 
309
- const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
310
-
311
- if (opt.assignGeneratedIds && opt.saveMethod !== 'update') {
309
+ if (opt.assignGeneratedIds) {
312
310
  // Insert rows one-by-one, to get their auto-generated id
313
311
 
314
312
  let i = -1
315
313
  for await (const row of rows) {
316
314
  i++
317
- const sql = insertSQL(table, [row], verb, this.cfg.logger)[0]!
318
- const { insertId } = await this.runSQL<OkPacket>({ sql })
319
-
320
- // Mutate the input row with insertIt
321
- 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
+ }
322
327
  }
323
328
 
324
329
  return
325
330
  }
326
331
 
332
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
333
+
327
334
  // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
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)
335
+ const sqls = insertSQL(table, rows, verb, this.cfg.logger)
336
336
 
337
- for await (const sql of sqls) {
338
- await this.runSQL({ sql })
339
- }
337
+ for await (const sql of sqls) {
338
+ await this.runSQL({ sql })
340
339
  }
341
340
  }
342
341