Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
80 changes: 44 additions & 36 deletions content/recipes/prisma.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,30 +65,20 @@ $ npx prisma init
This command creates a new `prisma` directory with the following contents:

- `schema.prisma`: Specifies your database connection and contains the database schema
- `prisma.config.ts`: A configuration file for your projects
- `.env`: A [dotenv](https://github.com/motdotla/dotenv) file, typically used to store your database credentials in a group of environment variables

#### Set the generator output path

> warning **Warning** In Prisma ORM 7, Prisma Client will no longer be generated in `node_modules` by default and will require an output path to be defined. [Learn more below on how to define an output path](https://www.prisma.io/docs/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path).

Specify your output `path` for the generated Prisma client either by passing `--output ../generated/prisma` during prisma init, or directly in your Prisma schema:
Specify your output `path` for the generated Prisma client either by passing `--output ../src/generated/prisma` during prisma init, or directly in your Prisma schema:

```groovy
generator client {
provider = "prisma-client-js"
output = "../generated/prisma"
provider = "prisma-client"
output = "../src/generated/prisma"
}
```

By default, Nest does not include the generated Prisma client in the build. To fix this, the path should be explicitly defined in `tsconfig.build.json`:

```json
{
"extends": "./tsconfig.json",
"include": ["src", "generated"],
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}
```

#### Set the database connection

Expand All @@ -97,12 +87,11 @@ Your database connection is configured in the `datasource` block in your `schema
```groovy
datasource db {
provider = "sqlite"
url = env("DATABASE_URL")
}

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

Expand All @@ -129,12 +118,11 @@ If you're using PostgreSQL, you have to adjust the `schema.prisma` and `.env` fi
```groovy
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

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

Expand All @@ -161,12 +149,11 @@ If you're using MySQL, you have to adjust the `schema.prisma` and `.env` files a
```groovy
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}

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

Expand All @@ -187,12 +174,11 @@ If you're using Microsoft SQL Server or Azure SQL Server, you have to adjust the
```groovy
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}

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

Expand Down Expand Up @@ -279,17 +265,39 @@ CREATE UNIQUE INDEX "User.email_unique" ON "User"("email");

#### Install and generate Prisma Client

Prisma Client is a type-safe database client that's _generated_ from your Prisma model definition. Because of this approach, Prisma Client can expose [CRUD](https://www.prisma.io/docs/concepts/components/prisma-client/crud) operations that are _tailored_ specifically to your models.
Prisma Client is a type-safe database client that's _generated_ from your Prisma model definition. Because of this approach, Prisma Client can expose [CRUD](https://www.prisma.io/docs/concepts/components/prisma-client/crud) operations that are _tailored_ specifically to your models.

To install Prisma Client in your project, run the following command in your terminal:

```bash
$ npm install @prisma/client
```

Note that during installation, Prisma automatically invokes the `prisma generate` command for you. In the future, you need to run this command after _every_ change to your Prisma models to update your generated Prisma Client.
Once installed, you can run the generate command to generate the types and Client needed for your project. If any changes are made to your schema, you will need to rerun the `generate` command to keep those types in sync.

```bash
$ npx prisma generate
```

In addition to Prisma Client, you also need to a driver adapter for the type of database you are working with. For SQLite, you can install the `@prisma/adapter-better-sqlite3` driver.

```bash
npm install @prisma/adapter-better-sqlite3
```

> info **Note** The `prisma generate` command reads your Prisma schema and updates the generated Prisma Client library inside `node_modules/@prisma/client`.
<details> <summary>Expand if you're using PostgreSQL, MySQL, MsSQL, or AzureSQL</summary>

- For PostgreSQL
```bash
npm install @prisma/adapter-pg
```

- For MySQL, MsSQL, AzureSQL:
```bash
npm install @prisma/adapter-mariadb`
```

</details>

#### Use Prisma Client in your NestJS services

Expand All @@ -300,19 +308,19 @@ When setting up your NestJS application, you'll want to abstract away the Prisma
Inside the `src` directory, create a new file called `prisma.service.ts` and add the following code to it:

```typescript
import { Injectable, OnModuleInit } from '@nestjs/common';
import { PrismaClient } from 'generated/prisma';
import { Injectable } from '@nestjs/common';
import { PrismaClient } from './generated/prisma/client';
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';

@Injectable()
export class PrismaService extends PrismaClient implements OnModuleInit {
async onModuleInit() {
await this.$connect();
export class PrismaService extends PrismaClient {
constructor() {
const adapter = new PrismaBetterSqlite3({ url: process.env.DATABASE_URL });
super({ adapter });
}
}
```

> info **Note** The `onModuleInit` is optional — if you leave it out, Prisma will connect lazily on its first call to the database.

Next, you can write services that you can use to make database calls for the `User` and `Post` models from your Prisma schema.

Still inside the `src` directory, create a new file called `user.service.ts` and add the following code to it:
Expand Down