π idb-helper β The easiest way to work with IndexedDB! This library simplifies IndexedDB operations, making it as intuitive as LocalStorage while providing better performance and scalability for large datasets.
πΉ IndexedDB operates asynchronously β Always use await when calling methods.
πΉ Be aware of browser limitations β Some browsers impose storage limits on IndexedDB.
πΉ Version management is crucial β If the database schema changes, older versions may face issues.
πΉ Bulk operations are more efficient β Use bulkInsert and bulkDelete for handling large datasets.
πΉ Indexed fields improve performance β getByIndex is faster than retrieving all items and filtering manually.
πΉ Use instead of LocalStorage β IndexedDB is a better choice for managing large amounts of data efficiently.
Install via NPM:
npm install idb-helperInstall via Yarn:
yarn add idb-helperInstall via Bun:
bun add idb-helper
Or use CDN:
<script src="https://cdn.jsdelivr.net/npm/idb-helper/dist/index.global.js"></script>import { IDBHelper } from "idb-helper";
const db = new IDBHelper("myDatabase", "myStore");
(async () => {
// 1οΈβ£ Add a new item
const id = await db.setItem({ id: 1, name: "Dilshod", age: 27 });
console.log("New item ID:", id);
// 2οΈβ£ Get an item by ID
const user = await db.getItem(1);
console.log("Fetched item:", user); // { id: 1, name: "Dilshod", age: 27 }
// 3οΈβ£ Get all items
const allUsers = await db.getAllItems();
console.log("All items:", allUsers);
// 4οΈβ£ Remove an item by ID
await db.removeItem(1);
console.log("Item removed.");
// 5οΈβ£ Clear all data
await db.clear();
console.log("Database cleared.");
// 6οΈβ£ Check if an item exists
const exists = await db.hasItem(1);
console.log("Item exists:", exists);
// 7οΈβ£ Count total items
const count = await db.count();
console.log("Total items:", count);
// 8οΈβ£ Update an item
await db.updateItem(1, { name: "Dilshod", age: 28 });
console.log("Item updated.");
// 9οΈβ£ Get item by index
const indexedItem = await db.getByIndex("name", "Dilshod");
console.log("Item by index:", indexedItem);
// π Get items by filter
const filteredItems = await db.getItemsByFilter((item) => item.age > 25);
console.log("Filtered items:", filteredItems);
// 1οΈβ£1οΈβ£ Get items by range
const rangedItems = await db.getItemsByRange("age", 20, 30);
console.log("Items in range:", rangedItems);
// 1οΈβ£2οΈβ£ Bulk insert multiple items
await db.bulkInsert([
{ id: 2, name: "Ali", age: 23 },
{ id: 3, name: "Sara", age: 25 },
]);
console.log("Bulk insert done.");
// 1οΈβ£3οΈβ£ Bulk delete multiple items
await db.bulkDelete([2, 3]);
console.log("Bulk delete done.");
// 1οΈβ£4οΈβ£ Close database
await db.closeDB();
console.log("Database closed.");
})();Hereβs how you can integrate idb-helper in a React component:
import React, { useEffect, useState } from "react";
import {IDBHelper} from "idb-helper";
const db = new IDBHelper("myDatabase", "myStore");
const App = () => {
const [items, setItems] = useState([]);
const [newItem, setNewItem] = useState("");
// Load all items on component mount
useEffect(() => {
const fetchData = async () => {
const allItems = await db.getAllItems();
setItems(allItems);
};
fetchData();
}, []);
// Add a new item
const handleAddItem = async () => {
if (!newItem) return;
const id = await db.setItem({ name: newItem });
setItems([...items, { id, name: newItem }]);
setNewItem("");
};
// Remove an item
const handleRemoveItem = async (id) => {
await db.removeItem(id);
setItems(items.filter((item) => item.id !== id));
};
return (
<div style={{ padding: "20px" }}>
<h2>IndexedDB React Example</h2>
<input
type="text"
value={newItem}
onChange={(e) => setNewItem(e.target.value)}
placeholder="Enter item name"
/>
<button onClick={handleAddItem}>Add Item</button>
<ul>
{items.map((item) => (
<li key={item.id}>
{item.name} <button onClick={() => handleRemoveItem(item.id)}>β</button>
</li>
))}
</ul>
</div>
);
};
export default App;β Simplifies working with IndexedDB β No more complex transactions!
β Works with Promise API β Fully asynchronous and easy to use.
β Easy as LocalStorage β SimplesetItem/getItemmethods.
β Supports Indexed Queries β Fetch items by indexed fields.
β Advanced Filtering β Retrieve data using custom filter functions.
β Range Queries β Get items between a min/max value.
β Bulk Operations β Insert and delete multiple records at once.
β Database Management β Clear, count, and close the database easily.
β Super Fast β Optimized for large data storage.
| Method | Type | Params | Returns | Description |
|---|---|---|---|---|
setItem<T> |
Promise<number> |
data: T & { id?: number } |
id |
Adds or updates an item in the database |
getItem<T> |
Promise<T> |
id: number |
T or undefined |
|
getAllItems<T> |
Promise<T[]> |
none |
T[] |
Fetches all stored items |
removeItem |
Promise<void> |
id: number |
void |
Deletes an item by ID |
clear |
Promise<void> |
none |
void |
Clears all items from the database |
hasItem |
Promise<boolean> |
id: number |
true/false |
Checks if an item with the given ID exists |
count |
Promise<number> |
none |
number |
Returns the total number of stored items |
updateItem<T> |
Promise<void> |
id: number, updates: Partial<T> |
void |
Partially updates an item by ID |
getByIndex<T> |
Promise<T> |
indexName: string, value: any |
T or undefined |
|
getItemsByFilter<T> |
Promise<T[]> |
filter: (item: T) => boolean |
T[] |
Fetches items that match a filter function |
getItemsByRange<T> |
Promise<T[]> |
startId: number, endId: number |
T[] |
Fetches items within an ID range |
bulkInsert<T> |
Promise<void> |
items: T[] |
void |
Inserts multiple items at once |
bulkDelete |
Promise<void> |
ids: number[] |
void |
Deletes multiple items at once |
closeDB |
void |
none |
void |
Closes the IndexedDB connection |
npm run dev # Development mode
npm run build # Construction
npm run test # Run testsMIT - Free and open source!
π Author: Dilshod Fayzullayev
π GitHub: bug4you/idb-helper