npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

idb-factory

v1.0.0

Published

Better window.indexedDB

Downloads

23

Readme

idb-factory

Better window.indexedDB.

This module provides consistent, modern API to window.indexedDB. It's especially useful for test environment, when you need to open/delete database multiple times.

For implementation details check well documented 100 lines of the source.

Example

import { open, del } from 'idb-factory'

// open database with version 1 and create stores
open('mydb', 1, upgradeCallback).then((db) => {
  expect(db.version).equal(2)
  // use db ...
  // delete database
  return del(db)
})

function upgradeCallback(e) {
  e.target.result.createObjectStore('books', { keyPath: 'id' })  
  e.target.result.createObjectStore('magazines')  
}

API

open and del returns Promise and handles blocked event, by repeating operation after 100ms.

open(dbName, [version], [upgradeCallback])

import { open } from 'idb-factory'

// open "mydb1" v1, and create store and index
const db1 = async open('mydb1', 1, (e) => {
  if (e.oldVersion < 1) {
    const store = e.target.result.createObjectStore('books', { keyPath: 'isbn' })
    store.createIndex('by_title', 'title', { unique: true })
  }  
})

// version and upgradeCallback are optional.
const db2 = async open('mydb2')

You can combine it with idb-schema and have a pretty good deal.

import { open } from 'idb-factory'
import Schema from 'idb-schema'

const schema = new Schema()
.version(1)
  .addStore('books', { key: 'isbn' })
  .addIndex('byTitle', 'title', { unique: true })
.version(2)
  .addStore('magazines')
  .addIndex('byPublisher', 'publisher')
  .addIndex('byFrequency', 'frequency')

const db = async open('mydb', schema.version(), schema.callback())  

del(db)

import { open, del } from 'idb-factory'
const db = async open('mydb')

// do something with db
// ...

// delete existing IDBDatabase instance
async del(db)

del(dbName)

import { del as deleteDatabase } from 'idb-factory'
async deleteDatabase('mydb') // delete database by name

cmp(val1, val2)

import { cmp } from 'idb-factory'

// Compare 2 values, using IndexedDB's comparison algorithm
console.assert(cmp('z', 'a') === 1)
console.assert(cmp([1], [1]) === 0)

global.forceIndexedDB

It is a specially global variable, which you can define to prior global.indexedDB.

function idb() {
  return global.forceIndexedDB
      || global.indexedDB
      || global.webkitIndexedDB
      || global.mozIndexedDB
      || global.msIndexedDB
      || global.shimIndexedDB
}

For example, due to WebKit bug you can't rewrite window.indexedDB, but Safari 8 implementation is really buggy, so you'd like to use shim and fallback to WebSQL.

import 'indexeddbshim'
import { open } from 'idb-factory'

if (isSafari8) {
  global.forceIndexedDB = global.shimIndexedDB
}

// use WebSQL implementation, only in Safari 8,
// and use IndexedDB in remaining browsers.
const db = async open('mydb')

LICENSE

MIT