Skip to content

Commit dee2b9c

Browse files
AmanVarshney01aidankmcalister
authored andcommitted
Update ORM docs (#7260)
* update * use pg adapter instead of accelerate * update * add dotenv in prisma.config.ts * fix warning note * update * update
1 parent d2719d7 commit dee2b9c

File tree

124 files changed

+913
-675
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

124 files changed

+913
-675
lines changed

content/200-orm/050-overview/100-introduction/100-what-is-prisma.mdx

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,11 @@ Every project that uses a tool from the Prisma ORM toolkit starts with a [Prisma
4141
```prisma
4242
datasource db {
4343
provider = "postgresql"
44-
url = env("DATABASE_URL")
4544
}
4645
4746
generator client {
48-
provider = "prisma-client-js"
47+
provider = "prisma-client"
48+
output = "./generated"
4949
}
5050
5151
model Post {
@@ -101,10 +101,36 @@ model User {
101101
102102
In this schema, you configure three things:
103103

104-
- **Data source**: Specifies your database connection (via an environment variable)
104+
- **Data source**: Specifies your database connection. Database connection URLs are configured in `prisma.config.ts`.
105105
- **Generator**: Indicates that you want to generate Prisma Client
106106
- **Data model**: Defines your application models
107107

108+
### Configuring database connections
109+
110+
Database connection URLs are configured in a `prisma.config.ts` file. Create a `prisma.config.ts` file in your project root:
111+
112+
```ts file=prisma.config.ts
113+
import 'dotenv/config'
114+
import { defineConfig, env } from 'prisma/config'
115+
116+
export default defineConfig({
117+
schema: 'prisma/schema.prisma',
118+
migrations: {
119+
path: 'prisma/migrations',
120+
seed: 'tsx ./prisma/seed.ts',
121+
},
122+
datasource: {
123+
url: env('DATABASE_URL'),
124+
},
125+
})
126+
```
127+
128+
:::info
129+
130+
When using Prisma CLI commands, environment variables are not automatically loaded. You'll need to use a package like `dotenv` to load environment variables from a `.env` file, or ensure your environment variables are set in your shell.
131+
132+
:::
133+
108134
### The Prisma schema data model
109135

110136
On this page, the focus is on the data model. You can learn more about [Data sources](/orm/prisma-schema/overview/data-sources) and [Generators](/orm/prisma-schema/overview/generators) on the respective docs pages.
@@ -142,9 +168,9 @@ Then, you can run `prisma generate`:
142168
npx prisma generate
143169
```
144170

145-
The `prisma generate` command reads your Prisma schema and _generates_ Prisma Client code. The code is [generated into the `node_modules/.prisma/client` folder by default](/orm/prisma-client/setup-and-configuration/generating-prisma-client#the-prismaclient-npm-package).
171+
The `prisma generate` command reads your Prisma schema and _generates_ Prisma Client code. The code is generated into the path specified in the `output` field of your generator block (e.g., `./generated` as shown in the schema example above).
146172

147-
After you change your data model, you'll need to manually re-generate Prisma Client by running `prisma generate` to ensure the code inside `node_modules/.prisma/client` gets updated.
173+
After you change your data model, you'll need to manually re-generate Prisma Client by running `prisma generate` to ensure the generated code gets updated.
148174

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

@@ -156,7 +182,7 @@ Once Prisma Client has been generated, you can import it in your code and send q
156182
<TabItem value="import">
157183

158184
```ts
159-
import { PrismaClient } from '@prisma/client'
185+
import { PrismaClient } from './generated/client'
160186

161187
const prisma = new PrismaClient()
162188
```
@@ -165,7 +191,7 @@ const prisma = new PrismaClient()
165191
<TabItem value="require">
166192

167193
```js
168-
const { PrismaClient } = require('@prisma/client')
194+
const { PrismaClient } = require('./generated/client')
169195

170196
const prisma = new PrismaClient()
171197
```

content/200-orm/050-overview/100-introduction/300-data-modeling.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -211,9 +211,9 @@ export type User = {
211211
In addition to the generated types, Prisma Client also provides a data access API that you can use once you've installed the `@prisma/client` package:
212212
213213
```js
214-
import { PrismaClient } from '@prisma/client'
214+
import { PrismaClient } from '../prisma/generated/client'
215215
// or
216-
// const { PrismaClient } = require('@prisma/client')
216+
// const { PrismaClient } = require('../prisma/generated/client')
217217

218218
const prisma = new PrismaClient()
219219

content/200-orm/050-overview/300-prisma-in-your-stack/01-rest.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,8 @@ datasource db {
4343
}
4444
4545
generator client {
46-
provider = "prisma-client-js"
46+
provider = "prisma-client"
47+
output = "./generated"
4748
}
4849
4950
model Post {

content/200-orm/050-overview/300-prisma-in-your-stack/03-fullstack.mdx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ datasource db {
4141
}
4242
4343
generator client {
44-
provider = "prisma-client-js"
44+
provider = "prisma-client"
45+
output = "./generated"
4546
}
4647
4748
model Post {

content/200-orm/050-overview/300-prisma-in-your-stack/04-is-prisma-an-orm.mdx

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,11 @@ As an example, here's a Prisma schema for a blog:
228228
```prisma
229229
datasource db {
230230
provider = "postgresql"
231-
url = env("DATABASE_URL")
232231
}
233232
234233
generator client {
235-
provider = "prisma-client-js"
234+
provider = "prisma-client"
235+
output = "./generated"
236236
}
237237
238238
model Post {
@@ -308,7 +308,8 @@ datasource db {
308308
}
309309
310310
generator client {
311-
provider = "prisma-client-js"
311+
provider = "prisma-client"
312+
output = "./generated"
312313
}
313314
```
314315

@@ -338,7 +339,8 @@ datasource db {
338339
}
339340
340341
generator client {
341-
provider = "prisma-client-js"
342+
provider = "prisma-client"
343+
output = "./generated"
342344
}
343345
344346
model Post {
@@ -380,7 +382,7 @@ So far, the article covered the concepts behind Prisma ORM, its implementation o
380382
Accessing the database with Prisma Client happens through the query methods it exposes. All queries return plain old JavaScript objects. Given the blog schema from above, fetching a user looks as follows:
381383

382384
```ts
383-
import { PrismaClient } from '@prisma/client'
385+
import { PrismaClient } from '../prisma/generated/client'
384386

385387
const prisma = new PrismaClient()
386388

content/200-orm/050-overview/500-databases/200-database-drivers.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ As of [v6.15.0](https://pris.ly/release/6.16.0), Prisma ORM can be used without
1818

1919
```prisma
2020
generator client {
21-
provider = "prisma-client-js" // or "prisma-client"
21+
provider = "prisma-client"
2222
output = "../src/generated/prisma"
2323
engineType = "client" // no Rust engine
2424
}
@@ -102,7 +102,7 @@ Earlier versions of Prisma ORM required you to first instantiate the driver itse
102102
```typescript
103103
import { createClient } from '@libsql/client'
104104
import { PrismaLibSQL } from '@prisma/adapter-libsql'
105-
import { PrismaClient } from '@prisma/client'
105+
import { PrismaClient } from '../prisma/generated/client'
106106

107107
// Old way of using driver adapters (before 6.6.0)
108108
const driver = createClient({
@@ -137,7 +137,7 @@ When using Prisma ORM's built-in drivers, the connection string is read from the
137137
On the other hand, when using a driver adapter, the connection string needs to be provided in your _application code_ when the driver adapter is set up initially. Here is how this is done for the `pg` driver and the `@prisma/adapter-pg` adapter:
138138

139139
```ts
140-
import { PrismaClient } from '@prisma/client'
140+
import { PrismaClient } from '../prisma/generated/client'
141141
import { PrismaPg } from '@prisma/adapter-pg'
142142

143143
const adapter = new PrismaPg({ connectionString: env.DATABASE_URL })
@@ -154,7 +154,7 @@ Let's assume you had `output` in your Prisma schema set to `../src/generated/cli
154154

155155
```prisma
156156
generator client {
157-
provider = "prisma-client-js"
157+
provider = "prisma-client"
158158
output = "../src/generated/client"
159159
}
160160
```

content/200-orm/050-overview/500-databases/300-postgresql.mdx

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,14 +25,33 @@ To connect to a PostgreSQL database server, you need to configure a [`datasource
2525
```prisma file=schema.prisma
2626
datasource db {
2727
provider = "postgresql"
28-
url = env("DATABASE_URL")
2928
}
3029
```
3130

31+
The database connection URL is configured in `prisma.config.ts`:
32+
33+
```ts file=prisma.config.ts
34+
import 'dotenv/config'
35+
import { defineConfig, env } from 'prisma/config'
36+
37+
export default defineConfig({
38+
schema: 'prisma/schema.prisma',
39+
datasource: {
40+
url: env('DATABASE_URL'),
41+
},
42+
})
43+
```
44+
45+
:::info
46+
47+
When using Prisma CLI commands, environment variables are not automatically loaded. You'll need to use a package like `dotenv` to load environment variables from a `.env` file, or ensure your environment variables are set in your shell.
48+
49+
:::
50+
3251
The fields passed to the `datasource` block are:
3352

3453
- `provider`: Specifies the `postgresql` data source connector.
35-
- `url`: Specifies the [connection URL](#connection-url) for the PostgreSQL database server. In this case, an [environment variable is used](/orm/prisma-schema/overview#accessing-environment-variables-from-the-schema) to provide the connection URL.
54+
- The `url` field is configured in `prisma.config.ts` and specifies the [connection URL](#connection-url) for the PostgreSQL database server.
3655

3756
## Using the `node-postgres` driver
3857

@@ -56,7 +75,7 @@ Now, when you instantiate Prisma Client, you need to pass an instance of Prisma
5675

5776
```ts
5877
import { PrismaPg } from '@prisma/adapter-pg'
59-
import { PrismaClient } from '@prisma/client'
78+
import { PrismaClient } from '../prisma/generated/client'
6079

6180
const connectionString = `${process.env.DATABASE_URL}`
6281

@@ -105,11 +124,11 @@ The following components make up the _base URL_ of your database, they are alway
105124
| Password | `PASSWORD` | Password for your database user |
106125
| Database | `DATABASE` | Name of the [database](https://www.postgresql.org/docs/12/manage-ag-overview.html) you want to use, e.g. `mydb` |
107126

108-
<Admonition type="info">
127+
:::info
109128

110129
You must [percentage-encode special characters](/orm/reference/connection-urls#special-characters).
111130

112-
</Admonition>
131+
:::
113132

114133
#### Arguments
115134

content/200-orm/050-overview/500-databases/400-mysql.mdx

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,33 @@ To connect to a MySQL database server, you need to configure a [`datasource`](/o
1616
```prisma file=schema.prisma showLineNumbers
1717
datasource db {
1818
provider = "mysql"
19-
url = env("DATABASE_URL")
2019
}
2120
```
2221

22+
The database connection URL is configured in `prisma.config.ts`:
23+
24+
```ts file=prisma.config.ts
25+
import 'dotenv/config'
26+
import { defineConfig, env } from 'prisma/config'
27+
28+
export default defineConfig({
29+
schema: 'prisma/schema.prisma',
30+
datasource: {
31+
url: env('DATABASE_URL'),
32+
},
33+
})
34+
```
35+
36+
:::info
37+
38+
When using Prisma CLI commands, environment variables are not automatically loaded. You'll need to use a package like `dotenv` to load environment variables from a `.env` file, or ensure your environment variables are set in your shell.
39+
40+
:::
41+
2342
The fields passed to the `datasource` block are:
2443

2544
- `provider`: Specifies the `mysql` data source connector, which is used both for MySQL and MariaDB.
26-
- `url`: Specifies the [connection URL](#connection-url) for the MySQL database server. In this case, an [environment variable is used](/orm/prisma-schema/overview#accessing-environment-variables-from-the-schema) to provide the connection URL.
45+
- The `url` field is configured in `prisma.config.ts` and specifies the [connection URL](#connection-url) for the MySQL database server.
2746

2847
## Using the `mariadb` driver
2948

@@ -83,11 +102,11 @@ The following components make up the _base URL_ of your database, they are alway
83102
| Password | `PASSWORD` | Password for your database user |
84103
| Database | `DATABASE` | Name of the [database](https://dev.mysql.com/doc/refman/8.0/en/creating-database.html) you want to use, e.g. `mydb` |
85104

86-
<Admonition type="info">
105+
:::info
87106

88107
You must [percentage-encode special characters](/orm/reference/connection-urls#special-characters).
89108

90-
</Admonition>
109+
:::
91110

92111
#### Arguments
93112

content/200-orm/050-overview/500-databases/500-sqlite.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ npm install @prisma/adapter-better-sqlite3
4646
Now, when you instantiate Prisma Client, you need to pass an instance of Prisma ORM's driver adapter to the `PrismaClient` constructor:
4747

4848
```ts
49-
import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3';
49+
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
5050
import { PrismaClient } from './generated/prisma';
5151

52-
const adapter = new PrismaBetterSQLite3({
52+
const adapter = new PrismaBetterSqlite3({
5353
url: "file:./prisma/dev.db"
5454
});
5555
const prisma = new PrismaClient({ adapter });
@@ -64,10 +64,10 @@ By default, driver adapters store `DateTime` values as **ISO 8601 strings**, whi
6464
However, if you need **100% backward compatibility** with Prisma ORM's native SQLite driver (for example, when migrating an existing database), you should use the `unixepoch-ms` format, which stores timestamps as the number of milliseconds since the Unix epoch:
6565

6666
```ts
67-
import { PrismaBetterSQLite3 } from '@prisma/adapter-better-sqlite3';
67+
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3';
6868
import { PrismaClient } from './generated/prisma';
6969

70-
const adapter = new PrismaBetterSQLite3({
70+
const adapter = new PrismaBetterSqlite3({
7171
url: "file:./prisma/dev.db"
7272
}, {
7373
timestampFormat: 'unixepoch-ms'

content/200-orm/050-overview/500-databases/600-mongodb.mdx

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ codeStyle: false
1212

1313
This guide discusses the concepts behind using Prisma ORM and MongoDB, explains the commonalities and differences between MongoDB and other database providers, and leads you through the process for configuring your application to integrate with MongoDB using Prisma ORM.
1414

15-
<Admonition type="info">
15+
:::info
1616

1717
To connect Prisma ORM with MongoDB, refer to our [Getting Started documentation](/getting-started/prisma-orm/quickstart/mongodb).
1818

19-
</Admonition>
19+
:::
2020

2121
</TopBlock>
2222

@@ -70,11 +70,11 @@ This section provides instructions for how to carry out tasks that require steps
7070

7171
Migrating your database over time is an important part of the development cycle. During development, you will need to update your Prisma schema (for example, to add new fields), then update the data in your development environment’s database, and eventually push both the updated schema and the new data to the production database.
7272

73-
<Admonition type="info">
73+
:::info
7474

7575
When using MongoDB, be aware that the “coupling” between your schema and the database is purposefully designed to be less rigid than with SQL databases; MongoDB will not enforce the schema, so you have to verify data integrity.
7676

77-
</Admonition>
77+
:::
7878

7979
These iterative tasks of updating the schema and the database can result in inconsistencies between your schema and the actual data in the database. Let’s look at one scenario where this can happen, and then examine several strategies for you and your team to consider for handling these inconsistencies.
8080

@@ -385,11 +385,11 @@ The fields passed to the `datasource` block are:
385385
- `provider`: Specifies the `mongodb` data source connector.
386386
- `url`: Specifies the [connection URL](#connection-url) for the MongoDB server. In this case, an [environment variable is used](/orm/more/development-environment/environment-variables) to provide the connection URL.
387387
388-
<Admonition type="warning">
388+
:::warning
389389
390390
The MongoDB database connector uses transactions to support nested writes. Transactions **require** a [replica set](https://www.mongodb.com/docs/manual/tutorial/deploy-replica-set/) deployment. The easiest way to deploy a replica set is with [Atlas](https://www.mongodb.com/docs/atlas/getting-started/). It's free to get started.
391391
392-
</Admonition>
392+
:::
393393
394394
## Connection details
395395
@@ -417,11 +417,11 @@ The following components make up the _base URL_ of your database:
417417
| Port | `PORT` | Port on which your database server is running, e.g. `1234`. If none is provided the default `27017` is used. |
418418
| Database | `DATABASE` | Name of the database to use. If none is specified but the `authSource` option is set then the `authSource` database name is used. If neither the database in the connection string nor the `authSource` option is specified then it defaults to `admin` |
419419
420-
<Admonition type="info">
420+
:::info
421421
422422
You must [percentage-encode special characters](/orm/reference/connection-urls#special-characters).
423423
424-
</Admonition>
424+
:::
425425
426426
#### Arguments
427427

0 commit comments

Comments
 (0)