In-memory ORM-style store with object filters and copies
Your team prototypes features against an in-memory store before the real database is ready. To make the later switch painless, the store mimics the query shape of a popular TypeScript ORM: filters are plain objects, not callbacks, so they can be logged, cached, and sent over the network.
Implement a class RecordStore.
const store = new RecordStore();
store.create(data) // => the stored record (a copy)
store.findMany({ where?, orderBy?, skip?, take? }?) // => array of records
store.findFirst({ where?, orderBy? }?) // => a record, or null
store.count({ where? }?) // => number
null.create(data) stores a copy of data with an id field: 1 for the first record, then 2, 3, and so on. Any id in data is ignored.create) never changes the store.null when filtering and sorting.where)A where object maps field names to conditions. A record matches when every entry matches. An empty or missing where matches every record.
{ role: 'admin' }, { archivedAt: null }) means “equals”.equals, not: === and !==.in, notIn: an array of allowed or forbidden values.lt, lte, gt, gte: comparisons. A null field value never matches these.contains, startsWith, endsWith: substring checks. Only string field values can match.mode: 'insensitive' makes equals, contains, startsWith and endsWith ignore case for strings. It is a setting, not a condition.AND: [where, ...]: every nested filter matches. An empty array matches everything.OR: [where, ...]: at least one nested filter matches. An empty array matches nothing.NOT: where: the nested filter does not match.orderBy is { field: 'asc' | 'desc' } or an array of them. The first entry is the primary sort and later entries break ties.< and >. null values sort last in both directions. Records that tie on every key keep creation order.orderBy, records come back in creation order.skip (default 0) and take (default: all) apply after sorting.const users = new RecordStore();
users.create({ name: 'Mila', email: 'mila@acme.io', age: 34, plan: 'pro' });
users.create({ name: 'Owen', email: 'OWEN@ACME.IO', age: 19, plan: 'free' });
users.create({ name: 'Zara', email: 'zara@mail.com', age: null, plan: 'pro' });
users.create({ name: 'Theo', email: 'theo@acme.io', age: 27 });
users.findMany({
where: { email: { endsWith: '@acme.io', mode: 'insensitive' }, age: { gte: 20 } },
}).map((u) => u.name);
// => ['Mila', 'Theo']
users.findMany({ where: { plan: null } }).map((u) => u.name);
// => ['Theo'] (a missing field reads as null)
users.findMany({
where: { OR: [{ plan: 'pro' }, { age: { lt: 20 } }], NOT: { name: 'Zara' } },
orderBy: { age: 'desc' },
}).map((u) => u.name);
// => ['Mila', 'Owen']
users.findMany({ orderBy: [{ age: 'asc' }], skip: 1, take: 2 }).map((u) => u.name);
// => ['Theo', 'Mila'] (Zara has no age, so she sorts last)
users.findFirst({ where: { name: { in: ['Nobody'] } } }); // => null
users.count({ where: { plan: { not: 'pro' } } }); // => 2 (Owen, Theo)