@naturalcycles/mysql-lib 3.11.1 → 3.11.2
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.d.ts +2 -2
- package/dist/mysql.db.js +2 -2
- package/package.json +1 -1
- package/src/mysql.db.ts +9 -4
package/dist/mysql.db.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
|
4
4
|
import { Connection, Pool, PoolConfig, PoolConnection, QueryOptions } from 'mysql';
|
|
5
5
|
export interface MysqlDBOptions extends CommonDBOptions {
|
|
6
6
|
}
|
|
7
|
-
export interface MysqlDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
7
|
+
export interface MysqlDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* @default false / undefined
|
|
@@ -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 ObjectWithId
|
|
53
|
+
saveBatch<ROW extends Partial<ObjectWithId>>(table: string, _rows: 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,7 +171,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
171
171
|
}));
|
|
172
172
|
}
|
|
173
173
|
// SAVE
|
|
174
|
-
async saveBatch(table, _rows,
|
|
174
|
+
async saveBatch(table, _rows, opt = {}) {
|
|
175
175
|
if (!_rows.length)
|
|
176
176
|
return;
|
|
177
177
|
// Stringify object values
|
|
@@ -179,7 +179,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
179
179
|
return v && typeof v === 'object' && !Buffer.isBuffer(v) ? JSON.stringify(v) : v;
|
|
180
180
|
}));
|
|
181
181
|
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
|
|
182
|
-
const sqls = (0, query_util_1.insertSQL)(table, rows, 'REPLACE', this.cfg.logger);
|
|
182
|
+
const sqls = (0, query_util_1.insertSQL)(table, rows, opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE', this.cfg.logger);
|
|
183
183
|
for await (const sql of sqls) {
|
|
184
184
|
await this.runSQL({ sql });
|
|
185
185
|
}
|
package/package.json
CHANGED
package/src/mysql.db.ts
CHANGED
|
@@ -34,7 +34,7 @@ import {
|
|
|
34
34
|
} from './schema/mysql.schema.util'
|
|
35
35
|
|
|
36
36
|
export interface MysqlDBOptions extends CommonDBOptions {}
|
|
37
|
-
export interface MysqlDBSaveOptions<ROW extends ObjectWithId = AnyObjectWithId>
|
|
37
|
+
export interface MysqlDBSaveOptions<ROW extends Partial<ObjectWithId> = AnyObjectWithId>
|
|
38
38
|
extends CommonDBSaveOptions<ROW> {}
|
|
39
39
|
|
|
40
40
|
/**
|
|
@@ -264,10 +264,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
264
264
|
}
|
|
265
265
|
|
|
266
266
|
// SAVE
|
|
267
|
-
override async saveBatch<ROW extends ObjectWithId
|
|
267
|
+
override async saveBatch<ROW extends Partial<ObjectWithId>>(
|
|
268
268
|
table: string,
|
|
269
269
|
_rows: ROW[],
|
|
270
|
-
|
|
270
|
+
opt: MysqlDBSaveOptions<ROW> = {},
|
|
271
271
|
): Promise<void> {
|
|
272
272
|
if (!_rows.length) return
|
|
273
273
|
|
|
@@ -279,7 +279,12 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
279
279
|
)
|
|
280
280
|
|
|
281
281
|
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
|
|
282
|
-
const sqls = insertSQL(
|
|
282
|
+
const sqls = insertSQL(
|
|
283
|
+
table,
|
|
284
|
+
rows,
|
|
285
|
+
opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE',
|
|
286
|
+
this.cfg.logger,
|
|
287
|
+
)
|
|
283
288
|
|
|
284
289
|
for await (const sql of sqls) {
|
|
285
290
|
await this.runSQL({ sql })
|