@tabular-state/database
TypeScript icon, indicating that this package has built-in type declarations

1.0.1-alpha.4 • Public • Published

@tabular-state/database

A database adapter for @tabular-state/store to persist state in IndexedDB.

Warning Persisting state is currently only supported in browsers with indexeddb support.

import { createStore } from '@tabular-state/store';

const store = createStore<Tables>();

Using IndexedDB

Uses idb-keyval under the hood.

npm install @tabular-state/database idb-keyval
pnpm install @tabular-state/database idb-keyval
yarn add @tabular-state/database idb-keyval
import { createIndexedDbAdapter } from '@tabular-state/database';

const database = ceateIndexedDbAdapter({
  autoPersistTables: [['users', 'id']],
  checkAutoPersistTables(tableName) {
    if (tableName === 'any-dynamic-database') {
      return 'customIdField';
    }
    return false;
  },
  onRevalidate(table, ids) {
    // do something when persisted state has been revalidated
  },
});
// or with specific namespace
const database = ceateIndexedDbAdapter({
  namespace: 'account-1',
  // ...
});

store.plugin(database);

Namespaces

It is possible to implement splitted databases by switching the database namespace. A possible use case is an application where the user can switch between multiple accounts and or workspaces.

database.setNamespace('account-2');
// this will clear current store and reload persisted state from account-2

Readme

Keywords

none

Package Sidebar

Install

npm i @tabular-state/database

Weekly Downloads

1

Version

1.0.1-alpha.4

License

MIT

Unpacked Size

20.5 kB

Total Files

12

Last publish

Collaborators

  • ps73