Reign
is a lightweight library for managing IndexedDB operations in modern web applications. It simplifies database interactions like creating, updating, retrieving, and deleting records using a clean and intuitive API.
- Easy setup and initialization of IndexedDB databases
- Support for multiple object store creation during database upgrades
- CRUD operations (
create
,read
,update
, anddelete
) with asynchronous promises - Designed for flexibility and reusability in modern JavaScript applications
You can install Reign IDB using npm:
npm install reign-idb
Import the Reign class in your JavaScript project:
import Reign from 'reign-idb';
Instantiate a Reign
object by providing the databaseName
, storeNames
(as an array), and version
parameters:
const db = new Reign({
databaseName: 'MyDatabase',
storeNames: ['Users', 'Products'], // Multiple stores supported
version: 1
});
Initialize the database connection before performing any operations:
await db.init();
Use the update
method to add a new record or update an existing one in a specific store:
const recordId = await db.update('Users', { name: 'John Doe', age: 30 });
console.log(`Record saved with ID: ${recordId}`);
Retrieve all records from a specific object store:
const users = await db.read('Users');
console.log(users);
Fetch a specific record by its ID from a specific store:
const user = await db.get('Users', 1);
console.log(user);
Delete a record by its ID from a specific store:
await db.delete('Users', 1);
console.log('Record deleted');
When you're done with database operations, it's good practice to close the connection:
db.close();
new Reign({ databaseName, storeNames, version });
databaseName
(String
): Name of the IndexedDB databasestoreNames
(String[]
): Array of object store names to createversion
(Number
): Version number for the database
Initializes the database connection. Creates the specified object stores if they don't already exist.
Returns: Promise<IDBDatabase>
Adds or updates a record in the specified object store.
storeName
(String
): Name of the object storedata
(Object
): The record to add or update
Returns: Promise<number>
β The ID of the added or updated record
Retrieves all records from the specified object store.
storeName
(String
): Name of the object store
Returns: Promise<Array<Object>>
Retrieves a specific record by its ID from the specified object store.
storeName
(String
): Name of the object storeid
(Number
): The ID of the record to retrieve
Returns: Promise<Object>
Deletes a specific record by its ID from the specified object store.
storeName
(String
): Name of the object storeid
(Number
): The ID of the record to delete
Returns: Promise<undefined>
Closes the active database connection. This is useful for cleanup when the database connection is no longer needed.
Returns: void
All methods reject with an appropriate error if an operation fails. Use try-catch
blocks or .catch
handlers to manage errors gracefully:
try {
const user = await db.get('Users', 1);
} catch (error) {
console.error('Error fetching record:', error);
}
Reign IDB requires modern browsers with IndexedDB support. Most contemporary browsers (Chrome, Firefox, Safari, Edge) are fully compatible.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is open-source and available under the MIT License.