@naturalcycles/mysql-lib 4.13.0 → 5.0.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 +6 -2
- package/dist/mysql.db.js +22 -22
- package/dist/mysqlKeyValueDB.d.ts +1 -1
- package/dist/mysqlKeyValueDB.js +5 -12
- package/dist/query.util.d.ts +1 -1
- package/dist/query.util.js +1 -1
- package/dist/schema/mysql.schema.util.js +1 -1
- package/package.json +3 -4
- package/src/mysql.db.ts +33 -33
- package/src/mysqlKeyValueDB.ts +7 -12
- package/src/query.util.ts +2 -2
- package/src/schema/mysql.schema.util.ts +1 -1
package/dist/mysql.db.d.ts
CHANGED
|
@@ -4,7 +4,7 @@ import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
|
4
4
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types';
|
|
5
5
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv';
|
|
6
6
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
7
|
-
import type { Connection, Pool,
|
|
7
|
+
import type { Connection, Pool, PoolConnection, PoolOptions, QueryOptions } from 'mysql2/promise';
|
|
8
8
|
export interface MysqlDBOptions extends CommonDBOptions {
|
|
9
9
|
}
|
|
10
10
|
export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
@@ -12,7 +12,7 @@ export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSa
|
|
|
12
12
|
/**
|
|
13
13
|
* @default false / undefined
|
|
14
14
|
*/
|
|
15
|
-
export interface MysqlDBCfg extends
|
|
15
|
+
export interface MysqlDBCfg extends PoolOptions {
|
|
16
16
|
/**
|
|
17
17
|
* If true - will log all produced SQL queries.
|
|
18
18
|
*/
|
|
@@ -55,6 +55,10 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
55
55
|
runSQLString(s: string): Promise<void>;
|
|
56
56
|
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
57
57
|
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Pipeline<ROW>;
|
|
58
|
+
/**
|
|
59
|
+
* Streaming is not part of the mysql2 promise API, so it goes through the underlying callback pool.
|
|
60
|
+
*/
|
|
61
|
+
streamSQL<ROW>(sql: string): Pipeline<ROW>;
|
|
58
62
|
saveBatch<ROW extends ObjectWithId>(table: string, rowsInput: readonly ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
|
|
59
63
|
/**
|
|
60
64
|
* Limitation: always returns [], regardless of which rows are actually deleted
|
package/dist/mysql.db.js
CHANGED
|
@@ -1,5 +1,4 @@
|
|
|
1
1
|
import { __decorate } from "tslib";
|
|
2
|
-
import { promisify } from 'node:util';
|
|
3
2
|
import { BaseCommonDB, commonDBFullSupport, CommonDBType, DBQuery } from '@naturalcycles/db-lib';
|
|
4
3
|
import { _Memo } from '@naturalcycles/js-lib/decorators/memo.decorator.js';
|
|
5
4
|
import { _assert } from '@naturalcycles/js-lib/error/assert.js';
|
|
@@ -7,7 +6,7 @@ import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
|
7
6
|
import { _filterUndefinedValues, _mapKeys, _mapValues, _omit } from '@naturalcycles/js-lib/object';
|
|
8
7
|
import { white } from '@naturalcycles/nodejs-lib/colors';
|
|
9
8
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
10
|
-
import * as mysql from '
|
|
9
|
+
import * as mysql from 'mysql2/promise';
|
|
11
10
|
import { dbQueryToSQLDelete, dbQueryToSQLSelect, dbQueryToSQLUpdate, insertSQL, } from './query.util.js';
|
|
12
11
|
import { jsonSchemaToMySQLDDL, mapNameFromMySQL, mysqlTableStatsToJsonSchemaField, } from './schema/mysql.schema.util.js';
|
|
13
12
|
const BOOLEAN_TYPES = new Set(['TINY', 'TINYINT', 'INT']);
|
|
@@ -44,6 +43,11 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
44
43
|
super();
|
|
45
44
|
this.cfg = {
|
|
46
45
|
typeCast,
|
|
46
|
+
// mysql2 defaults that differ from the `mysql` package, set back to what `mysql` did:
|
|
47
|
+
// utf8 (not utf8mb4) connection charset, JSON columns as strings, DECIMAL as numbers
|
|
48
|
+
charset: 'UTF8_GENERAL_CI',
|
|
49
|
+
jsonStrings: true,
|
|
50
|
+
decimalNumbers: true,
|
|
47
51
|
// charset: 'utf8mb4', // for emoji support
|
|
48
52
|
// host: 'localhost',
|
|
49
53
|
// user: MYSQL_USER,
|
|
@@ -59,7 +63,8 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
59
63
|
con.release();
|
|
60
64
|
}
|
|
61
65
|
pool() {
|
|
62
|
-
|
|
66
|
+
// mysql2 warns on options it doesn't know, so our own ones are left out
|
|
67
|
+
const pool = mysql.createPool(_omit(this.cfg, ['logSQL', 'debugConnections', 'logger']));
|
|
63
68
|
const { host, database, logger } = this.cfg;
|
|
64
69
|
logger.log(`connected to ${white(host + '/' + database)}`);
|
|
65
70
|
if (this.cfg.debugConnections) {
|
|
@@ -76,30 +81,24 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
76
81
|
logger.log(`release(${con.threadId})`);
|
|
77
82
|
});
|
|
78
83
|
}
|
|
79
|
-
pool.on('error', err => {
|
|
80
|
-
logger.error(err);
|
|
81
|
-
});
|
|
82
84
|
return pool;
|
|
83
85
|
}
|
|
84
86
|
async close() {
|
|
85
|
-
|
|
86
|
-
await promisify(pool.end.bind(pool))();
|
|
87
|
+
await this.pool().end();
|
|
87
88
|
this.cfg.logger.log('closed');
|
|
88
89
|
}
|
|
89
90
|
/**
|
|
90
91
|
* Be careful to always call `con.release()` when you get connection with this method.
|
|
91
92
|
*/
|
|
92
93
|
async getConnection() {
|
|
93
|
-
|
|
94
|
-
return await promisify(pool.getConnection.bind(pool))();
|
|
94
|
+
return await this.pool().getConnection();
|
|
95
95
|
}
|
|
96
96
|
/**
|
|
97
97
|
* Manually create a single (not pool) connection.
|
|
98
98
|
* Be careful to manage this connection yourself (open, release, etc).
|
|
99
99
|
*/
|
|
100
100
|
async createSingleConnection() {
|
|
101
|
-
const con = mysql.createConnection(this.cfg);
|
|
102
|
-
await promisify(con.connect.bind(con))();
|
|
101
|
+
const con = await mysql.createConnection(_omit(this.cfg, ['logSQL', 'debugConnections', 'logger']));
|
|
103
102
|
const { threadId } = con;
|
|
104
103
|
if (this.cfg.debugConnections) {
|
|
105
104
|
this.cfg.logger.log(`createSingleConnection(${threadId})`);
|
|
@@ -144,13 +143,8 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
144
143
|
if (this.cfg.logSQL) {
|
|
145
144
|
this.cfg.logger.log(_filterUndefinedValues({ msg: q.sql, values: q.values }));
|
|
146
145
|
}
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
if (err)
|
|
150
|
-
return reject(err);
|
|
151
|
-
resolve(res);
|
|
152
|
-
});
|
|
153
|
-
});
|
|
146
|
+
const [result] = await this.pool().query(q);
|
|
147
|
+
return result;
|
|
154
148
|
}
|
|
155
149
|
/**
|
|
156
150
|
* Allows to run semicolon-separated "SQL file".
|
|
@@ -174,13 +168,19 @@ export class MysqlDB extends BaseCommonDB {
|
|
|
174
168
|
if (!sql) {
|
|
175
169
|
return Pipeline.fromArray([]);
|
|
176
170
|
}
|
|
171
|
+
return this.streamSQL(sql).mapSync(row => _filterUndefinedValues(row, { mutate: true }));
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Streaming is not part of the mysql2 promise API, so it goes through the underlying callback pool.
|
|
175
|
+
*/
|
|
176
|
+
streamSQL(sql) {
|
|
177
177
|
if (this.cfg.logSQL)
|
|
178
178
|
this.cfg.logger.log(`stream: ${sql}`);
|
|
179
|
-
// todo:
|
|
180
|
-
// return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
179
|
+
// todo: mysql2 streams are `node:stream` Readables (unlike `mysql`'s `readable-stream@2`), so this can now use iterable helpers:
|
|
180
|
+
// return (this.pool().pool.query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
181
181
|
// _filterUndefinedValues(row, true),
|
|
182
182
|
// )
|
|
183
|
-
return Pipeline.from(this.pool().query(sql).stream())
|
|
183
|
+
return Pipeline.from(this.pool().pool.query(sql).stream());
|
|
184
184
|
}
|
|
185
185
|
// SAVE
|
|
186
186
|
async saveBatch(table, rowsInput, opt = {}) {
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { CommonDBCreateOptions } from '@naturalcycles/db-lib';
|
|
2
2
|
import type { CommonKeyValueDB, IncrementTuple, KeyValueDBTuple } from '@naturalcycles/db-lib/kv';
|
|
3
|
-
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
3
|
+
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
4
4
|
import type { MysqlDBCfg } from './mysql.db.js';
|
|
5
5
|
import { MysqlDB } from './mysql.db.js';
|
|
6
6
|
export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
package/dist/mysqlKeyValueDB.js
CHANGED
|
@@ -1,7 +1,6 @@
|
|
|
1
1
|
import { commonKeyValueDBFullSupport } from '@naturalcycles/db-lib/kv';
|
|
2
2
|
import { AppError } from '@naturalcycles/js-lib/error/error.util.js';
|
|
3
3
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js';
|
|
4
|
-
import { Pipeline } from '@naturalcycles/nodejs-lib/stream';
|
|
5
4
|
import { MysqlDB } from './mysql.db.js';
|
|
6
5
|
export class MySQLKeyValueDB {
|
|
7
6
|
cfg;
|
|
@@ -44,6 +43,8 @@ export class MySQLKeyValueDB {
|
|
|
44
43
|
await this.db.runSQL({ sql: `DROP TABLE IF EXISTS ${table}` });
|
|
45
44
|
}
|
|
46
45
|
async deleteByIds(table, ids) {
|
|
46
|
+
if (!ids.length)
|
|
47
|
+
return;
|
|
47
48
|
const sql = `DELETE FROM ${table} WHERE id in (${ids.map(id => `"${id}"`).join(',')})`;
|
|
48
49
|
if (this.cfg.logSQL)
|
|
49
50
|
this.db.cfg.logger.log(sql);
|
|
@@ -66,27 +67,19 @@ export class MySQLKeyValueDB {
|
|
|
66
67
|
let sql = `SELECT id FROM ${table}`;
|
|
67
68
|
if (limit)
|
|
68
69
|
sql += ` LIMIT ${limit}`;
|
|
69
|
-
|
|
70
|
-
this.db.cfg.logger.log(`stream: ${sql}`);
|
|
71
|
-
// todo: this is nice, but `mysql` package uses `readable-stream@2` which is not compatible with `node:stream` iterable helpers
|
|
72
|
-
// return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
|
|
73
|
-
return Pipeline.from(this.db.pool().query(sql).stream()).mapSync(r => r.id);
|
|
70
|
+
return this.db.streamSQL(sql).mapSync(r => r.id);
|
|
74
71
|
}
|
|
75
72
|
streamValues(table, limit) {
|
|
76
73
|
let sql = `SELECT v FROM ${table}`;
|
|
77
74
|
if (limit)
|
|
78
75
|
sql += ` LIMIT ${limit}`;
|
|
79
|
-
|
|
80
|
-
this.db.cfg.logger.log(`stream: ${sql}`);
|
|
81
|
-
return Pipeline.from(this.db.pool().query(sql).stream()).mapSync(row => row.v);
|
|
76
|
+
return this.db.streamSQL(sql).mapSync(row => row.v);
|
|
82
77
|
}
|
|
83
78
|
streamEntries(table, limit) {
|
|
84
79
|
let sql = `SELECT id,v FROM ${table}`;
|
|
85
80
|
if (limit)
|
|
86
81
|
sql += ` LIMIT ${limit}`;
|
|
87
|
-
|
|
88
|
-
this.db.cfg.logger.log(`stream: ${sql}`);
|
|
89
|
-
return Pipeline.from(this.db.pool().query(sql).stream()).mapSync(row => [row.id, row.v]);
|
|
82
|
+
return this.db.streamSQL(sql).mapSync(row => [row.id, row.v]);
|
|
90
83
|
}
|
|
91
84
|
async beginTransaction() {
|
|
92
85
|
await this.db.runSQL({ sql: `BEGIN TRANSACTION` });
|
package/dist/query.util.d.ts
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import type { DBQuery } from '@naturalcycles/db-lib';
|
|
2
2
|
import type { CommonLogger } from '@naturalcycles/js-lib/log';
|
|
3
|
-
import type { QueryOptions } from '
|
|
3
|
+
import type { QueryOptions } from 'mysql2/promise';
|
|
4
4
|
/**
|
|
5
5
|
* Returns `null` if it detects that 0 rows will be returned,
|
|
6
6
|
* e.g when `IN ()` (empty array) is used.
|
package/dist/query.util.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { _hb } from '@naturalcycles/js-lib';
|
|
2
2
|
import { consoleLogger } from '@naturalcycles/js-lib/log';
|
|
3
3
|
import { white, yellow } from '@naturalcycles/nodejs-lib/colors';
|
|
4
|
-
import * as mysql from '
|
|
4
|
+
import * as mysql from 'mysql2/promise';
|
|
5
5
|
import { mapNameToMySQL } from './schema/mysql.schema.util.js';
|
|
6
6
|
const MAX_PACKET_SIZE = 1024 * 1024; // 1Mb
|
|
7
7
|
const MAX_ROW_SIZE = 800 * 1024; // 1Mb - margin
|
package/package.json
CHANGED
|
@@ -5,12 +5,11 @@
|
|
|
5
5
|
"@naturalcycles/db-lib": "^10",
|
|
6
6
|
"@naturalcycles/js-lib": "^15",
|
|
7
7
|
"@naturalcycles/nodejs-lib": "^15",
|
|
8
|
-
"
|
|
9
|
-
"mysql": "^2",
|
|
8
|
+
"mysql2": "^3",
|
|
10
9
|
"tslib": "^2"
|
|
11
10
|
},
|
|
12
11
|
"devDependencies": {
|
|
13
|
-
"@naturalcycles/dev-lib": "
|
|
12
|
+
"@naturalcycles/dev-lib": "0.0.0",
|
|
14
13
|
"@types/node": "^26",
|
|
15
14
|
"typescript": "^7"
|
|
16
15
|
},
|
|
@@ -38,7 +37,7 @@
|
|
|
38
37
|
"engines": {
|
|
39
38
|
"node": ">=26.12.1"
|
|
40
39
|
},
|
|
41
|
-
"version": "
|
|
40
|
+
"version": "5.0.0",
|
|
42
41
|
"description": "MySQL client implementing CommonDB interface",
|
|
43
42
|
"keywords": [
|
|
44
43
|
"mysql",
|
package/src/mysql.db.ts
CHANGED
|
@@ -1,4 +1,3 @@
|
|
|
1
|
-
import { promisify } from 'node:util'
|
|
2
1
|
import type {
|
|
3
2
|
CommonDB,
|
|
4
3
|
CommonDBCreateOptions,
|
|
@@ -19,14 +18,14 @@ import { white } from '@naturalcycles/nodejs-lib/colors'
|
|
|
19
18
|
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
20
19
|
import type {
|
|
21
20
|
Connection,
|
|
22
|
-
OkPacket,
|
|
23
21
|
Pool,
|
|
24
|
-
PoolConfig,
|
|
25
22
|
PoolConnection,
|
|
23
|
+
PoolOptions,
|
|
26
24
|
QueryOptions,
|
|
25
|
+
ResultSetHeader,
|
|
27
26
|
TypeCast,
|
|
28
|
-
} from '
|
|
29
|
-
import * as mysql from '
|
|
27
|
+
} from 'mysql2/promise'
|
|
28
|
+
import * as mysql from 'mysql2/promise'
|
|
30
29
|
import {
|
|
31
30
|
dbQueryToSQLDelete,
|
|
32
31
|
dbQueryToSQLSelect,
|
|
@@ -46,7 +45,7 @@ export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSa
|
|
|
46
45
|
/**
|
|
47
46
|
* @default false / undefined
|
|
48
47
|
*/
|
|
49
|
-
export interface MysqlDBCfg extends
|
|
48
|
+
export interface MysqlDBCfg extends PoolOptions {
|
|
50
49
|
/**
|
|
51
50
|
* If true - will log all produced SQL queries.
|
|
52
51
|
*/
|
|
@@ -101,6 +100,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
101
100
|
super()
|
|
102
101
|
this.cfg = {
|
|
103
102
|
typeCast,
|
|
103
|
+
// mysql2 defaults that differ from the `mysql` package, set back to what `mysql` did:
|
|
104
|
+
// utf8 (not utf8mb4) connection charset, JSON columns as strings, DECIMAL as numbers
|
|
105
|
+
charset: 'UTF8_GENERAL_CI',
|
|
106
|
+
jsonStrings: true,
|
|
107
|
+
decimalNumbers: true,
|
|
104
108
|
// charset: 'utf8mb4', // for emoji support
|
|
105
109
|
// host: 'localhost',
|
|
106
110
|
// user: MYSQL_USER,
|
|
@@ -120,7 +124,8 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
120
124
|
|
|
121
125
|
@_Memo()
|
|
122
126
|
pool(): Pool {
|
|
123
|
-
|
|
127
|
+
// mysql2 warns on options it doesn't know, so our own ones are left out
|
|
128
|
+
const pool = mysql.createPool(_omit(this.cfg, ['logSQL', 'debugConnections', 'logger']))
|
|
124
129
|
const { host, database, logger } = this.cfg
|
|
125
130
|
logger.log(`connected to ${white(host + '/' + database)}`)
|
|
126
131
|
|
|
@@ -142,17 +147,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
142
147
|
})
|
|
143
148
|
}
|
|
144
149
|
|
|
145
|
-
pool.on('error', err => {
|
|
146
|
-
logger.error(err)
|
|
147
|
-
})
|
|
148
|
-
|
|
149
150
|
return pool
|
|
150
151
|
}
|
|
151
152
|
|
|
152
153
|
async close(): Promise<void> {
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
await promisify(pool.end.bind(pool))()
|
|
154
|
+
await this.pool().end()
|
|
156
155
|
this.cfg.logger.log('closed')
|
|
157
156
|
}
|
|
158
157
|
|
|
@@ -160,8 +159,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
160
159
|
* Be careful to always call `con.release()` when you get connection with this method.
|
|
161
160
|
*/
|
|
162
161
|
async getConnection(): Promise<PoolConnection> {
|
|
163
|
-
|
|
164
|
-
return await promisify(pool.getConnection.bind(pool))()
|
|
162
|
+
return await this.pool().getConnection()
|
|
165
163
|
}
|
|
166
164
|
|
|
167
165
|
/**
|
|
@@ -169,8 +167,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
169
167
|
* Be careful to manage this connection yourself (open, release, etc).
|
|
170
168
|
*/
|
|
171
169
|
async createSingleConnection(): Promise<Connection> {
|
|
172
|
-
const con = mysql.createConnection(
|
|
173
|
-
|
|
170
|
+
const con = await mysql.createConnection(
|
|
171
|
+
_omit(this.cfg, ['logSQL', 'debugConnections', 'logger']),
|
|
172
|
+
)
|
|
174
173
|
const { threadId } = con
|
|
175
174
|
|
|
176
175
|
if (this.cfg.debugConnections) {
|
|
@@ -237,12 +236,8 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
237
236
|
this.cfg.logger.log(_filterUndefinedValues({ msg: q.sql, values: q.values }))
|
|
238
237
|
}
|
|
239
238
|
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
if (err) return reject(err)
|
|
243
|
-
resolve(res)
|
|
244
|
-
})
|
|
245
|
-
})
|
|
239
|
+
const [result] = await this.pool().query(q)
|
|
240
|
+
return result as RESULT
|
|
246
241
|
}
|
|
247
242
|
|
|
248
243
|
/**
|
|
@@ -277,15 +272,20 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
277
272
|
return Pipeline.fromArray([])
|
|
278
273
|
}
|
|
279
274
|
|
|
275
|
+
return this.streamSQL<ROW>(sql).mapSync(row => _filterUndefinedValues(row, { mutate: true }))
|
|
276
|
+
}
|
|
277
|
+
|
|
278
|
+
/**
|
|
279
|
+
* Streaming is not part of the mysql2 promise API, so it goes through the underlying callback pool.
|
|
280
|
+
*/
|
|
281
|
+
streamSQL<ROW>(sql: string): Pipeline<ROW> {
|
|
280
282
|
if (this.cfg.logSQL) this.cfg.logger.log(`stream: ${sql}`)
|
|
281
283
|
|
|
282
|
-
// todo:
|
|
283
|
-
// return (this.pool().query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
284
|
+
// todo: mysql2 streams are `node:stream` Readables (unlike `mysql`'s `readable-stream@2`), so this can now use iterable helpers:
|
|
285
|
+
// return (this.pool().pool.query(sql).stream() as ReadableTyped<ROW>).map(row =>
|
|
284
286
|
// _filterUndefinedValues(row, true),
|
|
285
287
|
// )
|
|
286
|
-
return Pipeline.from<ROW>(this.pool().query(sql).stream())
|
|
287
|
-
_filterUndefinedValues(row, { mutate: true }),
|
|
288
|
-
)
|
|
288
|
+
return Pipeline.from<ROW>(this.pool().pool.query(sql).stream())
|
|
289
289
|
}
|
|
290
290
|
|
|
291
291
|
// SAVE
|
|
@@ -325,7 +325,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
325
325
|
} else {
|
|
326
326
|
// Create new
|
|
327
327
|
const sql = insertSQL(table, [row], 'INSERT', this.cfg.logger)[0]!
|
|
328
|
-
const { insertId } = await this.runSQL<
|
|
328
|
+
const { insertId } = await this.runSQL<ResultSetHeader>({ sql })
|
|
329
329
|
|
|
330
330
|
// Mutate the input row with insertIt
|
|
331
331
|
rowsInput[i]!.id = insertId as any // this is because we no longer support number ids in CommonDB
|
|
@@ -368,7 +368,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
368
368
|
const sql = dbQueryToSQLDelete(new DBQuery(table).filterEq('id', ids))
|
|
369
369
|
if (!sql) return 0
|
|
370
370
|
|
|
371
|
-
const { affectedRows } = await this.runSQL<
|
|
371
|
+
const { affectedRows } = await this.runSQL<ResultSetHeader>({ sql })
|
|
372
372
|
return affectedRows
|
|
373
373
|
}
|
|
374
374
|
|
|
@@ -378,7 +378,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
378
378
|
): Promise<number> {
|
|
379
379
|
const sql = dbQueryToSQLDelete(q)
|
|
380
380
|
if (!sql) return 0
|
|
381
|
-
const { affectedRows } = await this.runSQL<
|
|
381
|
+
const { affectedRows } = await this.runSQL<ResultSetHeader>({ sql })
|
|
382
382
|
return affectedRows
|
|
383
383
|
}
|
|
384
384
|
|
|
@@ -424,7 +424,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
424
424
|
const sql = dbQueryToSQLUpdate(q, patch)
|
|
425
425
|
if (!sql) return 0
|
|
426
426
|
|
|
427
|
-
const { affectedRows } = await this.runSQL<
|
|
427
|
+
const { affectedRows } = await this.runSQL<ResultSetHeader>({ sql })
|
|
428
428
|
return affectedRows
|
|
429
429
|
}
|
|
430
430
|
}
|
package/src/mysqlKeyValueDB.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { commonKeyValueDBFullSupport } from '@naturalcycles/db-lib/kv'
|
|
|
4
4
|
import { AppError } from '@naturalcycles/js-lib/error/error.util.js'
|
|
5
5
|
import { pMap } from '@naturalcycles/js-lib/promise/pMap.js'
|
|
6
6
|
import type { ObjectWithId } from '@naturalcycles/js-lib/types'
|
|
7
|
-
import { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
8
|
-
import type { QueryOptions } from '
|
|
7
|
+
import type { Pipeline } from '@naturalcycles/nodejs-lib/stream'
|
|
8
|
+
import type { QueryOptions } from 'mysql2/promise'
|
|
9
9
|
import type { MysqlDBCfg } from './mysql.db.js'
|
|
10
10
|
import { MysqlDB } from './mysql.db.js'
|
|
11
11
|
|
|
@@ -63,6 +63,8 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
63
63
|
}
|
|
64
64
|
|
|
65
65
|
async deleteByIds(table: string, ids: readonly string[]): Promise<void> {
|
|
66
|
+
if (!ids.length) return
|
|
67
|
+
|
|
66
68
|
const sql = `DELETE FROM ${table} WHERE id in (${ids.map(id => `"${id}"`).join(',')})`
|
|
67
69
|
if (this.cfg.logSQL) this.db.cfg.logger.log(sql)
|
|
68
70
|
await this.db.runSQL({ sql })
|
|
@@ -85,29 +87,22 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
85
87
|
streamIds(table: string, limit?: number): Pipeline<string> {
|
|
86
88
|
let sql = `SELECT id FROM ${table}`
|
|
87
89
|
if (limit) sql += ` LIMIT ${limit}`
|
|
88
|
-
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
89
90
|
|
|
90
|
-
|
|
91
|
-
// return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
|
|
92
|
-
return Pipeline.from<ObjectWithId>(this.db.pool().query(sql).stream()).mapSync(r => r.id)
|
|
91
|
+
return this.db.streamSQL<ObjectWithId>(sql).mapSync(r => r.id)
|
|
93
92
|
}
|
|
94
93
|
|
|
95
94
|
streamValues(table: string, limit?: number): Pipeline<Buffer> {
|
|
96
95
|
let sql = `SELECT v FROM ${table}`
|
|
97
96
|
if (limit) sql += ` LIMIT ${limit}`
|
|
98
|
-
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
99
97
|
|
|
100
|
-
return
|
|
98
|
+
return this.db.streamSQL<{ v: Buffer }>(sql).mapSync(row => row.v)
|
|
101
99
|
}
|
|
102
100
|
|
|
103
101
|
streamEntries(table: string, limit?: number): Pipeline<KeyValueDBTuple> {
|
|
104
102
|
let sql = `SELECT id,v FROM ${table}`
|
|
105
103
|
if (limit) sql += ` LIMIT ${limit}`
|
|
106
|
-
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
107
104
|
|
|
108
|
-
return
|
|
109
|
-
row => [row.id, row.v],
|
|
110
|
-
)
|
|
105
|
+
return this.db.streamSQL<{ id: string; v: Buffer }>(sql).mapSync(row => [row.id, row.v])
|
|
111
106
|
}
|
|
112
107
|
|
|
113
108
|
async beginTransaction(): Promise<void> {
|
package/src/query.util.ts
CHANGED
|
@@ -4,8 +4,8 @@ import { consoleLogger } from '@naturalcycles/js-lib/log'
|
|
|
4
4
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
5
5
|
import type { AnyObjectWithId } from '@naturalcycles/js-lib/types'
|
|
6
6
|
import { white, yellow } from '@naturalcycles/nodejs-lib/colors'
|
|
7
|
-
import type { QueryOptions } from '
|
|
8
|
-
import * as mysql from '
|
|
7
|
+
import type { QueryOptions } from 'mysql2/promise'
|
|
8
|
+
import * as mysql from 'mysql2/promise'
|
|
9
9
|
import { mapNameToMySQL } from './schema/mysql.schema.util.js'
|
|
10
10
|
|
|
11
11
|
const MAX_PACKET_SIZE = 1024 * 1024 // 1Mb
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import type { CommonLogger } from '@naturalcycles/js-lib/log'
|
|
2
2
|
import type { AnyObject } from '@naturalcycles/js-lib/types'
|
|
3
3
|
import type { JsonSchema } from '@naturalcycles/nodejs-lib/ajv'
|
|
4
|
-
import * as mysql from '
|
|
4
|
+
import * as mysql from 'mysql2/promise'
|
|
5
5
|
|
|
6
6
|
export interface MySQLTableStats {
|
|
7
7
|
Field: string // created
|