idb-wrapped
v0.0.0
Published
A warp of IndexedDB
Downloads
1
Readme
idb-wrapped test
A warp of IndexedDB.
Install
npm install idb-wrapped
Usage
import openDatabase, { Store, store } from 'idb-wrapped'
// Define stores:
@store('storeName', { autoIncrement: true })
class Test extends Store <{ a: string }> { }
@store('storeName1', { keyPath: 'id', autoIncrement: true, indexes: { name: { unique: true } } })
class Test1 extends Store <{ b: string }> { }
@store('storeName3')
class Test3 extends Store <string> {
public static createStore (db: Database) { /* db.createStore(...) */ }
}
// Open databse:
const fn = async () => {
const db = await openDatabase('databaseName', [Test, Test1, Test2])
const ret1 = await db.transaction([Test], async (it: Test) => {
await it.put({ a: '123' })
await it.add({ a: 'bc' })
const obj = await it.get(1)
obj.a = '233'
await it.put(obj, 1)
return obj.a
}, true /* readwrite */)
console.log(ret1) // '233'
const [obj1, obj2] = await db.transaction([Test], (it: Test) => Promise.all([it.get(1), it.get(2)])) // readonly
console.log(obj1, obj2)
}
fn().catch(console.error)
Notice
Due to the limitations of IndexedDB, you cannot use other Promise in a transaction.
await db.transaction([Test], async (it: Test) => {
const obj = await it.get(1)
const ret = await fetch('https://example.com').then(it => it.text()) // Note here, this is not allowed!
obj.a = ret
await it.put(ret, 1) // An exception will occur here
}, true)
So you should use it like this:
const obj = await db.transaction([Test], it => it.get(1))
obj.a = await fetch('https://example.com').then(it => it.text())
await db.transaction([Test], async it => {
await it.put(obj, 1)
}, true)
Author
Shirasawa