Skip to content

Commit 4fe9f05

Browse files
update prisma postgres guide
1 parent ec81e3b commit 4fe9f05

File tree

1 file changed

+24
-16
lines changed

1 file changed

+24
-16
lines changed

src/content/docs/en/guides/backend/prisma-postgres.mdx

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import ReadMore from '~/components/ReadMore.astro';
1313

1414
[Prisma Postgres](https://www.prisma.io/) is a fully managed, serverless Postgres database built for modern web apps.
1515

16-
## Connect via Prisma ORM (Recommended)
16+
## Connect with Prisma ORM (Recommended)
1717

1818
[Prisma ORM](https://www.prisma.io/orm) is the recommended way to connect to your Prisma Postgres database. It provides type-safe queries, migrations, and global performance.
1919

@@ -25,14 +25,14 @@ import ReadMore from '~/components/ReadMore.astro';
2525
Run the following commands to install the necessary Prisma dependencies:
2626

2727
```bash
28-
npm install prisma tsx --save-dev
29-
npm install @prisma/extension-accelerate @prisma/client
28+
npm install prisma tsx --save-dev
29+
npm install @prisma/adapter-pg @prisma/client
3030
```
3131

3232
Once installed, initialize Prisma in your project with the following command:
3333

3434
```bash
35-
npx prisma init --db --output ../src/generated/prisma
35+
npx prisma init --db --output ./generated
3636
```
3737

3838
You'll need to answer a few questions while setting up your Prisma Postgres database. Select the region closest to your location and a memorable name for your database, like "My Astro Project."
@@ -47,12 +47,10 @@ Even if you don't need any specific data models yet, Prisma requires at least on
4747

4848
The following example defines a `Post` model as a placeholder. Add the model to your schema to get started. You can safely delete or replace it later with models that reflect your actual data.
4949

50-
Update the generator provider from `prisma-client-js` to `prisma-client` in your `prisma/schema.prisma` file:
51-
52-
```prisma title="prisma/schema.prisma" {2} ins={11-16}
50+
```prisma title="prisma/schema.prisma" ins={11-16}
5351
generator client {
5452
provider = "prisma-client"
55-
output = "../src/generated/prisma"
53+
output = "./generated"
5654
}
5755
5856
datasource db {
@@ -70,6 +68,14 @@ model Post {
7068

7169
Learn more about configuring your Prisma ORM setup in the [Prisma schema reference](https://www.prisma.io/docs/concepts/components/prisma-schema).
7270

71+
### Generate client
72+
73+
Run the following command to generate the Prisma Client from your schema:
74+
75+
```bash
76+
npx prisma generate
77+
```
78+
7379
### Generate migration files
7480

7581
Run the following command to create the database tables and generate the Prisma Client from your schema. This will also create a `prisma/migrations/` directory with migration history files.
@@ -83,12 +89,12 @@ npx prisma migrate dev --name init
8389
Inside of `/src/lib`, create a `prisma.ts` file. This file will initialize and export your Prisma Client instance so you can query your database throughout your Astro project.
8490

8591
```typescript title="src/lib/prisma.ts"
86-
import { PrismaClient } from "../generated/prisma/client";
87-
import { withAccelerate } from "@prisma/extension-accelerate";
92+
import { PrismaPg } from '@prisma/adapter-pg';
93+
import { PrismaClient } from '../../prisma/generated/client';
8894

89-
const prisma = new PrismaClient({
90-
datasourceUrl: import.meta.env.DATABASE_URL,
91-
}).$extends(withAccelerate());
95+
const connectionString = import.meta.env.DATABASE_URL;
96+
const adapter = new PrismaPg({ connectionString });
97+
const prisma = new PrismaClient({ adapter });
9298

9399
export default prisma;
94100
```
@@ -127,15 +133,17 @@ const posts = await prisma.post.findMany({
127133

128134
It is best practice to handle queries in an API route. For more information on how to use Prisma ORM in your Astro project, see the [Astro + Prisma ORM Guide](https://www.prisma.io/docs/guides/astro).
129135

130-
## Direct TCP connection
131-
To connect to Prisma Postgres via direct TCP, you can create a direct connection string in your Prisma Console. This allows you to connect any other ORM, database library, or tool of your choice.
136+
## Connect with Other ORMs and Libraries
137+
138+
You can connect to Prisma Postgres via direct TCP using any other ORM, database library, or tool of your choice. Create a direct connection string in your Prisma Console to get started.
132139

133140
### Prerequisites
141+
- An Astro project with an adapter installed to enable [on-demand rendering (SSR)](/en/guides/on-demand-rendering/).
134142
- A [Prisma Postgres](https://pris.ly/ppg) database with a TCP enabled connection string
135143

136144
### Install dependencies
137145

138-
This example will make a direct TCP connection using [`pg`, a PostgreSQL client for Node.js](https://github.com/brianc/node-postgres).
146+
This example uses [`pg`, a PostgreSQL client for Node.js](https://github.com/brianc/node-postgres) to make a direct TCP connection.
139147

140148
Run the following command to install the `pg` package:
141149

0 commit comments

Comments
 (0)