@naturalcycles/mysql-lib 4.9.0 → 4.11.0
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
CHANGED
package/dist/mysql.db.js
CHANGED
|
@@ -3,7 +3,7 @@ import { promisify } from 'node:util';
|
|
|
3
3
|
import { BaseCommonDB, commonDBFullSupport, CommonDBType, DBQuery } from '@naturalcycles/db-lib';
|
|
4
4
|
import { _Memo } from '@naturalcycles/js-lib/decorators/memo.decorator.js';
|
|
5
5
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
6
|
-
import {
|
|
6
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
7
7
|
import { _filterUndefinedValues, _mapKeys, _mapValues, _omit } from '@naturalcycles/js-lib/object';
|
|
8
8
|
import { white } from '@naturalcycles/nodejs-lib/colors';
|
|
9
9
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
@@ -38,6 +38,7 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
38
38
|
updateSaveMethod: false, // todo: can be implemented
|
|
39
39
|
transactions: false, // todo: can be implemented
|
|
40
40
|
increment: false, // todo: can be implemented
|
|
41
|
+
timeMachine: false,
|
|
41
42
|
};
|
|
42
43
|
constructor(cfg = {}) {
|
|
43
44
|
super();
|
|
@@ -49,7 +50,7 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
49
50
|
// password: MYSQL_PW,
|
|
50
51
|
// database: MYSQL_DB,
|
|
51
52
|
...cfg,
|
|
52
|
-
logger:
|
|
53
|
+
logger: (cfg.logger || consoleLogger).child({ module: 'mysql' }),
|
|
53
54
|
};
|
|
54
55
|
}
|
|
55
56
|
cfg;
|
|
@@ -108,7 +109,7 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
108
109
|
con.on('end', () => this.cfg.logger.log(`createSingleConnection(${threadId}).end`));
|
|
109
110
|
}
|
|
110
111
|
con.on('error', err => {
|
|
111
|
-
this.cfg.logger.error(`createSingleConnection(${threadId}).error`, err);
|
|
112
|
+
this.cfg.logger.error({ msg: `createSingleConnection(${threadId}).error`, err });
|
|
112
113
|
});
|
|
113
114
|
return con;
|
|
114
115
|
}
|
|
@@ -140,8 +141,9 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
140
141
|
};
|
|
141
142
|
}
|
|
142
143
|
async runSQL(q) {
|
|
143
|
-
if (this.cfg.logSQL)
|
|
144
|
-
this.cfg.logger.log(
|
|
144
|
+
if (this.cfg.logSQL) {
|
|
145
|
+
this.cfg.logger.log(_filterUndefinedValues({ msg: q.sql, values: q.values }));
|
|
146
|
+
}
|
|
145
147
|
return await new Promise((resolve, reject) => {
|
|
146
148
|
this.pool().query(q, (err, res) => {
|
|
147
149
|
if (err)
|
package/dist/query.util.js
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { _hb } from '@naturalcycles/js-lib';
|
|
2
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
2
3
|
import { white, yellow } from '@naturalcycles/nodejs-lib/colors';
|
|
3
4
|
import * as mysql from 'mysql';
|
|
4
5
|
import { mapNameToMySQL } from './schema/mysql.schema.util.js';
|
|
@@ -38,7 +39,7 @@ export function dbQueryToSQLDelete(q) {
|
|
|
38
39
|
/**
|
|
39
40
|
* Returns array of sql statements to respect the max sql size.
|
|
40
41
|
*/
|
|
41
|
-
export function insertSQL(table, rows, verb = 'INSERT', logger =
|
|
42
|
+
export function insertSQL(table, rows, verb = 'INSERT', logger = consoleLogger) {
|
|
42
43
|
// INSERT INTO table_name (column1, column2, column3, ...)
|
|
43
44
|
// VALUES (value1, value2, value3, ...);
|
|
44
45
|
// oxlint-disable-next-line unicorn/no-array-reduce
|
|
@@ -83,7 +83,7 @@ export function mysqlTableStatsToJsonSchemaField(table, stats, logger) {
|
|
|
83
83
|
s.properties[name] = { type: 'number' };
|
|
84
84
|
}
|
|
85
85
|
else {
|
|
86
|
-
logger.log(s);
|
|
86
|
+
logger.log({ schema: s });
|
|
87
87
|
throw new Error(`Unknown mysql field type ${name} ${stat.Type}`);
|
|
88
88
|
}
|
|
89
89
|
});
|
package/package.json
CHANGED
|
@@ -10,9 +10,9 @@
|
|
|
10
10
|
"tslib": "^2"
|
|
11
11
|
},
|
|
12
12
|
"devDependencies": {
|
|
13
|
-
"@
|
|
14
|
-
"@
|
|
15
|
-
"
|
|
13
|
+
"@naturalcycles/dev-lib": "20.66.0",
|
|
14
|
+
"@types/node": "^26",
|
|
15
|
+
"typescript": "^7"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": "./dist/index.js"
|
|
@@ -36,9 +36,9 @@
|
|
|
36
36
|
"directory": "packages/mysql-lib"
|
|
37
37
|
},
|
|
38
38
|
"engines": {
|
|
39
|
-
"node": ">=
|
|
39
|
+
"node": ">=26.12.1"
|
|
40
40
|
},
|
|
41
|
-
"version": "4.
|
|
41
|
+
"version": "4.11.0",
|
|
42
42
|
"description": "MySQL client implementing CommonDB interface",
|
|
43
43
|
"keywords": [
|
|
44
44
|
"mysql",
|
package/src/mysql.db.ts
CHANGED
|
@@ -11,7 +11,7 @@ import { BaseCommonDB, commonDBFullSupport, CommonDBType, DBQuery } from '@natur
|
|
|
11
11
|
import { _Memo } from '@naturalcycles/js-lib/decorators/memo.decorator.js'
|
|
12
12
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js'
|
|
13
13
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
14
|
-
import {
|
|
14
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
15
15
|
import { _filterUndefinedValues, _mapKeys, _mapValues, _omit } from '@naturalcycles/js-lib/object'
|
|
16
16
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types'
|
|
17
17
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
@@ -58,7 +58,7 @@ export interface MysqlDBCfg extends PoolConfig {
|
|
|
58
58
|
debugConnections?: boolean
|
|
59
59
|
|
|
60
60
|
/**
|
|
61
|
-
* Default to `
|
|
61
|
+
* Default to `consoleLogger`
|
|
62
62
|
*/
|
|
63
63
|
logger?: CommonLogger
|
|
64
64
|
}
|
|
@@ -94,6 +94,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
94
94
|
updateSaveMethod: false, // todo: can be implemented
|
|
95
95
|
transactions: false, // todo: can be implemented
|
|
96
96
|
increment: false, // todo: can be implemented
|
|
97
|
+
timeMachine: false,
|
|
97
98
|
}
|
|
98
99
|
|
|
99
100
|
constructor(cfg: MysqlDBCfg = {}) {
|
|
@@ -106,7 +107,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
106
107
|
// password: MYSQL_PW,
|
|
107
108
|
// database: MYSQL_DB,
|
|
108
109
|
...cfg,
|
|
109
|
-
logger:
|
|
110
|
+
logger: (cfg.logger || consoleLogger).child({ module: 'mysql' }),
|
|
110
111
|
}
|
|
111
112
|
}
|
|
112
113
|
|
|
@@ -182,7 +183,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
182
183
|
}
|
|
183
184
|
|
|
184
185
|
con.on('error', err => {
|
|
185
|
-
this.cfg.logger.error(`createSingleConnection(${threadId}).error`, err)
|
|
186
|
+
this.cfg.logger.error({ msg: `createSingleConnection(${threadId}).error`, err })
|
|
186
187
|
})
|
|
187
188
|
|
|
188
189
|
return con
|
|
@@ -232,7 +233,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
232
233
|
}
|
|
233
234
|
|
|
234
235
|
async runSQL<RESULT>(q: QueryOptions): Promise<RESULT> {
|
|
235
|
-
if (this.cfg.logSQL)
|
|
236
|
+
if (this.cfg.logSQL) {
|
|
237
|
+
this.cfg.logger.log(_filterUndefinedValues({ msg: q.sql, values: q.values }))
|
|
238
|
+
}
|
|
236
239
|
|
|
237
240
|
return await new Promise<RESULT>((resolve, reject) => {
|
|
238
241
|
this.pool().query(q, (err, res) => {
|
package/src/query.util.ts
CHANGED
|
@@ -1,5 +1,6 @@
|
|
|
1
1
|
import type { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
|
|
2
2
|
import { _hb } from '@naturalcycles/js-lib'
|
|
3
|
+
import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
3
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
4
5
|
import type { AnyObjectWithId } from '@naturalcycles/js-lib/types'
|
|
5
6
|
import { white, yellow } from '@naturalcycles/nodejs-lib/colors'
|
|
@@ -55,7 +56,7 @@ export function insertSQL(
|
|
|
55
56
|
table: string,
|
|
56
57
|
rows: Record<any, any>[],
|
|
57
58
|
verb: 'INSERT' | 'REPLACE' = 'INSERT',
|
|
58
|
-
logger: CommonLogger =
|
|
59
|
+
logger: CommonLogger = consoleLogger,
|
|
59
60
|
): string[] {
|
|
60
61
|
// INSERT INTO table_name (column1, column2, column3, ...)
|
|
61
62
|
// VALUES (value1, value2, value3, ...);
|
|
@@ -96,17 +96,17 @@ export function mysqlTableStatsToJsonSchemaField<T extends AnyObject = any>(
|
|
|
96
96
|
}
|
|
97
97
|
|
|
98
98
|
if (t.includes('text') || t.includes('char')) {
|
|
99
|
-
s.properties![name] = { type: 'string' }
|
|
99
|
+
s.properties![name] = { type: 'string' }
|
|
100
100
|
} else if (t.includes('lob')) {
|
|
101
101
|
s.properties![name] = { instanceof: 'Buffer' }
|
|
102
102
|
} else if (t.startsWith('tinyint') || t.includes('(1)')) {
|
|
103
|
-
s.properties![name] = { type: 'boolean' }
|
|
103
|
+
s.properties![name] = { type: 'boolean' }
|
|
104
104
|
} else if (t === 'int' || t.startsWith('int(')) {
|
|
105
|
-
s.properties![name] = { type: 'integer' }
|
|
105
|
+
s.properties![name] = { type: 'integer' }
|
|
106
106
|
} else if (t.startsWith('float')) {
|
|
107
|
-
s.properties![name] = { type: 'number' }
|
|
107
|
+
s.properties![name] = { type: 'number' }
|
|
108
108
|
} else {
|
|
109
|
-
logger.log(s)
|
|
109
|
+
logger.log({ schema: s })
|
|
110
110
|
throw new Error(`Unknown mysql field type ${name as string} ${stat.Type}`)
|
|
111
111
|
}
|
|
112
112
|
})
|