@@ -2,16 +2,16 @@ import {
2
2
CommonDBCreateOptions ,
3
3
CommonKeyValueDB ,
4
4
commonKeyValueDBFullSupport ,
5
- KeyValueDBTuple ,
6
5
} from '@naturalcycles/db-lib'
7
- import { AppError , ObjectWithId , pMap , StringMap } from '@naturalcycles/js-lib'
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 : Buffer
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 < KeyValueDBTuple [ ] > {
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 : KeyValueDBTuple [ ] ) : Promise < void > {
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 < Buffer > {
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 : Buffer } > ) . map ( row => row . 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 < KeyValueDBTuple > {
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 > ) . map ( row => [
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
}
0 commit comments