MockDb is a tiny, test‑oriented helper that simulates a small subset of Amazon DynamoDB behaviors over an in‑memory array of JSON objects. Its goal is to make it easy to unit/integration test code that expects “Dynamo‑like” reads without needing to provision a real database.
What you get:
What you do NOT get:
MockDb uses @karmaniverous/entity-tools for type modeling and sorting.
You’ll typically install this as a dev dependency:
npm i -D @karmaniverous/mock-db
Node: >= 18 recommended (ESM module). The package ships both ESM and CJS outputs.
import type { Entity } from '@karmaniverous/mock-db'; // convenience re-export
import { MockDb, type QueryOptions } from '@karmaniverous/mock-db';
interface User extends Entity {
partition: string; // hash key
id: number;
name: string;
}
const users: User[] = [
{ partition: 'a', id: 4, name: 'Alice' },
{ partition: 'b', id: 3, name: 'Bob' },
{ partition: 'a', id: 2, name: 'Charlie' },
{ partition: 'a', id: 1, name: 'Dave' },
];
const db = new MockDb(users); // default async mean=100ms, std=20ms
// 1) Synchronous “scan” across all items with filter + sort
const scan = db.querySync({
filter: ({ id }) => id > 2,
sortOrder: [{ property: 'id' }],
});
// => { count: 2, items: [...], pageKey: undefined }
// 2) Asynchronous, paged, sorted “query” within a partition (hash key)
const opts: QueryOptions<User> = {
hashKey: 'partition',
hashValue: 'a',
indexComponents: ['partition', 'id'],
limit: 2,
sortOrder: [{ property: 'id' }],
};
let page = await db.query(opts, 100);
// => first two items plus pageKey for next page
page = await db.query({ ...opts, pageKey: page.pageKey }, 100);
// => next page (remaining items)
CommonJS example:
const { MockDb } = require('@karmaniverous/mock-db');
Replicates a limited set of DynamoDB scan/query behaviors over a local array.
Constructor
Methods
Options for query/querySync (T is a TranscodeRegistry):
Result from query/querySync:
To avoid a direct dependency on @karmaniverous/entity‑tools in your imports, the following types are re‑exported:
import type {
Entity,
SortOrder,
TranscodeRegistry,
DefaultTranscodeRegistry,
} from '@karmaniverous/mock-db';
You can still import them from @karmaniverous/entity‑tools if you prefer.
npm test (Vitest; coverage via V8)npm run lint (ESLint) • npm run lint:fix (ESLint + Prettier)npm run typecheck (tsc, no emit)npm run build (Rollup – ESM/CJS + dts)npm run docs (TypeDoc)See the online API Documentation for the complete, generated API reference.
This library is meant for fast, deterministic test runs where mocking a small subset of Dynamo behaviors is sufficient (hash key filtering, sorting, pagination). If you need to exercise full DynamoDB semantics, consider running a local emulator or integration tests against a real service.
Built for you with ❤️ on Bali! Find more tools & templates on my GitHub Profile.