Prisma-style in-memory client with unique fields and defaults
ORMs such as Prisma generate a typed client from a schema: for every model you get a delegate like db.user with methods such as create and findUnique. Your team wants the same API backed by memory, so unit tests can run without a database and later swap in the real client unchanged.
Implement createClient(schema). It returns a client with one delegate per model in schema.
const schema = {
user: {
email: { unique: true },
name: {},
role: { default: 'member' },
},
post: {
title: {},
slug: { unique: true },
views: { default: 0 },
createdAt: { default: () => Date.now() },
},
};
schema is a model name. Each model maps field names to options: unique (boolean) and default (a value, or a function that is called to produce one).id field that the client manages. It is not listed in the schema.null.client[model] has five methods. Each one returns a Promise. Every failure rejects that promise (never throws synchronously) with an Error whose code property is one of the codes below.
client.user.create({ data }) // => Promise<record>
client.user.findUnique({ where }) // => Promise<record | null>
client.user.findMany({ where }?) // => Promise<record[]>
client.user.update({ where, data }) // => Promise<record>
client.user.delete({ where }) // => Promise<record>
create({ data }) stores a new record and resolves with it. The record has id plus every schema field: the value from data if present, else the field’s default (calling it if it is a function, once per record), else null. Ids start at 1 for each model and go up by one; an id is never reused, even after a delete.findUnique({ where }) resolves with the one record that matches, or null. where must have exactly one key, and it must be id or a unique field. Otherwise reject with INVALID_WHERE.findMany({ where }) resolves with every record whose fields are === to every entry of where, in creation order. A missing argument or missing/empty where returns all records. Keys must be id or schema fields.update({ where, data }) changes the given fields of the record found like findUnique and resolves with the updated record. Fields not in data keep their values.delete({ where }) removes the record found like findUnique and resolves with it.code |
When |
|---|---|
UNKNOWN_FIELD |
data in create/update, or where in findMany, has a key that is not a schema field. data may not contain id either. |
INVALID_WHERE |
The where of findUnique, update or delete does not have exactly one key that is id or a unique field. |
NOT_FOUND |
update or delete finds no record. |
UNIQUE_VIOLATION |
create or update would give a unique field the same value as another record of the model. null never conflicts. |
A rejected create or update changes nothing: no record is stored or modified, and a failed create does not use up an id.
Records go in and out as copies. Changing an object passed to create/update, or an object you got back, never changes what is stored. Models are independent of each other, and so are clients.
const db = createClient(schema);
await db.user.create({ data: { email: 'ana@acme.io', name: 'Ana' } });
// => { id: 1, email: 'ana@acme.io', name: 'Ana', role: 'member' }
await db.user.create({ data: { email: 'raj@acme.io', name: 'Raj', role: 'admin' } });
// => { id: 2, email: 'raj@acme.io', name: 'Raj', role: 'admin' }
await db.user.findUnique({ where: { email: 'raj@acme.io' } }); // => { id: 2, ... }
await db.user.findMany({ where: { role: 'member' } }); // => [{ id: 1, ... }]
await db.user.update({ where: { id: 1 }, data: { role: 'owner' } });
// => { id: 1, email: 'ana@acme.io', name: 'Ana', role: 'owner' }
await db.user.create({ data: { email: 'ana@acme.io' } }); // rejects, code 'UNIQUE_VIOLATION'
await db.user.findUnique({ where: { name: 'Ana' } }); // rejects, code 'INVALID_WHERE'
await db.user.delete({ where: { id: 9 } }); // rejects, code 'NOT_FOUND'
await db.post.create({ data: { title: 'Hello', author: 'Ana' } }); // rejects, code 'UNKNOWN_FIELD'