@naturalcycles/mysql-lib 3.14.8 → 3.16.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 +2 -2
- package/dist/mysql.db.js +10 -9
- package/dist/mysqlKeyValueDB.d.ts +7 -1
- package/dist/mysqlKeyValueDB.js +11 -0
- package/dist/query.util.d.ts +1 -1
- package/dist/query.util.js +9 -10
- package/dist/schema/mysql.schema.util.js +4 -5
- package/package.json +10 -5
- package/src/mysql.db.ts +14 -14
- package/src/mysqlKeyValueDB.ts +23 -2
- package/src/query.util.ts +3 -3
- package/src/schema/mysql.schema.util.ts +1 -1
package/dist/mysql.db.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { BaseCommonDB, CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, CommonDBSupport, CommonDBType, DBQuery, RunQueryResult } from '@naturalcycles/db-lib';
|
|
2
2
|
import { CommonLogger, JsonSchemaObject, JsonSchemaRootObject, ObjectWithId } from '@naturalcycles/js-lib';
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
4
4
|
import { Connection, Pool, PoolConfig, PoolConnection, QueryOptions } from 'mysql';
|
|
@@ -68,5 +68,5 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
68
68
|
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
|
|
69
69
|
getTables(): Promise<string[]>;
|
|
70
70
|
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
71
|
-
|
|
71
|
+
patchByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: Partial<ROW>): Promise<number>;
|
|
72
72
|
}
|
package/dist/mysql.db.js
CHANGED
|
@@ -37,8 +37,9 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
37
37
|
this.dbType = db_lib_1.CommonDBType.relational;
|
|
38
38
|
this.support = {
|
|
39
39
|
...db_lib_1.commonDBFullSupport,
|
|
40
|
-
updateSaveMethod: false,
|
|
41
|
-
transactions: false,
|
|
40
|
+
updateSaveMethod: false, // todo: can be implemented
|
|
41
|
+
transactions: false, // todo: can be implemented
|
|
42
|
+
increment: false, // todo: can be implemented
|
|
42
43
|
};
|
|
43
44
|
this.cfg = {
|
|
44
45
|
typeCast,
|
|
@@ -157,7 +158,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
157
158
|
.split(';')
|
|
158
159
|
.map(s => s.trim())
|
|
159
160
|
.filter(Boolean);
|
|
160
|
-
for
|
|
161
|
+
for (const sql of queries) {
|
|
161
162
|
await this.runSQL({ sql });
|
|
162
163
|
}
|
|
163
164
|
}
|
|
@@ -191,12 +192,12 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
191
192
|
if (opt.assignGeneratedIds) {
|
|
192
193
|
// Insert rows one-by-one, to get their auto-generated id
|
|
193
194
|
let i = -1;
|
|
194
|
-
for
|
|
195
|
+
for (const row of rows) {
|
|
195
196
|
i++;
|
|
196
197
|
if (row.id) {
|
|
197
198
|
// Update already existing
|
|
198
199
|
const query = new db_lib_1.DBQuery(table).filterEq('id', row.id);
|
|
199
|
-
await this.
|
|
200
|
+
await this.patchByQuery(query, (0, js_lib_1._omit)(row, ['id']));
|
|
200
201
|
}
|
|
201
202
|
else {
|
|
202
203
|
// Create new
|
|
@@ -210,18 +211,18 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
210
211
|
}
|
|
211
212
|
if (opt.saveMethod === 'update') {
|
|
212
213
|
// TODO: This fails if a combination of entities with id and without id are parsed
|
|
213
|
-
for
|
|
214
|
+
for (const row of rows) {
|
|
214
215
|
// Update already existing
|
|
215
216
|
(0, js_lib_1._assert)(row.id, 'id is required for updating');
|
|
216
217
|
const query = new db_lib_1.DBQuery(table).filterEq('id', row.id);
|
|
217
|
-
await this.
|
|
218
|
+
await this.patchByQuery(query, (0, js_lib_1._omit)(row, ['id']));
|
|
218
219
|
}
|
|
219
220
|
return;
|
|
220
221
|
}
|
|
221
222
|
const verb = opt.saveMethod === 'insert' ? 'INSERT' : 'REPLACE';
|
|
222
223
|
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
|
|
223
224
|
const sqls = (0, query_util_1.insertSQL)(table, rows, verb, this.cfg.logger);
|
|
224
|
-
for
|
|
225
|
+
for (const sql of sqls) {
|
|
225
226
|
await this.runSQL({ sql });
|
|
226
227
|
}
|
|
227
228
|
}
|
|
@@ -271,7 +272,7 @@ class MysqlDB extends db_lib_1.BaseCommonDB {
|
|
|
271
272
|
});
|
|
272
273
|
return (0, mysql_schema_util_1.mysqlTableStatsToJsonSchemaField)(table, stats, this.cfg.logger);
|
|
273
274
|
}
|
|
274
|
-
async
|
|
275
|
+
async patchByQuery(q, patch) {
|
|
275
276
|
const sql = (0, query_util_1.dbQueryToSQLUpdate)(q, patch);
|
|
276
277
|
if (!sql)
|
|
277
278
|
return 0;
|
|
@@ -1,11 +1,15 @@
|
|
|
1
|
-
/// <reference types="node" />
|
|
2
1
|
import { CommonDBCreateOptions, CommonKeyValueDB, KeyValueDBTuple } from '@naturalcycles/db-lib';
|
|
2
|
+
import { StringMap } from '@naturalcycles/js-lib';
|
|
3
3
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
4
4
|
import { MysqlDB, MysqlDBCfg } from './mysql.db';
|
|
5
5
|
export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
6
6
|
cfg: MysqlDBCfg;
|
|
7
7
|
constructor(cfg: MysqlDBCfg);
|
|
8
8
|
db: MysqlDB;
|
|
9
|
+
support: {
|
|
10
|
+
increment: boolean;
|
|
11
|
+
count?: boolean;
|
|
12
|
+
};
|
|
9
13
|
ping(): Promise<void>;
|
|
10
14
|
close(): Promise<void>;
|
|
11
15
|
createTable(table: string, opt?: CommonDBCreateOptions): Promise<void>;
|
|
@@ -22,4 +26,6 @@ export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
22
26
|
beginTransaction(): Promise<void>;
|
|
23
27
|
endTransaction(): Promise<void>;
|
|
24
28
|
count(table: string): Promise<number>;
|
|
29
|
+
increment(_table: string, _id: string, _by?: number): Promise<number>;
|
|
30
|
+
incrementBatch(_table: string, _incrementMap: StringMap<number>): Promise<StringMap<number>>;
|
|
25
31
|
}
|
package/dist/mysqlKeyValueDB.js
CHANGED
|
@@ -1,11 +1,16 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.MySQLKeyValueDB = void 0;
|
|
4
|
+
const db_lib_1 = require("@naturalcycles/db-lib");
|
|
4
5
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
5
6
|
const mysql_db_1 = require("./mysql.db");
|
|
6
7
|
class MySQLKeyValueDB {
|
|
7
8
|
constructor(cfg) {
|
|
8
9
|
this.cfg = cfg;
|
|
10
|
+
this.support = {
|
|
11
|
+
...db_lib_1.commonKeyValueDBFullSupport,
|
|
12
|
+
increment: false,
|
|
13
|
+
};
|
|
9
14
|
this.db = new mysql_db_1.MysqlDB(this.cfg);
|
|
10
15
|
}
|
|
11
16
|
async ping() {
|
|
@@ -96,5 +101,11 @@ class MySQLKeyValueDB {
|
|
|
96
101
|
const rows = await this.db.runSQL({ sql });
|
|
97
102
|
return rows[0].cnt;
|
|
98
103
|
}
|
|
104
|
+
async increment(_table, _id, _by) {
|
|
105
|
+
throw new js_lib_1.AppError('MySQLKeyValueDB.increment() is not implemented');
|
|
106
|
+
}
|
|
107
|
+
async incrementBatch(_table, _incrementMap) {
|
|
108
|
+
throw new js_lib_1.AppError('MySQLKeyValueDB.incrementBatch() is not implemented');
|
|
109
|
+
}
|
|
99
110
|
}
|
|
100
111
|
exports.MySQLKeyValueDB = MySQLKeyValueDB;
|
package/dist/query.util.d.ts
CHANGED
|
@@ -16,4 +16,4 @@ export declare function dbQueryToSQLDelete(q: DBQuery<any>): string | null;
|
|
|
16
16
|
export declare function insertSQL(table: string, rows: Record<any, any>[], verb?: 'INSERT' | 'REPLACE', logger?: CommonLogger): string[];
|
|
17
17
|
export declare function insertSQLSingle(table: string, record: Record<any, any>): string;
|
|
18
18
|
export declare function insertSQLSetSingle(table: string, record: Record<any, any>): QueryOptions;
|
|
19
|
-
export declare function dbQueryToSQLUpdate(q: DBQuery<any>,
|
|
19
|
+
export declare function dbQueryToSQLUpdate(q: DBQuery<any>, patch: Record<any, any>): string | null;
|
package/dist/query.util.js
CHANGED
|
@@ -1,6 +1,11 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.dbQueryToSQLSelect = dbQueryToSQLSelect;
|
|
4
|
+
exports.dbQueryToSQLDelete = dbQueryToSQLDelete;
|
|
5
|
+
exports.insertSQL = insertSQL;
|
|
6
|
+
exports.insertSQLSingle = insertSQLSingle;
|
|
7
|
+
exports.insertSQLSetSingle = insertSQLSetSingle;
|
|
8
|
+
exports.dbQueryToSQLUpdate = dbQueryToSQLUpdate;
|
|
4
9
|
const tslib_1 = require("tslib");
|
|
5
10
|
const js_lib_1 = require("@naturalcycles/js-lib");
|
|
6
11
|
const nodejs_lib_1 = require("@naturalcycles/nodejs-lib");
|
|
@@ -25,7 +30,6 @@ function dbQueryToSQLSelect(q) {
|
|
|
25
30
|
tokens.push(...offsetLimitTokens(q));
|
|
26
31
|
return tokens.join(' ');
|
|
27
32
|
}
|
|
28
|
-
exports.dbQueryToSQLSelect = dbQueryToSQLSelect;
|
|
29
33
|
/**
|
|
30
34
|
* Returns null in "0 rows" case.
|
|
31
35
|
*/
|
|
@@ -40,7 +44,6 @@ function dbQueryToSQLDelete(q) {
|
|
|
40
44
|
tokens.push(...offsetLimitTokens(q));
|
|
41
45
|
return tokens.join(' ');
|
|
42
46
|
}
|
|
43
|
-
exports.dbQueryToSQLDelete = dbQueryToSQLDelete;
|
|
44
47
|
/**
|
|
45
48
|
* Returns array of sql statements to respect the max sql size.
|
|
46
49
|
*/
|
|
@@ -89,7 +92,6 @@ function insertSQL(table, rows, verb = 'INSERT', logger = console) {
|
|
|
89
92
|
return sqls;
|
|
90
93
|
// todo: handle "upsert" later
|
|
91
94
|
}
|
|
92
|
-
exports.insertSQL = insertSQL;
|
|
93
95
|
function insertSQLSingle(table, record) {
|
|
94
96
|
// INSERT INTO table_name (column1, column2, column3, ...)
|
|
95
97
|
// VALUES (value1, value2, value3, ...);
|
|
@@ -110,15 +112,13 @@ function insertSQLSingle(table, record) {
|
|
|
110
112
|
];
|
|
111
113
|
return tokens.join(' ');
|
|
112
114
|
}
|
|
113
|
-
exports.insertSQLSingle = insertSQLSingle;
|
|
114
115
|
function insertSQLSetSingle(table, record) {
|
|
115
116
|
return {
|
|
116
117
|
sql: `INSERT INTO ${mysql.escapeId(table)} SET ?`,
|
|
117
118
|
values: [record],
|
|
118
119
|
};
|
|
119
120
|
}
|
|
120
|
-
|
|
121
|
-
function dbQueryToSQLUpdate(q, record) {
|
|
121
|
+
function dbQueryToSQLUpdate(q, patch) {
|
|
122
122
|
// var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
|
|
123
123
|
const whereTokens = getWhereTokens(q);
|
|
124
124
|
if (!whereTokens)
|
|
@@ -127,14 +127,13 @@ function dbQueryToSQLUpdate(q, record) {
|
|
|
127
127
|
`UPDATE`,
|
|
128
128
|
mysql.escapeId(q.table),
|
|
129
129
|
`SET`,
|
|
130
|
-
Object.keys(
|
|
130
|
+
Object.keys(patch)
|
|
131
131
|
.map(f => mysql.escapeId((0, mysql_schema_util_1.mapNameToMySQL)(f)) + ' = ?')
|
|
132
132
|
.join(', '),
|
|
133
133
|
...whereTokens,
|
|
134
134
|
];
|
|
135
|
-
return mysql.format(tokens.join(' '), Object.values(
|
|
135
|
+
return mysql.format(tokens.join(' '), Object.values(patch));
|
|
136
136
|
}
|
|
137
|
-
exports.dbQueryToSQLUpdate = dbQueryToSQLUpdate;
|
|
138
137
|
function selectTokens(q) {
|
|
139
138
|
let fields = ['*'];
|
|
140
139
|
if (q._selectedFieldNames) {
|
|
@@ -1,6 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.
|
|
3
|
+
exports.jsonSchemaToMySQLDDL = jsonSchemaToMySQLDDL;
|
|
4
|
+
exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
|
|
5
|
+
exports.mapNameToMySQL = mapNameToMySQL;
|
|
6
|
+
exports.mapNameFromMySQL = mapNameFromMySQL;
|
|
4
7
|
const tslib_1 = require("tslib");
|
|
5
8
|
const mysql = tslib_1.__importStar(require("mysql"));
|
|
6
9
|
/**
|
|
@@ -56,7 +59,6 @@ function jsonSchemaToMySQLDDL(table, schema, opt = {}) {
|
|
|
56
59
|
lines.push(`) ENGINE=${engine}`);
|
|
57
60
|
return lines.join('\n');
|
|
58
61
|
}
|
|
59
|
-
exports.jsonSchemaToMySQLDDL = jsonSchemaToMySQLDDL;
|
|
60
62
|
function mysqlTableStatsToJsonSchemaField(table, stats, logger) {
|
|
61
63
|
const s = {
|
|
62
64
|
$id: `${table}.schema.json`,
|
|
@@ -94,7 +96,6 @@ function mysqlTableStatsToJsonSchemaField(table, stats, logger) {
|
|
|
94
96
|
});
|
|
95
97
|
return s;
|
|
96
98
|
}
|
|
97
|
-
exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
|
|
98
99
|
/**
|
|
99
100
|
* Because MySQL doesn't support `.` in field names and escapes them as tableName + fieldName.
|
|
100
101
|
*
|
|
@@ -103,8 +104,6 @@ exports.mysqlTableStatsToJsonSchemaField = mysqlTableStatsToJsonSchemaField;
|
|
|
103
104
|
function mapNameToMySQL(name) {
|
|
104
105
|
return name.replaceAll('.', '_dot_');
|
|
105
106
|
}
|
|
106
|
-
exports.mapNameToMySQL = mapNameToMySQL;
|
|
107
107
|
function mapNameFromMySQL(name) {
|
|
108
108
|
return name.replaceAll('_dot_', '.');
|
|
109
109
|
}
|
|
110
|
-
exports.mapNameFromMySQL = mapNameFromMySQL;
|
package/package.json
CHANGED
|
@@ -1,7 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@naturalcycles/mysql-lib",
|
|
3
3
|
"scripts": {
|
|
4
|
-
"prepare": "husky"
|
|
4
|
+
"prepare": "husky",
|
|
5
|
+
"build": "dev-lib build",
|
|
6
|
+
"test": "dev-lib test",
|
|
7
|
+
"lint": "dev-lib lint",
|
|
8
|
+
"bt": "dev-lib bt",
|
|
9
|
+
"lbt": "dev-lib lbt"
|
|
5
10
|
},
|
|
6
11
|
"dependencies": {
|
|
7
12
|
"@naturalcycles/db-lib": "^9.1.0",
|
|
@@ -11,8 +16,8 @@
|
|
|
11
16
|
"mysql": "^2.17.1"
|
|
12
17
|
},
|
|
13
18
|
"devDependencies": {
|
|
14
|
-
"@naturalcycles/dev-lib": "^
|
|
15
|
-
"@types/node": "^
|
|
19
|
+
"@naturalcycles/dev-lib": "^15.18.0",
|
|
20
|
+
"@types/node": "^22.7.4",
|
|
16
21
|
"dotenv": "^16.0.0",
|
|
17
22
|
"jest": "^29.0.0"
|
|
18
23
|
},
|
|
@@ -34,9 +39,9 @@
|
|
|
34
39
|
"url": "https://github.com/NaturalCycles/mysql-lib"
|
|
35
40
|
},
|
|
36
41
|
"engines": {
|
|
37
|
-
"node": ">=
|
|
42
|
+
"node": ">=20.13.0"
|
|
38
43
|
},
|
|
39
|
-
"version": "3.
|
|
44
|
+
"version": "3.16.0",
|
|
40
45
|
"description": "MySQL client implementing CommonDB interface",
|
|
41
46
|
"keywords": [
|
|
42
47
|
"mysql",
|
package/src/mysql.db.ts
CHANGED
|
@@ -1,17 +1,16 @@
|
|
|
1
1
|
import { Readable } from 'node:stream'
|
|
2
2
|
import { promisify } from 'node:util'
|
|
3
3
|
import {
|
|
4
|
-
DBPatch,
|
|
5
4
|
BaseCommonDB,
|
|
6
5
|
CommonDB,
|
|
7
6
|
CommonDBCreateOptions,
|
|
7
|
+
commonDBFullSupport,
|
|
8
8
|
CommonDBOptions,
|
|
9
9
|
CommonDBSaveOptions,
|
|
10
|
-
DBQuery,
|
|
11
|
-
RunQueryResult,
|
|
12
10
|
CommonDBSupport,
|
|
13
|
-
commonDBFullSupport,
|
|
14
11
|
CommonDBType,
|
|
12
|
+
DBQuery,
|
|
13
|
+
RunQueryResult,
|
|
15
14
|
} from '@naturalcycles/db-lib'
|
|
16
15
|
import {
|
|
17
16
|
_assert,
|
|
@@ -96,8 +95,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
96
95
|
|
|
97
96
|
override support: CommonDBSupport = {
|
|
98
97
|
...commonDBFullSupport,
|
|
99
|
-
updateSaveMethod: false,
|
|
100
|
-
transactions: false,
|
|
98
|
+
updateSaveMethod: false, // todo: can be implemented
|
|
99
|
+
transactions: false, // todo: can be implemented
|
|
100
|
+
increment: false, // todo: can be implemented
|
|
101
101
|
}
|
|
102
102
|
|
|
103
103
|
constructor(cfg: MysqlDBCfg = {}) {
|
|
@@ -253,7 +253,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
253
253
|
.map(s => s.trim())
|
|
254
254
|
.filter(Boolean)
|
|
255
255
|
|
|
256
|
-
for
|
|
256
|
+
for (const sql of queries) {
|
|
257
257
|
await this.runSQL({ sql })
|
|
258
258
|
}
|
|
259
259
|
}
|
|
@@ -310,12 +310,12 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
310
310
|
// Insert rows one-by-one, to get their auto-generated id
|
|
311
311
|
|
|
312
312
|
let i = -1
|
|
313
|
-
for
|
|
313
|
+
for (const row of rows) {
|
|
314
314
|
i++
|
|
315
315
|
if (row.id) {
|
|
316
316
|
// Update already existing
|
|
317
317
|
const query = new DBQuery(table).filterEq('id', row.id)
|
|
318
|
-
await this.
|
|
318
|
+
await this.patchByQuery(query, _omit(row, ['id']))
|
|
319
319
|
} else {
|
|
320
320
|
// Create new
|
|
321
321
|
const sql = insertSQL(table, [row], 'INSERT', this.cfg.logger)[0]!
|
|
@@ -331,11 +331,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
331
331
|
|
|
332
332
|
if (opt.saveMethod === 'update') {
|
|
333
333
|
// TODO: This fails if a combination of entities with id and without id are parsed
|
|
334
|
-
for
|
|
334
|
+
for (const row of rows) {
|
|
335
335
|
// Update already existing
|
|
336
336
|
_assert(row.id, 'id is required for updating')
|
|
337
337
|
const query = new DBQuery(table).filterEq('id', row.id)
|
|
338
|
-
await this.
|
|
338
|
+
await this.patchByQuery(query, _omit(row, ['id']))
|
|
339
339
|
}
|
|
340
340
|
return
|
|
341
341
|
}
|
|
@@ -344,7 +344,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
344
344
|
// inserts are split into multiple sentenses to respect the max_packet_size (1Mb usually)
|
|
345
345
|
const sqls = insertSQL(table, rows, verb, this.cfg.logger)
|
|
346
346
|
|
|
347
|
-
for
|
|
347
|
+
for (const sql of sqls) {
|
|
348
348
|
await this.runSQL({ sql })
|
|
349
349
|
}
|
|
350
350
|
}
|
|
@@ -409,9 +409,9 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
409
409
|
return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
|
|
410
410
|
}
|
|
411
411
|
|
|
412
|
-
override async
|
|
412
|
+
override async patchByQuery<ROW extends ObjectWithId>(
|
|
413
413
|
q: DBQuery<ROW>,
|
|
414
|
-
patch:
|
|
414
|
+
patch: Partial<ROW>,
|
|
415
415
|
): Promise<number> {
|
|
416
416
|
const sql = dbQueryToSQLUpdate(q, patch)
|
|
417
417
|
if (!sql) return 0
|
package/src/mysqlKeyValueDB.ts
CHANGED
|
@@ -1,5 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
|
|
1
|
+
import {
|
|
2
|
+
CommonDBCreateOptions,
|
|
3
|
+
CommonKeyValueDB,
|
|
4
|
+
commonKeyValueDBFullSupport,
|
|
5
|
+
KeyValueDBTuple,
|
|
6
|
+
} from '@naturalcycles/db-lib'
|
|
7
|
+
import { AppError, ObjectWithId, pMap, StringMap } from '@naturalcycles/js-lib'
|
|
3
8
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
4
9
|
import { QueryOptions } from 'mysql'
|
|
5
10
|
import { MysqlDB, MysqlDBCfg } from './mysql.db'
|
|
@@ -16,6 +21,11 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
16
21
|
|
|
17
22
|
db: MysqlDB
|
|
18
23
|
|
|
24
|
+
support = {
|
|
25
|
+
...commonKeyValueDBFullSupport,
|
|
26
|
+
increment: false,
|
|
27
|
+
}
|
|
28
|
+
|
|
19
29
|
async ping(): Promise<void> {
|
|
20
30
|
await this.db.ping()
|
|
21
31
|
}
|
|
@@ -114,4 +124,15 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
114
124
|
const rows = await this.db.runSQL<{ cnt: number }[]>({ sql })
|
|
115
125
|
return rows[0]!.cnt
|
|
116
126
|
}
|
|
127
|
+
|
|
128
|
+
async increment(_table: string, _id: string, _by?: number): Promise<number> {
|
|
129
|
+
throw new AppError('MySQLKeyValueDB.increment() is not implemented')
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async incrementBatch(
|
|
133
|
+
_table: string,
|
|
134
|
+
_incrementMap: StringMap<number>,
|
|
135
|
+
): Promise<StringMap<number>> {
|
|
136
|
+
throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented')
|
|
137
|
+
}
|
|
117
138
|
}
|
package/src/query.util.ts
CHANGED
|
@@ -142,7 +142,7 @@ export function insertSQLSetSingle(table: string, record: Record<any, any>): Que
|
|
|
142
142
|
}
|
|
143
143
|
}
|
|
144
144
|
|
|
145
|
-
export function dbQueryToSQLUpdate(q: DBQuery<any>,
|
|
145
|
+
export function dbQueryToSQLUpdate(q: DBQuery<any>, patch: Record<any, any>): string | null {
|
|
146
146
|
// var sql = mysql.format('UPDATE posts SET modified = ? WHERE id = ?', [CURRENT_TIMESTAMP, 42]);
|
|
147
147
|
const whereTokens = getWhereTokens(q)
|
|
148
148
|
if (!whereTokens) return null
|
|
@@ -151,13 +151,13 @@ export function dbQueryToSQLUpdate(q: DBQuery<any>, record: Record<any, any>): s
|
|
|
151
151
|
`UPDATE`,
|
|
152
152
|
mysql.escapeId(q.table),
|
|
153
153
|
`SET`,
|
|
154
|
-
Object.keys(
|
|
154
|
+
Object.keys(patch)
|
|
155
155
|
.map(f => mysql.escapeId(mapNameToMySQL(f)) + ' = ?')
|
|
156
156
|
.join(', '),
|
|
157
157
|
...whereTokens,
|
|
158
158
|
]
|
|
159
159
|
|
|
160
|
-
return mysql.format(tokens.join(' '), Object.values(
|
|
160
|
+
return mysql.format(tokens.join(' '), Object.values(patch))
|
|
161
161
|
}
|
|
162
162
|
|
|
163
163
|
function selectTokens(q: DBQuery<any>): string[] {
|