@singletn/indexeddb
TypeScript icon, indicating that this package has built-in type declarations

0.0.17 • Public • Published

@singletn/indexeddb  npm version

Persist your data

If you'd like to have a persistent state in indexeddb, you can do so by having your state extend IndexedDBSingletn.

The implementation is the same as using the SingletnState from @singletn/core.

import { IndexedDBSingletn } from '@singletn/indexeddb'

interface UserState {
  name: string
  email: string
  phoneNumber: string
}

export class User extends IndexedDBSingletn<UserState> {
  state = {
    name: '',
    email: '',
    phoneNumber: '',
  }

  public setUser = (user: UserState) => this.setState(user)

  public setName = (name) => this.setState({ name })

  public setEmail = (email) => this.setState({ email })

  // ...
}

Cleanup remark

Anytime that you use clearSingletns from @singletn/core, the databases created will be all cleared, and the data will, obviously, no longer persist.

If, however, you'd like to manually trigger a deletion of the database for any given singletn, you can call the clearDB method:

export class User extends IndexedDBSingletn<UserState> {
  // ...

  public cleanup = () => this.clearDB()

  // ...
}

Other ways to store your state

singletn also allows you to use different base states to store your data in other ways. Read more about it in the subprojects:

Readme

Keywords

none

Package Sidebar

Install

npm i @singletn/indexeddb

Weekly Downloads

0

Version

0.0.17

License

MIT

Unpacked Size

55.1 kB

Total Files

8

Last publish

Collaborators

  • nicreichert