db64
A Practical IndexedDB API
A more practcal alternative to localStorage. db64 supports all major browsers.
db64
- Promise based API
- Set and get single or multiple entries
- Delete single, multiple or all entries
- No versioning
- 2.1kB minified
import db64 from './db64.js'
db64.use('animals')
.store('birds')
.setEntries({warbler: 4, cuckoo: 3, emu: 2})
db64.from('birds').getEntries(['emu', 'warbler'])
db64.from('birds').delete(['cuckoo', 'emu'])
db64.clear('birds')
Why IndexedDB, why not localStorage?
- Better performance
- Asynchronous (localStorage is blocking)
- Larger storage quota (localStorage is capped at 5MB)
- Reliable (no type coercion)
- Uses the structuredClone algorithm
Practical challenges when using IndexedDB
- It's event driven, without promises
- It was designed to encourage versioning, which is not necessary for the majority of projects
- The API is considered as (low level) and is not a drop in replacement for localStorage
- Removing databases and stores is not straight forward and usually requires versioning
Install db64
npm i db64
Create or use an existing database [string] (the default DB name is "default")
db64.use('localDB')
Create a store and set data. See structured clone algorithm for supported types.
db64.store('fruits').set('some-key', 'some-value')
Get data [string]
const someKey = await db64.store('fruits').get('some-key')
Set multiple entries [Object] | [Array]
db64.store('fruits').setEntries({ bananas: 5, pears: 4, mangoes: 7, apples: 2 })
db64.store('fruits').setEntries([ 'bananas', 'pears', 'mangoes', 'apples' ])
Get multiple entries [Array]
const rosaceaeFruits = await db64.store('fruits').setEntries([ 'pears', 'apples' ])
Delete a single or multiple entries [String] | [Array]
db64.store('fruits').delete('mangoes')
db64.store('fruits').delete([ 'bananas', 'mangoes' ])
Empty an object store [String]
db64.clear('fruits')
Why bd64 opts out of deleting databases and object stores
Deleting existing versions of databases or object stores in IndexedDB is not feasible due to the requirement to create a new version, and old versions remain accessible. While compaction may optimize, it doesn't ensure the removal of unwanted data. db64 provides an effective solution by allowing you to clear an object store, removing all its data. This feature proves beneficial for any application, even in cases where empty stores cannot be removed. If you do require versioning consider using idb. If you're not building a progressive web app (PWA) you probably don't need versioning.
Contributors
Don't hesitate just contribute, it's a tiny library we will figure it out.
MIT (c) Julien Etienne 2023