@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 +22 -13
- package/dist/schema/mysql.schema.util.js +2 -2
- package/package.json +2 -2
- package/src/mysql.db.ts +24 -15
- package/src/schema/mysql.schema.util.ts +2 -2
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
|
-
|
|
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
|
-
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
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
|
-
|
|
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
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
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.
|
|
103
|
+
return name.replaceAll('.', '_dot_');
|
|
104
104
|
}
|
|
105
105
|
exports.mapNameToMySQL = mapNameToMySQL;
|
|
106
106
|
function mapNameFromMySQL(name) {
|
|
107
|
-
return name.
|
|
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": "^
|
|
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.
|
|
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
|
-
|
|
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
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
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
|
-
|
|
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
|
-
|
|
335
|
-
|
|
340
|
+
return
|
|
341
|
+
}
|
|
336
342
|
|
|
337
|
-
|
|
338
|
-
|
|
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.
|
|
129
|
+
return name.replaceAll('.', '_dot_')
|
|
130
130
|
}
|
|
131
131
|
|
|
132
132
|
export function mapNameFromMySQL(name: string): string {
|
|
133
|
-
return name.
|
|
133
|
+
return name.replaceAll('_dot_', '.')
|
|
134
134
|
}
|