@naturalcycles/mysql-lib 4.8.1 → 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.
@@ -22,7 +22,7 @@ export interface MysqlDBCfg extends PoolConfig {
22
22
  */
23
23
  debugConnections?: boolean;
24
24
  /**
25
- * Default to `console`
25
+ * Default to `consoleLogger`
26
26
  */
27
27
  logger?: CommonLogger;
28
28
  }
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 { commonLoggerPrefix } from '@naturalcycles/js-lib/log';
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: commonLoggerPrefix(cfg.logger || console, '[mysql]'),
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(...[q.sql, q.values].filter(Boolean));
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)
@@ -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 = console) {
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
@@ -48,12 +49,12 @@ export function insertSQL(table, rows, verb = 'INSERT', logger = console) {
48
49
  }
49
50
  return set;
50
51
  }, new Set());
51
- const fields = [...fieldSet];
52
+ const fields = Array.from(fieldSet);
52
53
  const start = [
53
54
  verb,
54
55
  `INTO`,
55
56
  mysql.escapeId(table),
56
- `(` + [...fields].map(f => mysql.escapeId(mapNameToMySQL(f))).join(',') + `)`,
57
+ `(` + fields.map(f => mysql.escapeId(mapNameToMySQL(f))).join(',') + `)`,
57
58
  `VALUES\n`,
58
59
  ].join(' ');
59
60
  const valueRows = rows.map(rec => {
@@ -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
- "@types/node": "^24",
14
- "dotenv": "^17",
15
- "@naturalcycles/dev-lib": "18.4.2"
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.8.1",
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 { commonLoggerPrefix } from '@naturalcycles/js-lib/log'
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 `console`
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: commonLoggerPrefix(cfg.logger || console, '[mysql]'),
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) this.cfg.logger.log(...[q.sql, q.values].filter(Boolean))
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 = console,
59
+ logger: CommonLogger = consoleLogger,
59
60
  ): string[] {
60
61
  // INSERT INTO table_name (column1, column2, column3, ...)
61
62
  // VALUES (value1, value2, value3, ...);
@@ -67,13 +68,13 @@ export function insertSQL(
67
68
  }
68
69
  return set
69
70
  }, new Set<string>())
70
- const fields = [...fieldSet]
71
+ const fields = Array.from(fieldSet)
71
72
 
72
73
  const start = [
73
74
  verb,
74
75
  `INTO`,
75
76
  mysql.escapeId(table),
76
- `(` + [...fields].map(f => mysql.escapeId(mapNameToMySQL(f))).join(',') + `)`,
77
+ `(` + fields.map(f => mysql.escapeId(mapNameToMySQL(f))).join(',') + `)`,
77
78
  `VALUES\n`,
78
79
  ].join(' ')
79
80
 
@@ -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' } as JsonSchema<T[keyof T]>
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' } as JsonSchema<T[keyof T]>
103
+ s.properties![name] = { type: 'boolean' }
104
104
  } else if (t === 'int' || t.startsWith('int(')) {
105
- s.properties![name] = { type: 'integer' } as JsonSchema<T[keyof T]>
105
+ s.properties![name] = { type: 'integer' }
106
106
  } else if (t.startsWith('float')) {
107
- s.properties![name] = { type: 'number' } as JsonSchema<T[keyof T]>
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
  })