Skip to content
This repository was archived by the owner on Mar 23, 2023. It is now read-only.

Commit 621e425

Browse files
fix: fix constructor (#58)
Level constructor is async so we need to wait for it and not run this in the `datastore-level` constructor. Co-authored-by: achingbrain <[email protected]>
1 parent 51cd55e commit 621e425

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

src/index.js

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -30,36 +30,45 @@ class LevelDatastore extends Adapter {
3030
*/
3131
constructor (path, opts) {
3232
super()
33-
34-
let database
33+
this.path = path
34+
this.opts = opts
3535

3636
if (opts && opts.db) {
37-
database = opts.db
37+
this.database = opts.db
3838
delete opts.db
3939
} else {
4040
// @ts-ignore
41-
database = require('level')
41+
this.database = require('level')
4242
}
43-
44-
this.db = this._initDb(database, path, opts)
4543
}
4644

47-
/**
48-
* @param {(arg0: any, arg1: any) => any} database
49-
* @param {string} path
50-
* @param {any} opts
51-
*/
52-
_initDb (database, path, opts = {}) {
53-
return database(path, {
54-
...opts,
55-
valueEncoding: 'binary',
56-
compression: false // same default as go
45+
_initDb () {
46+
return new Promise((resolve, reject) => {
47+
this.db = this.database(
48+
this.path,
49+
{
50+
...this.opts,
51+
valueEncoding: 'binary',
52+
compression: false // same default as go
53+
},
54+
/** @param {Error} [err] */
55+
(err) => {
56+
if (err) {
57+
return reject(err)
58+
}
59+
resolve(this.db)
60+
}
61+
)
5762
})
5863
}
5964

6065
async open () {
6166
try {
62-
await this.db.open()
67+
if (this.db) {
68+
await this.db.open()
69+
} else {
70+
this.db = await this._initDb()
71+
}
6372
} catch (err) {
6473
throw Errors.dbOpenFailedError(err)
6574
}
@@ -119,7 +128,7 @@ class LevelDatastore extends Adapter {
119128
}
120129

121130
close () {
122-
return this.db.close()
131+
return this.db && this.db.close()
123132
}
124133

125134
/**

0 commit comments

Comments
 (0)