indexeddb-package
v2.2.7
Published
NPM Package for saving, deleting data from an IndexedDB database
Downloads
58
Maintainers
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
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 totrue
if IndexedDB is supported, andfalse
otherwise.
- A
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);
};
});
};
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.
- A
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");
};
};
});
};
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.
- A
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();
};
};
});
};
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
- A
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();
};
};
});
};
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();
};
};
});
};