1.0.0 • Published 6 months ago

idb-migration v1.0.0

Weekly downloads
-
License
BSD-2-Clause
Repository
github
Last release
6 months ago

idb-migration

Helper function for migrating IndexedDB databases.

npm Package Version Minified Package Size Minified and Gzipped Package Size

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
1.0.0

6 months ago

0.0.0

6 months ago