@swan-io/indexed-db
TypeScript icon, indicating that this package has built-in type declarations

0.3.2 • Public • Published

@swan-io/indexed-db

Tests mit licence npm version bundlephobia

A resilient, Future-based key-value store for IndexedDB.

Installation

$ yarn add @swan-io/indexed-db
# --- or ---
$ npm install --save @swan-io/indexed-db

Quickstart

const store = openStore("myDatabaseName", "myStoreName");

store
  .setMany({
    firstName: "Mathieu",
    lastName: "Breton",
  })
  .flatMapOk(() => store.getMany(["firstName", "lastName"]))
  .flatMapOk(({ firstName, lastName }) => {
    console.log({
      firstName,
      lastName,
    });

    return store.clear();
  })
  .tapOk(() => {
    console.log("✅");
  });

API

Open a database, create a store if needed and returns methods to manipulate it.
Note that you can open multiple databases / stores, with different names.

const store = await openStore("myDatabaseName", "myStoreName", {
  enableInMemoryFallback: true, // keep data in-memory in cases of read failures (default: false)
  transactionRetries: 2, // retry failed transactions (default: 2)
  transactionTimeout: 500, // timeout a transaction when it takes too long (default: 500ms)
});

store.getMany

Get many values at once. Resolves with a record.

store
  .getMany(["firstName", "lastName"])
  .mapOk(({ firstName, lastName }) => console.log({ firstName, lastName }));

store.setMany

Set many key-value pairs at once.

store
  .setMany({ firstName: "Mathieu", lastName: "Breton" })
  .tapOk(() => console.log("✅"));

store.clear

Clear all values in the store.

store.clear().tapOk(() => console.log("✅"));

🙌 Acknowledgements

Package Sidebar

Install

npm i @swan-io/indexed-db

Weekly Downloads

124

Version

0.3.2

License

MIT

Unpacked Size

49.2 kB

Total Files

15

Last publish

Collaborators

  • swan-io