Skip to content

Commit

Permalink
add docs for ulid (#6571)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
chenkie and nikolasburk authored Jan 7, 2025
1 parent f008253 commit 12218a0
Showing 1 changed file with 76 additions and 1 deletion.
77 changes: 76 additions & 1 deletion content/200-orm/500-reference/100-prisma-schema-reference.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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`)

Expand All @@ -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
Expand Down Expand Up @@ -1314,6 +1315,43 @@ id String @id @default(auto()) @db.ObjectId @map("_id")

</TabbedContent>

##### Generate `ulid()` values as IDs

<TabbedContent code>

<TabItem value="Relational databases">

```prisma
model User {
id String @id @default(ulid())
name String
}
```

</TabItem>

<TabItem value="MongoDB">

```prisma
model User {
id String @id @default(ulid()) @map("_id")
name String
}
```

<Admonition type="warning">

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")
```

</Admonition>
</TabItem>

</TabbedContent>

##### Single-field IDs _without_ default values

In the following example, `id` does not have a default value:
Expand Down Expand Up @@ -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).
Expand All @@ -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
Expand Down Expand Up @@ -3148,6 +3188,41 @@ model User {
</TabItem>
</TabbedContent>

### `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

<TabbedContent code>
<TabItem value="Relational databases">

```prisma
model User {
id String @id @default(ulid())
name String
}
```

</TabItem>
<TabItem value="MongoDB">

```prisma
model User {
id String @id @default(ulid()) @map("_id")
name String
}
```

</TabItem>
</TabbedContent>

### `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.
Expand Down

0 comments on commit 12218a0

Please sign in to comment.