Field
const
Field:object
Defined in: define/field/index.ts:80
A factory object for creating field definitions for table schemas.
Type declaration
Section titled “Type declaration”any(
options
):FieldDefinition
<any
>
Defines an any
field, which can store any data type.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the any field.
Returns
Section titled “Returns”FieldDefinition
<any
>
Example
Section titled “Example”flexibleData: Field.any()
array()
Section titled “array()”array<
TElementDef
>(element
,options
):FieldDefinition
<InferFieldType
<TElementDef
>[]>
Defines an array
field.
Type Parameters
Section titled “Type Parameters”TElementDef
Section titled “TElementDef”TElementDef
extends FieldDefinition
<unknown
>
Parameters
Section titled “Parameters”element
Section titled “element”TElementDef
The field definition for the elements within the array.
options
Section titled “options”ArrayFieldOptions
<InferFieldType
<TElementDef
>> = {}
Options for the array field, such as max
size.
Returns
Section titled “Returns”FieldDefinition
<InferFieldType
<TElementDef
>[]>
Example
Section titled “Example”// An array of stringstags: Field.array(Field.string()),
// An array of objectsitems: Field.array(Field.object({ name: Field.string(), quantity: Field.number(),}))
bool()
Section titled “bool()”bool(
options
):FieldDefinition
<boolean
>
Defines a boolean
field.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Standard field options.
Returns
Section titled “Returns”FieldDefinition
<boolean
>
Example
Section titled “Example”Field.bool({ default: false })
bytes()
Section titled “bytes()”bytes(
options
):FieldDefinition
<ArrayBuffer
>
Defines a bytes
field for storing binary data.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the bytes field.
Returns
Section titled “Returns”FieldDefinition
<ArrayBuffer
>
Example
Section titled “Example”avatar: Field.bytes()
custom()
Section titled “custom()”custom<
T
>(typeString
,options
):FieldDefinition
<T
>
Defines a custom field type using a specific SurrealDB type string.
This is useful for types not built-in to the ORM, like duration
or geometry
.
Type Parameters
Section titled “Type Parameters”T
Parameters
Section titled “Parameters”typeString
Section titled “typeString”string
The SurrealQL type string (e.g., ‘duration’, ‘geometry
options
Section titled “options”CustomFieldOptions
= {}
Standard field options.
Returns
Section titled “Returns”FieldDefinition
<T
>
Example
Section titled “Example”Field.custom<number>('duration')
datetime()
Section titled “datetime()”datetime(
options
):FieldDefinition
<Date
>
Defines a datetime
field.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Standard field options.
Returns
Section titled “Returns”FieldDefinition
<Date
>
Example
Section titled “Example”Field.datetime({ default: 'time::now()' })
decimal()
Section titled “decimal()”decimal(
options
):FieldDefinition
<Decimal
>
Defines a decimal
field for high-precision numbers.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the decimal field.
Returns
Section titled “Returns”FieldDefinition
<Decimal
>
Example
Section titled “Example”balance: Field.decimal()
duration()
Section titled “duration()”duration(
options
):FieldDefinition
<Duration
>
Defines a duration
field for storing time durations.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the duration field.
Returns
Section titled “Returns”FieldDefinition
<Duration
>
Example
Section titled “Example”ttl: Field.duration()
float()
Section titled “float()”float(
options
):FieldDefinition
<number
>
Defines a float
field for floating-point numbers.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the float field.
Returns
Section titled “Returns”FieldDefinition
<number
>
Example
Section titled “Example”rating: Field.float()
geometry()
Section titled “geometry()”geometry<
T
>(type
,options
):FieldDefinition
<GeometryTypeMap
[T
]>
Defines a geometry
field for storing GeoJSON data.
Type Parameters
Section titled “Type Parameters”T
extends GeometryType
Parameters
Section titled “Parameters”T
The specific geometry type.
options
Section titled “options”FieldOptions
= {}
Options for the geometry field.
Returns
Section titled “Returns”FieldDefinition
<GeometryTypeMap
[T
]>
Example
Section titled “Example”// A single pointlocation: Field.geometry('point'),
int(
options
):FieldDefinition
<number
>
Defines an int
field for integers.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the int field.
Returns
Section titled “Returns”FieldDefinition
<number
>
Example
Section titled “Example”views: Field.int()
number()
Section titled “number()”number(
options
):FieldDefinition
<number
>
Defines a number
field (integer or float).
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Standard field options.
Returns
Section titled “Returns”FieldDefinition
<number
>
Example
Section titled “Example”Field.number({ default: 0 })
object()
Section titled “object()”object<
TSchema
>(schema
,options
):FieldDefinition
<InferShapeFromFields
<TSchema
>>
Defines an object
field with a nested schema.
Type Parameters
Section titled “Type Parameters”TSchema
Section titled “TSchema”TSchema
extends Record
<string
, FieldDefinition
<unknown
>>
Parameters
Section titled “Parameters”schema
Section titled “schema”TSchema
An object defining the shape of the nested object.
options
Section titled “options”ObjectFieldOptions
= {}
Options for the object field, such as flexible
.
Returns
Section titled “Returns”FieldDefinition
<InferShapeFromFields
<TSchema
>>
Example
Section titled “Example”meta: Field.object({ views: Field.number(), lastVisited: Field.datetime(),})
option()
Section titled “option()”option<
FD
>(fieldDefinition
):FieldDefinition
<undefined
|InferFieldType
<FD
>>
Makes a field optional. In SurrealDB, fields are required by default.
Wrapping a field definition with option()
makes it optional.
Type Parameters
Section titled “Type Parameters”FD
extends FieldDefinition
<unknown
>
Parameters
Section titled “Parameters”fieldDefinition
Section titled “fieldDefinition”FD
The field definition to make optional.
Returns
Section titled “Returns”FieldDefinition
<undefined
| InferFieldType
<FD
>>
Example
Section titled “Example”bio: Field.option(Field.string())
record()
Section titled “record()”record<
TModel
>(tableClassThunk
,options
):FieldDefinition
<InstanceType
<TModel
> |RecordId
<TModel
["_tableName"
]>>
Defines a record
field, which creates a standard Record Link to a record in another table.
This stores a RecordId
(a pointer). It does NOT use the experimental REFERENCE
feature and does not provide automatic referential integrity.
If the linked record is deleted, this field will hold a dangling reference.
Type Parameters
Section titled “Type Parameters”TModel
Section titled “TModel”TModel
extends AnyModelClass
Parameters
Section titled “Parameters”tableClassThunk
Section titled “tableClassThunk”() => TModel
A thunk () => ModelClass
returning the model being referenced. This is required to prevent circular dependencies.
options
Section titled “options”RecordFieldOptions
= {}
Options for the record field.
Returns
Section titled “Returns”FieldDefinition
<InstanceType
<TModel
> | RecordId
<TModel
["_tableName"
]>>
Example
Section titled “Example”// Creates a link to a User record.author: Field.record(() => User)
set<
TElementDef
>(element
,options
):FieldDefinition
<Set
<InferFieldType
<TElementDef
>>>
Defines a set
field, which is a collection of unique values.
Type Parameters
Section titled “Type Parameters”TElementDef
Section titled “TElementDef”TElementDef
extends FieldDefinition
<unknown
>
Parameters
Section titled “Parameters”element
Section titled “element”TElementDef
The field definition for the elements within the set.
options
Section titled “options”ArrayFieldOptions
<InferFieldType
<TElementDef
>> = {}
Options for the set field.
Returns
Section titled “Returns”FieldDefinition
<Set
<InferFieldType
<TElementDef
>>>
Example
Section titled “Example”// A set of unique tagstags: Field.set(Field.string())
string()
Section titled “string()”string(
options
):FieldDefinition
<string
>
Defines a string
field.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Standard field options.
Returns
Section titled “Returns”FieldDefinition
<string
>
Example
Section titled “Example”Field.string({ assert: '$value.length > 0' })
uuid()
Section titled “uuid()”uuid(
options
):FieldDefinition
<Uuid
>
Defines a uuid
field.
Parameters
Section titled “Parameters”options
Section titled “options”FieldOptions
= {}
Options for the uuid field.
Returns
Section titled “Returns”FieldDefinition
<Uuid
>
Example
Section titled “Example”uniqueId: Field.uuid()