Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ Every project that uses a tool from the Prisma ORM toolkit starts with a [Prisma
```prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

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

model Post {
Expand Down Expand Up @@ -101,10 +101,36 @@ model User {

In this schema, you configure three things:

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

### Configuring database connections

Database connection URLs are configured in a `prisma.config.ts` file. Create a `prisma.config.ts` file in your project root:

```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
migrations: {
path: 'prisma/migrations',
seed: 'tsx ./prisma/seed.ts',
},
datasource: {
url: env('DATABASE_URL'),
},
})
```

:::info

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.

:::

### The Prisma schema data model

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.
Expand Down Expand Up @@ -142,9 +168,9 @@ Then, you can run `prisma generate`:
npx prisma generate
```

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).
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).

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.
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.

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

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

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

const prisma = new PrismaClient()
```
Expand All @@ -165,7 +191,7 @@ const prisma = new PrismaClient()
<TabItem value="require">

```js
const { PrismaClient } = require('@prisma/client')
const { PrismaClient } = require('./generated/client')

const prisma = new PrismaClient()
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,9 +211,9 @@ export type User = {
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:
```js
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '../prisma/generated/client'
// or
// const { PrismaClient } = require('@prisma/client')
// const { PrismaClient } = require('../prisma/generated/client')

const prisma = new PrismaClient()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ datasource db {
}

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

model Post {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@ datasource db {
}

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

model Post {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -228,11 +228,11 @@ As an example, here's a Prisma schema for a blog:
```prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}

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

model Post {
Expand Down Expand Up @@ -308,7 +308,8 @@ datasource db {
}

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

Expand Down Expand Up @@ -338,7 +339,8 @@ datasource db {
}

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

model Post {
Expand Down Expand Up @@ -380,7 +382,7 @@ So far, the article covered the concepts behind Prisma ORM, its implementation o
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:

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

const prisma = new PrismaClient()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ As of [v6.15.0](https://pris.ly/release/6.16.0), Prisma ORM can be used without

```prisma
generator client {
provider = "prisma-client-js" // or "prisma-client"
provider = "prisma-client"
output = "../src/generated/prisma"
engineType = "client" // no Rust engine
}
Expand Down Expand Up @@ -102,7 +102,7 @@ Earlier versions of Prisma ORM required you to first instantiate the driver itse
```typescript
import { createClient } from '@libsql/client'
import { PrismaLibSQL } from '@prisma/adapter-libsql'
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '../prisma/generated/client'

// Old way of using driver adapters (before 6.6.0)
const driver = createClient({
Expand Down Expand Up @@ -137,7 +137,7 @@ When using Prisma ORM's built-in drivers, the connection string is read from the
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:

```ts
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '../prisma/generated/client'
import { PrismaPg } from '@prisma/adapter-pg'

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

```prisma
generator client {
provider = "prisma-client-js"
provider = "prisma-client"
output = "../src/generated/client"
}
```
Expand Down
29 changes: 24 additions & 5 deletions content/200-orm/050-overview/500-databases/300-postgresql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,33 @@ To connect to a PostgreSQL database server, you need to configure a [`datasource
```prisma file=schema.prisma
datasource db {
provider = "postgresql"
url = env("DATABASE_URL")
}
```

The database connection URL is configured in `prisma.config.ts`:

```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
datasource: {
url: env('DATABASE_URL'),
},
})
```

:::info

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.

:::

The fields passed to the `datasource` block are:

- `provider`: Specifies the `postgresql` data source connector.
- `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.
- The `url` field is configured in `prisma.config.ts` and specifies the [connection URL](#connection-url) for the PostgreSQL database server.

## Using the `node-postgres` driver

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

```ts
import { PrismaPg } from '@prisma/adapter-pg'
import { PrismaClient } from '@prisma/client'
import { PrismaClient } from '../prisma/generated/client'

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

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

<Admonition type="info">
:::info

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

</Admonition>
:::

#### Arguments

Expand Down
27 changes: 23 additions & 4 deletions content/200-orm/050-overview/500-databases/400-mysql.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,33 @@ To connect to a MySQL database server, you need to configure a [`datasource`](/o
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
```

The database connection URL is configured in `prisma.config.ts`:

```ts file=prisma.config.ts
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

export default defineConfig({
schema: 'prisma/schema.prisma',
datasource: {
url: env('DATABASE_URL'),
},
})
```

:::info

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.

:::

The fields passed to the `datasource` block are:

- `provider`: Specifies the `mysql` data source connector, which is used both for MySQL and MariaDB.
- `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.
- The `url` field is configured in `prisma.config.ts` and specifies the [connection URL](#connection-url) for the MySQL database server.

## Using the `mariadb` driver

Expand Down Expand Up @@ -83,11 +102,11 @@ The following components make up the _base URL_ of your database, they are alway
| Password | `PASSWORD` | Password for your database user |
| 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` |

<Admonition type="info">
:::info

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

</Admonition>
:::

#### Arguments

Expand Down
8 changes: 4 additions & 4 deletions content/200-orm/050-overview/500-databases/500-sqlite.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ npm install @prisma/adapter-better-sqlite3
Now, when you instantiate Prisma Client, you need to pass an instance of Prisma ORM's driver adapter to the `PrismaClient` constructor:

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

const adapter = new PrismaBetterSQLite3({
const adapter = new PrismaBetterSqlite3({
url: "file:./prisma/dev.db"
});
const prisma = new PrismaClient({ adapter });
Expand All @@ -64,10 +64,10 @@ By default, driver adapters store `DateTime` values as **ISO 8601 strings**, whi
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:

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

const adapter = new PrismaBetterSQLite3({
const adapter = new PrismaBetterSqlite3({
url: "file:./prisma/dev.db"
}, {
timestampFormat: 'unixepoch-ms'
Expand Down
16 changes: 8 additions & 8 deletions content/200-orm/050-overview/500-databases/600-mongodb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ codeStyle: false

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.

<Admonition type="info">
:::info

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

</Admonition>
:::

</TopBlock>

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

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.

<Admonition type="info">
:::info

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.

</Admonition>
:::

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.

Expand Down Expand Up @@ -385,11 +385,11 @@ The fields passed to the `datasource` block are:
- `provider`: Specifies the `mongodb` data source connector.
- `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.

<Admonition type="warning">
:::warning

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.

</Admonition>
:::

## Connection details

Expand Down Expand Up @@ -417,11 +417,11 @@ The following components make up the _base URL_ of your database:
| Port | `PORT` | Port on which your database server is running, e.g. `1234`. If none is provided the default `27017` is used. |
| 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` |

<Admonition type="info">
:::info

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

</Admonition>
:::

#### Arguments

Expand Down
Loading
Loading