local-db-storage
IndexedDB
wrapper that mimics localStorage
API
Install
npm install local-db-storage
Why
- If you want to use
IndexedDB
but don't want to deal with its complex API. - If you want to store more data than what
localStorage
supports but still want the same API. - The most popular library
localForage
(25k stars) is complex and unmaintained. - I've been consistent in maintaining
use-local-storage-state
(400k downloads per month) for the past 4 years.
Usage
import dbStorage from 'local-db-storage'
async function addTodo(todo): Promise<void> {
await dbStorage.setItem(todo.id, todo)
}
async function getTodo(id: string): Promise<Todo> {
await dbStorage.getItem<Todo>(id)
}
API
getItem<T>(key: string): Promise<T>
Like localStorage.getItem()
but async.
setItem(key: string, value: any): Promise<void>
Like localStorage.setItem()
but async.
Note: It supports non-primitive values (ex: objects). It also supports circular references.
removeItem(key: string): Promise<void>
Like localStorage.removeItem()
but async.
clear(): Promise<void>
Like localStorage.clear()
but async.
DBStorage(name: string)
Creates a new DBStorage
instance.
import { DBStorage } from 'local-db-storage'
const dbStorage = new DBStorage('my-custom-db-storage')
await dbStorage.setItem('initial', 'hello world')
Related