Socket
Socket
Sign inDemoInstall

@cloudparker/easy-idb

Package Overview
Dependencies
0
Maintainers
1
Versions
43
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    @cloudparker/easy-idb

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


Version published
Maintainers
1
Install size
40.0 kB
Created

Readme

Source

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.

Keywords

FAQs

Last updated on 07 Oct 2023

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc