Skip to main content
Version: 3.0 Alpha

Function

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.

Syntax​

Definition​

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 {}

Application​

id String @default(FUNC_NAME(ARGS))
  • FUNC_NAME

    Function name.

  • ARGS

    Argument list. See Parameters for details.

Example:

id String @default(uuid(4))

Parameters​

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

Predefined functions​

uuid()​

function uuid(): String {}

Generates a globally unique identifier based on the UUID spec.

cuid()​

function cuid(version: Int?): String {}

Generates a unique identifier based on the CUID spec. Pass 2 as an argument to use cuid2.

nanoid()​

function nanoid(length: Int?): String {}

Generates an identifier based on the nanoid spec.

ulid()​

function ulid(): String {}

Generates a unique identifier based on the ULID spec.

now()​

function now(): DateTime {}

Gets current date-time.

autoincrement()​

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.

dbgenerated()​

function dbgenerated(expr: String): Any {}

Represents default values that cannot be expressed in ZModel (such as random()).

auth()​

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.

Comments
Feel free to ask questions, give feedback, or report issues.

Don't Spam


You can edit/delete your comments by going directly to the discussion, clicking on the 'comments' link below