
Security News
Open Source Maintainers Feeling the Weight of the EU’s Cyber Resilience Act
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
react-indexeddb-toolkit
Advanced tools
A complete TypeScript toolkit for IndexedDB in React applications
A complete TypeScript toolkit for IndexedDB in React applications with custom hooks and easy-to-use APIs.
npm install react-indexeddb-toolkit
import { useIndexedDB } from "react-indexeddb-toolkit";
interface User {
id: string;
name: string;
email: string;
}
function UserComponent() {
const { data, save, remove, isLoading, error } = useIndexedDB<User>({
dbName: "myapp",
storeName: "users",
keyPath: "id",
});
const addUser = async () => {
await save({
id: Date.now().toString(),
name: "John Doe",
email: "john@example.com",
});
};
if (isLoading) return <div>Loading...</div>;
if (error) return <div>Error: {error}</div>;
return (
<div>
<button onClick={addUser}>Add User</button>
{data.map((user) => (
<div key={user.id}>
{user.name} - {user.email}
<button onClick={() => remove(user.id)}>Delete</button>
</div>
))}
</div>
);
}
const {
data,
isLoading,
error,
save,
remove,
update,
findById,
clear,
refresh,
} = useIndexedDB<T>(config);
config
: Configuration object for the IndexedDB setupdata
: Array of all items in the storeisLoading
: Boolean indicating if data is being loadederror
: Error message if any operation failssave
: Function to save/update an itemremove
: Function to delete an item by IDupdate
: Function to update an item partiallyfindById
: Function to find an item by IDclear
: Function to clear all datarefresh
: Function to reload data from the databaseinterface DBConfig {
dbName: string; // Database name
version?: number; // Database version (default: 1)
storeName: string; // Object store name
keyPath?: string; // Key path (default: 'id')
indexes?: DBIndex[]; // Array of indexes
}
interface DBIndex {
name: string; // Index name
keyPath: string; // Key path for the index
options?: {
// Index options
unique?: boolean;
multiEntry?: boolean;
};
}
const { data, save } = useIndexedDB<User>({
dbName: "myapp",
storeName: "users",
keyPath: "id",
indexes: [
{
name: "email",
keyPath: "email",
options: { unique: true },
},
{
name: "name",
keyPath: "name",
},
],
});
import { IndexedDBManager } from "react-indexeddb-toolkit";
const dbManager = new IndexedDBManager<User>({
dbName: "myapp",
storeName: "users",
keyPath: "id",
});
// Initialize the database
await dbManager.init();
// Save data
await dbManager.save({ id: "1", name: "John", email: "john@example.com" });
// Get all data
const users = await dbManager.getAll();
// Get by ID
const user = await dbManager.getById("1");
// Delete
await dbManager.delete("1");
// Clear all
await dbManager.clear();
const { data, save, error } = useIndexedDB<User>(config);
const handleSave = async (user: User) => {
try {
await save(user);
console.log("User saved successfully");
} catch (err) {
console.error("Failed to save user:", err);
}
};
// Or use the error state
if (error) {
console.error("Database error:", error);
}
This package is written in TypeScript and provides full type definitions. All functions are properly typed, and you can use your own interfaces for type safety:
interface Product {
id: string;
name: string;
price: number;
category: string;
inStock: boolean;
}
const { data, save } = useIndexedDB<Product>({
dbName: "store",
storeName: "products",
keyPath: "id",
});
// TypeScript will enforce the Product interface
await save({
id: "1",
name: "Laptop",
price: 999.99,
category: "Electronics",
inStock: true,
});
IndexedDB is supported in all modern browsers. For older browser support, consider using a polyfill.
Contributions are welcome! Please feel free to submit a Pull Request.
MIT
FAQs
A complete TypeScript toolkit for IndexedDB in React applications
The npm package react-indexeddb-toolkit receives a total of 122 weekly downloads. As such, react-indexeddb-toolkit popularity was classified as not popular.
We found that react-indexeddb-toolkit demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The EU Cyber Resilience Act is prompting compliance requests that open source maintainers may not be obligated or equipped to handle.
Security News
Crates.io adds Trusted Publishing support, enabling secure GitHub Actions-based crate releases without long-lived API tokens.
Research
/Security News
Undocumented protestware found in 28 npm packages disrupts UI for Russian-language users visiting Russian and Belarusian domains.