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

Commit fb5bed4

Browse files
committed
fix: if we are in a transaction, use the transaction
1 parent 807e425 commit fb5bed4

File tree

1 file changed

+24
-6
lines changed

1 file changed

+24
-6
lines changed

src/index.js

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,13 @@ class IdbDatastore extends Adapter {
8484
tx.onabort = cleanup
8585

8686
this._tx = {
87-
tx,
87+
store: tx.store,
8888
done
8989
}
9090
}
9191

9292
// we only operate on one object store so the tx.store property is set
93-
return this._tx.tx.store
93+
return this._tx.store
9494
}
9595

9696
async * _queryIt (q) {
@@ -146,7 +146,11 @@ class IdbDatastore extends Adapter {
146146

147147
async put (key, val) {
148148
try {
149-
await this._getStore().put(val, key.toBuffer())
149+
if (this._tx) {
150+
await this._tx.store.put(val, key.toBuffer())
151+
} else {
152+
await this.store.put(this.location, val, key.toBuffer())
153+
}
150154
} catch (err) {
151155
throw Errors.dbWriteFailedError(err)
152156
}
@@ -155,7 +159,11 @@ class IdbDatastore extends Adapter {
155159
async get (key) {
156160
let value
157161
try {
158-
value = await this._getStore().get(key.toBuffer())
162+
if (this._tx) {
163+
value = await this._tx.store.get(key.toBuffer())
164+
} else {
165+
value = await this.store.get(this.location, key.toBuffer())
166+
}
159167
} catch (err) {
160168
throw Errors.dbWriteFailedError(err)
161169
}
@@ -169,7 +177,13 @@ class IdbDatastore extends Adapter {
169177

170178
async has (key) {
171179
try {
172-
const res = await this._getStore().getKey(key.toBuffer())
180+
let res
181+
182+
if (this._tx) {
183+
res = await this._tx.store.getKey(key.toBuffer())
184+
} else {
185+
res = await this.store.getKey(this.location, key.toBuffer())
186+
}
173187

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

181195
async delete (key) {
182196
try {
183-
await this._getStore().delete(key.toBuffer())
197+
if (this._tx) {
198+
await this._tx.store.delete(key.toBuffer())
199+
} else {
200+
await this.store.delete(this.location, key.toBuffer())
201+
}
184202
} catch (err) {
185203
throw Errors.dbDeleteFailedError(err)
186204
}

0 commit comments

Comments
 (0)