2.2.7 • Published 5 months ago

indexeddb-package v2.2.7

Weekly downloads
-
License
ISC
Repository
github
Last release
5 months ago

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();
      };
    };
  });
};
2.2.7

5 months ago

2.2.6

5 months ago

2.2.5

5 months ago

2.2.4

5 months ago

2.2.3

5 months ago

2.2.2

5 months ago

2.2.1

5 months ago

2.2.0

5 months ago

2.1.9

5 months ago

2.1.8

5 months ago

2.1.7

5 months ago

2.1.6

5 months ago

2.1.5

5 months ago

2.1.4

5 months ago

2.1.3

5 months ago

2.1.2

5 months ago

2.1.1

5 months ago

2.1.0

5 months ago

2.0.9

5 months ago

2.0.8

5 months ago

2.0.7

5 months ago

2.0.6

6 months ago

2.0.5

6 months ago

2.0.4

6 months ago

2.0.3

6 months ago

2.0.2

6 months ago

2.0.1

6 months ago

2.0.0

6 months ago

1.0.15

6 months ago

1.0.14

6 months ago

1.0.13

6 months ago

1.0.12

6 months ago

1.0.11

6 months ago

1.0.10

6 months ago

1.0.9

6 months ago

1.0.8

6 months ago

1.0.7

6 months ago

1.0.6

6 months ago

1.0.5

6 months ago

1.0.4

6 months ago

1.0.3

6 months ago

1.0.2

6 months ago

1.0.1

6 months ago

1.0.0

6 months ago