@@ -2,16 +2,17 @@ import {
22 CommonDBCreateOptions ,
33 CommonKeyValueDB ,
44 commonKeyValueDBFullSupport ,
5+ KeyValueDBTuple ,
56} from '@naturalcycles/db-lib'
67import { 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'
89import { ReadableTyped } from '@naturalcycles/nodejs-lib'
910import { QueryOptions } from 'mysql'
1011import { MysqlDB , MysqlDBCfg } from './mysql.db'
1112
12- interface KeyValueObject < V > {
13+ interface KeyValueObject {
1314 id : string
14- v : V
15+ v : Buffer
1516}
1617
1718export class MySQLKeyValueDB implements CommonKeyValueDB {
@@ -45,12 +46,12 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
4546 await this . db . runSQL ( { sql } )
4647 }
4748
48- async getByIds < V > ( table : string , ids : string [ ] ) : Promise < KeyValueTuple < string , V > [ ] > {
49+ async getByIds ( table : string , ids : string [ ] ) : Promise < KeyValueDBTuple [ ] > {
4950 if ( ! ids . length ) return [ ]
5051
5152 const sql = `SELECT id,v FROM ${ table } where id in (${ ids . map ( id => `"${ id } "` ) . join ( ',' ) } )`
5253
53- const rows = await this . db . runSQL < KeyValueObject < V > [ ] > ( { sql } )
54+ const rows = await this . db . runSQL < KeyValueObject [ ] > ( { sql } )
5455
5556 return rows . map ( ( { id, v } ) => [ id , v ] )
5657 }
@@ -68,7 +69,7 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
6869 await this . db . runSQL ( { sql } )
6970 }
7071
71- async saveBatch < V > ( table : string , entries : KeyValueTuple < string , V > [ ] ) : Promise < void > {
72+ async saveBatch ( table : string , entries : KeyValueDBTuple [ ] ) : Promise < void > {
7273 const statements : QueryOptions [ ] = entries . map ( ( [ id , buf ] ) => {
7374 return {
7475 sql : `INSERT INTO ${ table } (id, v) VALUES (?, ?)` ,
@@ -90,20 +91,20 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
9091 return ( this . db . pool ( ) . query ( sql ) . stream ( ) as ReadableTyped < ObjectWithId > ) . map ( row => row . id )
9192 }
9293
93- streamValues < V > ( table : string , limit ?: number ) : ReadableTyped < V > {
94+ streamValues ( table : string , limit ?: number ) : ReadableTyped < Buffer > {
9495 let sql = `SELECT v FROM ${ table } `
9596 if ( limit ) sql += ` LIMIT ${ limit } `
9697 if ( this . cfg . logSQL ) this . db . cfg . logger . log ( `stream: ${ sql } ` )
9798
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 )
99100 }
100101
101- streamEntries < V > ( table : string , limit ?: number ) : ReadableTyped < KeyValueTuple < string , V > > {
102+ streamEntries ( table : string , limit ?: number ) : ReadableTyped < KeyValueDBTuple > {
102103 let sql = `SELECT id,v FROM ${ table } `
103104 if ( limit ) sql += ` LIMIT ${ limit } `
104105 if ( this . cfg . logSQL ) this . db . cfg . logger . log ( `stream: ${ sql } ` )
105106
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 => [
107108 row . id ,
108109 row . v ,
109110 ] )
@@ -125,10 +126,6 @@ export class MySQLKeyValueDB implements CommonKeyValueDB {
125126 return rows [ 0 ] ! . cnt
126127 }
127128
128- async increment ( _table : string , _id : string , _by ?: number ) : Promise < number > {
129- throw new AppError ( 'MySQLKeyValueDB.increment() is not implemented' )
130- }
131-
132129 async incrementBatch ( _table : string , _entries : IncrementTuple [ ] ) : Promise < IncrementTuple [ ] > {
133130 throw new AppError ( 'MySQLKeyValueDB.incrementBatch() is not implemented' )
134131 }
0 commit comments