Skip to content

Commit

Permalink
Link fixes and small task (#1261)
Browse files Browse the repository at this point in the history
  • Loading branch information
mhwelander authored Feb 10, 2021
1 parent 2c74d16 commit 5c80e9f
Show file tree
Hide file tree
Showing 39 changed files with 786 additions and 272 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -387,7 +387,7 @@ Right now, there's a few minor "issues" with the data model:
- The `Post` and `Profile` relation fields on `User` as well as the `User` relation field on `Profile` are all uppercased. To adhere to Prisma's [naming conventions](../../reference/api-reference/prisma-schema-reference#naming-conventions-1) <span class="api"></span>, both fields should be lowercased to `post`, `profile` and `user`.
- Even after lowercasing, the `post` field on `User` is still slightly misnamed. That's because it actually refers to a [list](../../concepts/components/prisma-schema/data-model#type-modifiers) of posts – a better name therefore would be the plural form: `posts`.

These changes are relevant for the generated Prisma Client API where using lowercased relation fields `author`, `posts`, `profile` and `user` will feel more natural and idiomatic to JavaScript/TypeScript developers. You can therefore [configure your Prisma Client API](../../concepts/components/prisma-client/generating-prisma-client/customizing-the-prisma-client-api).
These changes are relevant for the generated Prisma Client API where using lowercased relation fields `author`, `posts`, `profile` and `user` will feel more natural and idiomatic to JavaScript/TypeScript developers. You can therefore [configure your Prisma Client API](../../concepts/components/prisma-client/working-with-prismaclient/use-custom-model-and-field-names).

Because [relation fields](../../concepts/components/prisma-schema/relations#relation-fields) are _virtual_ (i.e. they _do not directly manifest in the database_), you can manually rename them in your Prisma schema without touching the database:

Expand Down Expand Up @@ -466,7 +466,7 @@ const user = await prisma.myUser.create({
})
```

Learn more about this on the [Configuring your Prisma Client API](../../concepts/components/prisma-client/generating-prisma-client/customizing-the-prisma-client-api) page.
Learn more about this on the [Configuring your Prisma Client API](../../concepts/components/prisma-client/working-with-prismaclient/use-custom-model-and-field-names) page.

## Install and generate Prisma Client

Expand All @@ -476,7 +476,7 @@ To get started with Prisma Client, you need to install the `@prisma/client` pack
npm install @prisma/client
```

Notice that the [`@prisma/client` node module](../../concepts/components/prisma-client/generating-prisma-client#the-prismaclient-npm-package) references a folder named `.prisma\client`. The `.prisma\client` folder contains your generated Prisma client, and is modified each time you change the schema and run the following command:
Notice that the [`@prisma/client` node module](../../concepts/components/prisma-client/working-with-prismaclient/generating-prisma-client#the-prismaclient-npm-package) references a folder named `.prisma\client`. The `.prisma\client` folder contains your generated Prisma client, and is modified each time you change the schema and run the following command:

```terminal copy
npx prisma generate
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ There are two major workflows for "getting" a data model into your Prisma schema
- Manually writing the data model and mapping it to the database with [Prisma Migrate](../../components/prisma-migrate)
- Generating the data model by [introspecting](../../components/introspection) a database

Once the data model is defined, you can [generate Prisma Client](../../components/prisma-client/generating-prisma-client) which will expose CRUD and more queries for the defined models. If you're using TypeScript, you'll get full type-safety for all queries (even when only retrieving the subsets of a model's fields).
Once the data model is defined, you can [generate Prisma Client](../../components/prisma-client/working-with-prismaclient/generating-prisma-client) which will expose CRUD and more queries for the defined models. If you're using TypeScript, you'll get full type-safety for all queries (even when only retrieving the subsets of a model's fields).

### Accessing your database with Prisma Client

Expand All @@ -103,7 +103,7 @@ After you change your data model, you'll need to manually re-generate Prisma Cli
prisma generate
```

Notice that the [`@prisma/client` node module](../../components/prisma-client/generating-prisma-client#the-prismaclient-npm-package) references a folder named `.prisma\client`. The `.prisma\client` folder contains your generated Prisma client, and is modified each time you change the schema and run the following command:
Notice that the [`@prisma/client` node module](../../components/prisma-client/working-with-prismaclient/generating-prisma-client#the-prismaclient-npm-package) references a folder named `.prisma\client`. The `.prisma\client` folder contains your generated Prisma client, and is modified each time you change the schema and run the following command:

#### Using Prisma Client to send queries to your database

Expand Down Expand Up @@ -210,7 +210,7 @@ The typical workflow when using **SQL migrations and introspection** is slightly

1. Manually adjust your database schema using SQL or a third-party migration tool
1. (Re-)introspect your database
1. Optionally [(re-)configure your Prisma Client API](../../components/prisma-client/generating-prisma-client/customizing-the-prisma-client-api))
1. Optionally [(re-)configure your Prisma Client API](../../components/prisma-client/working-with-prismaclient/use-custom-model-and-field-names))
1. (Re-)generate Prisma Client
1. Use Prisma Client in your application code to access your database

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ const user = await prisma.user.findUnique({
})
```

With this query, `user`'s type will also include `Post`s which can be accessed with the `posts` array property:
With this query, `user`'s type will also include `Post`s which can be accessed with the `posts` array field:

```ts
console.log(user.posts[0].title)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ The corresponding database looks like this:
</details>

The following example uses the Prisma Client generated from this model to create a `User` record and three nested `Post` records, each with one or two nested `Category` records:
The following query uses the Prisma Client generated from this model to create:

- A `User` record
- Three nested `Post` records
- Two nested `Category` records

<TabbedContent tabs={[<FileWithIcon text="Query Example" icon="code"/> , <FileWithIcon text="Copy-Paste Example" icon="code"/>]}>

Expand Down Expand Up @@ -237,7 +241,7 @@ With this model definition, Prisma automatically maps the `Comment` model to the

> **Note**: You can also [`@map`](../../../reference/api-reference/prisma-schema-reference#map) <span class="api"></span> a column name or enum value, and `@@map` an enum.
`@map` and `@@map` allow you to [tune the shape of your Prisma Client API](../prisma-client/generating-prisma-client/customizing-the-prisma-client-api#using-map-and-map-to-rename-fields-and-models-in-the-prisma-client-api) by decoupling model and field names from table and column names in the underlying database.
`@map` and `@@map` allow you to [tune the shape of your Prisma Client API](../prisma-client/working-with-prismaclient/use-custom-model-and-field-names#using-map-and-map-to-rename-fields-and-models-in-the-prisma-client-api) by decoupling model and field names from table and column names in the underlying database.

## Defining fields

Expand Down Expand Up @@ -506,7 +510,7 @@ If you do not specify an index name, introspection generates [a name based on Pr

You can define enums in your data model [if they're supported by the data source you use](../../../reference/database-reference/database-features#misc).

Enums are considered [scalar](#field-types) types in the Prisma data model. They're therefore [by default](../prisma-client/select-fields#the-default-selection-set) included as return values in [Prisma Client queries](../prisma-client/crud).
Enums are considered [scalar](#field-types) types in the Prisma data model. They're therefore [by default](../prisma-client/select-fields#return-the-default-selection-set) included as return values in [Prisma Client queries](../prisma-client/crud).

Enums are defined via the [`enum`](../../../reference/api-reference/prisma-schema-reference#enum) <span class="api"></span> block. For example, a `User` has a `Role`:

Expand Down Expand Up @@ -537,7 +541,7 @@ model Post {
}
```

[`cuid()`](../../../reference/api-reference/prisma-schema-reference#cuid) <span class="api"></span> and [`uuid()`](../../../reference/api-reference/prisma-schema-reference#uuid) <span class="api"></span> are implemented by Prisma and therefore are not "visible" in the underlying database schema. You can still use them when using [introspection](../introspection) by [manually changing your Prisma schema](../prisma-client/generating-prisma-client/customizing-the-prisma-client-api) and [generating Prisma Client](../prisma-client/generating-prisma-client), in that case the values will be generated by Prisma's [query engine](../prisma-client/query-engine)
[`cuid()`](../../../reference/api-reference/prisma-schema-reference#cuid) <span class="api"></span> and [`uuid()`](../../../reference/api-reference/prisma-schema-reference#uuid) <span class="api"></span> are implemented by Prisma and therefore are not "visible" in the underlying database schema. You can still use them when using [introspection](../introspection) by [manually changing your Prisma schema](../prisma-client/working-with-prismaclient/use-custom-model-and-field-names) and [generating Prisma Client](../prisma-client/working-with-prismaclient/generating-prisma-client), in that case the values will be generated by Prisma's [query engine](../prisma-client/query-engine)

`autoincrement()`, `now` and `dbgenerated()` are always "implemented" on the database-level, meaning they manifest in the database schema and can be recognized through introspection.

Expand Down Expand Up @@ -575,9 +579,9 @@ const allUsers = await prisma.user.findMany()

### Type definitions

Prisma Client not only provides a query API for models, it also generates type definitions that reflect your model structures. These are part of the generated [`@prisma/client`](../prisma-client/generating-prisma-client#the-prismaclient-npm-package) node module in a file called `index.d.ts`.
Prisma Client not only provides a query API for models, it also generates type definitions that reflect your model structures. These are part of the generated [`@prisma/client`](../prisma-client/working-with-prismaclient/generating-prisma-client#the-prismaclient-npm-package) node module in a file called `index.d.ts`.

When using TypeScript, these type definitions ensure that all your database queries are entirely type safe and validated at compile-time (even partial queries using [`select`](../prisma-client/select-fields#select) or [`include`](../prisma-client/select-fields#include)).
When using TypeScript, these type definitions ensure that all your database queries are entirely type safe and validated at compile-time (even partial queries using [`select`](../../../reference/api-reference/prisma-client-reference#select) <span class="api"></span> or [`include`](../../../reference/api-reference/prisma-client-reference/#include) <span class="api"></span>).

Even when using plain JavaScript, the type definitions are still included in the `@prisma/client` node module, enabling features like [IntelliSense](https://code.visualstudio.com/docs/editor/intellisense)/autocompletion in your editor.

Expand All @@ -594,4 +598,4 @@ export type User = {
}
```
Note that the relation fields `posts` and `profile` are not included in the type definition by default. However, if you need variations of the `User` type you can still define them using some of [Prisma Client's generated helper types](../prisma-client/generating-prisma-client) (in this case, these helper types would be called `UserGetIncludePayload` and `UserGetSelectPayload`).
Note that the relation fields `posts` and `profile` are not included in the type definition by default. However, if you need variations of the `User` type you can still define them using some of [Prisma Client's generated helper types](../prisma-client/working-with-prismaclient/generating-prisma-client) (in this case, these helper types would be called `UserGetIncludePayload` and `UserGetSelectPayload`).
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ In the underlying database, this query:
1. Creates a `User` with an auto-generated `id` (for example, `20`)
2. Creates two new `Post` records and sets the `authorId` of both records to `20`

The following example retrieves a `User` by `id` and includes any related `Post` records:
The following query retrieves a `User` by `id` and includes any related `Post` records:

```ts
const getAuthor = await prisma.user.findUnique({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,57 +1,69 @@
---
title: 'Generating Prisma Client'
metaTitle: 'Generating Prisma Client (Reference)'
title: 'Generating the client'
metaTitle: 'Generating the client (Concepts)'
metaDescription: 'This page explains how to generate Prisma Client. It also provides additional context on the generated client, typical workflows and Node.js configuration.'
---

<TopBlock>

This page explains how to generate Prisma Client. Generating Prisma Client requires the Prisma CLI installed on your machine (learn more [here](../../prisma-cli/installation)).
Prisma Client is an auto-generated database client that's tailored to your database schema. By default, Prisma Client is generated into the `node_modules/@prisma/client` folder, but [you can specify a custom location](#using-a-custom-output-path).

Prisma Client is an auto-generated database client that's tailored to your database schema. By default, it lives inside your `node_modules` directory in `node_modules/@prisma/client`. This location can be adjusted if needed.
To generate and instantiate Prisma Client:

Generating Prisma Client requires three steps:
1. Ensure that you have [Prisma CLI installed on your machine](../../prisma-cli/installation).

1. Add the following `generator` definition to your Prisma schema:

```prisma
generator client {
provider = "prisma-client-js"
}
```

1. Install the `@prisma/client` npm package:
```

```terminal
npm install @prisma/client
```

1. Generate Prisma Client with the following command:
```

```terminal
prisma generate
```

**Important**: You need to re-run the `prisma generate` command after every change that's made to your Prisma schema to update the generated Prisma Client code.
1. You can now [instantiate the Prisma Client](instantiate-prisma-client) in your code:

Here is a graphical illustration of the typical workflow for the Prisma Client generation:
<TabbedContent tabs={[<FileWithIcon text="TypeScript" icon="code"/>, <FileWithIcon text="JavaScript" icon="code"/>]}>
<tab>

![Graphical illustration of the typical workflow for the Prisma Client generation](https://i.imgur.com/aRJmVFY.png)
```ts
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB
```

Note also that `prisma generate` is _automatically_ invoked when you're installing the `@prisma/client` npm package. So, when you're initially setting up Prisma Client, you can typically save the third step from the list above.
</tab>

Once generated, you can import and instantiate [`PrismaClient`](../../../../reference/api-reference/prisma-client-reference#prismaclient) <span class="api"></span> in your code as follows:
<tab>

```js
import { PrismaClient } from '@prisma/client'
```js
const { PrismaClient } = require('@prisma/client')
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB
```

const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB
```
</tab>

or
</TabbedContent>

```js
const { PrismaClient } = require('@prisma/client')
> **Important**: You need to re-run the `prisma generate` command after every change that's made to your Prisma schema to update the generated Prisma Client code.
const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB
```
Here is a graphical illustration of the typical workflow for the Prisma Client generation:

![Graphical illustration of the typical workflow for the Prisma Client generation](https://i.imgur.com/aRJmVFY.png)

Note also that `prisma generate` is _automatically_ invoked when you're installing the `@prisma/client` npm package. So, when you're initially setting up Prisma Client, you can typically save the third step from the list above.

</TopBlock>

Expand All @@ -60,7 +72,7 @@ const prisma = new PrismaClient()
The `@prisma/client` npm package consists of two key parts:

- The `@prisma/client` module itself, which only changes when you re-install the package
- The `.prisma/client` folder, which is the [default location](#specifying-a-custom-output-path) for the unique Prisma client generated from your schema
- The `.prisma/client` folder, which is the [default location](#using-a-custom-output-path) for the unique Prisma client generated from your schema

`@prisma/client/index.d.ts` exports `.prisma/client`:

Expand All @@ -76,36 +88,15 @@ import { PrismaClient } from '@prisma/client'

The Prisma client is generated from your Prisma schema and is unique to your project. Each time you change the schema (for example, by performing a [schema migration](../../prisma-migrate)) and run `prisma generate`, the client code changes:

![The .prisma and @prisma folders](generating-prisma-client/prisma-client-node-module.png)
![The .prisma and @prisma folders](prisma-client-node-module.png)

The `.prisma` folder is unaffected by [pruning](https://docs.npmjs.com/cli/prune.html) in Node.js package managers.

## Specifying the target location for Prisma Client

The Prisma Client generator can be specified as follows in your schema file:

```prisma
generator client {
provider = "prisma-client-js"
}
```
## The location of Prisma Client

Note that this is equivalent to specifying the default `output` path `./node_modules/.prisma/client`:

```prisma
generator client {
provider = "prisma-client-js"
output = "./node_modules/.prisma/client"
}
```

After running `prisma generate` for either of these schema files, the Prisma Client package will be located in:

```
node_modules/.prisma/client
```
If you do not specify an a custom `output` in the `generator` block, Prisma Client is generated into the `./node_modules/.prisma/client` folder by default. There are [some advantages to maintaining the default location](#why-is-prisma-client-generated-into-node_modulesprismaclient-by-default).

## Specifying a custom `output` path
### Using a custom `output` path

You can also specify a custom `output` path on the `generator` configuration, for example:

Expand All @@ -122,15 +113,15 @@ After running `prisma generate` for that schema file, the Prisma Client package
./src/generated/client
```

To import the Prisma Client from a custom location:
To import the `PrismaClient` from a custom location:

```ts
import { PrismaClient } from '../prisma/src/generated/client'
```

## Why is Prisma Client generated into `node_modules/.prisma/client` by default?
### Why is Prisma Client generated into `node_modules/.prisma/client` by default?

### Importing Prisma Client
#### Importing Prisma Client

By generating Prisma Client into `node_modules/.prisma/client` and exporting it from `node_modules/@prisma/client`, you can import it and instantiate the client in your code as follows:

Expand All @@ -152,7 +143,7 @@ const prisma = new PrismaClient()
// use `prisma` in your application to read and write data in your DB
```

### Keeping the query engine binary out of version control by default
#### Keeping the query engine binary out of version control by default

Prisma Client is based on a _query engine_ that's running as a sidecar process alongside your application. This query engine _binary_ is downloaded when `prisma generate` is invoked and stored in the `output` path.

Expand Down
Loading

0 comments on commit 5c80e9f

Please sign in to comment.