11import { CommonDBCreateOptions , CommonKeyValueDB , KeyValueDBTuple } from '@naturalcycles/db-lib'
2- import { pMap } from '@naturalcycles/js-lib'
3- import { Debug , readableCreate , ReadableTyped } from '@naturalcycles/nodejs-lib'
2+ import { CommonLogger , pMap } from '@naturalcycles/js-lib'
3+ import { readableCreate , ReadableTyped } from '@naturalcycles/nodejs-lib'
44import { boldWhite } from '@naturalcycles/nodejs-lib/dist/colors'
55import { Database , open } from 'sqlite'
66import * as sqlite3 from 'sqlite3'
@@ -27,17 +27,27 @@ export interface SQLiteKeyValueDBCfg {
2727 * @default false
2828 */
2929 debug ?: boolean
30+
31+ /**
32+ * Defaults to `console`
33+ */
34+ logger ?: CommonLogger
3035}
3136
3237interface KeyValueObject {
3338 id : string
3439 v : Buffer
3540}
3641
37- const log = Debug ( 'nc:sqlite' )
38-
3942export class SqliteKeyValueDB implements CommonKeyValueDB {
40- constructor ( public cfg : SQLiteKeyValueDBCfg ) { }
43+ constructor ( cfg : SQLiteKeyValueDBCfg ) {
44+ this . cfg = {
45+ logger : console ,
46+ ...cfg ,
47+ }
48+ }
49+
50+ cfg : SQLiteKeyValueDBCfg & { logger : CommonLogger }
4151
4252 _db ?: Database
4353
@@ -55,13 +65,13 @@ export class SqliteKeyValueDB implements CommonKeyValueDB {
5565 mode : OPEN_READWRITE | OPEN_CREATE , // tslint:disable-line
5666 ...this . cfg ,
5767 } )
58- log ( `${ boldWhite ( this . cfg . filename ) } opened` )
68+ this . cfg . logger . log ( `${ boldWhite ( this . cfg . filename ) } opened` )
5969 }
6070
6171 async close ( ) : Promise < void > {
6272 if ( ! this . _db ) return
6373 await this . db . close ( )
64- log ( `${ boldWhite ( this . cfg . filename ) } closed` )
74+ this . cfg . logger . log ( `${ boldWhite ( this . cfg . filename ) } closed` )
6575 }
6676
6777 async ping ( ) : Promise < void > {
@@ -72,7 +82,7 @@ export class SqliteKeyValueDB implements CommonKeyValueDB {
7282 if ( opt . dropIfExists ) await this . dropTable ( table )
7383
7484 const sql = `create table ${ table } (id TEXT PRIMARY KEY, v BLOB NOT NULL)`
75- console . log ( sql )
85+ this . cfg . logger . log ( sql )
7686 await this . db . exec ( sql )
7787 }
7888
@@ -85,7 +95,7 @@ export class SqliteKeyValueDB implements CommonKeyValueDB {
8595
8696 async deleteByIds ( table : string , ids : string [ ] ) : Promise < void > {
8797 const sql = deleteByIdsSQL ( table , ids )
88- if ( this . cfg . debug ) console . log ( sql )
98+ if ( this . cfg . debug ) this . cfg . logger . log ( sql )
8999 await this . db . run ( sql )
90100 }
91101
@@ -95,7 +105,7 @@ export class SqliteKeyValueDB implements CommonKeyValueDB {
95105 */
96106 async getByIds ( table : string , ids : string [ ] ) : Promise < KeyValueDBTuple [ ] > {
97107 const sql = selectKVSQL ( table , ids )
98- if ( this . cfg . debug ) console . log ( sql )
108+ if ( this . cfg . debug ) this . cfg . logger . log ( sql )
99109 const rows = await this . db . all < KeyValueObject [ ] > ( sql )
100110 // console.log(rows)
101111 return rows . map ( r => [ r . id , r . v ] )
@@ -109,7 +119,7 @@ export class SqliteKeyValueDB implements CommonKeyValueDB {
109119
110120 await pMap ( statements , async statement => {
111121 const [ sql , params ] = statement
112- if ( this . cfg . debug ) console . log ( sql )
122+ if ( this . cfg . debug ) this . cfg . logger . log ( sql )
113123 await this . db . run ( sql , ...params )
114124 } )
115125
@@ -199,7 +209,7 @@ export class SqliteKeyValueDB implements CommonKeyValueDB {
199209 async count ( table : string ) : Promise < number > {
200210 const sql = `SELECT count(*) as cnt FROM ${ table } `
201211
202- if ( this . cfg . debug ) console . log ( sql )
212+ if ( this . cfg . debug ) this . cfg . logger . log ( sql )
203213
204214 const { cnt } = ( await this . db . get < { cnt : number } > ( sql ) ) !
205215 return cnt
0 commit comments