Skip to content
This repository was archived by the owner on May 25, 2025. It is now read-only.

Commit ffd76e9

Browse files
fix: support CommonLogger
1 parent cb4feda commit ffd76e9

File tree

6 files changed

+681
-595
lines changed

6 files changed

+681
-595
lines changed

CHANGELOG.md

Lines changed: 1 addition & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1 @@
1-
## [1.2.1](https://github.com/NaturalCycles/sqlite-lib/compare/v1.2.0...v1.2.1) (2021-10-17)
2-
3-
4-
### Bug Fixes
5-
6-
* adapt to latest db-lib ([f043e61](https://github.com/NaturalCycles/sqlite-lib/commit/f043e61dced26d5d434a6e7b5f3f1af5b1dc7011))
7-
8-
# [1.2.0](https://github.com/NaturalCycles/sqlite-lib/compare/v1.1.0...v1.2.0) (2021-08-25)
9-
10-
11-
### Features
12-
13-
* `count` method on KeyValueDB ([3724a9e](https://github.com/NaturalCycles/sqlite-lib/commit/3724a9edad394e27052ad9c3af8c285ef60837cd))
14-
15-
# [1.1.0](https://github.com/NaturalCycles/sqlite-lib/compare/v1.0.3...v1.1.0) (2021-08-24)
16-
17-
18-
### Features
19-
20-
* implement streaming methods of KeyValueDB ([9da8ba7](https://github.com/NaturalCycles/sqlite-lib/commit/9da8ba7a62087b8ec36bfef8cb6c142fd43e972a))
21-
22-
## [1.0.3](https://github.com/NaturalCycles/sqlite-lib/compare/v1.0.2...v1.0.3) (2021-07-04)
23-
24-
25-
### Bug Fixes
26-
27-
* adapt naming `KV` => `KeyValue` ([193e80b](https://github.com/NaturalCycles/sqlite-lib/commit/193e80b370e7bcb52a63aeb516bb9745799b943d))
28-
29-
## [1.0.2](https://github.com/NaturalCycles/sqlite-lib/compare/v1.0.1...v1.0.2) (2021-07-03)
30-
31-
32-
### Bug Fixes
33-
34-
* don't begin/end transaction in saveBatch ([e01ec93](https://github.com/NaturalCycles/sqlite-lib/commit/e01ec9329122d4160cd26b83da3fcb522ac6fb42))
35-
36-
## [1.0.1](https://github.com/NaturalCycles/sqlite-lib/compare/v1.0.0...v1.0.1) (2021-06-27)
37-
38-
39-
### Bug Fixes
40-
41-
* test ([e72d0d1](https://github.com/NaturalCycles/sqlite-lib/commit/e72d0d10fc5c632f7307b6eef25b339b2d20c162))
42-
43-
# 1.0.0 (2021-06-27)
44-
45-
46-
### Features
47-
48-
* first version ([90035cc](https://github.com/NaturalCycles/sqlite-lib/commit/90035cce71bfc2bdd00f2f88982771c7dd597f54))
1+
See [Github Releases](https://github.com/NaturalCycles/sqlite-lib/releases)

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
"url": "https://github.com/NaturalCycles/sqlite-lib"
3535
},
3636
"engines": {
37-
"node": ">=12.13.1"
37+
"node": ">=14.15.0"
3838
},
3939
"version": "1.2.1",
4040
"description": "CommonDB implementation based on SQLite",

src/sqlite.db.ts

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ import {
55
CommonDBOptions,
66
CommonDBSaveOptions,
77
} from '@naturalcycles/db-lib'
8-
import { JsonSchemaObject, ObjectWithId } from '@naturalcycles/js-lib'
9-
import { Debug } from '@naturalcycles/nodejs-lib'
8+
import { CommonLogger, JsonSchemaObject, ObjectWithId } from '@naturalcycles/js-lib'
109
import { boldWhite } from '@naturalcycles/nodejs-lib/dist/colors'
1110
import { Database, open } from 'sqlite'
1211
import { OPEN_CREATE, OPEN_READWRITE } from 'sqlite3'
@@ -25,15 +24,21 @@ export interface SQLiteDBCfg {
2524
* @default sqlite.Database
2625
*/
2726
driver?: any
28-
}
2927

30-
const log = Debug('nc:sqlite')
28+
logger?: CommonLogger
29+
}
3130

3231
export class SQLiteDB extends BaseCommonDB implements CommonDB {
33-
constructor(public cfg: SQLiteDBCfg) {
32+
constructor(cfg: SQLiteDBCfg) {
3433
super()
34+
this.cfg = {
35+
logger: console,
36+
...cfg,
37+
}
3538
}
3639

40+
cfg: SQLiteDBCfg & { logger: CommonLogger }
41+
3742
_db?: Database
3843

3944
get db(): Database {
@@ -50,13 +55,13 @@ export class SQLiteDB extends BaseCommonDB implements CommonDB {
5055
mode: OPEN_READWRITE | OPEN_CREATE, // tslint:disable-line
5156
...this.cfg,
5257
})
53-
log(`${boldWhite(this.cfg.filename)} opened`)
58+
this.cfg.logger.log(`${boldWhite(this.cfg.filename)} opened`)
5459
}
5560

5661
async close(): Promise<void> {
5762
if (!this._db) return
5863
await this.db.close()
59-
log(`${boldWhite(this.cfg.filename)} closed`)
64+
this.cfg.logger.log(`${boldWhite(this.cfg.filename)} closed`)
6065
}
6166

6267
override async ping(): Promise<void> {

src/sqliteKeyValueDB.ts

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { 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'
44
import { boldWhite } from '@naturalcycles/nodejs-lib/dist/colors'
55
import { Database, open } from 'sqlite'
66
import * 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

3237
interface KeyValueObject {
3338
id: string
3439
v: Buffer
3540
}
3641

37-
const log = Debug('nc:sqlite')
38-
3942
export 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

src/test/setupJest.ts

Lines changed: 0 additions & 3 deletions
This file was deleted.

0 commit comments

Comments
 (0)