Skip to content
🚀 This documentation is for unreal-orm 1.0.0 alpha which requires SurrealDB 2.0 SDK. For use with version 1.x, see here.

Selecting Records

Every model has a static select method that generates a SELECT query. The select option is the key to type-safe projections.

const users = await User.select(db);
const names = await User.select(db, {
select: { name: true, email: true },
});
// Type: { name: string; email: string }[]
const posts = await Post.select(db, {
select: { title: true, author: { name: true, email: true } },
fetch: ['author'],
});
// author is expanded into { name: string; email: string }
const posts = await Post.select(db, {
select: { '*': true, author: { name: true } },
fetch: ['author'],
});

Use the typed helper to give a SurrealQL expression a TypeScript type:

import { typed } from 'unreal-orm';
const posts = await Post.select(db, {
select: {
title: true,
commentCount: typed<number>(surql`count(<-comment)`),
},
});
const users = await User.select(db, {
omit: { password: true },
});
// Type: Omit<User, 'password'>[]
const users = await User.select(db, {
where: (f) => f.age.gte(18).and(f.active.isTrue()),
});

Or raw SurrealQL:

const users = await User.select(db, {
where: surql`age >= 18 AND active = true`,
});
const posts = await Post.select(db, {
orderBy: [{ field: 'createdAt', order: 'DESC' }],
start: 20,
limit: 10,
});
Post.select(db, {
from: new RecordId('post', 'abc'), // single record
only: true, // SELECT ... FROM ONLY
groupBy: ['author'], // GROUP BY
split: ['tags'], // SPLIT AT
timeout: '30s',
explain: true,
DEBUG: true,
});

See Where Builder for every field-proxy operator.