README
Goals
- simplify
IDBObjectStore
interactions - promise-based API, avoiding events
- expose low-friction multi-query transactions
- small, like idb-keyval and nanodb
- not large like localForage or Dexie or JsStore
- tests
IndexedDB model
- The browser exposes databases, identified by a string name
- Databases contain object stores, identified by a string name
- Object stores can have transactions applied to them with any number of read/write/delete actions
For now, this library ignores some IndexedDB features (version numbers, multiple object stores per database) for simplicity's sake.
API
import smallIndexedDb from 'small-indexeddb'
promise = smallIndexedDb(databaseName)
Returns a promise that resolves to a transaction
function for the given database. The store will be created if it does not exist yet.
Under the hood, the store name will be the same as the database name.
promise = transaction(mode, callbackFunction)
mode
must be either 'readonly'
, 'readwrite'
or 'readwriteflush'
.
callbackFunction
will be called immediately with a single IDBObjectStore
parameter.
Your callback function must return either an IDBRequest
or an array of IDBRequests
.
The returned promise will return the result of the single IDBRequest
, or an array containing the results of all the requests you returned.
async function main() {
const transaction = await smallIndexedDb('sweetness')
await transaction(`readwrite`, idbStore => idbStore.put(`totally a`, `a`))
const [ a, b ] = await transaction(`readonly`, idbStore => [
idbStore.get(`a`),
idbStore.get(`b`),
])
console.log(a, b)
}