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 @@ -109,11 +109,11 @@ In this schema, you configure three things:
- **Generator**: Indicates that you want to generate Prisma Client
- **Data model**: Defines your application models

### The Prisma ORM data model
### 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.

#### Functions of Prisma ORM models
#### Functions of Prisma schema data models

The data model is a collection of [models](/orm/prisma-schema/data-model/models#defining-models). A model has two major functions:

Expand Down Expand Up @@ -245,7 +245,7 @@ As mentioned above, there are two ways for "getting" your data model into the Pr

With **Prisma Migrate**, Prisma ORM's integrated database migration tool, the workflow looks as follows:

1. Manually adjust your [Prisma ORM data model](/orm/prisma-schema/data-model/models)
1. Manually adjust your [Prisma schema data model](/orm/prisma-schema/data-model/models)
1. Migrate your development database using the `prisma migrate dev` CLI command
1. Use Prisma Client in your application code to access your database

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ No matter if you're a SQL newcomer or veteran, Prisma ORM will give you a signif

Here are a couple of the guiding principles and general practices we apply when designing and building our tools:

- [make the right thing easy](https://www.jason.af/right-thing-easy-thing/)
- [make the right thing easy](https://git.io/right-thing-easy-thing)
- [pit of success](https://blog.codinghorror.com/falling-into-the-pit-of-success/)
- offer intelligent autocompletion where possible
- build powerful editor extensions (e.g. for [VS Code](https://marketplace.visualstudio.com/items?itemName=Prisma.prisma))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,20 +24,20 @@ Note that a GraphQL schema can be written code-first or SDL-first. Check out thi
The GraphQL schema and HTTP server are typically handled by separate libraries. Here is an overview of current GraphQL server tools and their purpose:

| Library (npm package) | Purpose | Compatible with Prisma ORM | Prisma integration |
| :-------------------- | :-------------------------- | :--------------------- | :----------------------------------------------------------------------------- |
| `graphql` | GraphQL schema (code-first) | Yes | No |
| `graphql-tools` | GraphQL schema (SDL-first) | Yes | No |
| `type-graphql` | GraphQL schema (code-first) | Yes | [`typegraphql-prisma`](https://www.npmjs.com/package/typegraphql-prisma) |
| `nexus` | GraphQL schema (code-first) | Yes | [`nexus-prisma`](https://graphql-nexus.github.io/nexus-prisma) _Early Preview_ |
| `apollo-server` | HTTP server | Yes | n/a |
| `express-graphql` | HTTP server | Yes | n/a |
| `fastify-gql` | HTTP server | Yes | n/a |
| `graphql-yoga` | HTTP server | Yes | n/a |
| :-------------------- | :-------------------------- | :------------------------- | :----------------------------------------------------------------------------- |
| `graphql` | GraphQL schema (code-first) | Yes | No |
| `graphql-tools` | GraphQL schema (SDL-first) | Yes | No |
| `type-graphql` | GraphQL schema (code-first) | Yes | [`typegraphql-prisma`](https://www.npmjs.com/package/typegraphql-prisma) |
| `nexus` | GraphQL schema (code-first) | Yes | [`nexus-prisma`](https://graphql-nexus.github.io/nexus-prisma) _Early Preview_ |
| `apollo-server` | HTTP server | Yes | n/a |
| `express-graphql` | HTTP server | Yes | n/a |
| `fastify-gql` | HTTP server | Yes | n/a |
| `graphql-yoga` | HTTP server | Yes | n/a |

In addition to these standalone and single-purpose libraries, there are several projects building integrated _application frameworks_:

| Framework | Stack | Built by | Prisma ORM | Description |
| :---------------------------------- | :-------- | :------------------------------------------------ | :--------------------- | :------------------------------------- |
| Framework | Stack | Built by | Prisma ORM | Description |
| :---------------------------------- | :-------- | :------------------------------------------------ | :------------------------- | :------------------------------------- |
| [Redwood.js](https://redwoodjs.com) | Fullstack | [Tom Preston-Werner](https://github.com/mojombo/) | Built on top of Prisma ORM | _Bringing full-stack to the JAMstack._ |

> **Note**: If you notice any GraphQL libraries/frameworks missing from the list, please let us know.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ toc_max_heading_level: 4

## Default built-in drivers

One of Prisma Client's components is the [Query Engine](/orm/more/under-the-hood/engines) . The Query Engine is responsible for transforming Prisma Client queries to SQL statements. The Query Engine connects to your database using the included drivers that don't require additional setup. The built-in drivers use TCP connections to connect to the database.
One of Prisma Client's components is the [Query Engine](/orm/more/under-the-hood/engines). The Query Engine is responsible for transforming Prisma Client queries to SQL statements. The Query Engine connects to your database via TCP using built-in drivers that don't require additional setup.

![Query flow from the user application to the database with Prisma Client](./images/drivers/qe-query-execution-flow.png)

Expand All @@ -20,11 +20,13 @@ Prisma Client will use the Query Engine to transform the Prisma Client query to

![Query flow from the user application to the database using Prisma Client and driver adapters](./images/drivers/qe-query-engine-adapter.png)

There are 2 different types of driver adapters:
There are two different types of driver adapters:

- [Database driver adapters](#database-driver-adapters)
- [Serverless driver adapters](#serverless-driver-adapters)

> **Note**: Driver adapters enable [edge deployments](/orm/prisma-client/deployment/edge/overview) of applications that use Prisma ORM.

### Database driver adapters

You can connect to your database using a Node.js-based driver from Prisma Client using a database driver adapter. Prisma maintains the following database driver adapters:
Expand All @@ -41,13 +43,13 @@ Prisma ORM maintains the following serverless driver adapters:
- [Neon](/orm/overview/databases/neon#how-to-use-neons-serverless-driver-with-prisma-orm-preview)
- [PlanetScale](/orm/overview/databases/planetscale#how-to-use-the-planetscale-serverless-driver-with-prisma-orm-preview)

## Community maintained database driver adapters
### Community-maintained database driver adapters

You can also build your own driver adapter for the database you're using. The following is a list of community maintained driver adapters:

- [TiDB](https://github.com/tidbcloud/prisma-adapter)

### How to use driver adapters
## How to use driver adapters

To use this feature:

Expand All @@ -72,6 +74,26 @@ To use this feature:
- [PlanetScale](/orm/overview/databases/planetscale#how-to-use-the-planetscale-serverless-driver-with-prisma-orm-preview)
- [Turso](/orm/overview/databases/turso#how-to-connect-and-query-a-turso-database)

## Notes about using driver adapters

### Driver adapters don't read the connection string from the Prisma schema

When using Prisma ORM's built-in drivers, the connection string is read from the `url` field of the `datasource` block in your Prisma schema.

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 highlight=5,normal
import { PrismaClient } from '@prisma/client'
import { PrismaPg } from '@prisma/adapter-pg'
import { Pool } from 'pg'

const pool = new Pool({ connectionString: env.DATABASE_URL })
const adapter = new PrismaPg(pool)
const prisma = new PrismaClient({ adapter })
```

See the docs for the driver adapter you're using for concrete setup instructions.

### Driver adapters and custom output paths

Since Prisma 5.9.0, when using the driver adapters Preview feature along with a [custom output path for Prisma Client](/orm/prisma-client/setup-and-configuration/generating-prisma-client#using-a-custom-output-path), you cannot reference Prisma Client using a relative path.
Expand Down
41 changes: 35 additions & 6 deletions content/200-orm/050-overview/500-databases/400-mysql.mdx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
---
title: 'MySQL'
title: 'MySQL/MariaDB'
metaTitle: 'MySQL database connector'
metaDescription: 'This page explains how Prisma can connect to a MySQL database using the MySQL database connector.'
metaDescription: 'This page explains how Prisma can connect to a MySQL or MariaDB database using the MySQL database connector.'
tocDepth: 3
---

<TopBlock title={frontMatter.title}>

The MySQL data source connector connects Prisma ORM to a [MySQL](https://www.mysql.com/) database server.
The MySQL data source connector connects Prisma ORM to a [MySQL](https://www.mysql.com/) or [MariaDB](https://mariadb.org/) database server.

By default, the MySQL connector contains a database driver responsible for connecting to your database. You can use a [driver adapter](/orm/overview/databases/database-drivers#driver-adapters) (Preview) to connect to your database using a JavaScript database driver from Prisma Client.

Expand Down Expand Up @@ -50,7 +50,7 @@ The following components make up the _base URL_ of your database, they are alway
| Name | Placeholder | Description |
| :------- | :---------- | :------------------------------------------------------------------------------------------------------------------ |
| Host | `HOST` | IP address/domain of your database server, e.g. `localhost` |
| Port | `PORT` | Port on which your database server is running, e.g. `5432` |
| Port | `PORT` | Port on which your database server is running, e.g. `5432` (default is `3306`, or no port when using Unix socket) |
| User | `USER` | Name of your database user, e.g. `janedoe` |
| 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` |
Expand Down Expand Up @@ -112,8 +112,8 @@ mysql://USER:PASSWORD@HOST:PORT/DATABASE?sslidentity=client-identity.p12&sslpass

### Connecting via sockets

To connect to your MySQL database via sockets, you must add a `socket` field as a _query parameter_ to the connection URL (instead of setting it as the `host` part of the URI).
The value of this parameter then must point to the directory that contains the socket, e.g.: `mysql://USER:POST@localhost/database?socket=/var/run/mysql/`
To connect to your MySQL/MariaDB database via a socket, you must add a `socket` field as a _query parameter_ to the connection URL (instead of setting it as the `host` part of the URI).
The value of this parameter then must point to the directory that contains the socket, e.g. on a default installation of MySQL/MariaDB on Ubuntu or Debian use: `mysql://USER:POST@localhost/database?socket=/run/mysqld/mysqld.sock`

Note that `localhost` is required, the value itself is ignored and can be anything.

Expand All @@ -139,6 +139,20 @@ The MySQL connector maps the [scalar types](/orm/prisma-schema/data-model/models
| `Json` | `JSON` | Supported in MySQL 5.7+ only |
| `Bytes` | `LONGBLOB` |

### Native type mapping from Prisma ORM to MariaDB

| Prisma ORM | MariaDB | Notes |
| ---------- | ---------------- | -------------------------------------------------- |
| `String` | `VARCHAR(191)` | |
| `Boolean` | `BOOLEAN` | In MariaDB `BOOLEAN` is a synonym for `TINYINT(1)` |
| `Int` | `INT` | |
| `BigInt` | `BIGINT` | |
| `Float` | `DOUBLE` | |
| `Decimal` | `DECIMAL(65,30)` | |
| `DateTime` | `DATETIME(3)` | |
| `Json` | `LONGTEXT` | See https://mariadb.com/kb/en/json-data-type/ |
| `Bytes` | `LONGBLOB` | |

### Native type mappings

When introspecting a MySQL database, the database types are mapped to Prisma ORM according to the following table:
Expand Down Expand Up @@ -204,3 +218,18 @@ model Device {
## Engine

If you are using a version of MySQL where MyISAM is the default engine, you must specify `ENGINE = InnoDB;` when you create a table. If you introspect a database that uses a different engine, relations in the Prisma Schema are not created (or lost, if the relation already existed).

## Permissions

A fresh new installation of MySQL/MariaDB has by default only a `root` database user. Do not use `root` user in your Prisma configuration, but instead create a database and database user for each application. On most Linux hosts (e.g. Ubuntu) you can simply run this as the Linux `root` user (which automatically has database `root` access as well):

```
mysql -e "CREATE DATABASE IF NOT EXISTS $DB_PRISMA;"
mysql -e "GRANT ALL PRIVILEGES ON $DB_PRISMA.* TO $DB_USER@'%' IDENTIFIED BY '$DB_PASSWORD';"
```

The above is enough to run the `prisma db pull` and `prisma db push` commands. In order to also run `prisma migrate` commands these permissions need to be granted:

```
mysql -e "GRANT CREATE, DROP, REFERENCES, ALTER ON *.* TO $DB_USER@'%';"
```
8 changes: 4 additions & 4 deletions content/200-orm/050-overview/500-databases/600-mongodb.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ There are a number of strategies you could use for migrating this schema:

Then when you encounter a missing `phoneNumber`, the value will be coerced into `000-000-0000`.

- **"No breaking changes" updates**: this strategy builds on the first one, with further consensus amongst your team that you don't rename or delete fields, only add new fields, and always define the new fields as optional. This policy can be reenforced by adding checks in the CI/CD process to verify that there are no backwards-incompatible changes to the schema.
- **"No breaking changes" updates**: this strategy builds on the first one, with further consensus amongst your team that you don't rename or delete fields, only add new fields, and always define the new fields as optional. This policy can be reinforced by adding checks in the CI/CD process to verify that there are no backwards-incompatible changes to the schema.

- **"All-at-once" updates**: this strategy is similar to traditional migrations in relational databases, where all data is updated to reflect the new schema. In the scenario above, you would create a script to add a value for the phone number field to all existing users in your database. You can then make the field a required field in the application because the schema and the data are consistent.

Expand Down Expand Up @@ -562,9 +562,9 @@ MongoDB types that are currently unsupported:

When introspecting a MongoDB database, Prisma ORM uses the relevant [scalar types](/orm/prisma-schema/data-model/models#scalar-fields). Some special types also get additional native type annotations:

| MongoDB (Type \| Aliases) | Prisma ORM| Supported | Native database type attribute | Notes |
| ------------------------- | -------- | :-------: | :----------------------------- | :---- |
| `objectId` | `String` | ✔️ | `@db.ObjectId` | |
| MongoDB (Type \| Aliases) | Prisma ORM | Supported | Native database type attribute | Notes |
| ------------------------- | ---------- | :-------: | :----------------------------- | :---- |
| `objectId` | `String` | ✔️ | `@db.ObjectId` | |

[Introspection](/orm/prisma-schema/introspection) adds native database types that are **not yet supported** as [`Unsupported`](/orm/reference/prisma-schema-reference#unsupported) fields:

Expand Down
Loading