Skip to content
Closed
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,35 @@ 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 { 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'),
},
})
```

<Admonition type="info">

**Note**: 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.

</Admonition>

### 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 +167,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 +181,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 +190,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
24 changes: 21 additions & 3 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,32 @@ 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 { defineConfig, env } from 'prisma/config'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
import { defineConfig, env } from 'prisma/config'
import 'dotenv/config'
import { defineConfig, env } from 'prisma/config'

@AmanVarshney01 let's add the import 'dotenv/config' to all the config files.


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

<Admonition type="info">

**Note**: 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.

</Admonition>
Comment on lines +44 to +48
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
<Admonition type="info">
**Note**: 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.
</Admonition>
:::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.
:::

@AmanVarshney01 can you update the components to use the more modern components while making changes in the PR?


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 +74,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
22 changes: 20 additions & 2 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,32 @@ 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 { defineConfig, env } from 'prisma/config'

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

<Admonition type="info">

**Note**: 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.

</Admonition>

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
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
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,32 @@ To connect to a Microsoft SQL Server database, you need to configure a [`datasou
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "sqlserver"
url = env("DATABASE_URL")
}
```

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

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

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

<Admonition type="info">

**Note**: 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.

</Admonition>

The fields passed to the `datasource` block are:

- `provider`: Specifies the `sqlserver` data source connector.
- `url`: Specifies the [connection URL](#connection-details) for the Microsoft SQL Server database. 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-details) for the Microsoft SQL Server database.


## Using the `node-mssql` driver
Expand All @@ -47,7 +65,7 @@ Now, when you instantiate Prisma Client, you need to pass an instance of Prisma

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

const config = {
server: 'localhost',
Expand Down
22 changes: 20 additions & 2 deletions content/200-orm/050-overview/500-databases/840-cockroachdb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -99,14 +99,32 @@ To connect to a CockroachDB database server, you need to configure a [`datasourc
```prisma file=schema.prisma showLineNumbers
datasource db {
provider = "cockroachdb"
url = env("DATABASE_URL")
}
```

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

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

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

<Admonition type="info">

**Note**: 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.

</Admonition>

The fields passed to the `datasource` block are:

- `provider`: Specifies the `cockroachdb` data source connector.
- `url`: Specifies the [connection URL](#connection-details) for the CockroachDB 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-details) for the CockroachDB database server.

<Admonition type="info">

Expand Down
Loading
Loading