From 149003532ea8d8ba3c6a5268cf2e501793e305df Mon Sep 17 00:00:00 2001 From: Nikolas Date: Wed, 4 Dec 2024 14:33:37 +0100 Subject: [PATCH 1/2] document id generation methods (#6474) * document id generation methods * Update content/200-orm/500-reference/100-prisma-schema-reference.mdx * 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: Ankur Datta <64993082+ankur-arch@users.noreply.github.com> --- .../100-prisma-schema-reference.mdx | 127 +++++++++++++++--- 1 file changed, 112 insertions(+), 15 deletions(-) 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 be22945929..84d0c804f4 100644 --- a/content/200-orm/500-reference/100-prisma-schema-reference.mdx +++ b/content/200-orm/500-reference/100-prisma-schema-reference.mdx @@ -1600,7 +1600,11 @@ Defines a [default value for a field](/orm/prisma-schema/data-model/models#defin - [`sequence()`](#sequence) (CockroachDB only) - [`dbgenerated(...)`](#dbgenerated) - [`cuid()`](#cuid) + - [`cuid(2)`](#cuid) - [`uuid()`](#uuid) + - [`uuid(4)`](#uuid) + - [`uuid(7)`](#uuid) + - [`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). - Default values are not allowed on relation fields in the Prisma schema. Note however that you can still define default values on the fields backing a relation (the ones listed in the `fields` argument in the `@relation` attribute). A default value on the field backing a relation will mean that relation is populated automatically for you. @@ -3017,15 +3021,14 @@ model User { Generate a globally unique identifier based on the [`cuid`](https://github.com/ericelliott/cuid) spec. +If you'd like to use [`cuid2`](https://github.com/paralleldrive/cuid2) values, you can pass `2` as an argument to the `cuid` function: `cuid(2)`. + #### Remarks -- Compatible with `String` +- Compatible with `String`. - Implemented by Prisma ORM and therefore not "visible" in the underlying database schema. You can still use `cuid()` when using [introspection](/orm/prisma-schema/introspection) by [manually changing your Prisma schema](/orm/prisma-client/setup-and-configuration/custom-model-and-field-names) and [generating Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client), in that case the values will be generated by Prisma's [query engine](/orm/more/under-the-hood/engines). - Since the length of `cuid()` output is undefined per the cuid creator, a safe field size is 30 characters, in order to allow for enough characters for very large values. If you set the field size as less than 30, and then a larger value is generated by `cuid()`, you might see Prisma Client errors such as `Error: The provided value for the column is too long for the column's type.` - -##### MongoDB - -- `cuid()` does not generate a valid `ObjectId` - [use the `@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `cuid()` if your `_id` field is not of type `ObjectId`. +- For **MongoDB**: `cuid()` does not generate a valid `ObjectId`. You can use [`@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `cuid()` if your `_id` field is not of type `ObjectId`. #### Examples @@ -3054,35 +3057,127 @@ model User { +##### Generate `cuid(2)` values as IDs based on the `cuid2` spec + + + + +```prisma +model User { + id String @id @default(cuid(2)) + name String +} +``` + + + + +```prisma +model User { + id String @id @default(cuid(2)) @map("_id") + name String +} +``` + + + + ### `uuid()` -Generate a globally unique identifier based on the [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) spec, version 4 (random). +Generate a globally unique identifier based on the [UUID](https://en.wikipedia.org/wiki/Universally_unique_identifier) spec. Prisma ORM supports versions 4 (default) and 7. #### Remarks -- Compatible with `String` +- Compatible with `String`. - Implemented by Prisma ORM and therefore not "visible" in the underlying database schema. You can still use `uuid()` when using [introspection](/orm/prisma-schema/introspection) by [manually changing your Prisma schema](/orm/prisma-client/setup-and-configuration/custom-model-and-field-names) and [generating Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client), in that case the values will be generated by Prisma ORM's [query engine](/orm/more/under-the-hood/engines). +- For **relational databases**: If you do not want to use Prisma ORM's `uuid()` function, you can use [the native database function with `dbgenerated`](#override-default-value-behavior-for-supported-types). +- For **MongoDB**: `uuid()` does not generate a valid `ObjectId`. You can use [`@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `uuid()` if your `_id` field is not of type `ObjectId`. - +#### Examples -**Note (Relational databases)**: If you do not want to use Prisma ORM's `uuid()` function, you can use [the native database function with `dbgenerated`](#override-default-value-behavior-for-supported-types). +##### Generate `uuid()` values as IDs using UUID v4 - + + -##### MongoDB +```prisma +model User { + id String @id @default(uuid()) + name String +} +``` -- `uuid()` does not generate a valid `ObjectId` - [use the `@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `uuid()` if your `_id` field is not of type `ObjectId`. + + + +```prisma +model User { + id String @id @default(uuid()) @map("_id") + name String +} +``` + + + + +##### Generate `uuid(7)` values as IDs using UUID v7 + + + + +```prisma +model User { + id String @id @default(uuid(7)) + name String +} +``` + + + + +```prisma +model User { + id String @id @default(uuid(7)) @map("_id") + name String +} +``` + + + + +### `nanoid()` + +Generated values based on the [Nano ID](https://github.com/ai/nanoid) spec. + +:::info + +Nano ID is quite comparable to UUID v4 (random-based). It has a similar number of random bits in the ID (126 in Nano ID and 122 in UUID), so it has a similar collision probability: + +For there to be a one in a billion chance of duplication, 103 trillion version 4 IDs must be generated. + +There are two main differences between Nano ID and UUID v4: + +- Nano ID uses a bigger alphabet, so a similar number of random bits are packed in just 21 symbols instead of 36. +- Nano ID code is 4 times smaller than uuid/v4 package: 130 bytes instead of 423. + +::: + +#### Remarks + +- Compatible with `String`. +- Implemented by Prisma ORM and therefore not "visible" in the underlying database schema. You can still use `uuid()` when using [introspection](/orm/prisma-schema/introspection) by [manually changing your Prisma schema](/orm/prisma-client/setup-and-configuration/custom-model-and-field-names) and [generating Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client), in that case the values will be generated by Prisma ORM's [query engine](/orm/more/under-the-hood/engines). +- For **MongoDB**: `nanoid()` does not generate a valid `ObjectId`. You can use [`@db.ObjectId` syntax](#generate-objectid-as-ids-mongodb-only) if you want to use `ObjectId` in the underlying database. However, you can still use `nanoid()` if your `_id` field is not of type `ObjectId`. #### Examples -##### Generate `uuid()` values as IDs +##### Generate `nanoid()` values as IDs ```prisma model User { - id String @id @default(uuid()) + id String @id @default(nanoid()) name String } ``` @@ -3092,7 +3187,7 @@ model User { ```prisma model User { - id String @id @default(uuid()) @map("_id") + id String @id @default(nanoid()) @map("_id") name String } ``` @@ -3100,6 +3195,8 @@ model User { + + ### `now()` Set a timestamp of the time when a record is created. From 4bb510f6fa5b7179bff93722b0e90c1d8a18388e Mon Sep 17 00:00:00 2001 From: Ankur Datta <64993082+ankur-arch@users.noreply.github.com> Date: Wed, 4 Dec 2024 19:51:45 +0600 Subject: [PATCH 2/2] feat: add default `statement_cache_size` value (#6485) --- content/200-orm/050-overview/500-databases/300-postgresql.mdx | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/content/200-orm/050-overview/500-databases/300-postgresql.mdx b/content/200-orm/050-overview/500-databases/300-postgresql.mdx index 1ea784fa7c..daa20db190 100644 --- a/content/200-orm/050-overview/500-databases/300-postgresql.mdx +++ b/content/200-orm/050-overview/500-databases/300-postgresql.mdx @@ -151,7 +151,7 @@ The following arguments can be used: | `host` | No | | Points to a directory that contains a socket to be used for the connection | | `socket_timeout` | No | | Maximum number of seconds to wait until a single query terminates | | `pgbouncer` | No | `false` | Configure the Engine to [enable PgBouncer compatibility mode](/orm/prisma-client/setup-and-configuration/databases-connections/pgbouncer) | -| `statement_cache_size` | No | `500` | Since 2.1.0: Specifies the number of [prepared statements](#prepared-statement-caching) cached per connection | +| `statement_cache_size` | No | `100` | Since 2.1.0: Specifies the number of [prepared statements](#prepared-statement-caching) cached per connection | | `application_name` | No | | Since 3.3.0: Specifies a value for the application_name configuration parameter | | `channel_binding` | No | `prefer` | Since 4.8.0: Specifies a value for the channel_binding configuration parameter | | `options` | No | | Since 3.8.0: Specifies command line options to send to the server at connection start | @@ -305,6 +305,6 @@ The two queries after parameterization will be the same, and the second query ca SELECT * FROM user WHERE name = $1 ``` -Every database connection maintained by Prisma Client has a separate cache for storing prepared statements. The size of this cache can be tweaked with the `statement_cache_size` parameter in the connection string. By default, Prisma Client caches 500 statements per connection. +Every database connection maintained by Prisma Client has a separate cache for storing prepared statements. The size of this cache can be tweaked with the `statement_cache_size` parameter in the connection string. By default, Prisma Client caches `100` statements per connection. Due to the nature of pgBouncer, if the `pgbouncer` parameter is set to `true`, the prepared statement cache is automatically disabled for that connection.