In-memory database with hasMany and belongsTo relation queries
A library app keeps its data in memory while offline: authors, books, and loans. The UI code wants to ask for exactly the shape it renders (“the titles of this author’s books after 1980, newest first”) instead of stitching tables together by hand. Build the small query layer that makes that possible.
Implement createDatabase(relations). It returns an object with three methods:
const db = createDatabase(relations);
db.insert(model, record); // => a copy of the stored record
db.findMany(model, query?); // => an array of result objects
db.findById(model, id, query?); // => one result object, or null
relations maps a model name to its relation fields. Each relation is { model, kind, foreignKey }:
kind: 'hasMany': an array of every record in model whose foreignKey field equals (===) this record’s id, in insertion order.kind: 'belongsTo': the record in model whose id equals this record’s foreignKey field, or null if there is none.Models do not need to be declared. Inserting into a model creates it. findMany on a model with no records returns [].
record and returns another copy. Changing the input object later does not change the stored record.Error if record.id is undefined or null, or if the model already has a record with that id.A query is an object with any of these keys:
where: an object of field conditions. A record matches when every condition holds. A condition is either a plain value (matches with ===) or an operator object whose operators must all hold:
equals, not: === and !==.in: the field value is in the given array (===).gt, gte, lt, lte: compare with >, >=, <, <=. They only match when the field value has the same typeof as the operand, so { year: { gt: 0 } } never matches a missing or null year. Any other operator name throws an Error.
orderBy: { field: 'asc' | 'desc' } with one field. Records with equal values keep insertion order.take: keep at most this many records, applied after where and orderBy.select: only the listed keys appear in the result. A key set to true is a field or relation; a relation key may instead hold a nested query. Keys set to false are ignored. A selected field that the record does not have is left out.include: every field of the record plus the listed relations. A value is true or a nested query. Including a name that is not a relation of the model throws an Error.Using select and include in the same query object throws an Error. This applies at every nesting level.
Nested queries use the same rules. For a hasMany relation, its where, orderBy, and take filter the related records. For a belongsTo relation, only select and include apply.
findById uses only the query’s select and include.
Every returned object (including nested ones) is new. Mutating a result never changes stored data.
const db = createDatabase({
authors: { books: { model: 'books', kind: 'hasMany', foreignKey: 'authorId' } },
books: { author: { model: 'authors', kind: 'belongsTo', foreignKey: 'authorId' } },
});
db.insert('authors', { id: 1, name: 'Octavia Butler' });
db.insert('authors', { id: 2, name: 'Ted Chiang' });
db.insert('books', { id: 10, title: 'Kindred', year: 1979, authorId: 1 });
db.insert('books', { id: 11, title: 'Parable of the Sower', year: 1993, authorId: 1 });
db.insert('books', { id: 12, title: 'Exhalation', year: 2019, authorId: 2 });
db.findMany('authors', {
where: { name: { not: 'Ted Chiang' } },
select: {
name: true,
books: { where: { year: { gte: 1980 } }, select: { title: true } },
},
});
// => [{ name: 'Octavia Butler', books: [{ title: 'Parable of the Sower' }] }]
db.findById('books', 12, { include: { author: { select: { name: true } } } });
// => { id: 12, title: 'Exhalation', year: 2019, authorId: 2, author: { name: 'Ted Chiang' } }
db.findMany('books', { orderBy: { year: 'desc' }, take: 2, select: { title: true } });
// => [{ title: 'Exhalation' }, { title: 'Parable of the Sower' }]
db.findById('books', 99); // => null
db.findMany('books', { select: { title: true }, include: { author: true } }); // throws
null).findById looks records up by id; where treats id like any other field.