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

Commit

Permalink
fix: add guard for un open db
Browse files Browse the repository at this point in the history
  • Loading branch information
hugomrdias committed Apr 1, 2020
1 parent a2464b7 commit d06332d
Showing 1 changed file with 22 additions and 0 deletions.
22 changes: 22 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function typedarrayToBuffer (arr) {
class IdbDatastore {
constructor (location) {
this.location = location
this.store = null
}

async open () {
Expand All @@ -54,6 +55,9 @@ class IdbDatastore {
}

async put (key, val) {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
try {
await this.store.put(this.location, val, key.toBuffer())
} catch (err) {
Expand All @@ -62,6 +66,9 @@ class IdbDatastore {
}

async get (key) {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
let value
try {
value = await this.store.get(this.location, key.toBuffer())
Expand All @@ -77,6 +84,9 @@ class IdbDatastore {
}

async has (key) {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
try {
await this.get(key)
} catch (err) {
Expand All @@ -87,6 +97,9 @@ class IdbDatastore {
}

async delete (key) {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
try {
await this.store.delete(this.location, key.toBuffer())
} catch (err) {
Expand All @@ -106,6 +119,9 @@ class IdbDatastore {
dels.push(key.toBuffer())
},
commit: async () => {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
const tx = this.store.transaction(this.location, 'readwrite')
const store = tx.store
await Promise.all(puts.map(p => store.put(p[1], p[0])))
Expand All @@ -116,6 +132,9 @@ class IdbDatastore {
}

query (q) {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
let limit = 0

let it = (async function * (store, location) {
Expand Down Expand Up @@ -157,6 +176,9 @@ class IdbDatastore {
}

close () {
if (this.store === null) {
throw new Error('Datastore needs to be opened.')
}
return this.store.close()
}

Expand Down

0 comments on commit d06332d

Please sign in to comment.