MasterRecord
Query Language
Real JavaScript lambdas, safe parameters, zero SQL strings.
MasterRecord queries read like JavaScript. You filter with arrow functions and bind values through $$ placeholders, which are always parameterized — so there is no string concatenation and no injection risk.
Creating & saving#
create.js
// Active Record style — the entity saves itself
const user = db.User.new();
user.name = 'Teela';
user.email = 'teela@eternia.dev';
await user.save();
// Entity Framework style — change-tracked context
const u = db.User.new();
u.name = 'Adam';
await db.saveChanges();Reading#
Filtering with where#
where.js
// One value bound through $$
const alice = await db.User.where((u) => u.email == $$, 'alice@example.com').single();
// Combine conditions with .and()
const admins = await db.User
.where((u) => u.active == true)
.and((u) => u.role == $$, 'admin')
.toList();LIKE and IN#
like-in.js
// LIKE
const johns = await db.User.where((u) => u.name.like($$), 'John%').toList();
// IN — string form
const some = await db.User.where((u) => u.id.any($$), '1,2,3').toList();
// IN — array form
const ids = [1, 2, 3];
const more = await db.User.where((u) => $$.includes(u.id), ids).toList();Executing a query#
| Method | Returns |
|---|---|
.toList() | An array of all matching entities. |
.single() | Exactly one entity (or null). |
.first() | The first matching entity. |
Tip
Always
await the terminal call. On SQLite it resolves synchronously; on MySQL/Postgres it is a real promise — awaiting works on all three so your code stays portable.Ordering, pagination & counting#
advanced.js
const page = await db.Post
.where((p) => p.published == true)
.orderBy((p) => p.created_at, 'desc')
.skip(20)
.take(10)
.toList();
const total = await db.Post.where((p) => p.published == true).count();Including relationships#
include.js
const post = await db.Post
.where((p) => p.id == $$, id)
.include('User')
.include('Tags')
.single();
console.log(post.User.name, post.Tags.length);Updating & deleting#
mutate.js
// Update
const u = await db.User.where((x) => x.id == $$, id).single();
u.name = 'He-Man';
await u.save();
// Delete (Active Record)
await u.delete();
// Delete (context)
db.User.remove(u);
await db.saveChanges();