Skip to content

Commit

Permalink
improve connection management docs
Browse files Browse the repository at this point in the history
  • Loading branch information
nikolasburk committed Feb 27, 2020
1 parent 3fb72b0 commit c6b9dd1
Showing 1 changed file with 32 additions and 20 deletions.
52 changes: 32 additions & 20 deletions docs/prisma-client-js/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ async function main() {

## Aggregations

In addition to CRUD operations, Prisma Client JS also allows for [_aggregation_ queries](https://github.com/prisma/prisma-client-js/issues/5).
In addition to CRUD operations, Prisma Client JS also allows for [_aggregation_ queries](https://github.com/prisma/prisma-client-js/issues/5).

### `count`

Expand Down Expand Up @@ -178,8 +178,8 @@ You can provide the `id` as input value in [`create`](#create) and [`update`](#u
```ts
const user = await prisma.user.create({
data: {
id: 1
}
id: 1,
},
})
```

Expand Down Expand Up @@ -282,10 +282,10 @@ Creates a new `PrismaClient` instance.

#### Options

| Name | Type | Required | Description |
| ------------- | ------------- | -------- | ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `debug` | `boolean` | No | When set to `true`, the `PrismaClient` instance prints additional logging output to the console when sending requests to Prisma's query engine. **Default**: `false`. |
| `log` | `boolean | LogOption[]` | No | This allows to specify one of the following log levels: `INFO`, `WARN`, `QUERY`. If set to `true`, all log levels are applied. If set to `false`, no log levels are applied. **Default**: `true`.
| Name | Type | Required | Description |
| ------- | ----------------------- | -------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `debug` | `boolean` | No | When set to `true`, the `PrismaClient` instance prints additional logging output to the console when sending requests to Prisma's query engine. **Default**: `false`. |
| `log` | `boolean | LogOption[]` | No | This allows to specify one of the following log levels: `INFO`, `WARN`, `QUERY`. If set to `true`, all log levels are applied. If set to `false`, no log levels are applied. **Default**: `true`. |

#### Examples

Expand Down Expand Up @@ -417,8 +417,8 @@ Updates an existing or creates a new record and returns the corresponding object
```ts
const user = await prisma.user.upsert({
where: { id: 1 },
update: { name: "ALICE" },
create: { name: "ALICE" }
update: { name: 'ALICE' },
create: { name: 'ALICE' },
})
```

Expand Down Expand Up @@ -470,7 +470,7 @@ The `count()` method doesn't take any input arguments.
#### Examples

```ts
const userCount = await prisma.user.count()
const userCount = await prisma.user.count()
// userCount = 42
```

Expand Down Expand Up @@ -569,9 +569,9 @@ const prisma = new PrismaClient({ debug: true })
You can also configure log levels via the `log` option:

```ts
const prisma = new PrismaClient({
const prisma = new PrismaClient({
debug: true,
log: true
log: true,
})
```

Expand All @@ -586,15 +586,17 @@ To specify more fine-grained log-levels, you can pass an array of log options to
```ts
const prisma = new PrismaClient({
debug: true,
log: [{
level: 'QUERY'
}]
log: [
{
level: 'QUERY',
},
],
})
```

## Reusing query sub-parts

You can reuse subparts of a Prisma Client JS query by not immediately evaluating the promise that's returned by any Prisma Client JS API call. Depending on your evaluating the promise, you can do this either by leaving out the prepended `await` keyword or the appended call to `.then()`.
You can reuse subparts of a Prisma Client JS query by not immediately evaluating the promise that's returned by any Prisma Client JS API call. Depending on your evaluating the promise, you can do this either by leaving out the prepended `await` keyword or the appended call to `.then()`.

```ts
// Note the missing `await` here.
Expand All @@ -614,9 +616,19 @@ Prisma Client JS connects and disconnects from your data sources using the follo

Unless you want to employ a specific optimization, calling `prisma.connect()` is not necessary thanks to the _lazy connect_ behavior: The `PrismaClient` instance connects lazily when the first request is made to the API (`connect()` is called for you under the hood).

If you need the first request to respond instantly and can't wait for the lazy connection to be established, you can explicitly call `prisma.connect()` to establish a connection to prismae data source.
If you need the first request to respond instantly and can't wait for the lazy connection to be established, you can explicitly call `prisma.connect()` to establish a connection to the Prisma data source.

**IMPORTANT**: It is recommended to always explicitly call `prisma.disconnect()` in your code. Generally the `PrismaClient` instance disconnects automatically. However, if your program terminates but still prismas an unhandled promise rejection, the port will keep the connection to the data source open beyond the lifetime of your program!
**IMPORTANT**: It is recommended to always explicitly call `prisma.disconnect()` in your code. Also, be sure to disconnect even when an exception is thrown:

```ts
main()
.catch(e => {
throw e
})
.finally(async () => {
await prisma.disconnect()
})
```

## Error formatting

Expand All @@ -630,7 +642,7 @@ There are 3 error formatting levels:
2. **Colorless Error**: Same as pretty errors, just without colors.
3. **Minimal Error**: The raw error message.

In order to configure these different error formatting levels, we have two options:
In order to configure these different error formatting levels, we have two options:

- Setting the config options via environment variables
- Providing the config options to the `PrismaClient` constructor
Expand Down Expand Up @@ -661,4 +673,4 @@ As the `errorFormat` property is optional, you still can just instantiate Prisma

```ts
const prisma = new PrismaClient()
```
```

0 comments on commit c6b9dd1

Please sign in to comment.