0.1.1 • Published 7 months ago

@cloudparker/easy-idb v0.1.1

Weekly downloads
-
License
MIT
Repository
-
Last release
7 months ago

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.
0.0.40

11 months ago

0.0.41

10 months ago

0.0.42

10 months ago

0.0.43

9 months ago

0.0.44

9 months ago

0.1.1

7 months ago

0.0.37

11 months ago

0.0.39

11 months ago

0.0.30

11 months ago

0.0.31

11 months ago

0.0.32

11 months ago

0.0.33

11 months ago

0.0.34

11 months ago

0.0.35

11 months ago

0.0.36

11 months ago

0.0.29

11 months ago

0.0.20

1 year ago

0.0.22

1 year ago

0.0.23

12 months ago

0.0.24

12 months ago

0.0.25

12 months ago

0.0.15

1 year ago

0.0.16

1 year ago

0.0.17

1 year ago

0.0.18

1 year ago

0.0.19

1 year ago

0.0.10

1 year ago

0.0.11

1 year ago

0.0.12

1 year ago

0.0.13

1 year ago

0.0.14

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.26

12 months ago

0.0.9

1 year ago

0.0.27

12 months ago

0.0.8

1 year ago

0.0.28

12 months ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.7

1 year ago

0.0.6

1 year ago

0.0.1

1 year ago