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

idbx

v2.1.0

Published

No-bullshit indexedDB wrapper functions with promises, iterators and shortcuts.

Downloads

20

Readme

IDBX

No-bullshit indexedDB wrapper functions with promises, iterators and shortcuts. ESM only (Browser, Deno & Node).

Migration: Breaking Changes to v1

  1. I removed the idbx.open function in favor of idbx.openDB.
  2. I removed the idbx.batch function, because it has not been well tested and it does not serve this library because of its complexity.
  3. If you want to use idbx.batch, I've moved the code to a separate module called idbatch. You can find it here:
  • NodeJS https://www.npmjs.com/package/idbatch
  • Deno https://deno.land/x/idbatch
  • Browser https://esm.sh/idbatch
  1. I removed the idbx.addBulk, idbx.putBulk and idbx.delBulk functions in favor of idbx.add, idbx.put and idbx.del. You can now pass an array of items to these functions and they will be added as well as a single item.

Getting started

Deno:

import * as idbx from "https://deno.land/x/idbx";

Node/Deno (same thing):

import * as idbx from "npm:idbx";

Browser:

import * as idbx from "https://esm.sh/idbx";

Access a database

const db = await idbx.openDB("testdb");

Create a database

const db = await idbx.openDB("testdb", {
  upgrade(db, event) {
    const store = db.createObjectStore("store");
    store.createIndex("name_index", "name", { unique: true });
  },
  blocked(event) {
    // on upgrade this event will be fired until all tabs
    // connected to the database are closed or reloaded.
  },
});

Easy access a store

Shorthand code to get a store from a database.

IDBX:

const store = idbx.getStore(db, "store");

It's just a convenience function, Native API is two lines of code ;-)

Native API:

const tx = db.transaction("store", "readonly");
const store = tx.objectStore("store");

Add an item

await idbx.add(store, { name: "foo" });

Get an item

const item = await idbx.get(store, "foo");

Get all items

const items = await idbx.getAll(store);

Iterate over items

for await (const item of idbx.iterate(store)) {
  console.log(item);
}

API Reference

idbx.openDB(name: string, options?: IDBXOptions): Promise

Options:

  • version?: number - The database version.
  • upgrade?: (db: IDBDatabase, event: IDBVersionChangeEvent) => void - The upgrade callback.
  • blocked?: (event: IDBVersionChangeEvent) => void - The blocked callback.

Returns a promise that resolves to an IDBDatabase instance.

const db = await idbx.openDB("testdb", { ... });

idbx.getStore(db: IDBDatabase, name: string, key?: IDBValidKey): IDBObjectStore

Returns an IDBObjectStore instance.

const store = idbx.getStore(db, "store");

idbx.getIndex(db: IDBDatabase, name: string, indexName: string): IDBIndex

Returns an IDBIndex instance.

const index = idbx.getIndex(db, "store", "name_index");

idbx.add(store: IDBObjectStore, item: T | T[], key?: IDBValidKey): Promise

Parameters:

  • store: IDBObjectStore - The store to add the item to.
  • item: T | T[] - The item to add.
  • key?: IDBValidKey - The key to add the item with.

Returns a promise that resolves to the key of the added item.

// add a single item
const key = await idbx.add(store, { name: "foo" });

// or add multiple items at once
const keys = await idbx.add(store, [{ name: "foo" }, { name: "bar" }]);

idbx.put(store: IDBObjectStore, item: T, key?: IDBValidKey): Promise

Parameters:

  • store: IDBObjectStore - The store to add the item to.
  • item: T | T[] - The item to add.
  • key?: IDBValidKey - The key to add the item with.

Returns a promise that resolves to the key of the added item.

// update a single item
const key = await idbx.put(store, { name: "foo" });

// or update multiple items at once
const keys = await idbx.put(store, [{ name: "foo" }, { name: "bar" }]);

idbx.del(store: IDBObjectStore, key: IDBValidKey): Promise

Parameters:

  • store: IDBObjectStore - The store to remove the item from.
  • key: IDBValidKey - The key to remove the item with.

Removes one or more items from the store.

// remove a single item
await idbx.del(store, "foo");

// or remove multiple items at once
await idbx.del(store, ["foo", "bar"]);

idbx.clear(store: IDBObjectStore): Promise

Parameters:

  • store: IDBObjectStore - The store to clear.

Returns a promise that resolves to the number of deleted items.

const count = await idbx.clear(store);

idbx.get(store: IDBObjectStore | IDBIndex, query: IDBValidKey): Promise<T | undefined>

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to get the item from.
  • query: IDBValidKey - The key to get the item with.

Returns a promise that resolves to the item.

const item = await idbx.get(store, "foo");

idbx.getAll(store: IDBObjectStore | IDBIndex, query?: IDBValidKey | IDBKeyRange, count?: number): Promise<T[]>

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to get the item from.
  • query?: IDBValidKey | IDBKeyRange - The key or key range to get the items with.
  • count?: number - The maximum number of items to get.

Returns a promise that resolves to an array of items.

const items = await idbx.getAll(store, IDBKeyRange.bound("foo", "bar"));

idbx.getKey(store: IDBObjectStore | IDBIndex, query: IDBValidKey): Promise<IDBValidKey | undefined>

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to get the key from.
  • query: IDBValidKey - The key to get the key with.

Returns a promise that resolves to the key.

const key = await idbx.getKey(store, "foo");

idbx.getAllKeys(store: IDBObjectStore | IDBIndex, query?: IDBValidKey | IDBKeyRange, count?: number): Promise<IDBValidKey[]>

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to get the keys from.
  • query?: IDBValidKey | IDBKeyRange - The key or key range to get the keys with.
  • count?: number - The maximum number of keys to get.

Returns a promise that resolves to an array of keys.

const keys = await idbx.getAllKeys(store, IDBKeyRange.bound("foo", "bar"));

idbx.count(store: IDBObjectStore | IDBIndex, query?: IDBValidKey | IDBKeyRange): Promise

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to count the items from.
  • query?: IDBValidKey | IDBKeyRange - The key or key range to count the items with.

Returns a promise that resolves to the number of items.

const count = await idbx.count(store, IDBKeyRange.bound("foo", "bar"));

idbx.iterate(store, query?, direction?): AsyncIterable

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to iterate over.
  • query?: IDBValidKey | IDBKeyRange | null - The key or key range to iterate over.
  • direction?: IDBCursorDirection - The direction to iterate in.

Returns an async iterable that yields items.

for await (const item of idbx.iterate(store)) {
  console.log(item);
}

idbx.iterateKeys(store, query?, direction?): AsyncIterable

Parameters:

  • store: IDBObjectStore | IDBIndex - The store or index to iterate over.
  • query?: IDBValidKey | IDBKeyRange | null - The key or key range to iterate over.
  • direction?: IDBCursorDirection - The direction to iterate in.

Returns an async iterable that yields keys.

for await (const key of idbx.iterateKeys(store)) {
  console.log(key);
}