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

Commit

Permalink
fix: fix constructor (#58)
Browse files Browse the repository at this point in the history
Level constructor is async so we need to wait for it and not run this in the `datastore-level` constructor.

Co-authored-by: achingbrain <alex@achingbrain.net>
  • Loading branch information
hugomrdias and achingbrain authored Jan 22, 2021
1 parent 51cd55e commit 621e425
Showing 1 changed file with 27 additions and 18 deletions.
45 changes: 27 additions & 18 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,36 +30,45 @@ class LevelDatastore extends Adapter {
*/
constructor (path, opts) {
super()

let database
this.path = path
this.opts = opts

if (opts && opts.db) {
database = opts.db
this.database = opts.db
delete opts.db
} else {
// @ts-ignore
database = require('level')
this.database = require('level')
}

this.db = this._initDb(database, path, opts)
}

/**
* @param {(arg0: any, arg1: any) => any} database
* @param {string} path
* @param {any} opts
*/
_initDb (database, path, opts = {}) {
return database(path, {
...opts,
valueEncoding: 'binary',
compression: false // same default as go
_initDb () {
return new Promise((resolve, reject) => {
this.db = this.database(
this.path,
{
...this.opts,
valueEncoding: 'binary',
compression: false // same default as go
},
/** @param {Error} [err] */
(err) => {
if (err) {
return reject(err)
}
resolve(this.db)
}
)
})
}

async open () {
try {
await this.db.open()
if (this.db) {
await this.db.open()
} else {
this.db = await this._initDb()
}
} catch (err) {
throw Errors.dbOpenFailedError(err)
}
Expand Down Expand Up @@ -119,7 +128,7 @@ class LevelDatastore extends Adapter {
}

close () {
return this.db.close()
return this.db && this.db.close()
}

/**
Expand Down

0 comments on commit 621e425

Please sign in to comment.