From 12218a05183171151a654b2969af92b4473273e8 Mon Sep 17 00:00:00 2001 From: Ryan Chenkie Date: Tue, 7 Jan 2025 11:08:23 -0500 Subject: [PATCH] add docs for ulid (#6571) * add docs for ulid * Update content/200-orm/500-reference/100-prisma-schema-reference.mdx * Update content/200-orm/500-reference/100-prisma-schema-reference.mdx --------- Co-authored-by: Nikolas --- .../100-prisma-schema-reference.mdx | 77 ++++++++++++++++++- 1 file changed, 76 insertions(+), 1 deletion(-) diff --git a/content/200-orm/500-reference/100-prisma-schema-reference.mdx b/content/200-orm/500-reference/100-prisma-schema-reference.mdx index e782a27187..526989463f 100644 --- a/content/200-orm/500-reference/100-prisma-schema-reference.mdx +++ b/content/200-orm/500-reference/100-prisma-schema-reference.mdx @@ -1166,6 +1166,7 @@ Defines a single-field ID on the model. - [`autoincrement()`](#autoincrement) - [`cuid()`](#cuid) - [`uuid()`](#uuid) + - [`ulid()`](#ulid) - Can be defined on any scalar field (`String`, `Int`, `enum`) @@ -1190,7 +1191,7 @@ Defines a single-field ID on the model. id String @db.ObjectId @map("_id") @default(auto()) ``` -- [`cuid()`](#cuid) and [`uuid()`](#uuid) are supported but do not generate a valid `ObjectId` - use `auto()` instead for `@id` +- [`cuid()`](#cuid), [`uuid()`](#uuid) and [`ulid()`](#ulid) are supported but do not generate a valid `ObjectId` - use `auto()` instead for `@id` - `autoincrement()` is **not supported** #### Arguments @@ -1314,6 +1315,43 @@ id String @id @default(auto()) @db.ObjectId @map("_id") +##### Generate `ulid()` values as IDs + + + + + +```prisma +model User { + id String @id @default(ulid()) + name String +} +``` + + + + + +```prisma +model User { + id String @id @default(ulid()) @map("_id") + name String +} +``` + + + +You cannot use `ulid()` to generate a default value if your `id` field is of type `ObjectId`. Use the following syntax to generate a valid `ObjectId`: + +```prisma +id String @id @default(auto()) @db.ObjectId @map("_id") +``` + + + + + + ##### Single-field IDs _without_ default values In the following example, `id` does not have a default value: @@ -1607,6 +1645,7 @@ Defines a [default value for a field](/orm/prisma-schema/data-model/models#defin - [`uuid()`](#uuid) - [`uuid(4)`](#uuid) - [`uuid(7)`](#uuid) + - [`ulid()`](#ulid) - [`nanoid()`](#nanoid) - [`now()`](#now) - Default values that cannot yet be represented in the Prisma schema are represented by the [`dbgenerated(...)` function](#dbgenerated) when you use [introspection](/orm/prisma-schema/introspection). @@ -1620,6 +1659,7 @@ Defines a [default value for a field](/orm/prisma-schema/data-model/models#defin - [`auto()`](#auto) (can only be used with `@db.ObjectId` to generate an `ObjectId` in MongoDB) - [`cuid()`](#cuid) - [`uuid()`](#uuid) + - [`ulid()`](#ulid) - [`now()`](#now) #### Arguments @@ -3148,6 +3188,41 @@ model User { +### `ulid()` + +Generate a universally unique lexicographically sortable identifier based on the [ULID](https://github.com/ulid/spec) spec. + +#### Remarks + +- `ulid()` will produce 128-bit random identifier represented as a 26-character long alphanumeric string, e.g.: `01ARZ3NDEKTSV4RRFFQ69G5FAV` + +#### Examples + +##### Generate `ulid()` values as IDs + + + + +```prisma +model User { + id String @id @default(ulid()) + name String +} +``` + + + + +```prisma +model User { + id String @id @default(ulid()) @map("_id") + name String +} +``` + + + + ### `nanoid()` Generated values based on the [Nano ID](https://github.com/ai/nanoid) spec. `nanoid()` accepts an integer value between 2 and 255 that specifies the _length_ of the generate ID value, e.g. `nanoid(16)` will generated ID with 16 characters. If you don't provide a value to the nanoid() function, the default value is 21.