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

mol_db

v0.0.1193

Published

Static typed facade for [IndexedDB](https://developer.mozilla.org/en-US/docs/Web/API/IndexedDB_API) with simple API.

Downloads

5,212

Readme

$mol_db

Static typed facade for IndexedDB with simple API.

Teaser

type User = {
	name: string
	admin: boolean
}

// Static typed schema
type DB = {
	User: {
		Key: [ string ]
		Doc: User
		Indexes: {
			Name: [ string ]
		}
	}
}

// Automatic migrations
const db = await $mol_db< DB >( '$my_app',
	mig => mig.store_make( 'User' ),
	mig => {
		const { User } = mig.stores
		User.index_make( 'Name', [ 'name' ] )
	},
)

// Writing transaction
const { User } = db.change( 'User' ).stores
await User.put({ name: 'Jin', admin: true })

// Reading transaction
const { Name } = db.read( 'User' ).User.indexes
const jins = await Name.select([ 'Jin' ])

IndexedDB Structure

  • Database contains named Stores
    • Store contains Documents by primary keys and named Indexes
      • Document contains any data
      • Index points to Documents

DB life Cycle

DB Schema Example

type ACME = {
	Users: {
		Key: number
		Doc: {
			name: {
				first: string
				last: string
			}
			age: number
		}
		Indexes: {
			names: [ string, string ]
			ages: [ number ]
		}
	}
	Articles: {
		Key: string
		Doc: {
			title: string
			content: string
		}
		Indexes: {
			full: [ string, string ]
		}
	}
}

Open DB without Migrations

const db = await $$.$mol_db< ACME >( 'ACME' )

Open DB with automatic migrations

const db = await $$.$mol_db< ACME >( 'ACME',
	mig => mig.store_make( 'Users' ),
	mig => mig.stores.Users.index_make( 'ages', [ 'age' ] ),
	mig => mig.stores.Users.index_make( 'names', [ 'name.first', 'name.last' ], true ),
	mig => mig.store_make( 'Articles' ),
	mig => mig.stores.Articles.index_make( 'full', [ 'title', 'content' ] ),
	// mig => mig.stores.Articles.index_drop( 'full' ),
	// mig => mig.store_drop( 'Articles' ),
)

There is 5 migrations. And DB version is 6. After uncommenting last 2 rows, it applies 2 additional migrations and DB version will be 8.

Close DB Connection

db.destructor()

Delete DB

db.kill()

Transaction Life Cycle

Read Only Transactions

const { Users, Articles } = db.read( 'Users', 'Articles' )

Read/Write Trasactions

const trans = db.change( 'Users', 'Articles' )
const { Users, Articles } = trans.stores

// ...

trans.abort()
// or
await trans.commit()

Uncommitted transaction without errors will be committed automatically. Any modification error aborts transaction.

Documents Life Cycle

By Primary key

Insert with Auto Incremental Primary Key

const key = await Users.put({
	first: 'Jin',
	last: 'Nin',
	age: 36,
})

Put/Overwrite by Primary Key

await Users.put( {
	first: 'Jin',
	last: 'Nin',
	age: 37,
}, 1 )

Get One By Primary Key

const user = await Users.get( 1 )

Select 10 By Primary Keys

const users = await Users.get( $mol_dom_context.IDBKeyRange.bound( 10, 50 ), 10 )

Count By Primary Keys

const count = await Users.count( $mol_dom_context.IDBKeyRange.bound( 10, 50 ) )

By Index

const { names, ages } = users.indexes

Get One By Index

const user = await names.get([ 'Jin', 'Nin' ])

Select 10 By Index

const users = await ages.get( [ 18 ], 10 )

Count By Primary Keys

const count = await Users.count([ 18 ])

Usage from NPM

npm install mol_db

import { $mol_db } from 'mol_db'