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

fix: fix constructor #58

Merged
merged 2 commits into from
Jan 22, 2021
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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