super-indexdb
v1.0.0
Published
A minimal, auto-migrating wrapper for idb.
Maintainers
Readme
I will assume the README is for your IndexedDB Starter Project which includes the Table class with the findByRange functionality you were working on.
Here is a simple, concise, and complete README for that project.
🚀 IndexedDB Starter Project
This project provides a clean, class-based wrapper for working with the browser's native IndexedDB database, focusing on simple CRUD (Create, Read, Update, Delete) and advanced range queries.
🎯 How It Works
This project simplifies IndexedDB by using the Table class to manage a single Object Store (similar to a SQL table).
- Initialization: The
Databaseclass is a singleton that connects to IndexedDB, creates necessary Object Stores, and sets up any required Indexes. - Interaction: The
Tableclass uses a modern, Promise-based API to wrap raw IndexedDB requests, eliminating the need to handle low-level event listeners. - Range Queries: The
findByRangemethod allows you to query data using flexible boundary options (gte,gt,lte,lt), which are translated directly into nativeIDBKeyRangeobjects.
🛠️ How to Use It
1. Installation
Assuming you have Node.js and a package manager:
npm install # or yarn install / pnpm install2. Initialization & Setup
First, you instantiate the database. This typically happens once in your application's entry point.
import { Database } from './src/database'; // Adjust path as needed
// 1. Define your database name and Object Store names
const DB_NAME = 'MySpacedRepDb';
const STORE_NAME = 'cards';
// 2. Instantiate the database
const db = new Database(DB_NAME, 1, (db, oldVersion) => {
// This is the migration/upgrade function
if (oldVersion < 1) {
const store = db.createObjectStore(STORE_NAME, { keyPath: 'id' });
// Example: Create an index for querying by 'deckId'
store.createIndex('deckId', 'deckId', { unique: false });
}
});
// 3. Get a reference to your table
const cardsTable = db.getTable(STORE_NAME);3. Basic CRUD Operations
The cardsTable object now provides simple methods:
| Action | Method | Example |
| :--- | :--- | :--- |
| Create | add(value) | await cardsTable.add({ id: 1, front: 'Hello' }); |
| Read | get(key) | const card = await cardsTable.get(1); |
| Update | put(value) | await cardsTable.put({ id: 1, front: 'Hi there' }); |
| Delete | delete(key) | await cardsTable.delete(1); |
4. Advanced Range Query
Use the findByRange method to fetch items based on an Index and a Range Configuration.
The range object accepts any combination of boundary keys:
| Key | Description |
| :--- | :--- |
| gte | Greater Than or Equal to (inclusive lower bound) |
| gt | Greater Than (exclusive lower bound) |
| lte | Less Than or Equal to (inclusive upper bound) |
| lt | Less Than (exclusive upper bound) |
Example: Find cards where deckId is between 5 and 10 (inclusive)
const rangeQuery = {
gte: 5, // Deck IDs greater than or equal to 5
lte: 10 // Deck IDs less than or equal to 10
};
const cards = await cardsTable.findByRange('deckId', rangeQuery);
// cards will contain all items where card.deckId >= 5 AND card.deckId <= 10Ready to start adding some data and running your own queries?
