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);
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()
fixEnsure 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.
fixReplace require('async-idb-orm') with import { ... } from 'async-idb-orm'. Audit
Dependencies
typescriptoptionalTypeScript is required at compile time for type definitions and type safety; not a runtime dependency.