Array Operations
Overview
Working with array fields in Cerial — declaration, default behavior, update operators, and schema-level constraints.
Cerial supports array fields on any model. Declare them with the Type[] syntax, and you get typed push/unset operators, full replacement, query filtering, and schema-level decorators out of the box.
Array Field Declaration
Append [] to any supported type to declare an array field:
model User {
id Record @id
nicknames String[]
scores Int[]
ratings Float[]
flags Bool[]
loginDates Date[]
tagIds Record[]
addresses Address[]
trackers Uuid[]
durations Duration[]
amounts Decimal[]
attachments Bytes[]
waypoints Geometry[]
}Supported Array Types
| Type | Description |
|---|---|
String[] | Array of strings |
Int[] | Array of integers |
Float[] | Array of floating-point numbers |
Bool[] | Array of booleans |
Date[] | Array of datetime values |
Record[] | Array of SurrealDB record references |
ObjectName[] | Array of embedded objects (e.g., Address[]) |
Uuid[] | Array of UUID values |
Duration[] | Array of duration values |
Decimal[] | Array of arbitrary-precision decimals |
Bytes[] | Array of binary data |
Geometry[] | Array of geospatial values |
See Field Types for full details on each type.
Default Behavior
All array fields default to an empty array [] when creating a record if no value is provided:
const user = await client.db.User.create({
data: { name: 'Alice' },
});
// user.nicknames => []
// user.scores => []
// user.ratings => []This applies to every array type. You never need to explicitly pass [] on create unless you want to be explicit about it.
For more on how absent fields work in Cerial, see NONE vs null.
Capabilities
Array fields support four categories of operations:
- Update operators — Use
pushto append elements andunsetto remove elements from an existing array without replacing it. See Push & Unset. - Full replacement — Replace the entire array contents with a new value. See Replace Array.
- Query operators — Filter records by array contents using
has,hasAll,hasAny, andisEmpty. See the Filtering section. - Schema decorators — Enforce uniqueness, sort order, or set semantics at the database level with
@distinct,@sort, and@set. See Array Decorators.
Sections
- Push & Unset — Add and remove elements from arrays
- Replace Array — Replace entire array contents
- Array Decorators — Enforce uniqueness, sort order, and set semantics at the database level