Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit 1da0d3f

Browse files
feat: assignGeneratedIds support
1 parent 75861d4 commit 1da0d3f

File tree

2 files changed

+95
-82
lines changed

2 files changed

+95
-82
lines changed

src/mysql.db.ts

Lines changed: 31 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,15 @@ import {
2323
} from '@naturalcycles/js-lib'
2424
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
2525
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'
2735
import * as mysql from 'mysql'
2836
import { dbQueryToSQLDelete, dbQueryToSQLSelect, insertSQL } from './query.util'
2937
import {
@@ -266,25 +274,38 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
266274
// SAVE
267275
override async saveBatch<ROW extends Partial<ObjectWithId>>(
268276
table: string,
269-
_rows: ROW[],
277+
rowsInput: ROW[],
270278
opt: MysqlDBSaveOptions<ROW> = {},
271279
): Promise<void> {
272-
if (!_rows.length) return
280+
if (!rowsInput.length) return
273281

274282
// Stringify object values
275-
const rows = _rows.map(row =>
283+
const rows = rowsInput.map(row =>
276284
_mapValues(row, (_k, v) => {
277285
return v && typeof v === 'object' && !Buffer.isBuffer(v) ? JSON.stringify(v) : v
278286
}),
279287
)
280288

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+
281307
// 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)
288309

289310
for await (const sql of sqls) {
290311
await this.runSQL({ sql })

yarn.lock

Lines changed: 64 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -761,18 +761,18 @@
761761
typescript "^4.0.2"
762762

763763
"@naturalcycles/db-lib@^8.16.2":
764-
version "8.39.0"
765-
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-8.39.0.tgz#faea5ba0ca3c9539d6f13a8942f36c6bdcfc05d0"
766-
integrity sha512-46iC5VQowvhtJvmJtQ+kwmDQnSPklRqCGbgqu0Kw9VERNHgCZ9MkAdIzfyQABoLrnA3vMo5amiTSdHNlSOCeRw==
764+
version "8.40.0"
765+
resolved "https://registry.yarnpkg.com/@naturalcycles/db-lib/-/db-lib-8.40.0.tgz#ce497adb059fbff6272926c925fec68a0c6650ef"
766+
integrity sha512-cmlUWOdeuWEZWu36V3fpBS1HwcF0HZNPKR/YmdjJHBhfzrewXS64CFfQ06KZ+7W5RABC3X+3zMUlsfnfyd3LYw==
767767
dependencies:
768768
"@naturalcycles/js-lib" "^14.0.0"
769769
"@naturalcycles/nodejs-lib" "^12.0.0"
770770
fs-extra "^10.0.0"
771771

772772
"@naturalcycles/dev-lib@^12.7.5":
773-
version "12.19.3"
774-
resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-12.19.3.tgz#03d1e46d137a3b0db733053f96c38c6b03f19387"
775-
integrity sha512-hk1Q+9Mmc63mpD0H03g43z6lndu5FJRUGVNYkpJTOKhegmi3uh2a5jmYLnzg7UaQtsDAI1MmFiAfHwKxoImqpQ==
773+
version "12.20.1"
774+
resolved "https://registry.yarnpkg.com/@naturalcycles/dev-lib/-/dev-lib-12.20.1.tgz#1395de8653fcdba7f7437150dae1ffa6b96cf621"
775+
integrity sha512-9WolsAzHXsuzuMUqCqkB3ZdmGTm0PoTDx8ZzsfFOXs0xAJ1kwGZ0Diz+11Ddzn8SDUjOlD/zQrRz4wP6/wMWxA==
776776
dependencies:
777777
"@commitlint/cli" "^17.0.0"
778778
"@commitlint/config-conventional" "^17.0.0"
@@ -817,12 +817,11 @@
817817
tslib "^2.0.0"
818818

819819
"@naturalcycles/nodejs-lib@^12.0.0", "@naturalcycles/nodejs-lib@^12.37.0", "@naturalcycles/nodejs-lib@^12.8.0":
820-
version "12.70.0"
821-
resolved "https://registry.yarnpkg.com/@naturalcycles/nodejs-lib/-/nodejs-lib-12.70.0.tgz#8e22c5004e3b2b2cf0da1b28c1685224565205be"
822-
integrity sha512-QS3H61jRZ5BpXZmQR1WnslmgMxEUA+KH6NHZjj4cS5Ud363aqsV8Zw7WiSdaQEd6/59+4n23FweQslk9tWD/Rw==
820+
version "12.70.1"
821+
resolved "https://registry.yarnpkg.com/@naturalcycles/nodejs-lib/-/nodejs-lib-12.70.1.tgz#449baad7db473b3956fec3085bb0ccac82743e6c"
822+
integrity sha512-U5wxyN58zcl4vKvDddqWoTsI1Nnso571ehZfg2Scz0BK+lRzq08i5EbopUxBFCdz//cgZSjxKCJTIsNGjef/Fw==
823823
dependencies:
824824
"@naturalcycles/js-lib" "^14.0.0"
825-
"@types/lru-cache" "^7.4.0"
826825
"@types/through2-concurrent" "^2.0.0"
827826
ajv "^8.6.2"
828827
ajv-formats "^2.1.0"
@@ -1044,13 +1043,6 @@
10441043
dependencies:
10451044
"@types/node" "*"
10461045

1047-
"@types/lru-cache@^7.4.0":
1048-
version "7.10.10"
1049-
resolved "https://registry.yarnpkg.com/@types/lru-cache/-/lru-cache-7.10.10.tgz#3fa937c35ff4b3f6753d5737915c9bf8e693a713"
1050-
integrity sha512-nEpVRPWW9EBmx2SCfNn3ClYxPL7IktPX12HhIoSc/H5mMjdeW3+YsXIpseLQ2xF35+OcpwKQbEUw5VtqE4PDNA==
1051-
dependencies:
1052-
lru-cache "*"
1053-
10541046
"@types/minimist@^1.2.0":
10551047
version "1.2.2"
10561048
resolved "https://registry.yarnpkg.com/@types/minimist/-/minimist-1.2.2.tgz#ee771e2ba4b3dc5b372935d549fd9617bf345b8c"
@@ -1130,13 +1122,13 @@
11301122
"@types/yargs-parser" "*"
11311123

11321124
"@typescript-eslint/eslint-plugin@^5.0.0":
1133-
version "5.24.0"
1134-
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.24.0.tgz#59f9ed21fc4490444b75b250e4ce81e4b7bba18f"
1135-
integrity sha512-6bqFGk6wa9+6RrU++eLknKyDqXU1Oc8nyoLu5a1fU17PNRJd9UBr56rMF7c4DRaRtnarlkQ4jwxUbvBo8cNlpw==
1125+
version "5.25.0"
1126+
resolved "https://registry.yarnpkg.com/@typescript-eslint/eslint-plugin/-/eslint-plugin-5.25.0.tgz#e8ce050990e4d36cc200f2de71ca0d3eb5e77a31"
1127+
integrity sha512-icYrFnUzvm+LhW0QeJNKkezBu6tJs9p/53dpPLFH8zoM9w1tfaKzVurkPotEpAqQ8Vf8uaFyL5jHd0Vs6Z0ZQg==
11361128
dependencies:
1137-
"@typescript-eslint/scope-manager" "5.24.0"
1138-
"@typescript-eslint/type-utils" "5.24.0"
1139-
"@typescript-eslint/utils" "5.24.0"
1129+
"@typescript-eslint/scope-manager" "5.25.0"
1130+
"@typescript-eslint/type-utils" "5.25.0"
1131+
"@typescript-eslint/utils" "5.25.0"
11401132
debug "^4.3.4"
11411133
functional-red-black-tree "^1.0.1"
11421134
ignore "^5.2.0"
@@ -1145,68 +1137,68 @@
11451137
tsutils "^3.21.0"
11461138

11471139
"@typescript-eslint/parser@^5.0.0":
1148-
version "5.24.0"
1149-
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.24.0.tgz#fe6563483f264aa9fe48b7fd9b329aa5f985841a"
1150-
integrity sha512-4q29C6xFYZ5B2CXqSBBdcS0lPyfM9M09DoQLtHS5kf+WbpV8pBBhHDLNhXfgyVwFnhrhYzOu7xmg02DzxeF2Uw==
1140+
version "5.25.0"
1141+
resolved "https://registry.yarnpkg.com/@typescript-eslint/parser/-/parser-5.25.0.tgz#fb533487147b4b9efd999a4d2da0b6c263b64f7f"
1142+
integrity sha512-r3hwrOWYbNKP1nTcIw/aZoH+8bBnh/Lh1iDHoFpyG4DnCpvEdctrSl6LOo19fZbzypjQMHdajolxs6VpYoChgA==
11511143
dependencies:
1152-
"@typescript-eslint/scope-manager" "5.24.0"
1153-
"@typescript-eslint/types" "5.24.0"
1154-
"@typescript-eslint/typescript-estree" "5.24.0"
1144+
"@typescript-eslint/scope-manager" "5.25.0"
1145+
"@typescript-eslint/types" "5.25.0"
1146+
"@typescript-eslint/typescript-estree" "5.25.0"
11551147
debug "^4.3.4"
11561148

1157-
"@typescript-eslint/scope-manager@5.24.0":
1158-
version "5.24.0"
1159-
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.24.0.tgz#ac8c4d65064204b596909c204fe9b7e30c3f51b2"
1160-
integrity sha512-WpMWipcDzGmMzdT7NtTjRXFabx10WleLUGrJpuJLGaxSqpcyq5ACpKSD5VE40h2nz3melQ91aP4Du7lh9FliCA==
1149+
"@typescript-eslint/scope-manager@5.25.0":
1150+
version "5.25.0"
1151+
resolved "https://registry.yarnpkg.com/@typescript-eslint/scope-manager/-/scope-manager-5.25.0.tgz#e78f1484bca7e484c48782075219c82c6b77a09f"
1152+
integrity sha512-p4SKTFWj+2VpreUZ5xMQsBMDdQ9XdRvODKXN4EksyBjFp2YvQdLkyHqOffakYZPuWJUDNu3jVXtHALDyTv3cww==
11611153
dependencies:
1162-
"@typescript-eslint/types" "5.24.0"
1163-
"@typescript-eslint/visitor-keys" "5.24.0"
1154+
"@typescript-eslint/types" "5.25.0"
1155+
"@typescript-eslint/visitor-keys" "5.25.0"
11641156

1165-
"@typescript-eslint/type-utils@5.24.0":
1166-
version "5.24.0"
1167-
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.24.0.tgz#aad47227e89cceec9175b1a67df69ebcd79f9421"
1168-
integrity sha512-uGi+sQiM6E5CeCZYBXiaIvIChBXru4LZ1tMoeKbh1Lze+8BO9syUG07594C4lvN2YPT4KVeIupOJkVI+9/DAmQ==
1157+
"@typescript-eslint/type-utils@5.25.0":
1158+
version "5.25.0"
1159+
resolved "https://registry.yarnpkg.com/@typescript-eslint/type-utils/-/type-utils-5.25.0.tgz#5750d26a5db4c4d68d511611e0ada04e56f613bc"
1160+
integrity sha512-B6nb3GK3Gv1Rsb2pqalebe/RyQoyG/WDy9yhj8EE0Ikds4Xa8RR28nHz+wlt4tMZk5bnAr0f3oC8TuDAd5CPrw==
11691161
dependencies:
1170-
"@typescript-eslint/utils" "5.24.0"
1162+
"@typescript-eslint/utils" "5.25.0"
11711163
debug "^4.3.4"
11721164
tsutils "^3.21.0"
11731165

1174-
"@typescript-eslint/types@5.24.0":
1175-
version "5.24.0"
1176-
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.24.0.tgz#565ff94a4b89073265e27514dc65225d18aabe6c"
1177-
integrity sha512-Tpg1c3shTDgTmZd3qdUyd+16r/pGmVaVEbLs+ufuWP0EruVbUiEOmpBBQxBb9a8iPRxi8Rb2oiwOxuZJzSq11A==
1166+
"@typescript-eslint/types@5.25.0":
1167+
version "5.25.0"
1168+
resolved "https://registry.yarnpkg.com/@typescript-eslint/types/-/types-5.25.0.tgz#dee51b1855788b24a2eceeae54e4adb89b088dd8"
1169+
integrity sha512-7fWqfxr0KNHj75PFqlGX24gWjdV/FDBABXL5dyvBOWHpACGyveok8Uj4ipPX/1fGU63fBkzSIycEje4XsOxUFA==
11781170

1179-
"@typescript-eslint/typescript-estree@5.24.0":
1180-
version "5.24.0"
1181-
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.24.0.tgz#30b9cbc1af475b9e02aca34cfe9ba9e1bb820143"
1182-
integrity sha512-zcor6vQkQmZAQfebSPVwUk/FD+CvnsnlfKXYeQDsWXRF+t7SBPmIfNia/wQxCSeu1h1JIjwV2i9f5/DdSp/uDw==
1171+
"@typescript-eslint/typescript-estree@5.25.0":
1172+
version "5.25.0"
1173+
resolved "https://registry.yarnpkg.com/@typescript-eslint/typescript-estree/-/typescript-estree-5.25.0.tgz#a7ab40d32eb944e3fb5b4e3646e81b1bcdd63e00"
1174+
integrity sha512-MrPODKDych/oWs/71LCnuO7NyR681HuBly2uLnX3r5i4ME7q/yBqC4hW33kmxtuauLTM0OuBOhhkFaxCCOjEEw==
11831175
dependencies:
1184-
"@typescript-eslint/types" "5.24.0"
1185-
"@typescript-eslint/visitor-keys" "5.24.0"
1176+
"@typescript-eslint/types" "5.25.0"
1177+
"@typescript-eslint/visitor-keys" "5.25.0"
11861178
debug "^4.3.4"
11871179
globby "^11.1.0"
11881180
is-glob "^4.0.3"
11891181
semver "^7.3.7"
11901182
tsutils "^3.21.0"
11911183

1192-
"@typescript-eslint/utils@5.24.0", "@typescript-eslint/utils@^5.10.0":
1193-
version "5.24.0"
1194-
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.24.0.tgz#7a854028040a305ddea129328e45cfb2c6406e75"
1195-
integrity sha512-K05sbWoeCBJH8KXu6hetBJ+ukG0k2u2KlgD3bN+v+oBKm8adJqVHpSSLHNzqyuv0Lh4GVSAUgZ5lB4icmPmWLw==
1184+
"@typescript-eslint/utils@5.25.0", "@typescript-eslint/utils@^5.10.0":
1185+
version "5.25.0"
1186+
resolved "https://registry.yarnpkg.com/@typescript-eslint/utils/-/utils-5.25.0.tgz#272751fd737733294b4ab95e16c7f2d4a75c2049"
1187+
integrity sha512-qNC9bhnz/n9Kba3yI6HQgQdBLuxDoMgdjzdhSInZh6NaDnFpTUlwNGxplUFWfY260Ya0TRPvkg9dd57qxrJI9g==
11961188
dependencies:
11971189
"@types/json-schema" "^7.0.9"
1198-
"@typescript-eslint/scope-manager" "5.24.0"
1199-
"@typescript-eslint/types" "5.24.0"
1200-
"@typescript-eslint/typescript-estree" "5.24.0"
1190+
"@typescript-eslint/scope-manager" "5.25.0"
1191+
"@typescript-eslint/types" "5.25.0"
1192+
"@typescript-eslint/typescript-estree" "5.25.0"
12011193
eslint-scope "^5.1.1"
12021194
eslint-utils "^3.0.0"
12031195

1204-
"@typescript-eslint/visitor-keys@5.24.0":
1205-
version "5.24.0"
1206-
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.24.0.tgz#bb3e9a788ccd50c53e03557e4e203599880c3123"
1207-
integrity sha512-qzGwSXMyMnogcAo+/2fU+jhlPPVMXlIH2PeAonIKjJSoDKl1+lJVvG5Z5Oud36yU0TWK2cs1p/FaSN5J2OUFYA==
1196+
"@typescript-eslint/visitor-keys@5.25.0":
1197+
version "5.25.0"
1198+
resolved "https://registry.yarnpkg.com/@typescript-eslint/visitor-keys/-/visitor-keys-5.25.0.tgz#33aa5fdcc5cedb9f4c8828c6a019d58548d4474b"
1199+
integrity sha512-yd26vFgMsC4h2dgX4+LR+GeicSKIfUvZREFLf3DDjZPtqgLx5AJZr6TetMNwFP9hcKreTTeztQYBTNbNoOycwA==
12081200
dependencies:
1209-
"@typescript-eslint/types" "5.24.0"
1201+
"@typescript-eslint/types" "5.25.0"
12101202
eslint-visitor-keys "^3.3.0"
12111203

12121204
JSONStream@^1.0.4:
@@ -2024,9 +2016,9 @@ error-ex@^1.3.1:
20242016
is-arrayish "^0.2.1"
20252017

20262018
es-abstract@^1.19.0, es-abstract@^1.19.1, es-abstract@^1.19.2, es-abstract@^1.19.5:
2027-
version "1.20.0"
2028-
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.20.0.tgz#b2d526489cceca004588296334726329e0a6bfb6"
2029-
integrity sha512-URbD8tgRthKD3YcC39vbvSDrX23upXnPcnGAjQfgxXF5ID75YcENawc9ZX/9iTP9ptUyfCLIxTTuMYoRfiOVKA==
2019+
version "1.20.1"
2020+
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.20.1.tgz#027292cd6ef44bd12b1913b828116f54787d1814"
2021+
integrity sha512-WEm2oBhfoI2sImeM4OF2zE2V3BYdSF+KnSi9Sidz51fQHd7+JuF8Xgcj9/0o+OWeIeIS/MiuNnlruQrJf16GQA==
20302022
dependencies:
20312023
call-bind "^1.0.2"
20322024
es-to-primitive "^1.2.1"
@@ -2047,7 +2039,7 @@ es-abstract@^1.19.0, es-abstract@^1.19.1, es-abstract@^1.19.2, es-abstract@^1.19
20472039
object-inspect "^1.12.0"
20482040
object-keys "^1.1.1"
20492041
object.assign "^4.1.2"
2050-
regexp.prototype.flags "^1.4.1"
2042+
regexp.prototype.flags "^1.4.3"
20512043
string.prototype.trimend "^1.0.5"
20522044
string.prototype.trimstart "^1.0.5"
20532045
unbox-primitive "^1.0.2"
@@ -3658,18 +3650,18 @@ lowercase-keys@^2.0.0:
36583650
resolved "https://registry.yarnpkg.com/lowercase-keys/-/lowercase-keys-2.0.0.tgz#2603e78b7b4b0006cbca2fbcc8a3202558ac9479"
36593651
integrity sha512-tqNXrS78oMOE73NMxK4EMLQsQowWf8jKooH9g7xPavRT706R6bkQJ6DY2Te7QukaZsulxa30wQ7bk0pm4XiHmA==
36603652

3661-
lru-cache@*, lru-cache@^7.4.0:
3662-
version "7.10.1"
3663-
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.10.1.tgz#db577f42a94c168f676b638d15da8fb073448cab"
3664-
integrity sha512-BQuhQxPuRl79J5zSXRP+uNzPOyZw2oFI9JLRQ80XswSvg21KMKNtQza9eF42rfI/3Z40RvzBdXgziEkudzjo8A==
3665-
36663653
lru-cache@^6.0.0:
36673654
version "6.0.0"
36683655
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-6.0.0.tgz#6d6fe6570ebd96aaf90fcad1dafa3b2566db3a94"
36693656
integrity sha512-Jo6dJ04CmSjuznwJSS3pUeWmd/H0ffTlkXXgwZi+eq1UCmqQwCh+eLsYOYCwY991i2Fah4h1BEMCx4qThGbsiA==
36703657
dependencies:
36713658
yallist "^4.0.0"
36723659

3660+
lru-cache@^7.4.0:
3661+
version "7.10.1"
3662+
resolved "https://registry.yarnpkg.com/lru-cache/-/lru-cache-7.10.1.tgz#db577f42a94c168f676b638d15da8fb073448cab"
3663+
integrity sha512-BQuhQxPuRl79J5zSXRP+uNzPOyZw2oFI9JLRQ80XswSvg21KMKNtQza9eF42rfI/3Z40RvzBdXgziEkudzjo8A==
3664+
36733665
make-dir@^3.0.0:
36743666
version "3.1.0"
36753667
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
@@ -4315,7 +4307,7 @@ regexp-tree@^0.1.24, regexp-tree@~0.1.1:
43154307
resolved "https://registry.yarnpkg.com/regexp-tree/-/regexp-tree-0.1.24.tgz#3d6fa238450a4d66e5bc9c4c14bb720e2196829d"
43164308
integrity sha512-s2aEVuLhvnVJW6s/iPgEGK6R+/xngd2jNQ+xy4bXNDKxZKJH6jpPHY6kVeVv1IeLCHgswRj+Kl3ELaDjG6V1iw==
43174309

4318-
regexp.prototype.flags@^1.4.1:
4310+
regexp.prototype.flags@^1.4.3:
43194311
version "1.4.3"
43204312
resolved "https://registry.yarnpkg.com/regexp.prototype.flags/-/regexp.prototype.flags-1.4.3.tgz#87cab30f80f66660181a3bb7bf5981a872b367ac"
43214313
integrity sha512-fjggEOO3slI6Wvgjwflkc4NFRCTZAu5CnNfBd5qOMYhWdn67nJBBu34/TkD++eeFmd8C9r9jfXJ27+nSiRkSUA==

0 commit comments

Comments
 (0)