| sidebar_position | 7 |
|---|
Functions are used to provide values for attribute arguments, e.g., current DateTime, an auto-increment Int, etc. They can be used in place of attribute arguments, like:
model Model {
...
serial Int @default(autoincrement())
createdAt DateTime @default(now())
}ZModel's standard library provides a set of predefined functions, plugins can provide additional functions, and you can also define your own functions in the schema.
function NAME(PARAMS): RETURN_TYPE {}-
NAME
Function name. Must be a valid identifier.
-
PARAMS
Parameters. See Parameters for details.
-
RETURN_TYPE
Return type. Must be a valid type as described in Parameters.
Example:
function uuid(version: Int?): String {}id String @default(FUNC_NAME(ARGS))-
FUNC_NAME
Function name.
-
ARGS
Argument list. See Parameters for details.
Example:
id String @default(uuid(4))A function can have zero or more parameters. A parameter has a name and a type.
Valid parameter types include:
- String
- Boolean
- Int
- BigInt
- Float
- Decimal
- DateTime
- Bytes
- Any
Parameter's type can also carry the following suffix:
- [] to indicate it's a list type
- ? to indicate it's optional
:::info Functions related to input validation are documented in a separate page. :::
function uuid(): String {}Generates a globally unique identifier based on the UUID spec.
function cuid(version: Int?): String {}Generates a unique identifier based on the CUID spec. Pass 2 as an argument to use cuid2.
function nanoid(length: Int?): String {}Generates an identifier based on the nanoid spec.
function ulid(): String {}Generates a unique identifier based on the ULID spec.
function now(): DateTime {}Gets current date-time.
function autoincrement(): Int {}Creates a sequence of integers in the underlying database and assign the incremented values to the ID values of the created records based on the sequence.
function dbgenerated(expr: String): Any {}Represents default values that cannot be expressed in ZModel (such as random()).
function auth(): AUTH_TYPE {}Gets the current login user. The return type is resolved to a model or type annotated with the @@auth attribute, and if not available, a model or type named User.