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

@cloudparker/easy-idb

v0.1.1

Published

A lightweight, promise-based JavaScript library for working with IndexedDB, providing simplicity and ease of use.

Downloads

59

Readme

easy-idb

A lightweight and straightforward library for storing data in browser IndexedDB. It offers a simple promise-based API for all CRUD operations in the database.

Installation

npm i @cloudparker/easy-idb --save

Simple Usage

Open Todo database

import { Database, Store, IdbWhere } from '@cloudparker/easy-idb';
import type { StoreDefinitionType } from '@cloudparker/easy-idb';

let dbName = 'todo-db';
let version = 1;
let db: Database | null = null;

let storeDefinitions: StoreDefinitionType[] = [
    { name: 'todos', primaryKey: '_id', autoIncrement: true },
];


export let todosStore: Store | undefined;

export async function openDatabase() {
    db = new Database({ name: dbName, version, stores: storeDefinitions });
    let results = await db.openDatabase();
    todosStore = results.todos;
    console.log('Db opened');
}

Other Usage

import { Database, Store, type StoreDefinitionType, IdbWhere } from '@cloudparker/easy-idb';

// ...

let todosStore: Store;
let usersStore: Store;
let db: Database | null = null;
let dbName = 'test-db';
let version = 1;
let storeDefinitions: StoreDefinitionType[] = [
  { name: 'todos', primaryKey: '_id', autoIncrement: true },
  { name: 'users', primaryKey: '_id', autoIncrement: true, indexes: ['updatedAt'] }
];

db = new Database({ name: dbName, version, storeDefinitions });

// After opening the database, it will return all the Object Store instances based on the provided store names.
let results = await db.openDatabase(({ db, oldVersion, newVersion }) => {
  // TODO: handle data migration logic here
  switch(newVersion) {
    case 1:
      // Handle data migration for version 1
      break;
    case 2:
      // Handle data migration for version 2
      break;
  }
  console.log({ db, oldVersion, newVersion });
});

todosStore = results.todos; // todos store instance
usersStore = results.users; // users store instance

console.log('Db opened');

await todosStore.insert({ task: 'Task1' });  // Insert one document

await todosStore.insert([{ task: 'Task1' }, { task: 'Task2' }]);  // Insert multiple documents

await todosStore.count(); // count the records in the store

await todosStore.get({ where: IdbWhere('_id', '==', 1) }); // return the document with primaryKey value 1 (where primaryKey field is `_id`)
// Result : { _id: 1, task: 'Task1' }

await todosStore.getAll(); // Read all records from a store. Use this method carefully when the store size is large.

await todosStore.find({ where: IdbWhere('_id', '>=', 1)); // Read all records, irrespective of the store size

await todosStore.find({ desc: true }); // Read all records in descending order

await todosStore.find({ unique: true }); // Read all unique records, based on the id index

// Read all documents that match the custom query. Query based on primary key or other index ranges, or filter by custom filter function. It also allows transforming the values using the map function.
await todosStore.find({ where: IdbWhere('_id', '>=', 1), skip, limit, desc, unique,  filter, map });

await todosStore.update({ _id: 1, task: 'Task First' });

await todosStore.update({ _id: 1,   date : new Date()}, { merge: true });

await todosStore.update([{ _id: 1, task: 'Task First' }, { _id: 2, task: 'Task Second' }]);

await todosStore.upsert({ _id: 1, task: 'Task First' });

await todosStore.remove(1); // Remove record from store with _id = 1
await todosStore.remove([1, 2]); // Remove record from store with _id = 1, 2
await todosStore.remove({data: 1}); // Remove record from store with _id = 1
await todosStore.remove({ data: [1, 2], where: IdbWhere('_id', '==', 3) }); // Remove multiple records from stores.