Skip to content

Commit e1188a5

Browse files
sanny-ioymc9
andauthored
doc: formatting IDs (#536)
* doc: formatting IDs * doc: move default value generator doc to the "modeling" part, add parameter documentation ref guide * doc: add links to function ref * remove the unused md file * update --------- Co-authored-by: ymc9 <104139426+ymc9@users.noreply.github.com>
1 parent b81d5bb commit e1188a5

2 files changed

Lines changed: 42 additions & 12 deletions

File tree

docs/modeling/model.md

Lines changed: 18 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,15 +144,17 @@ model User {
144144
}
145145
```
146146

147+
### Default values
148+
147149
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:
148150

149-
- `now()`: returns the current timestamp
150-
- `cuid()`: returns a CUID
151-
- `uuid()`: returns a UUID
152-
- `ulid()`: returns a ULID
153-
- `nanoid()`: returns a Nano ID
154-
- `autoincrement()`: returns an auto-incrementing integer (only for integer fields)
155-
- `dbgenerated("...")`: calls a native db function
151+
- [`now()`](../reference/zmodel/function.md#now): returns the current timestamp
152+
- [`cuid()`](../reference/zmodel/function.md#cuid): returns a CUID
153+
- [`uuid()`](../reference/zmodel/function.md#uuid): returns a UUID
154+
- [`ulid()`](../reference/zmodel/function.md#ulid): returns a ULID
155+
- [`nanoid()`](../reference/zmodel/function.md#nanoid): returns a Nano ID
156+
- [`autoincrement()`](../reference/zmodel/function.md#autoincrement): returns an auto-incrementing integer (only for integer fields)
157+
- [`dbgenerated("...")`](../reference/zmodel/function.md#dbgenerated): calls a native db function
156158

157159
```zmodel
158160
model User {
@@ -162,6 +164,15 @@ model User {
162164
}
163165
```
164166

167+
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:
168+
169+
```zmodel
170+
model User {
171+
// generate a UUID v4 with "user_" prefix
172+
id String @id @default(uuid(4, "user_%s"))
173+
}
174+
```
175+
165176
## Native type mapping
166177

167178
Besides giving a field a type, you can also specify the native database type to use with the `@db.` series of attributes.

docs/reference/zmodel/function.md

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -89,35 +89,54 @@ Functions related to input validation are documented in a [separate page](./inpu
8989
### uuid()
9090

9191
```zmodel
92-
function uuid(): String {}
92+
function uuid(version: Int?, format: String?): String {}
9393
```
9494

9595
Generates a globally unique identifier based on the UUID spec.
9696

97+
*Parameters:*
98+
99+
- `version` (optional): The UUID version to generate. Supported values are `4` (default) and `7`.
100+
- `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.
101+
97102
### cuid()
98103

99104
```zmodel
100-
function cuid(version: Int?): String {}
105+
function cuid(version: Int?, format: String?): String {}
101106
```
102107

103-
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).
108+
Generates a unique identifier based on the [CUID](https://github.com/ericelliott/cuid) spec.
109+
110+
*Parameters:*
111+
112+
- `version` (optional): The CUID version to generate. Supported values are `1` (default) and `2`.
113+
- `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.
104114

105115
### nanoid()
106116

107117
```zmodel
108-
function nanoid(length: Int?): String {}
118+
function nanoid(length: Int?, format: String?): String {}
109119
```
110120

111121
Generates an identifier based on the [nanoid](https://github.com/ai/nanoid) spec.
112122

123+
*Parameters:*
124+
125+
- `length` (optional): The length of the generated id.
126+
- `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.
127+
113128
### ulid()
114129

115130
```zmodel
116-
function ulid(): String {}
131+
function ulid(format: String?): String {}
117132
```
118133

119134
Generates a unique identifier based on the [ULID](https://github.com/ulid/spec) spec.
120135

136+
*Parameters:*
137+
138+
- `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.
139+
121140
### now()
122141

123142
```zmodel

0 commit comments

Comments
 (0)