@oreefy/idb
v1.0.0
Published
A very lightweight, promise-based library for storing data in the browser's native IndexedDB on the client side.
Maintainers
Readme
@oreefy/http
A very lightweight, promise-based library for storing data in the browser’s native IndexedDB on the client side.
@oreefy/idb is a library built for the Oreefy ecosystem. The package is officially developed, maintained, and fully controlled by Oreefy, ensuring long-term stability, consistency, and compatibility across the ecosystem.
Browser only: This package is designed exclusively for
frontend/browserenvironments. It requires the nativeIndexedDBAPI and is not intended for Node.js, server-side, or other non-browser environments.
Required Capabilities
- JavaScript / TypeScript
- A browser environment with native
IndexedDBsupport - Frontend/client-side application
Example
The following example demonstrates a typical client-side usage:
import { idbInstance } from "@oreefy/idb";
async function example() {
try {
const idb = idbInstance();
// Store data
await idb.set("user", { id: 1234, name: "your_name" });
// Retrieve data
const user = await idb.get<{ id: number; name: string }>("user");
console.log(user);
// Delete specific data
await idb.delete("user");
// Clear all package data
await idb.clear();
} catch (err) {
console.error("IndexedDB operation failed:", err);
}
}
example(); // call the functionAPI Reference
Import idbInstance and create an IndexedDB instance:
import { idbInstance } from "@oreefy/idb";
try {
const idb = idbInstance();
await idb.get();
await idb.set();
await idb.delete();
await idb.clear();
} catch (err) {
console.error(err);
}All operations are asynchronous and return Promises.
idb.get()
Retrieves data stored under a specific key.
import { idbInstance } from "@oreefy/idb";
try {
const idb = idbInstance();
await idb.get<T>("name"); // Promise<T | null>
// Example
const user = await idb.get<{ name: string; age: number }>("user");
if (user) {
console.log(user.name, user.age);
}
} catch (err) {
console.error(err);
}Returns the stored value if the key exists. If no value is found, it returns null.
idb.set()
Stores or updates data using the specified key. If the key already exists, its existing value is replaced.
import { idbInstance } from "@oreefy/idb";
try {
const idb = idbInstance();
await idb.set<T>("name"); // Promise<T>
// Example
const user = { name: "your_name", isAdmin: true };
const result = await idb.set<{ name: string; isAdmin: boolean }>(
"user",
user,
);
console.log(result); // { name: "your_name", isAdmin: true }
} catch (err) {
console.error(err);
}Returns the same data that was stored.
idb.delete()
Deletes the data stored under a specific key.
import { idbInstance } from "@oreefy/idb";
try {
const idb = idbInstance();
await idb.delete("name"); // Promise<true>
// Example
const result = await idb.delete("user");
console.log(result); // true
} catch (err) {
console.error(err);
}If the specified key does not exist, the operation still completes successfully.
idb.clear()
Removes all data stored by @oreefy/idb.
import { idbInstance } from "@oreefy/idb";
try {
const idb = idbInstance();
await idb.clear(); // Promise<true>
// Example
await idb.clear();
console.log("All data cleared");
} catch (err) {
console.error(err);
}Warning: clear() removes all data stored by this package. Use it carefully.
Storage Details
@oreefy/idb uses the browser's native IndexedDB API internally. The library manages its own IndexedDB database and object store, so you do not need to manually open a database or create an object store.
You only need to work with the simple API:
- idb.get()
- idb.set()
- idb.delete()
- idb.clear()
The actual storage is persistent browser storage and remains available across page reloads and browser sessions, subject to the browser's storage policies.
Browser Compatibility
This package depends on the native IndexedDB Web API. It is intended for modern browsers that provide IndexedDB support.
It is not designed for:
- Node.js
- Server-side applications
- Backend services
- Serverless runtime environments
- Cloudflare Workers
- Other non-browser JavaScript runtimes
Use this package when the code is running in a browser/client environment.
Error Handling
IndexedDB operations may fail due to browser storage restrictions, quota limitations, unavailable storage, or other browser-level errors.
All API methods return Promises, so errors can be handled with standard try...catch:
import { idbInstance } from "@oreefy/idb";
try {
const idb = idbInstance();
await idb.set("key", "value");
} catch (err) {
console.error("Failed to store data:", err);
}About Oreefy
Oreefy is an affordable business ecosystem designed for small to enterprise businesses. Oreefy provides essential software you need for your modern business within a single ecosystem. It will save you significant time, effort, and money.
License
MIT © Oreefy
