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 fb5bed4
Showing 1 changed file with 24 additions and 6 deletions.
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 fb5bed4

Please sign in to comment.