Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Client constructor #480

Merged
merged 16 commits into from
Jun 30, 2020
Merged
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
---
title: 'Constructor'
metaTitle: 'Prisma Client Constructor (Reference)'
metaDescription: 'This page describes the Prisma Client constructor parameters.'
---

## Overview

This page describes the Prisma Client constructor parameters.

## `datasources`

Use the `datasources` constructor parameter to programatically override the URL of a datasource - for example, as part of an integration test. To override a datasource:

1. Define a datasource in the `schema.prisma` file. In the following example, there is a single datasource named `my_database`:

```prisma
datasource my_database {
provider = "sqlite"
url = env("DATABASE_URL")
}
```
Jolg42 marked this conversation as resolved.
Show resolved Hide resolved

The `DATABASE_URL` environment variable defines the following value:

```
DATABASE_URL="file:./dev.db"
```

> **Note**: You must re-generate the Prisma Client each time you add or rename a datasource. Datasource names are included in the generated client.

2. Instantiate a client and override the connection string of the `my_database` datasource:

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

const prisma = new PrismaClient({
datasources: {
| my_database: 'file:./dev_qa.db',
Jolg42 marked this conversation as resolved.
Show resolved Hide resolved
mhwelander marked this conversation as resolved.
Show resolved Hide resolved
},
})
```

> **Note**: If you define multiple datasources in `schema.prisma`, Prisma defaults to the first entry.
Copy link
Member

Choose a reason for hiding this comment

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

Why is that? Do we not define which one it should be applied to by using my_database?

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member

Choose a reason for hiding this comment

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

There was a enabled property in the schema before that got removed (never saw it, just the remains)

Copy link
Member

Choose a reason for hiding this comment

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

What is the connection to this comment? Should it not use the datasource with the name you supply above, my_database in this example?

Copy link
Contributor

Choose a reason for hiding this comment

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

This is actually touching on undefined behavior, I think the Note is correct, but we need to fix that.
Even though you provide a specific datasource as a constructor arg, it could even be, that in our current implementation, it might not be used, if it's not the first datasource in the schema.
We don't have tests for that and didn't define this behavior clearly yet, but should definitely do that asap.


See [Datasources](../prisma-schema/data-sources) for more information.

## `log`

Use the `log` constructor parameter to determine the type and level of logging. The following configuration raises an event each time the Prisma Client queries the database:

```ts
const prisma = new PrismaClient({
log: [{ emit: 'event', level: 'query' }],
})
```

See [Logging](logging) for more information.

## `errorFormat`

Use the `errorFormat` constructor parameter to determine the level and formatting of errors returned by Prisma. The following configuration outputs raw error messages without formatting or extended information about the error:

```ts
const prisma = new PrismaClient({
errorFormat: 'minimal', // Defaults to 'colorless'
})
```

See [Error formatting](error-formatting) for more information.