这是indexloc提供的服务,不要输入任何密码
Skip to content

chore: add best practices to Prisma ORM example #9794

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

Merged
merged 7 commits into from
Jan 24, 2025
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
8 changes: 0 additions & 8 deletions examples/with-prisma/.env.example
Original file line number Diff line number Diff line change
@@ -1,9 +1 @@
# Environment variables declared in this file are automatically made available to Prisma.
# See the documentation for more detail: https://pris.ly/d/prisma-schema#accessing-environment-variables-from-the-schema

# Prisma supports the native connection string format for PostgreSQL, MySQL, SQLite, SQL Server, MongoDB (Preview) and CockroachDB (Preview).
# See the documentation for all the connection string options: https://pris.ly/d/connection-strings

# Defaults to the local database deployed from the `docker-compose.yml` stack

DATABASE_URL="mysql://root@127.0.0.1:3306/turborepo"
159 changes: 124 additions & 35 deletions examples/with-prisma/README.md
Original file line number Diff line number Diff line change
@@ -1,24 +1,16 @@
# Turborepo starter
# Turborepo + Prisma ORM starter

This is a community-maintained example. If you experience a problem, please submit a pull request with a fix. GitHub Issues will be closed.

## Using this example

Run the following command:

```sh
npx create-turbo@latest -e with-prisma
```
This is a example designed to help you quickly set up a Turborepo monorepo with a Next.js app and Prisma ORM. This is a community-maintained example. If you experience a problem, please submit a pull request with a fix. GitHub Issues will be closed.

## What's inside?

This turborepo includes the following packages/apps:

### Apps and Packages
### Apps and packages

- `web`: a [Next.js](https://nextjs.org/) app
- `@repo/eslint-config`: `eslint` configurations (includes `eslint-config-next` and `eslint-config-prettier`)
- `@repo/database`: [Prisma](https://prisma.io/) ORM wrapper to manage & access your database
- `@repo/database`: [Prisma ORM](https://prisma.io/) to manage & access your database
- `@repo/typescript-config`: `tsconfig.json`s used throughout the monorepo

Each package/app is 100% [TypeScript](https://www.typescriptlang.org/).
Expand All @@ -30,74 +22,171 @@ This turborepo has some additional tools already setup for you:
- [TypeScript](https://www.typescriptlang.org/) for static type checking
- [ESLint](https://eslint.org/) for code linting
- [Prettier](https://prettier.io) for code formatting
- [Prisma](https://prisma.io/) for database ORM
- [Docker Compose](https://docs.docker.com/compose/) for local database
- [Prisma ORM](https://prisma.io/) for accessing the database
- [Docker Compose](https://docs.docker.com/compose/) for a local MySQL database

## Getting started

### Database
Follow these steps to set up and run your Turborepo project with Prisma ORM:

We use [Prisma](https://prisma.io/) to manage & access our database. As such you will need a database for this project, either locally or hosted in the cloud.
### 1. Create a Turborepo project

To make this process easier, we offer a [`docker-compose.yml`](https://docs.docker.com/compose/) file to deploy a MySQL server locally with a new database named `turborepo` (To change this update the `MYSQL_DATABASE` environment variable in the `docker-compose.yml` file):
Start by creating a new Turborepo project using the following command:

```sh
npx create-turbo@latest -e with-prisma
```

Choose your desired package manager when prompted and a name for the app (e.g., `my-turborepo`). This will scaffold a new Turborepo project with Prisma ORM included and dependencies installed.

Navigate to your project directory:

```bash
cd my-turborepo
cd ./my-turborepo
```

### 2. Setup a local database with Docker Compose

We use [Prisma ORM](https://prisma.io/) to manage and access our database. As such you will need a database for this project, either locally or hosted in the cloud.

To make this process easier, a [`docker-compose.yml` file](./docker-compose.yml) is included to setup a MySQL server locally with a new database named `turborepo`:

Start the MySQL database using Docker Compose:

```sh
docker-compose up -d
```

Once deployed you will need to copy the `.env.example` file to `.env` in order for Prisma to have a `DATABASE_URL` environment variable to access.
To change the default database name, update the `MYSQL_DATABASE` environment variable in the [`docker-compose.yml` file](/docker-compose.yml).

### 3. Setup environment variables

Once the database is ready, copy the `.env.example` file to the [`/packages/database`](./packages/database/) and [`/apps/web`](./apps/web/) directories as `.env`:

```bash
cp .env.example .env
cp .env.example ./packages/database/.env
cp .env.example ./apps/web/.env
```

This ensures Prisma has access to the `DATABASE_URL` environment variable, which is required to connect to your database.

If you added a custom database name, or use a cloud based database, you will need to update the `DATABASE_URL` in your `.env` accordingly.

Once deployed & up & running, you will need to create & deploy migrations to your database to add the necessary tables. This can be done using [Prisma Migrate](https://www.prisma.io/migrate):
### 4. Migrate your database

Once your database is running, you’ll need to create and apply migrations to set up the necessary tables. Run the database migration command:

```bash
npx prisma migrate dev
# Using npm
npm run db:migrate:dev
```

If you need to push any existing migrations to the database, you can use either the Prisma db push or the Prisma migrate deploy command(s):
<details>

<summary>Expand for <code>yarn</code>, <code>pnpm</code> or <code>bun</code></summary>

```bash
yarn run db:push
# Using yarn
yarn run db:migrate:dev

# OR
# Using pnpm
pnpm run db:migrate:dev

yarn run db:migrate:deploy
# Using bun
bun run db:migrate:dev
```

There is slight difference between the two commands & [Prisma offers a breakdown on which command is best to use](https://www.prisma.io/docs/concepts/components/prisma-migrate/db-push#choosing-db-push-or-prisma-migrate).
</details>

You’ll be prompted to name the migration. Once you provide a name, Prisma will create and apply the migration to your database.

> Note: The `db:migrate:dev` script (located in [packages/database/package.json](/packages/database/package.json)) uses [Prisma Migrate](https://www.prisma.io/migrate) under the hood.

An optional additional step is to seed some initial or fake data to your database using [Prisma's seeding functionality](https://www.prisma.io/docs/guides/database/seed-database).
For production environments, always push schema changes to your database using the [`prisma migrate deploy` command](https://www.prisma.io/docs/orm/prisma-client/deployment/deploy-database-changes-with-prisma-migrate). You can find an example `db:migrate:deploy` script in the [`package.json` file](/packages/database/package.json) of the `database` package.

To do this update check the seed script located in `packages/database/src/seed.ts` & add or update any users you wish to seed to the database.
### 5. Seed your database

Once edited run the following command to run tell Prisma to run the seed script defined in the Prisma configuration:
To populate your database with initial or fake data, use [Prisma's seeding functionality](https://www.prisma.io/docs/guides/database/seed-database).

Update the seed script located at [`packages/database/src/seed.ts`](/packages/database/src/seed.ts) to include any additional data that you want to seed. Once edited, run the seed command:

```bash
# Using npm
npm run db:seed
```

<details>

<summary>Expand for <code>yarn</code>, <code>pnpm</code> or <code>bun</code></summary>

```bash
# Using yarn
yarn run db:seed

# Using pnpm
pnpm run db:seed

# Using bun
bun run db:seed
```

For further more information on migrations, seeding & more, we recommend reading through the [Prisma Documentation](https://www.prisma.io/docs/).
</details>

### Build
### 6. Build your application

To build all apps and packages, run the following command:
To build all apps and packages in the monorepo, run:

```bash
# Using npm
npm run build
```

<details>

<summary>Expand for <code>yarn</code>, <code>pnpm</code> or <code>bun</code></summary>

```bash
# Using yarn
yarn run build

# Using pnpm
pnpm run build

# Using bun
bun run build
```

### Develop
</details>

### 7. Start the application

To develop all apps and packages, run the following command:
Finally, start your application with:

```bash
yarn run dev
```

<details>

<summary>Expand for <code>yarn</code>, <code>pnpm</code> or <code>bun</code></summary>

```bash
# Using yarn
yarn run dev

# Using pnpm
pnpm run dev

# Using bun
bun run dev
```

</details>

Your app will be running at `http://localhost:3000`. Open it in your browser to see it in action!

You can also read the official [detailed step-by-step guide from Prisma ORM](https://pris.ly/guide/turborepo?utm_campaign=turborepo-example) to build a project from scratch using Turborepo and Prisma ORM.

## Useful Links

Learn more about the power of Turborepo:
Expand Down
5 changes: 3 additions & 2 deletions examples/with-prisma/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
"scripts": {
"build": "turbo run build",
"db:migrate:deploy": "turbo run db:migrate:deploy",
"db:migrate:dev": "turbo run db:migrate:dev",
"db:push": "turbo run db:push",
"db:seed": "turbo run db:seed",
"dev": "turbo run dev",
Expand All @@ -20,7 +21,7 @@
},
"devDependencies": {
"prettier": "^3.2.5",
"prisma": "5.10.2",
"prisma": "latest",
"tsx": "4.19.1",
"turbo": "^2.0.3"
},
Expand All @@ -30,6 +31,6 @@
"packageManager": "yarn@1.22.19",
"name": "with-prisma",
"dependencies": {
"@prisma/client": "5.10.2"
"@prisma/client": "latest"
}
}
1 change: 0 additions & 1 deletion examples/with-prisma/packages/database/.env

This file was deleted.

4 changes: 2 additions & 2 deletions examples/with-prisma/packages/database/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,13 @@
"studio": "prisma studio"
},
"dependencies": {
"@prisma/client": "^5.10.2"
"@prisma/client": "latest"
},
"devDependencies": {
"@repo/eslint-config": "*",
"@repo/typescript-config": "*",
"eslint": "^8.57.0",
"prisma": "^5.10.2",
"prisma": "latest",
"rimraf": "^5.0.5",
"tsup": "^8.0.2",
"tsx": "4.19.1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
relationMode = "prisma"
}

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

model User {
Expand Down
11 changes: 7 additions & 4 deletions examples/with-prisma/packages/database/src/client.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
import { PrismaClient } from "@prisma/client";
import { PrismaClient } from "../generated/client";

export const prisma = global.prisma || new PrismaClient();
const globalForPrisma = global as unknown as { prisma: PrismaClient };

if (process.env.NODE_ENV !== "production") global.prisma = prisma;
export const prisma =
globalForPrisma.prisma || new PrismaClient();

export * from "@prisma/client";
if (process.env.NODE_ENV !== "production") globalForPrisma.prisma = prisma;

export * from "../generated/client";
2 changes: 1 addition & 1 deletion examples/with-prisma/packages/database/src/seed.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { prisma } from "./client";

import type { User } from "@prisma/client";
import type { User } from "../generated/client";

const DEFAULT_USERS = [
// Add your own user to pre-populate the database with
Expand Down
19 changes: 15 additions & 4 deletions examples/with-prisma/turbo.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,26 @@
"inputs": ["$TURBO_DEFAULT$", ".env*"],
"outputs": ["dist/**", ".next/**", "!.next/cache/**"]
},
"db:migrate:deploy": {},
"db:push": {},
"db:seed": {},
"db:migrate:dev": {
"cache": false,
"persistent": true
},
"db:migrate:deploy": {
"cache": false
},
"db:push": {
"cache": false
},
"db:seed": {
"cache": false
},
"dev": {
"cache": false,
"persistent": true
},
"generate": {
"dependsOn": ["^generate"]
"dependsOn": ["^generate"],
"cache": false
},
"lint": {}
}
Expand Down
Loading
Loading