Cookbook
Need a quick pattern without reading the whole tutorial? Grab a ready-made recipe below 👇
All snippets assume
db
is an activeSurreal
connection and models are already defined.
Pagination with start
, limit
, and orderBy
Section titled “Pagination with start, limit, and orderBy”// Fetch the next page of posts ordered by newest firstconst pageSize = 10;const posts = await Post.select(db, { orderBy: [{ field: 'createdAt', order: 'desc' }], start: 20, // skip first 2 pages limit: pageSize,});
Soft Delete (isDeleted
flag)
Section titled “Soft Delete (isDeleted flag)”class Post extends Table.normal({ name: 'post', fields: { title: Field.string(), content: Field.string(), isDeleted: Field.bool({ default: 'false' }), },}) { // Convenience helpers async softDelete(db: Surreal) { return this.update(db, { isDeleted: true }); }
static async allActive(db: Surreal) { return this.select(db, { where: 'isDeleted = false' }); }}
Enum Helper
Section titled “Enum Helper”// Authorised values enforced in SurrealDBexport const Roles = ['admin', 'editor', 'viewer'] as const;export type Role = (typeof Roles)[number];
class User extends Table.normal({ name: 'user', fields: { role: Field_custom<Role>('string').assert(`$value INSIDE ${JSON.stringify(Roles)}`), },}) {}
Patch Update with merge
Section titled “Patch Update with merge”// Partially update fields without replacing the whole recordawait Post.merge(db, 'post:123', { content: 'Updated body' });
Parameterised Full-Text Search
Section titled “Parameterised Full-Text Search”await Post.select(db, { where: 'title ~~ $q OR content ~~ $q', vars: { q: '*orm*' },});
📖 Continue exploring the Tutorial or deep-dive into the API Reference.