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

indexeddb-package

v2.2.7

Published

NPM Package for saving, deleting data from an IndexedDB database

Downloads

58

Readme

Overview

The indexeddb-package

Installation

To use the indexeddb-package on a JavaScript project, you can install it using npm:

npm i indexeddb-package

Then, you can import the necessary functions in your project.

Functions

  1. isIndexedDBSupported(dbName: string): Promise<boolean>
  • This checks if IndexedDB is supported in the current browser.

  • Parameters:

    • dbName: The name of the IndexedDB database to check.
  • Returns:

    • A Promise that resolves to true if IndexedDB is supported, and false otherwise.
const isIndexedDBSupported = (dbName) => {
  return new Promise((resolve, reject) => {
    const request = idb.open(dbName, 1);

    request.onsuccess = () => {
      // IndexedDB is supported
      resolve(true);
      request.result.close();
    };

    request.onerror = () => {
      // IndexedDB is not supported
      resolve(false);
    };
  });
};
  1. insertDataInIndexedDb(dbName: string, userDataArray: Array<object>): Promise<string>
  • Inserts data into the specified IndexedDB database.

  • Parameters

    • dbName: The name of the IndexedDB database.
    • userDataArray: An array of objects containing the data to be inserted into storage
  • Returns:

    • A Promise that resolves to to a success message if the data is inserted successfully.
const insertDataInIndexedDb = (dbName, userDataArray) => {
  return new Promise((resolve, reject) => {
    if (!idb) {
      reject("This browser doesn't support IndexedDB");
      return;
    }

    const request = idb.open(dbName, 1);

    request.onerror = function () {
      reject("An error occurred with IndexedDB");
    };

    request.onsuccess = function () {
      const db = request.result;
      const tx = db.transaction("userData", "readwrite");
      const userData = tx.objectStore("userData");

      userDataArray.forEach((item) => userData.add(item));

      tx.oncomplete = function () {
        db.close();
        resolve("Data inserted successfully");
      };
    };
  });
};
  1. retrieveDataFromIndexedDb(dbName: string): Promise<Array<object>>
  • Retrieves all data from the specified IndexedDB database.

  • Parameters

    • dbName: The name of the IndexedDB database.
  • Returns

    • A Promise that resolves to an array of objects representing the retrieved data.
const retrieveDataFromIndexedDb = (dbName) => {
  return new Promise((resolve, reject) => {
    if (!idb) {
      reject("This browser doesn't support IndexedDB");
      return;
    }

    const request = idb.open(dbName, 1);

    request.onerror = function () {
      reject("An error occurred with IndexedDB");
    };

    request.onsuccess = function () {
      const db = request.result;
      const tx = db.transaction("userData", "readonly");
      const userData = tx.objectStore("userData");

      const getRequest = userData.getAll();

      getRequest.onsuccess = function () {
        resolve(getRequest.result);
      };

      tx.oncomplete = function () {
        db.close();
      };
    };
  });
};
  1. deleteDataFromIndexedDb(dbName: string, id: any): Promise<string>
  • Deletes data with the specified ID from the IndexedDB database.

  • Parameters

    • dbName: The name of the IndexedDB database.
    • id: The ID of the data to be deleted
  • Return

    • A Promise that resolves to a success message if the data is deleted successfully
const deleteDataFromIndexedDb = (dbName, id) => {
  return new Promise((resolve, reject) => {
    if (!idb) {
      reject("This browser doesn't support IndexedDB");
      return;
    }

    const request = idb.open(dbName, 1);

    request.onerror = function () {
      reject("An error occurred with IndexedDB");
    };

    request.onsuccess = function () {
      const db = request.result;
      const tx = db.transaction("userData", "readwrite");
      const userData = tx.objectStore("userData");

      const deleteRequest = userData.delete(id);

      deleteRequest.onsuccess = function () {
        resolve("Data deleted successfully");
      };

      tx.oncomplete = function () {
        db.close();
      };
    };
  });
};
  1. updateDataInIndexedDb(dbName: string, updatedData: object): Promise<string>
  • Updates data in the IndexedDB database with the provided updated data.

  • Parameter:

    • dbName: The name of the IndexedDB database.
    • updatedData: The updated data object
  • Returns:

  • A Promise that resolves to a success message if the data is updated successfully.

const updateDataInIndexedDb = (dbName, updatedData) => {
  return new Promise((resolve, reject) => {
    if (!idb) {
      reject("This browser doesn't support IndexedDB");
      return;
    }

    const request = idb.open(dbName, 1);

    request.onerror = function () {
      reject("An error occurred with IndexedDB");
    };

    request.onsuccess = function () {
      const db = request.result;
      const tx = db.transaction("userData", "readwrite");
      const userData = tx.objectStore("userData");

      const putRequest = userData.put(updatedData);

      putRequest.onsuccess = function () {
        resolve("Data updated successfully");
      };

      tx.oncomplete = function () {
        db.close();
      };
    };
  });
};