@naturalcycles/mysql-lib 3.14.0 → 3.14.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.js +13 -3
- package/dist/query.util.js +2 -2
- package/dist/schema/mysql.schema.util.js +2 -2
- package/package.json +2 -2
- package/src/mysql.db.ts +16 -6
- package/src/query.util.ts +1 -1
- package/src/schema/mysql.schema.util.ts +2 -2
package/dist/mysql.db.js
CHANGED
|
@@ -6,7 +6,7 @@ const node_stream_1 = require("node:stream");
|
|
|
6
6
|
const node_util_1 = require("node:util");
|
|
7
7
|
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
8
8
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
9
|
-
const
|
|
9
|
+
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
10
10
|
const mysql = require("mysql");
|
|
11
11
|
const query_util_1 = require("./query.util");
|
|
12
12
|
const mysql_schema_util_1 = require("./schema/mysql.schema.util");
|
|
@@ -52,7 +52,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
52
52
|
pool() {
|
|
53
53
|
const pool = mysql.createPool(this.cfg);
|
|
54
54
|
const { host, database, logger } = this.cfg;
|
|
55
|
-
logger.log(`connected to ${(0,
|
|
55
|
+
logger.log(`connected to ${(0, nodejs_lib_1.white)(host + '/' + database)}`);
|
|
56
56
|
if (this.cfg.debugConnections) {
|
|
57
57
|
pool.on('acquire', con => {
|
|
58
58
|
logger.log(`acquire(${con.threadId})`);
|
|
@@ -210,6 +210,16 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
210
210
|
}
|
|
211
211
|
return;
|
|
212
212
|
}
|
|
213
|
+
if (opt.saveMethod === 'update') {
|
|
214
|
+
// TODO: This fails if a combination of entities with id and without id are parsed
|
|
215
|
+
for await (const row of rows) {
|
|
216
|
+
// Update already existing
|
|
217
|
+
(0, js_lib_1._assert)(row.id, 'id is required for updating');
|
|
218
|
+
const query = new db_lib_1.DBQuery(table).filterEq('id', row.id);
|
|
219
|
+
await this.updateByQuery(query, (0, js_lib_1._omit)(row, ['id']));
|
|
220
|
+
}
|
|
221
|
+
return;
|
|
222
|
+
}
|
|
213
223
|
const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
|
|
214
224
|
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
|
|
215
225
|
const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
|
|
@@ -271,7 +281,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
271
281
|
return affectedRows;
|
|
272
282
|
}
|
|
273
283
|
}
|
|
284
|
+
exports.MysqlDB = MysqlDB;
|
|
274
285
|
tslib_1.__decorate([
|
|
275
286
|
(0, js_lib_1._Memo)()
|
|
276
287
|
], MysqlDB.prototype, "pool", null);
|
|
277
|
-
exports.MysqlDB = MysqlDB;
|
package/dist/query.util.js
CHANGED
|
@@ -2,7 +2,7 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.dbQueryToSQLUpdate = exports.insertSQLSetSingle = exports.insertSQLSingle = exports.insertSQL = exports.dbQueryToSQLDelete = exports.dbQueryToSQLSelect = void 0;
|
|
4
4
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
|
-
const
|
|
5
|
+
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
6
6
|
const mysql = require("mysql");
|
|
7
7
|
const mysql_schema_util_1 = require("./schema/mysql.schema.util");
|
|
8
8
|
const MAX_PACKET_SIZE = 1024 * 1024; // 1Mb
|
|
@@ -84,7 +84,7 @@ function insertSQL(table, rows, verb = 'INSERT', logger = console) {
|
|
|
84
84
|
if (sql) {
|
|
85
85
|
sqls.push(sql); // last one
|
|
86
86
|
}
|
|
87
|
-
logger.log(`${(0,
|
|
87
|
+
logger.log(`${(0, nodejs_lib_1.white)(table)} large sql query (${(0, nodejs_lib_1.yellow)((0, js_lib_1._hb)(full.length))}) was split into ${(0, nodejs_lib_1.yellow)(sqls.length)} chunks`);
|
|
88
88
|
return sqls;
|
|
89
89
|
// todo: handle "upsert" later
|
|
90
90
|
}
|
|
@@ -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.14.
|
|
39
|
+
"version": "3.14.2",
|
|
40
40
|
"description": "MySQL client implementing CommonDB interface",
|
|
41
41
|
"keywords": [
|
|
42
42
|
"mysql",
|
package/src/mysql.db.ts
CHANGED
|
@@ -25,7 +25,7 @@ import {
|
|
|
25
25
|
ObjectWithId,
|
|
26
26
|
} from '@naturalcycles/js-lib'
|
|
27
27
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
28
|
-
import { white } from '@naturalcycles/nodejs-lib
|
|
28
|
+
import { white } from '@naturalcycles/nodejs-lib'
|
|
29
29
|
import {
|
|
30
30
|
Connection,
|
|
31
31
|
OkPacket,
|
|
@@ -193,7 +193,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
193
193
|
if (!ids.length) return []
|
|
194
194
|
const q = new DBQuery<ROW>(table).filterEq('id', ids)
|
|
195
195
|
const { rows } = await this.runQuery(q, opt)
|
|
196
|
-
return rows.map(r => _mapKeys(r, k => mapNameFromMySQL(k)) as any)
|
|
196
|
+
return rows.map(r => _mapKeys(r, k => mapNameFromMySQL(k as string)) as any)
|
|
197
197
|
}
|
|
198
198
|
|
|
199
199
|
// QUERY
|
|
@@ -209,13 +209,13 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
209
209
|
}
|
|
210
210
|
|
|
211
211
|
const rows = (await this.runSQL<ROW[]>({ sql })).map(
|
|
212
|
-
row => _mapKeys(_filterUndefinedValues(row, true), k => mapNameFromMySQL(k)) as any,
|
|
212
|
+
row => _mapKeys(_filterUndefinedValues(row, true), k => mapNameFromMySQL(k as string)) as any,
|
|
213
213
|
)
|
|
214
214
|
|
|
215
215
|
// edge case where 0 fields are selected
|
|
216
216
|
if (q._selectedFieldNames?.length === 0) {
|
|
217
217
|
return {
|
|
218
|
-
rows: rows.map(_ => ({} as any)
|
|
218
|
+
rows: rows.map(_ => ({}) as any),
|
|
219
219
|
}
|
|
220
220
|
}
|
|
221
221
|
|
|
@@ -293,7 +293,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
293
293
|
|
|
294
294
|
// Stringify object values
|
|
295
295
|
const rows = rowsInput.map(row =>
|
|
296
|
-
_mapValues(row, (_k, v) => {
|
|
296
|
+
_mapValues<ROW>(row, (_k, v) => {
|
|
297
297
|
if (v && typeof v === 'object' && !Buffer.isBuffer(v)) {
|
|
298
298
|
// This is to avoid implicit Date stringification and mismatch: it gets saved as Date, but loaded as String
|
|
299
299
|
_assert(
|
|
@@ -329,8 +329,18 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
329
329
|
return
|
|
330
330
|
}
|
|
331
331
|
|
|
332
|
-
|
|
332
|
+
if (opt.saveMethod === 'update') {
|
|
333
|
+
// TODO: This fails if a combination of entities with id and without id are parsed
|
|
334
|
+
for await (const row of rows) {
|
|
335
|
+
// Update already existing
|
|
336
|
+
_assert(row.id, 'id is required for updating')
|
|
337
|
+
const query = new DBQuery(table).filterEq('id', row.id)
|
|
338
|
+
await this.updateByQuery(query, _omit(row, ['id']))
|
|
339
|
+
}
|
|
340
|
+
return
|
|
341
|
+
}
|
|
333
342
|
|
|
343
|
+
const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE'
|
|
334
344
|
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
|
|
335
345
|
const sqls = insertSQL(table, rows, verb, this.cfg.logger)
|
|
336
346
|
|
package/src/query.util.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
|
|
2
2
|
import { _hb, CommonLogger } from '@naturalcycles/js-lib'
|
|
3
|
-
import { white, yellow } from '@naturalcycles/nodejs-lib
|
|
3
|
+
import { white, yellow } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { QueryOptions } from 'mysql'
|
|
5
5
|
import * as mysql from 'mysql'
|
|
6
6
|
import { mapNameToMySQL } from './schema/mysql.schema.util'
|
|
@@ -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
|
}
|