2.0.0 • Published 1 year ago
local-db-storage v2.0.0
local-db-storage
IndexedDBwrapper that mimicslocalStorageAPI
Install
npm install local-db-storageWhy
- If you want to use
IndexedDBbut don't want to deal with its complex API. - If you want to store more data than what
localStoragesupports 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
use-local-storage-state— React hook that persists data inlocalStorage.use-session-storage-state— React hook that persists data insessionStorage.