@@ -2,16 +2,17 @@ import {
2
2
CommonDBCreateOptions ,
3
3
CommonKeyValueDB ,
4
4
commonKeyValueDBFullSupport ,
5
+ KeyValueDBTuple ,
5
6
} from '@naturalcycles/db-lib'
6
7
import { IncrementTuple } from '@naturalcycles/db-lib/dist/kv/commonKeyValueDB'
7
- import { AppError , KeyValueTuple , ObjectWithId , pMap } from '@naturalcycles/js-lib'
8
+ import { AppError , ObjectWithId , pMap } from '@naturalcycles/js-lib'
8
9
import { ReadableTyped } from '@naturalcycles/nodejs-lib'
9
10
import { QueryOptions } from 'mysql'
10
11
import { MysqlDB , MysqlDBCfg } from './mysql.db'
11
12
12
- interface KeyValueObject < V > {
13
+ interface KeyValueObject {
13
14
id : string
14
- v : V
15
+ v : Buffer
15
16
}
16
17
17
18
export class MySQLKeyValueDB implements CommonKeyValueDB {
@@ -45,12 +46,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
45
46
await this . db . runSQL ( { sql } )
46
47
}
47
48
48
- async getByIds < V > ( table : string , ids : string [ ] ) : Promise < KeyValueTuple < string , V > [ ] > {
49
+ async getByIds ( table : string , ids : string [ ] ) : Promise < KeyValueDBTuple [ ] > {
49
50
if ( ! ids . length ) return [ ]
50
51
51
52
const sql = `SELECT id,v FROM ${ table } where id in (${ ids . map ( id => `"${ id } "` ) . join ( ',' ) } )`
52
53
53
- const rows = await this . db . runSQL < KeyValueObject < V > [ ] > ( { sql } )
54
+ const rows = await this . db . runSQL < KeyValueObject [ ] > ( { sql } )
54
55
55
56
return rows . map ( ( { id, v } ) => [ id , v ] )
56
57
}
@@ -68,7 +69,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
68
69
await this . db . runSQL ( { sql } )
69
70
}
70
71
71
- async saveBatch < V > ( table : string , entries : KeyValueTuple < string , V > [ ] ) : Promise < void > {
72
+ async saveBatch ( table : string , entries : KeyValueDBTuple [ ] ) : Promise < void > {
72
73
const statements : QueryOptions [ ] = entries . map ( ( [ id , buf ] ) => {
73
74
return {
74
75
sql : `INSERT INTO ${ table } (id, v) VALUES (?, ?)` ,
@@ -90,20 +91,20 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
90
91
return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < ObjectWithId > ) . map ( row => row . id )
91
92
}
92
93
93
- streamValues < V > ( table : string , limit ?: number ) : ReadableTyped < V > {
94
+ streamValues ( table : string , limit ?: number ) : ReadableTyped < Buffer > {
94
95
let sql = `SELECT v FROM ${ table } `
95
96
if ( limit ) sql += ` LIMIT ${ limit } `
96
97
if ( this . cfg . logSQL ) this . db . cfg . logger . log ( `stream: ${ sql } ` )
97
98
98
- return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < { v : V } > ) . map ( row => row . v )
99
+ return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < { v : Buffer } > ) . map ( row => row . v )
99
100
}
100
101
101
- streamEntries < V > ( table : string , limit ?: number ) : ReadableTyped < KeyValueTuple < string , V > > {
102
+ streamEntries ( table : string , limit ?: number ) : ReadableTyped < KeyValueDBTuple > {
102
103
let sql = `SELECT id,v FROM ${ table } `
103
104
if ( limit ) sql += ` LIMIT ${ limit } `
104
105
if ( this . cfg . logSQL ) this . db . cfg . logger . log ( `stream: ${ sql } ` )
105
106
106
- return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < KeyValueObject < V > > ) . map ( row => [
107
+ return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < KeyValueObject > ) . map ( row => [
107
108
row . id ,
108
109
row . v ,
109
110
] )
@@ -125,10 +126,6 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
125
126
return rows [ 0 ] ! . cnt
126
127
}
127
128
128
- async increment ( _table : string , _id : string , _by ?: number ) : Promise < number > {
129
- throw new AppError ( 'MySQLKeyValueDB.increment() is not implemented' )
130
- }
131
-
132
129
async incrementBatch ( _table : string , _entries : IncrementTuple [ ] ) : Promise < IncrementTuple [ ] > {
133
130
throw new AppError ( 'MySQLKeyValueDB.incrementBatch() is not implemented' )
134
131
}
0 commit comments