Registry / database / async-idb-orm

async-idb-orm

JSON →
library7.4.1jsnpmunverified

Promise-based IndexedDB wrapper with an ORM-like API, supporting Active Records, relations, migrations, and reactive selectors. Current stable version is 7.4.1, released frequently. Differentiators include a tagged template literal for key ranges, rich collection methods (find, findMany, min/max with relation loading), TypeScript-first design with full type inference, and reactive selectors via subscriptions. It targets modern browsers with IndexedDB, is ESM-only, and has no external dependencies.

npm install async-idb-orm
INSTALL
IMPORT
SIG · ASYNC-IDB-ORM
A
async-idb-orm
databasejavascriptv7.4.1
harness data pending
Install & Compatibility
Where this runs

No compatibility data collected yet for this library.

Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

idb
import { idb } from 'async-idb-orm'
const idb = require('async-idb-orm')
The package is ESM-only; require() will throw an error. Use named import for 'idb'.
Collection
import { Collection } from 'async-idb-orm'
Collection is a class (not a factory) used to create collection definitions.
range
import { range } from 'async-idb-orm'
range is a tagged template literal function for creating IDBKeyRange objects.
Selector
import { Selector } from 'async-idb-orm'
Selector is a class for creating reactive selectors that compute derived data.

Complete example: define a collection with indexes and transformers, create a reactive selector, initialize the database, and perform create, find, and range queries.

import { idb, Collection, Selector, range } from 'async-idb-orm'; // Define User type and DTO type User = { id: string; name: string; age: number; createdAt: number; updatedAt?: number; }; type UserDTO = { name: string; age: number }; // Create a collection definition const users = Collection.create<User, UserDTO>() .withKeyPath('id') .withIndexes([ { key: 'age', name: 'idx_age' }, ]) .withTransformers({ create: (dto) => ({ ...dto, id: crypto.randomUUID(), createdAt: Date.now(), }), update: (record) => ({ ...record, updatedAt: Date.now() }), }); // Define a reactive selector const schema = { users }; const relations = {}; const userSummary = Selector.create<typeof schema, typeof relations>().as(async (ctx) => { const allUsers = await ctx.users.all(); return { totalUsers: allUsers.length, averageAge: allUsers.reduce((sum, u) => sum + u.age, 0) / (allUsers.length || 1) }; }); // Initialize the database const db = idb('myapp', { schema, relations, selectors: { userSummary }, version: 1 }); async function main() { // Create a user const user = await db.collections.users.create({ name: 'Alice', age: 30 }); console.log('Created user:', user); // Find a user by ID const found = await db.collections.users.find(user.id); console.log('Found user:', found); // Query using index range const youngUsers = await db.collections.users.getIndexRange('idx_age', range`< ${25}`); console.log('Users under 25:', youngUsers); // Use a selector const summary = await db.selectors.userSummary.get(); console.log('Summary:', summary); } main().catch(console.error);
Debug
Known issues
breakingESM-only; CJS require() not supported.
fix
Use ES module imports (import syntax) and ensure your project uses ESM (type: 'module' in package.json or .mjs extension).
affects: >=7.0
deprecatedThe API changed significantly from v6 to v7; Collection.create() now returns a builder object.
fix
Update code: use .withKeyPath(), .withIndexes(), .withTransformers() chain; see migration guide in the repo.
affects: 7.0
gotchaTransformers' create function must return an object that includes the key path; if autoIncrement: true, the key is optional in the return.
fix
Ensure the 'create' transformer returns an object with the keyPath property (e.g., id) if not auto-incremented, else the key will be undefined.
affects: >=7.0
gotchaWhen using range tagged template, ensure the expression is a value, not a string; e.g., range`>= ${30}` not range`>= ${'30'}`.
fix
Use numeric values in the template expressions; the range function expects the values to be numbers or strings matching the index key type.
affects: >=7.0
Errors
Common errors & fixes
TypeError: (intermediate value).getIndexRange is not a function
Calling .getIndexRange() on a non-existent collection or on a collection object that hasn't been initialized via idb()
fix
Ensure you're calling getIndexRange on db.collections.<name>.getIndexRange(...) after the database has been opened (await db.ready if necessary).
ReferenceError: require is not defined in ES module scope
Using require() instead of import; the package is ESM-only.
fix
Replace require('async-idb-orm') with import { ... } from 'async-idb-orm'.
Upgrade
Version history
7.4.1latest on npm
Audit
Dependencies
typescriptoptionalTypeScript is required at compile time for type definitions and type safety; not a runtime dependency.
Agent activity
19 hits · last 30 days
node
14
OpenAI (training)
1
Resources
async-idb-orm — npm install async-idb-orm · libregistry