diff --git a/docs/modeling/model.md b/docs/modeling/model.md index c5975918..2c42d9b7 100644 --- a/docs/modeling/model.md +++ b/docs/modeling/model.md @@ -144,15 +144,17 @@ model User { } ``` +### Default values + A default value can be specified for a field with the `@default` attribute. The value can be a literal, an enum value, or a supported function call, including: -- `now()`: returns the current timestamp -- `cuid()`: returns a CUID -- `uuid()`: returns a UUID -- `ulid()`: returns a ULID -- `nanoid()`: returns a Nano ID -- `autoincrement()`: returns an auto-incrementing integer (only for integer fields) -- `dbgenerated("...")`: calls a native db function +- [`now()`](../reference/zmodel/function.md#now): returns the current timestamp +- [`cuid()`](../reference/zmodel/function.md#cuid): returns a CUID +- [`uuid()`](../reference/zmodel/function.md#uuid): returns a UUID +- [`ulid()`](../reference/zmodel/function.md#ulid): returns a ULID +- [`nanoid()`](../reference/zmodel/function.md#nanoid): returns a Nano ID +- [`autoincrement()`](../reference/zmodel/function.md#autoincrement): returns an auto-incrementing integer (only for integer fields) +- [`dbgenerated("...")`](../reference/zmodel/function.md#dbgenerated): calls a native db function ```zmodel model User { @@ -162,6 +164,15 @@ model User { } ``` +Prefixing and suffixing entity IDs is becoming more common in database design, usually by including the model name in the generated ID. To support this pattern, functions that generate `String` IDs (`cuid()`, `uuid()`, `ulid()`, `nanoid()`) takes an optional `format` argument to allow passing in a pattern that controls the output format. `%s` in the pattern will be replaced by the generated id. For example: + +```zmodel +model User { + // generate a UUID v4 with "user_" prefix + id String @id @default(uuid(4, "user_%s")) +} +``` + ## Native type mapping Besides giving a field a type, you can also specify the native database type to use with the `@db.` series of attributes. diff --git a/docs/reference/zmodel/function.md b/docs/reference/zmodel/function.md index 66c0bfa1..1ec2ba4d 100644 --- a/docs/reference/zmodel/function.md +++ b/docs/reference/zmodel/function.md @@ -89,35 +89,54 @@ Functions related to input validation are documented in a [separate page](./inpu ### uuid() ```zmodel -function uuid(): String {} +function uuid(version: Int?, format: String?): String {} ``` Generates a globally unique identifier based on the UUID spec. +*Parameters:* + +- `version` (optional): The UUID version to generate. Supported values are `4` (default) and `7`. +- `format` (optional): A pattern to control the output format. `%s` in the pattern will be replaced by the generated id. Use escaped `\\%s` to have a literal `%s` in the output. + ### cuid() ```zmodel -function cuid(version: Int?): String {} +function cuid(version: Int?, format: String?): String {} ``` -Generates a unique identifier based on the [CUID](https://github.com/ericelliott/cuid) spec. Pass `2` as an argument to use [cuid2](https://github.com/paralleldrive/cuid2). +Generates a unique identifier based on the [CUID](https://github.com/ericelliott/cuid) spec. + +*Parameters:* + +- `version` (optional): The CUID version to generate. Supported values are `1` (default) and `2`. +- `format` (optional): A pattern to control the output format. `%s` in the pattern will be replaced by the generated id. Use escaped `\\%s` to have a literal `%s` in the output. ### nanoid() ```zmodel -function nanoid(length: Int?): String {} +function nanoid(length: Int?, format: String?): String {} ``` Generates an identifier based on the [nanoid](https://github.com/ai/nanoid) spec. +*Parameters:* + +- `length` (optional): The length of the generated id. +- `format` (optional): A pattern to control the output format. `%s` in the pattern will be replaced by the generated id. Use escaped `\\%s` to have a literal `%s` in the output. + ### ulid() ```zmodel -function ulid(): String {} +function ulid(format: String?): String {} ``` Generates a unique identifier based on the [ULID](https://github.com/ulid/spec) spec. +*Parameters:* + +- `format` (optional): A pattern to control the output format. `%s` in the pattern will be replaced by the generated id. Use escaped `\\%s` to have a literal `%s` in the output. + ### now() ```zmodel