From 027ceadb1c5531a65d8e62a7d60d597bfc645381 Mon Sep 17 00:00:00 2001 From: Hugo Dias Date: Thu, 21 Jan 2021 21:28:40 +0000 Subject: [PATCH 1/2] fix: fix constructor Level constructor call db.open which is async so we need to wait for it and not run this in the constructor. --- src/index.js | 48 +++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 19 deletions(-) diff --git a/src/index.js b/src/index.js index d375a34..c69b478 100644 --- a/src/index.js +++ b/src/index.js @@ -30,36 +30,46 @@ class LevelDatastore extends Adapter { */ constructor (path, opts) { super() - - let database - + this.path = path + this.opts = opts + this.isCreated = false 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() + this.isCreated = true + } } catch (err) { throw Errors.dbOpenFailedError(err) } @@ -119,7 +129,7 @@ class LevelDatastore extends Adapter { } close () { - return this.db.close() + return this.db && this.db.close() } /** From fe18a811ab5e49c9caf9e47b286bb4285010a4e1 Mon Sep 17 00:00:00 2001 From: achingbrain Date: Fri, 22 Jan 2021 18:52:01 +0000 Subject: [PATCH 2/2] chore: remove unused field --- src/index.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/index.js b/src/index.js index c69b478..8638c45 100644 --- a/src/index.js +++ b/src/index.js @@ -32,7 +32,7 @@ class LevelDatastore extends Adapter { super() this.path = path this.opts = opts - this.isCreated = false + if (opts && opts.db) { this.database = opts.db delete opts.db @@ -68,7 +68,6 @@ class LevelDatastore extends Adapter { await this.db.open() } else { this.db = await this._initDb() - this.isCreated = true } } catch (err) { throw Errors.dbOpenFailedError(err)