0.1.0 • Published 1 year ago

persistdb v0.1.0

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

PersistDB

PersistDB is a TypeScript package that provides a convenient interface for working with IndexedDB in web applications. It allows you to perform common operations such as adding, updating, removing, and retrieving data from an IndexedDB database.

Installation

You can install the package via npm:

npm install persistdb

Usage

import PersistDB from 'persistdb';

const db = new PersistDB('MyDatabase', 'MyStore');

Methods

Example : add(item: T): Promise

  • Adds an item to the IndexedDB store.
  • item: The item to be added.
  • Returns: A promise that resolves with the generated key (ID) of the added item.
// Adding an object

db.add({ id: 1, name: 'Item 1', data: [1, 2, 3] })
  .then(id => {
    console.log('Item added with id:', id);
  })
  .catch(error => {
    console.error('Error adding item:', error);
  });

Example : update(id: number, item: T): Promise

  • Updates an existing item in the IndexedDB store.
  • id: The ID of the item to update.
  • item: The updated item.
  • Returns: A promise that resolves when the item is successfully updated.
// Updating an item

db.update(1, { id: 1, name: 'Updated Item 1', data: [4, 5, 6] })
  .then(() => {
    console.log('Item updated');
  })
  .catch(error => {
    console.error('Error updating item:', error);
  });

Example : remove(id: number): Promise

  • Removes an item from the IndexedDB store.
  • id: The ID of the item to remove.
  • Returns: A promise that resolves when the item is successfully removed.
// Removing an item

db.remove(1)
  .then(() => {
    console.log('Item removed');
  })
  .catch(error => {
    console.error('Error removing item:', error);
  });

Example : get(id: number): Promise<T | undefined>

  • Retrieves an item from the IndexedDB store by its ID.
  • id: The ID of the item to retrieve.
  • Returns: A promise that resolves with the retrieved item, or undefined if not found.
// Getting an item by ID

db.get<{ id: number; name: string; data: number[] }>(1)
  .then(item => {
    console.log('Retrieved item:', item);
  })
  .catch(error => {
    console.error('Error getting item:', error);
  });

Example : getAll(): Promise<T[]>

  • Retrieves all items from the IndexedDB store.

  • Returns: A promise that resolves with an array of all items in the store.

// Getting all items

db.getAll<{ id: number; name: string; data: number[] }>()
  .then(items => {
    console.log('All items:', items);
  })
  .catch(error => {
    console.error('Error getting all items:', error);
  });

Feel free to customize the README according to your package's specific features and usage instructions.

0.1.0

1 year ago

0.0.6

1 year ago

0.0.5

1 year ago

0.0.4

1 year ago

0.0.3

1 year ago

0.0.2

1 year ago

0.0.1

1 year ago