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

@indb/database

v0.3.5

Published

The database package exposes a type-safe, promise-based wrapper around the IndexedDB API.

Downloads

37

Readme

@indb/database

The database package exposes a type-safe, promise-based wrapper around the IndexedDB API.

API Docs

Example

An example database looks something like this:

import { Database, type SchemaDefinition } from '@indb/database';

type Todo = {
  id: string;
  listId: string;
  title: string;
  createdAt: number;
};

type List = {
  id: string;
  title: string;
};

type Tables = {
  todos: Todo;
  lists: List;
};

const DATABASE_NAME = 'todos';
const SCHEMA: SchemaDefinition<Tables> = {
  version: 1,
  stores: {
    lists: {
      keyPath: 'id',
    },
    todos: {
      keyPath: 'id',
      indices: {
        listId: {},
      },
    },
  },
};

const database = new Database<Tables>(DATABASE_NAME, SCHEMA);

This creates a new database, and will create the lists and todos stores, as well as a listId index on the todos store, so you can easily pull all the todos that are related to a specific list.

Features

Automatic Schema Migrations

Schemas are defined giving names of stores, key paths, and indices, and whenever the version of the schema is changed @indb/database will diff the current state of the schema and the requested schema and make the desired changes.

Schemas are type-safe, in that they must define table definitions for all of the types registered with the database.

Type-safe

The Tables type passed to the database will make sure you only interact with tables named in the type, and will cast all data from the associated tables with that type.

Additionally, schema definitions requre that each table have a definition, and that the key and index paths are valid paths (even deep paths are checked).

Promise-based

All of the actions you can take on the database return Promises, so you don't have to work with the lower-level onerror and oncomplete etc. handlers of the various objects produced by calls to the IndexedDB API.

Event Dispatching

Every mutation to the database dispatches an action that can be listened for via a handler on database.addEventListener('changed', handler). Additionally, every change to the database will also dispatch an event on a BroadcastChannel that other instances of Database in other tabs and windows will listen to.

So, Database instances act as event aggregators for all other Database instances, meaning that changes in other tabs or windows will dispatch events in others, allowing data to keep up to date no matter where the actions occur.

The stores package provides some classes that are designed to efficiently keep in-memory representations of parts of the database by using this event dispatching functionality.