@@ -2,16 +2,16 @@ import {
22 CommonDBCreateOptions ,
33 CommonKeyValueDB ,
44 commonKeyValueDBFullSupport ,
5- KeyValueDBTuple ,
65} 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'
88import { ReadableTyped } from '@naturalcycles/nodejs-lib'
99import { QueryOptions } from 'mysql'
1010import { MysqlDB , MysqlDBCfg } from './mysql.db'
1111
12- interface KeyValueObject {
12+ interface KeyValueObject < V > {
1313 id : string
14- v : Buffer
14+ v : V
1515}
1616
1717export class MySQLKeyValueDB implements CommonKeyValueDB {
@@ -45,12 +45,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
4545 await this . db . runSQL ( { sql } )
4646 }
4747
48- async getByIds ( table : string , ids : string [ ] ) : Promise < KeyValueDBTuple [ ] > {
48+ async getByIds < V > ( table : string , ids : string [ ] ) : Promise < KeyValueTuple < string , V > [ ] > {
4949 if ( ! ids . length ) return [ ]
5050
5151 const sql = `SELECT id,v FROM ${ table } where id in (${ ids . map ( id => `"${ id } "` ) . join ( ',' ) } )`
5252
53- const rows = await this . db . runSQL < KeyValueObject [ ] > ( { sql } )
53+ const rows = await this . db . runSQL < KeyValueObject < V > [ ] > ( { sql } )
5454
5555 return rows . map ( ( { id, v } ) => [ id , v ] )
5656 }
@@ -68,7 +68,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
6868 await this . db . runSQL ( { sql } )
6969 }
7070
71- async saveBatch ( table : string , entries : KeyValueDBTuple [ ] ) : Promise < void > {
71+ async saveBatch < V > ( table : string , entries : KeyValueTuple < string , V > [ ] ) : Promise < void > {
7272 const statements : QueryOptions [ ] = entries . map ( ( [ id , buf ] ) => {
7373 return {
7474 sql : `INSERT INTO ${ table } (id, v) VALUES (?, ?)` ,
@@ -90,20 +90,20 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
9090 return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < ObjectWithId > ) . map ( row => row . id )
9191 }
9292
93- streamValues ( table : string , limit ?: number ) : ReadableTyped < Buffer > {
93+ streamValues < V > ( table : string , limit ?: number ) : ReadableTyped < V > {
9494 let sql = `SELECT v FROM ${ table } `
9595 if ( limit ) sql += ` LIMIT ${ limit } `
9696 if ( this . cfg . logSQL ) this . db . cfg . logger . log ( `stream: ${ sql } ` )
9797
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 )
9999 }
100100
101- streamEntries ( table : string , limit ?: number ) : ReadableTyped < KeyValueDBTuple > {
101+ streamEntries < V > ( table : string , limit ?: number ) : ReadableTyped < KeyValueTuple < string , V > > {
102102 let sql = `SELECT id,v FROM ${ table } `
103103 if ( limit ) sql += ` LIMIT ${ limit } `
104104 if ( this . cfg . logSQL ) this . db . cfg . logger . log ( `stream: ${ sql } ` )
105105
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 => [
107107 row . id ,
108108 row . v ,
109109 ] )
@@ -129,10 +129,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
129129 throw new AppError ( 'MySQLKeyValueDB.increment() is not implemented' )
130130 }
131131
132- async incrementBatch (
133- _table : string ,
134- _incrementMap : StringMap < number > ,
135- ) : Promise < StringMap < number > > {
132+ async incrementBatch ( _table : string , _entries : IncrementTuple [ ] ) : Promise < IncrementTuple [ ] > {
136133 throw new AppError ( 'MySQLKeyValueDB.incrementBatch() is not implemented' )
137134 }
138135}
0 commit comments