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

Commit

Permalink
fix: if we are in a transaction, use the transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
achingbrain committed Jun 10, 2020
1 parent 807e425 commit 5c7f97b
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"dependencies": {
"buffer": "^5.5.0",
"idb": "^5.0.2",
"interface-datastore": "^1.0.2"
"interface-datastore": "^1.0.3"
},
"devDependencies": {
"aegir": "^22.0.0",
Expand Down
30 changes: 24 additions & 6 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,13 +84,13 @@ class IdbDatastore extends Adapter {
tx.onabort = cleanup

this._tx = {
tx,
store: tx.store,
done
}
}

// we only operate on one object store so the tx.store property is set
return this._tx.tx.store
return this._tx.store
}

async * _queryIt (q) {
Expand Down Expand Up @@ -146,7 +146,11 @@ class IdbDatastore extends Adapter {

async put (key, val) {
try {
await this._getStore().put(val, key.toBuffer())
if (this._tx) {
await this._tx.store.put(val, key.toBuffer())
} else {
await this.store.put(this.location, val, key.toBuffer())
}
} catch (err) {
throw Errors.dbWriteFailedError(err)
}
Expand All @@ -155,7 +159,11 @@ class IdbDatastore extends Adapter {
async get (key) {
let value
try {
value = await this._getStore().get(key.toBuffer())
if (this._tx) {
value = await this._tx.store.get(key.toBuffer())
} else {
value = await this.store.get(this.location, key.toBuffer())
}
} catch (err) {
throw Errors.dbWriteFailedError(err)
}
Expand All @@ -169,7 +177,13 @@ class IdbDatastore extends Adapter {

async has (key) {
try {
const res = await this._getStore().getKey(key.toBuffer())
let res

if (this._tx) {
res = await this._tx.store.getKey(key.toBuffer())
} else {
res = await this.store.getKey(this.location, key.toBuffer())
}

return Boolean(res)
} catch (err) {
Expand All @@ -180,7 +194,11 @@ class IdbDatastore extends Adapter {

async delete (key) {
try {
await this._getStore().delete(key.toBuffer())
if (this._tx) {
await this._tx.store.delete(key.toBuffer())
} else {
await this.store.delete(this.location, key.toBuffer())
}
} catch (err) {
throw Errors.dbDeleteFailedError(err)
}
Expand Down

0 comments on commit 5c7f97b

Please sign in to comment.