@naturalcycles/mysql-lib 3.13.0 → 3.14.1

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,41 @@ 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
  }
206
- // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
207
213
  if (opt.saveMethod === 'update') {
214
+ // TODO: This fails if a combination of entities with id and without id are parsed
208
215
  for await (const row of rows) {
209
- (0, js_lib_1._assert)(row.id, 'Cannot update without providing an id');
216
+ // Update already existing
217
+ (0, js_lib_1._assert)(row.id, 'id is required for updating');
210
218
  const query = new db_lib_1.DBQuery(table).filterEq('id', row.id);
211
219
  await this.updateByQuery(query, (0, js_lib_1._omit)(row, ['id']));
212
220
  }
221
+ return;
213
222
  }
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
- }
223
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
224
+ // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
225
+ const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
226
+ for await (const sql of sqls) {
227
+ await this.runSQL({ sql });
219
228
  }
220
229
  }
221
230
  // DELETE
@@ -100,10 +100,10 @@ exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
100
100
  * @param name
101
101
  */
102
102
  function mapNameToMySQL(name) {
103
- return name.replace(/\./g, '_dot_');
103
+ return name.replaceAll('.', '_dot_');
104
104
  }
105
105
  exports.mapNameToMySQL = mapNameToMySQL;
106
106
  function mapNameFromMySQL(name) {
107
- return name.replace(/_dot_/g, '.');
107
+ return name.replaceAll('_dot_', '.');
108
108
  }
109
109
  exports.mapNameFromMySQL = mapNameFromMySQL;
package/package.json CHANGED
@@ -12,7 +12,7 @@
12
12
  },
13
13
  "devDependencies": {
14
14
  "@naturalcycles/dev-lib": "^13.2.1",
15
- "@types/node": "^18.7.13",
15
+ "@types/node": "^20.2.3",
16
16
  "dotenv": "^16.0.0",
17
17
  "jest": "^29.0.0"
18
18
  },
@@ -36,7 +36,7 @@
36
36
  "engines": {
37
37
  "node": ">=16.15.0"
38
38
  },
39
- "version": "3.13.0",
39
+ "version": "3.14.1",
40
40
  "description": "MySQL client implementing CommonDB interface",
41
41
  "keywords": [
42
42
  "mysql",
package/src/mysql.db.ts CHANGED
@@ -306,37 +306,46 @@ 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
 
327
- // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
328
332
  if (opt.saveMethod === 'update') {
333
+ // TODO: This fails if a combination of entities with id and without id are parsed
329
334
  for await (const row of rows) {
330
- _assert(row.id, 'Cannot update without providing an id')
335
+ // Update already existing
336
+ _assert(row.id, 'id is required for updating')
331
337
  const query = new DBQuery(table).filterEq('id', row.id)
332
338
  await this.updateByQuery(query, _omit(row, ['id']))
333
339
  }
334
- } else {
335
- const sqls = insertSQL(table, rows, verb, this.cfg.logger)
340
+ return
341
+ }
336
342
 
337
- for await (const sql of sqls) {
338
- await this.runSQL({ sql })
339
- }
343
+ const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
344
+ // inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
345
+ const sqls = insertSQL(table, rows, verb, this.cfg.logger)
346
+
347
+ for await (const sql of sqls) {
348
+ await this.runSQL({ sql })
340
349
  }
341
350
  }
342
351
 
@@ -126,9 +126,9 @@ export function mysqlTableStatsToJsonSchemaField<T extends AnyObject = any>(
126
126
  * @param name
127
127
  */
128
128
  export function mapNameToMySQL(name: string): string {
129
- return name.replace(/\./g, '_dot_')
129
+ return name.replaceAll('.', '_dot_')
130
130
  }
131
131
 
132
132
  export function mapNameFromMySQL(name: string): string {
133
- return name.replace(/_dot_/g, '.')
133
+ return name.replaceAll('_dot_', '.')
134
134
  }