Selecting Records
Every model has a static select method that generates a SELECT query. The select option is the key to type-safe projections.
All records
Section titled “All records”const users = await User.select(db);Projection
Section titled “Projection”const names = await User.select(db, { select: { name: true, email: true },});// Type: { name: string; email: string }[]Nested records
Section titled “Nested records”const posts = await Post.select(db, { select: { title: true, author: { name: true, email: true } }, fetch: ['author'],});// author is expanded into { name: string; email: string }Select all plus a relation
Section titled “Select all plus a relation”const posts = await Post.select(db, { select: { '*': true, author: { name: true } }, fetch: ['author'],});Computed fields
Section titled “Computed fields”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)`), },});Omit fields
Section titled “Omit fields”const users = await User.select(db, { omit: { password: true },});// Type: Omit<User, 'password'>[]Filtering
Section titled “Filtering”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`,});Ordering and pagination
Section titled “Ordering and pagination”const posts = await Post.select(db, { orderBy: [{ field: 'createdAt', order: 'DESC' }], start: 20, limit: 10,});Other options
Section titled “Other options”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.