@naturalcycles/mysql-lib 3.14.6 → 3.14.7
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 +11 -11
- package/package.json +1 -1
- package/src/mysql.db.ts +14 -17
- package/src/query.util.ts +4 -4
package/dist/mysql.db.d.ts
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
import { DBPatch, BaseCommonDB, CommonDB, CommonDBCreateOptions, CommonDBOptions, CommonDBSaveOptions, DBQuery, RunQueryResult, CommonDBSupport, CommonDBType } from '@naturalcycles/db-lib';
|
|
2
|
-
import {
|
|
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';
|
|
5
5
|
export interface MysqlDBOptions extends CommonDBOptions {
|
|
6
6
|
}
|
|
7
|
-
export interface MysqlDBSaveOptions<ROW extends
|
|
7
|
+
export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {
|
|
8
8
|
}
|
|
9
9
|
/**
|
|
10
10
|
* @default false / undefined
|
|
@@ -42,22 +42,22 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
42
42
|
* Be careful to manage this connection yourself (open, release, etc).
|
|
43
43
|
*/
|
|
44
44
|
createSingleConnection(): Promise<Connection>;
|
|
45
|
-
getByIds<ROW extends
|
|
46
|
-
runQuery<ROW extends
|
|
45
|
+
getByIds<ROW extends ObjectWithId>(table: string, ids: string[], opt?: MysqlDBOptions): Promise<ROW[]>;
|
|
46
|
+
runQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): Promise<RunQueryResult<ROW>>;
|
|
47
47
|
runSQL<RESULT>(q: QueryOptions): Promise<RESULT>;
|
|
48
48
|
/**
|
|
49
49
|
* Allows to run semicolon-separated "SQL file".
|
|
50
50
|
* E.g "ddl reset script".
|
|
51
51
|
*/
|
|
52
52
|
runSQLString(s: string): Promise<void>;
|
|
53
|
-
runQueryCount<ROW extends
|
|
54
|
-
streamQuery<ROW extends
|
|
55
|
-
saveBatch<ROW extends
|
|
53
|
+
runQueryCount<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
54
|
+
streamQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: MysqlDBOptions): ReadableTyped<ROW>;
|
|
55
|
+
saveBatch<ROW extends ObjectWithId>(table: string, rowsInput: ROW[], opt?: MysqlDBSaveOptions<ROW>): Promise<void>;
|
|
56
56
|
/**
|
|
57
57
|
* Limitation: always returns [], regardless of which rows are actually deleted
|
|
58
58
|
*/
|
|
59
59
|
deleteByIds(table: string, ids: string[], _opt?: MysqlDBOptions): Promise<number>;
|
|
60
|
-
deleteByQuery<ROW extends
|
|
60
|
+
deleteByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, _opt?: CommonDBOptions): Promise<number>;
|
|
61
61
|
/**
|
|
62
62
|
* Use with caution!
|
|
63
63
|
*/
|
|
@@ -65,8 +65,8 @@ export declare class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
65
65
|
/**
|
|
66
66
|
* dropIfExists=true needed as a safety check
|
|
67
67
|
*/
|
|
68
|
-
createTable<ROW extends
|
|
68
|
+
createTable<ROW extends ObjectWithId>(table: string, schema: JsonSchemaObject<ROW>, opt?: CommonDBCreateOptions): Promise<void>;
|
|
69
69
|
getTables(): Promise<string[]>;
|
|
70
|
-
getTableSchema<ROW extends
|
|
71
|
-
updateByQuery<ROW extends
|
|
70
|
+
getTableSchema<ROW extends ObjectWithId>(table: string): Promise<JsonSchemaRootObject<ROW>>;
|
|
71
|
+
updateByQuery<ROW extends ObjectWithId>(q: DBQuery<ROW>, patch: DBPatch<ROW>): Promise<number>;
|
|
72
72
|
}
|
package/package.json
CHANGED
package/src/mysql.db.ts
CHANGED
|
@@ -20,13 +20,11 @@ import {
|
|
|
20
20
|
_mapValues,
|
|
21
21
|
_Memo,
|
|
22
22
|
_omit,
|
|
23
|
-
AnyPartialObjectWithId,
|
|
24
23
|
CommonLogger,
|
|
25
24
|
commonLoggerPrefix,
|
|
26
25
|
JsonSchemaObject,
|
|
27
26
|
JsonSchemaRootObject,
|
|
28
|
-
|
|
29
|
-
Saved,
|
|
27
|
+
ObjectWithId,
|
|
30
28
|
} from '@naturalcycles/js-lib'
|
|
31
29
|
import { ReadableTyped, white } from '@naturalcycles/nodejs-lib'
|
|
32
30
|
import {
|
|
@@ -48,8 +46,7 @@ import {
|
|
|
48
46
|
} from './schema/mysql.schema.util'
|
|
49
47
|
|
|
50
48
|
export interface MysqlDBOptions extends CommonDBOptions {}
|
|
51
|
-
export interface MysqlDBSaveOptions<ROW extends
|
|
52
|
-
extends CommonDBSaveOptions<ROW> {}
|
|
49
|
+
export interface MysqlDBSaveOptions<ROW extends ObjectWithId> extends CommonDBSaveOptions<ROW> {}
|
|
53
50
|
|
|
54
51
|
/**
|
|
55
52
|
* @default false / undefined
|
|
@@ -196,11 +193,11 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
196
193
|
}
|
|
197
194
|
|
|
198
195
|
// GET
|
|
199
|
-
override async getByIds<ROW extends
|
|
196
|
+
override async getByIds<ROW extends ObjectWithId>(
|
|
200
197
|
table: string,
|
|
201
198
|
ids: string[],
|
|
202
199
|
opt: MysqlDBOptions = {},
|
|
203
|
-
): Promise<
|
|
200
|
+
): Promise<ROW[]> {
|
|
204
201
|
if (!ids.length) return []
|
|
205
202
|
const q = new DBQuery<ROW>(table).filterEq('id', ids)
|
|
206
203
|
const { rows } = await this.runQuery(q, opt)
|
|
@@ -208,10 +205,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
208
205
|
}
|
|
209
206
|
|
|
210
207
|
// QUERY
|
|
211
|
-
override async runQuery<ROW extends
|
|
208
|
+
override async runQuery<ROW extends ObjectWithId>(
|
|
212
209
|
q: DBQuery<ROW>,
|
|
213
210
|
_opt: MysqlDBOptions = {},
|
|
214
|
-
): Promise<RunQueryResult<
|
|
211
|
+
): Promise<RunQueryResult<ROW>> {
|
|
215
212
|
const sql = dbQueryToSQLSelect(q)
|
|
216
213
|
if (!sql) {
|
|
217
214
|
return {
|
|
@@ -261,7 +258,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
261
258
|
}
|
|
262
259
|
}
|
|
263
260
|
|
|
264
|
-
override async runQueryCount<ROW extends
|
|
261
|
+
override async runQueryCount<ROW extends ObjectWithId>(
|
|
265
262
|
q: DBQuery<ROW>,
|
|
266
263
|
_opt?: CommonDBOptions,
|
|
267
264
|
): Promise<number> {
|
|
@@ -269,10 +266,10 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
269
266
|
return (rows[0] as any)._count
|
|
270
267
|
}
|
|
271
268
|
|
|
272
|
-
override streamQuery<ROW extends
|
|
269
|
+
override streamQuery<ROW extends ObjectWithId>(
|
|
273
270
|
q: DBQuery<ROW>,
|
|
274
271
|
_opt: MysqlDBOptions = {},
|
|
275
|
-
): ReadableTyped<
|
|
272
|
+
): ReadableTyped<ROW> {
|
|
276
273
|
const sql = dbQueryToSQLSelect(q)
|
|
277
274
|
if (!sql) {
|
|
278
275
|
return Readable.from([])
|
|
@@ -295,7 +292,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
295
292
|
}
|
|
296
293
|
|
|
297
294
|
// SAVE
|
|
298
|
-
override async saveBatch<ROW extends
|
|
295
|
+
override async saveBatch<ROW extends ObjectWithId>(
|
|
299
296
|
table: string,
|
|
300
297
|
rowsInput: ROW[],
|
|
301
298
|
opt: MysqlDBSaveOptions<ROW> = {},
|
|
@@ -373,7 +370,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
373
370
|
return affectedRows
|
|
374
371
|
}
|
|
375
372
|
|
|
376
|
-
override async deleteByQuery<ROW extends
|
|
373
|
+
override async deleteByQuery<ROW extends ObjectWithId>(
|
|
377
374
|
q: DBQuery<ROW>,
|
|
378
375
|
_opt?: CommonDBOptions,
|
|
379
376
|
): Promise<number> {
|
|
@@ -393,7 +390,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
393
390
|
/**
|
|
394
391
|
* dropIfExists=true needed as a safety check
|
|
395
392
|
*/
|
|
396
|
-
override async createTable<ROW extends
|
|
393
|
+
override async createTable<ROW extends ObjectWithId>(
|
|
397
394
|
table: string,
|
|
398
395
|
schema: JsonSchemaObject<ROW>,
|
|
399
396
|
opt: CommonDBCreateOptions = {},
|
|
@@ -410,7 +407,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
410
407
|
.filter(Boolean)
|
|
411
408
|
}
|
|
412
409
|
|
|
413
|
-
override async getTableSchema<ROW extends
|
|
410
|
+
override async getTableSchema<ROW extends ObjectWithId>(
|
|
414
411
|
table: string,
|
|
415
412
|
): Promise<JsonSchemaRootObject<ROW>> {
|
|
416
413
|
const stats = await this.runSQL<MySQLTableStats[]>({
|
|
@@ -420,7 +417,7 @@ export class MysqlDB extends BaseCommonDB implements CommonDB {
|
|
|
420
417
|
return mysqlTableStatsToJsonSchemaField<ROW>(table, stats, this.cfg.logger)
|
|
421
418
|
}
|
|
422
419
|
|
|
423
|
-
override async updateByQuery<ROW extends
|
|
420
|
+
override async updateByQuery<ROW extends ObjectWithId>(
|
|
424
421
|
q: DBQuery<ROW>,
|
|
425
422
|
patch: DBPatch<ROW>,
|
|
426
423
|
): Promise<number> {
|
package/src/query.util.ts
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { DBQuery, DBQueryFilterOperator } from '@naturalcycles/db-lib'
|
|
2
|
-
import { _hb, CommonLogger } from '@naturalcycles/js-lib'
|
|
2
|
+
import { _hb, AnyObjectWithId, CommonLogger } from '@naturalcycles/js-lib'
|
|
3
3
|
import { white, yellow } from '@naturalcycles/nodejs-lib'
|
|
4
4
|
import { QueryOptions } from 'mysql'
|
|
5
5
|
import * as mysql from 'mysql'
|
|
@@ -160,7 +160,7 @@ export function dbQueryToSQLUpdate(q: DBQuery<any>, record: Record<any, any>): s
|
|
|
160
160
|
return mysql.format(tokens.join(' '), Object.values(record))
|
|
161
161
|
}
|
|
162
162
|
|
|
163
|
-
function selectTokens(q: DBQuery): string[] {
|
|
163
|
+
function selectTokens(q: DBQuery<any>): string[] {
|
|
164
164
|
let fields = ['*']
|
|
165
165
|
|
|
166
166
|
if (q._selectedFieldNames) {
|
|
@@ -192,7 +192,7 @@ function offsetLimitTokens(q: DBQuery<any>): string[] {
|
|
|
192
192
|
return tokens
|
|
193
193
|
}
|
|
194
194
|
|
|
195
|
-
function groupOrderTokens(q: DBQuery): string[] {
|
|
195
|
+
function groupOrderTokens(q: DBQuery<AnyObjectWithId>): string[] {
|
|
196
196
|
const t: string[] = []
|
|
197
197
|
|
|
198
198
|
if (q._groupByFieldNames?.length) {
|
|
@@ -216,7 +216,7 @@ const OP_MAP: Partial<Record<DBQueryFilterOperator, string>> = {
|
|
|
216
216
|
/**
|
|
217
217
|
* Returns `null` for "guaranteed 0 rows" cases.
|
|
218
218
|
*/
|
|
219
|
-
function getWhereTokens(q: DBQuery): string[] | null {
|
|
219
|
+
function getWhereTokens(q: DBQuery<any>): string[] | null {
|
|
220
220
|
if (!q._filters.length) return []
|
|
221
221
|
|
|
222
222
|
let returnNull = false
|