@naturalcycles/mysql-lib 4.9.0 → 4.10.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';
|
|
@@ -49,7 +49,7 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
49
49
|
// password: MYSQL_PW,
|
|
50
50
|
// database: MYSQL_DB,
|
|
51
51
|
...cfg,
|
|
52
|
-
logger:
|
|
52
|
+
logger: (cfg.logger || consoleLogger).child({ module: 'mysql' }),
|
|
53
53
|
};
|
|
54
54
|
}
|
|
55
55
|
cfg;
|
|
@@ -108,7 +108,7 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
108
108
|
con.on('end', () => this.cfg.logger.log(`createSingleConnection(${threadId}).end`));
|
|
109
109
|
}
|
|
110
110
|
con.on('error', err => {
|
|
111
|
-
this.cfg.logger.error(`createSingleConnection(${threadId}).error`, err);
|
|
111
|
+
this.cfg.logger.error({ msg: `createSingleConnection(${threadId}).error`, err });
|
|
112
112
|
});
|
|
113
113
|
return con;
|
|
114
114
|
}
|
|
@@ -140,8 +140,9 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
140
140
|
};
|
|
141
141
|
}
|
|
142
142
|
async runSQL(q) {
|
|
143
|
-
if (this.cfg.logSQL)
|
|
144
|
-
this.cfg.logger.log(
|
|
143
|
+
if (this.cfg.logSQL) {
|
|
144
|
+
this.cfg.logger.log(_filterUndefinedValues({ msg: q.sql, values: q.values }));
|
|
145
|
+
}
|
|
145
146
|
return await new Promise((resolve, reject) => {
|
|
146
147
|
this.pool().query(q, (err, res) => {
|
|
147
148
|
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.64.0",
|
|
14
|
+
"@types/node": "^26",
|
|
15
|
+
"typescript": "^7"
|
|
16
16
|
},
|
|
17
17
|
"exports": {
|
|
18
18
|
".": "./dist/index.js"
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"engines": {
|
|
39
39
|
"node": ">=24.10.0"
|
|
40
40
|
},
|
|
41
|
-
"version": "4.
|
|
41
|
+
"version": "4.10.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
|
}
|
|
@@ -106,7 +106,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
106
106
|
// password: MYSQL_PW,
|
|
107
107
|
// database: MYSQL_DB,
|
|
108
108
|
...cfg,
|
|
109
|
-
logger:
|
|
109
|
+
logger: (cfg.logger || consoleLogger).child({ module: 'mysql' }),
|
|
110
110
|
}
|
|
111
111
|
}
|
|
112
112
|
|
|
@@ -182,7 +182,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
182
182
|
}
|
|
183
183
|
|
|
184
184
|
con.on('error', err => {
|
|
185
|
-
this.cfg.logger.error(`createSingleConnection(${threadId}).error`, err)
|
|
185
|
+
this.cfg.logger.error({ msg: `createSingleConnection(${threadId}).error`, err })
|
|
186
186
|
})
|
|
187
187
|
|
|
188
188
|
return con
|
|
@@ -232,7 +232,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
232
232
|
}
|
|
233
233
|
|
|
234
234
|
async runSQL<RESULT>(q: QueryOptions): Promise<RESULT> {
|
|
235
|
-
if (this.cfg.logSQL)
|
|
235
|
+
if (this.cfg.logSQL) {
|
|
236
|
+
this.cfg.logger.log(_filterUndefinedValues({ msg: q.sql, values: q.values }))
|
|
237
|
+
}
|
|
236
238
|
|
|
237
239
|
return await new Promise<RESULT>((resolve, reject) => {
|
|
238
240
|
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
|
})
|