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
43 changes: 35 additions & 8 deletions docs/pages/getting-started/adapters/prisma.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -15,32 +15,55 @@ import { Accordion, Accordions } from "@/components/Accordion"
### Installation

```bash npm2yarn
npm install @prisma/client @auth/prisma-adapter
npm install @prisma/client @prisma/extension-accelerate @auth/prisma-adapter
npm install prisma --save-dev
```

### Environment Variables

Prisma needs to set up the environment variable to establish a connection with your database and retrieve data. Prisma requires the `DATABASE_URL` environment variable to create the connection. For more information, read the [docs](https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/relational-databases/connect-your-database-typescript-postgresql).
If you're using Prisma Postgres, the `DATABASE_URL` will be automatically set up during initialization. For other databases, you'll need to manually configure the `DATABASE_URL` environment variable. For more information, read the [docs](https://www.prisma.io/docs/getting-started/setup-prisma/start-from-scratch/relational-databases/connect-your-database-typescript-postgresql).

```sh
DATABASE_URL=postgresql://USER:PASSWORD@HOST:PORT/DATABASE?schema=SCHEMA
```

### Configuration

First, initialize Prisma in your project. If you're using Prisma Postgres, run:

```bash
npx prisma init --db --output ./src/generated/prisma
```

This will create a Prisma Postgres database, set up your schema file, and configure the output directory for the generated Prisma Client.

For other databases, run:

```bash
npx prisma init --output ./src/generated/prisma
```

Then manually configure your `DATABASE_URL` in the `.env` file.

To improve performance using `Prisma ORM`, we can set up the Prisma instance to ensure only one instance is created throughout the project and then import it from any file as needed. This approach avoids recreating instances of PrismaClient every time it is used. Finally, we can import the Prisma instance from the `auth.ts` file configuration.

```ts filename="prisma.ts"
import { PrismaClient } from "@prisma/client"
import { PrismaClient } from "../src/generated/client"
import { withAccelerate } from "@prisma/extension-accelerate"

const globalForPrisma = globalThis as unknown as { prisma: PrismaClient }

export const prisma = globalForPrisma.prisma || new PrismaClient()
export const prisma =
globalForPrisma.prisma || new PrismaClient().$extends(withAccelerate())

if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma
```

<Callout type="info">
If you're not using Prisma Postgres with Accelerate, you can omit the
`withAccelerate()` extension and delete `.$extends(withAccelerate())`.
</Callout>

<Callout type="warning">
We recommend using version `@prisma/[email protected]` or above if using
middleware or any other edge runtime(s). See [edge
Expand Down Expand Up @@ -147,7 +170,8 @@ datasource db {
}

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

model User {
Expand Down Expand Up @@ -231,7 +255,8 @@ datasource db {
}

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

model User {
Expand Down Expand Up @@ -330,7 +355,8 @@ datasource db {
}

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

model User {
Expand Down Expand Up @@ -416,7 +442,8 @@ datasource db {
}

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

model User {
Expand Down
Loading