Learn about our RFC process, Open RFC meetings & more.Join in the discussion! »

small-indexeddb

2.0.0 • Public • Published

Goals

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)
}

License

WTFPL

Install

npm i small-indexeddb

DownloadsWeekly Downloads

3

Version

2.0.0

License

WTFPL

Unpacked Size

10.2 kB

Total Files

6

Last publish

Collaborators

  • avatar