1.0.0 • Published 6 months ago
idb-migration v1.0.0
idb-migration
Helper function for migrating IndexedDB databases.
Features
- Morph based migration, version is automatically incremented
- Fix "promise not resolved" bug when creating object store and index in the same IndexedDB upgrade
- Typescript support
Installation
npm install idb-migration
You can also install idb-migration
with pnpm, yarn, or slnpm
Usage Example
Runnable example is available in examples/index.ts
import { morphDatabase } from 'idb-migration'
let db = await morphDatabase({
db_name: 'app-db',
object_stores: [
{
store_name: 'user-store',
key_path: 'id',
auto_increment: true,
indexes: [
{ key_path: 'username', unique: true, multi_entry: false },
{ key_path: 'email', unique: true, multi_entry: false },
],
},
],
})
let txn = db.transaction('user-store', 'readwrite')
let store = txn.objectStore('user-store')
await store.add({
username: 'john',
email: 'john@example.com',
region: 'us',
})
let us_users = await store.index('region').getAll('us')
let total_users = await store.count()
Typescript Signature
import { IndexNames, StoreNames } from 'idb'
export function morphDatabase<DBTypes>(options: {
db_name: string
object_stores_to_delete?: string[]
object_stores: {
store_name: StoreNames<DBTypes>
key_path: string
auto_increment: boolean
indexes_to_delete?: string[]
indexes: {
index_name?: IndexNames<DBTypes, StoreNames<DBTypes>>
key_path: string
unique: boolean
multi_entry: boolean
}[]
}[]
})
License
This project is licensed with BSD-2-Clause
This is free, libre, and open-source software. It comes down to four essential freedoms [ref]:
- The freedom to run the program as you wish, for any purpose
- The freedom to study how the program works, and change it so it does your computing as you wish
- The freedom to redistribute copies so you can help others
- The freedom to distribute copies of your modified versions to others