@naturalcycles/mysql-lib 3.16.0 → 3.16.1
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/mysqlKeyValueDB.d.ts +8 -7
- package/dist/mysqlKeyValueDB.js +1 -1
- package/package.json +1 -1
- package/src/mysqlKeyValueDB.ts +12 -15
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-
import { CommonDBCreateOptions, CommonKeyValueDB
|
|
2
|
-
import {
|
|
1
|
+
import { CommonDBCreateOptions, CommonKeyValueDB } from '@naturalcycles/db-lib';
|
|
2
|
+
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB';
|
|
3
|
+
import { KeyValueTuple } from '@naturalcycles/js-lib';
|
|
3
4
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib';
|
|
4
5
|
import { MysqlDB, MysqlDBCfg } from './mysql.db';
|
|
5
6
|
export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
@@ -13,19 +14,19 @@ export declare class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
13
14
|
ping(): Promise<void>;
|
|
14
15
|
close(): Promise<void>;
|
|
15
16
|
createTable(table: string, opt?: CommonDBCreateOptions): Promise<void>;
|
|
16
|
-
getByIds(table: string, ids: string[]): Promise<
|
|
17
|
+
getByIds<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]>;
|
|
17
18
|
/**
|
|
18
19
|
* Use with caution!
|
|
19
20
|
*/
|
|
20
21
|
dropTable(table: string): Promise<void>;
|
|
21
22
|
deleteByIds(table: string, ids: string[]): Promise<void>;
|
|
22
|
-
saveBatch(table: string, entries:
|
|
23
|
+
saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void>;
|
|
23
24
|
streamIds(table: string, limit?: number): ReadableTyped<string>;
|
|
24
|
-
streamValues(table: string, limit?: number): ReadableTyped<
|
|
25
|
-
streamEntries(table: string, limit?: number): ReadableTyped<
|
|
25
|
+
streamValues<V>(table: string, limit?: number): ReadableTyped<V>;
|
|
26
|
+
streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>>;
|
|
26
27
|
beginTransaction(): Promise<void>;
|
|
27
28
|
endTransaction(): Promise<void>;
|
|
28
29
|
count(table: string): Promise<number>;
|
|
29
30
|
increment(_table: string, _id: string, _by?: number): Promise<number>;
|
|
30
|
-
incrementBatch(_table: string,
|
|
31
|
+
incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]>;
|
|
31
32
|
}
|
package/dist/mysqlKeyValueDB.js
CHANGED
|
@@ -104,7 +104,7 @@ class MySQLKeyValueDB {
|
|
|
104
104
|
async increment(_table, _id, _by) {
|
|
105
105
|
throw new js_lib_1.AppError('MySQLKeyValueDB.increment() is not implemented');
|
|
106
106
|
}
|
|
107
|
-
async incrementBatch(_table,
|
|
107
|
+
async incrementBatch(_table, _entries) {
|
|
108
108
|
throw new js_lib_1.AppError('MySQLKeyValueDB.incrementBatch() is not implemented');
|
|
109
109
|
}
|
|
110
110
|
}
|
package/package.json
CHANGED
package/src/mysqlKeyValueDB.ts
CHANGED
|
@@ -2,16 +2,16 @@ import {
|
|
|
2
2
|
CommonDBCreateOptions,
|
|
3
3
|
CommonKeyValueDB,
|
|
4
4
|
commonKeyValueDBFullSupport,
|
|
5
|
-
KeyValueDBTuple,
|
|
6
5
|
} from '@naturalcycles/db-lib'
|
|
7
|
-
import {
|
|
6
|
+
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB'
|
|
7
|
+
import { AppError, KeyValueTuple, ObjectWithId, pMap } from '@naturalcycles/js-lib'
|
|
8
8
|
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
|
|
9
9
|
import { QueryOptions } from 'mysql'
|
|
10
10
|
import { MysqlDB, MysqlDBCfg } from './mysql.db'
|
|
11
11
|
|
|
12
|
-
interface KeyValueObject {
|
|
12
|
+
interface KeyValueObject<V> {
|
|
13
13
|
id: string
|
|
14
|
-
v:
|
|
14
|
+
v: V
|
|
15
15
|
}
|
|
16
16
|
|
|
17
17
|
export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
@@ -45,12 +45,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
45
45
|
await this.db.runSQL({ sql })
|
|
46
46
|
}
|
|
47
47
|
|
|
48
|
-
async getByIds(table: string, ids: string[]): Promise<
|
|
48
|
+
async getByIds<V>(table: string, ids: string[]): Promise<KeyValueTuple<string, V>[]> {
|
|
49
49
|
if (!ids.length) return []
|
|
50
50
|
|
|
51
51
|
const sql = `SELECT id,v FROM ${table} where id in (${ids.map(id => `"${id}"`).join(',')})`
|
|
52
52
|
|
|
53
|
-
const rows = await this.db.runSQL<KeyValueObject[]>({ sql })
|
|
53
|
+
const rows = await this.db.runSQL<KeyValueObject<V>[]>({ sql })
|
|
54
54
|
|
|
55
55
|
return rows.map(({ id, v }) => [id, v])
|
|
56
56
|
}
|
|
@@ -68,7 +68,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
68
68
|
await this.db.runSQL({ sql })
|
|
69
69
|
}
|
|
70
70
|
|
|
71
|
-
async saveBatch(table: string, entries:
|
|
71
|
+
async saveBatch<V>(table: string, entries: KeyValueTuple<string, V>[]): Promise<void> {
|
|
72
72
|
const statements: QueryOptions[] = entries.map(([id, buf]) => {
|
|
73
73
|
return {
|
|
74
74
|
sql: `INSERT INTO ${table} (id, v) VALUES (?, ?)`,
|
|
@@ -90,20 +90,20 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
90
90
|
return (this.db.pool().query(sql).stream() as ReadableTyped<ObjectWithId>).map(row => row.id)
|
|
91
91
|
}
|
|
92
92
|
|
|
93
|
-
streamValues(table: string, limit?: number): ReadableTyped<
|
|
93
|
+
streamValues<V>(table: string, limit?: number): ReadableTyped<V> {
|
|
94
94
|
let sql = `SELECT v FROM ${table}`
|
|
95
95
|
if (limit) sql += ` LIMIT ${limit}`
|
|
96
96
|
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
97
97
|
|
|
98
|
-
return (this.db.pool().query(sql).stream() as ReadableTyped<{ v:
|
|
98
|
+
return (this.db.pool().query(sql).stream() as ReadableTyped<{ v: V }>).map(row => row.v)
|
|
99
99
|
}
|
|
100
100
|
|
|
101
|
-
streamEntries(table: string, limit?: number): ReadableTyped<
|
|
101
|
+
streamEntries<V>(table: string, limit?: number): ReadableTyped<KeyValueTuple<string, V>> {
|
|
102
102
|
let sql = `SELECT id,v FROM ${table}`
|
|
103
103
|
if (limit) sql += ` LIMIT ${limit}`
|
|
104
104
|
if (this.cfg.logSQL) this.db.cfg.logger.log(`stream: ${sql}`)
|
|
105
105
|
|
|
106
|
-
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject
|
|
106
|
+
return (this.db.pool().query(sql).stream() as ReadableTyped<KeyValueObject<V>>).map(row => [
|
|
107
107
|
row.id,
|
|
108
108
|
row.v,
|
|
109
109
|
])
|
|
@@ -129,10 +129,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
|
|
|
129
129
|
throw new AppError('MySQLKeyValueDB.increment() is not implemented')
|
|
130
130
|
}
|
|
131
131
|
|
|
132
|
-
async incrementBatch(
|
|
133
|
-
_table: string,
|
|
134
|
-
_incrementMap: StringMap<number>,
|
|
135
|
-
): Promise<StringMap<number>> {
|
|
132
|
+
async incrementBatch(_table: string, _entries: IncrementTuple[]): Promise<IncrementTuple[]> {
|
|
136
133
|
throw new AppError('MySQLKeyValueDB.incrementBatch() is not implemented')
|
|
137
134
|
}
|
|
138
135
|
}
|