Types and values
Choose types for stored fields, arguments, and results. The declaration determines what the database accepts and what a generated client sends.
Scalar types
The number in a fixed-width numeric type is its size in bytes. For example, Int4 is a signed 32-bit integer and UInt8 is an unsigned 64-bit integer.
| Type | Values and selection |
|---|---|
Bool | A boolean. Use true or false. |
Int1, Int2, Int4, Int8 | Signed integers occupying 1, 2, 4, or 8 bytes. |
UInt1, UInt2, UInt4, UInt8 | Unsigned integers of the same widths. A numeric literal takes its type from context. |
Long, ULong | Arbitrary-precision signed or unsigned integers, subject to value-size limits. |
Float4, Float8 | Binary32 and binary64 floating point. NaN is rejected, either sign of zero compares equal, and infinities are valid. |
Decimal, Decimal<S> | Exact decimal values. Decimal carries a per-value scale. Decimal<2> requires scale 2: coefficient 125 represents 1.25. A fixed scale may also be negative. |
Text, Bytes | UTF-8 text or uninterpreted bytes. text.byte_length counts UTF-8 bytes. |
Date, Time | Days since 1970-01-01, or microseconds since midnight within one day. |
Timestamp, Duration | Microseconds since the Unix epoch, or a signed count of elapsed microseconds. A timestamp has no stored time-zone name. |
Id<T> | An identity scoped to table T. Two table types give different ID types. |
Optional values, tuples, structs, and enums
T? admits null. A row lookup returns an optional row. ?. reads a member when present and propagates null when absent. () is the unit result. Tuples such as (Float8, Float8) have positional fields .0 and .1.
Use a struct for named fields and an enum for alternatives. A variant can carry a payload. A match selects an alternative and binds its payload by name.
export enum Visibility {
Public,
Shared {
team: Text,
},
}
export struct Draft {
title: Text,
summary: Text?,
tags: List<Text, max: 12>,
visibility: Visibility,
location: (Float8, Float8),
}
fn label(draft: Draft) -> Text {
match draft.visibility {
Public => { return draft.title; },
Shared(shared) => { return shared.team; },
}
}Lists, sets, and maps
| Type | How to use it |
|---|---|
List<T> | Ordered values with zero-based positions. A list can contain duplicates. Append through a mutable local, iterate with for, or inspect .length. |
Set<T> | Distinct members. Use a set for membership tests and insertions. Stored set members must be non-null scalar values. |
Map<K, V> | Associate a key with a value. Stored map keys are non-null scalar values. A nullable value such as Map<Text, Bool?> permits a present key with a null value. |
Add max: n as the final type argument to bound an individual collection’s number of elements or entries. For example, List<Text, max: 12> admits up to 12 items. The limit counts items, not their encoded bytes. Optional containers and optional elements are separate choices: List<Text>? and List<Text?> describe different values.
Generate a typed primary key
Declare an Id<Entry> generated field and omit it from an insert. The returned InsertResult<Entry> exposes .id and the full primary key via .key. Treat IDs as opaque. Do not derive creation order or counts from their numeric representation.
table Entry {
id: Id<Entry> generated,
title: Text,
primary key(id),
}
export write fn new_entry(draft: Draft) -> Id<Entry> {
let created = Entry.insert({
title: label(draft),
});
return created.id;
}Function and authorization types
Iterator<T> is a single-pass result from a generator or scan. Stored rows and index projections are deeply read-only views. Readonly<T> describes that access, and .copy() produces an editable value.
WriteResult carries write counts; requesting returning: true adds returned records. Generated-ID tables use InsertResult<T> and InsertManyResult<T> for insert results. AuthToken<Claims, Role> carries typed application claims. See Identity and permissions for subjects and token issuance.