|
| 1 | +# Prisma Postgres Example: Next.js 15 Starter (Monorepo, Webpack, Node.js, ESM) |
| 2 | + |
| 3 | +This project showcases how to use the Prisma ORM with Prisma Postgres in an ESM monorepo, containing a Next.js application and Prisma package definition. |
| 4 | + |
| 5 | +It also demonstrates that [@prisma/nextjs-monorepo-workaround-plugin](https://www.npmjs.com/package/@prisma/nextjs-monorepo-workaround-plugin) is no longer needed, as the `prisma-client` generator supports ESM and monorepos out of the box. |
| 6 | + |
| 7 | +## Project Structure |
| 8 | + |
| 9 | +``` |
| 10 | +. |
| 11 | +├── packages |
| 12 | +│ ├── next-app # Next.js application |
| 13 | +│ │ ├── app |
| 14 | +│ │ ├── components |
| 15 | +│ │ ├── lib |
| 16 | +│ │ ├── next.config.mjs |
| 17 | +│ │ ├── package.json |
| 18 | +│ │ ├── postcss.config.mjs |
| 19 | +│ │ └── tsconfig.json |
| 20 | +│ └── prisma # Prisma Client definition and re-export |
| 21 | +│ ├── package.json |
| 22 | +│ ├── prisma |
| 23 | +│ │ └── schema.prisma # Prisma schema file |
| 24 | +│ ├── src |
| 25 | +│ │ ├── enums.ts |
| 26 | +│ │ └── index.ts |
| 27 | +│ └── tsconfig.json |
| 28 | +├── pnpm-workspace.yaml # Pnpm workspace configuration file |
| 29 | +└── tsconfig.json |
| 30 | +``` |
| 31 | + |
| 32 | +## Prerequisites |
| 33 | + |
| 34 | +To successfully run the project, you will need the following: |
| 35 | + |
| 36 | +- Two **Prisma Postgres** connection strings: |
| 37 | + - Your **Prisma Postgres + Accelerate connection string** (containing your **Prisma API key**) which you can get by enabling Postgres in a project in your [Prisma Data Platform](https://pris.ly/pdp) account. You will use this connection string to run Prisma migrations. |
| 38 | + - Your **Prisma Postgres direct TCP connection string** which you will use with Prisma Client. |
| 39 | + Learn more in the [docs](https://www.prisma.io/docs/postgres/database/direct-connections). |
| 40 | +- [`pnpm`](https://pnpm.io/) installed globally to manage the monorepo workspace. |
| 41 | + |
| 42 | +## Tech Stack |
| 43 | + |
| 44 | +- Next.js 15 |
| 45 | + - Runtime: Node.js 20.19.0 |
| 46 | + - Bundler: Webpack 5 |
| 47 | +- ESM |
| 48 | + - `package.json` contains `{ "type": "module" }` |
| 49 | + - `next.config.js` -> `next.config.mjs` |
| 50 | + - `postcss.config.js` -> `postcss.config.mjs` |
| 51 | +- Prisma Client with the `prisma-client` generator |
| 52 | + See the [Prisma schema file](./packages/prisma/prisma/schema.prisma) for details. |
| 53 | + |
| 54 | + ```prisma |
| 55 | + generator client { |
| 56 | + provider = "prisma-client" |
| 57 | + output = "../lib/generated/prisma" |
| 58 | + previewFeatures = ["driverAdapters", "queryCompiler"] |
| 59 | + runtime = "nodejs" |
| 60 | + } |
| 61 | + ``` |
| 62 | + |
| 63 | +## Getting started |
| 64 | + |
| 65 | +### 1. Clone the repository |
| 66 | + |
| 67 | +Clone the repository, navigate into it and install dependencies: |
| 68 | + |
| 69 | +``` |
| 70 | +git clone [email protected]:prisma/prisma-examples.git --depth=1 |
| 71 | +cd prisma-examples/generator-prisma-client/nextjs-starter-webpack-monorepo |
| 72 | +pnpm install |
| 73 | +``` |
| 74 | + |
| 75 | +### 2. Configure environment variables |
| 76 | + |
| 77 | +Create a `.envrc` in the root of the project directory: |
| 78 | + |
| 79 | +```bash |
| 80 | +touch .envrc |
| 81 | +``` |
| 82 | + |
| 83 | +Now, open the `.envrc` file and set the `DATABASE_URL` environment variables with the values of your connection string and your Prisma Postgres connection string: |
| 84 | + |
| 85 | +```bash |
| 86 | +# .envrc |
| 87 | + |
| 88 | +# Prisma Postgres connection string (used for migrations) |
| 89 | +export DATABASE_URL="__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__" |
| 90 | + |
| 91 | +# Postgres connection string (used for queries by Prisma Client) |
| 92 | +export DATABASE_URL="__YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__" |
| 93 | + |
| 94 | +NEXT_PUBLIC_URL="http://localhost:3000" |
| 95 | +``` |
| 96 | + |
| 97 | +Note that `__YOUR_PRISMA_POSTGRES_CONNECTION_STRING__` is a placeholder value that you need to replace with the values of your Prisma Postgres + Accelerate connection string. Notice that the Accelerate connection string has the following structure: `prisma+postgres://accelerate.prisma-data.net/?api_key=<api_key_value>`. |
| 98 | + |
| 99 | +Note that `__YOUR_PRISMA_POSTGRES_DIRECT_CONNECTION_STRING__` is a placeholder value that you need to replace with the values of your Prisma Postgres direct TCP connection string. The direct connection string has the following structure: `postgres://<username>:<password>@<host>:<port>/<database>`. |
| 100 | + |
| 101 | +Expose the environment variables to your project by running the following command: |
| 102 | + |
| 103 | +```bash |
| 104 | +source .envrc |
| 105 | +``` |
| 106 | + |
| 107 | +### 3. Run a migration to create the database structure and seed the database |
| 108 | + |
| 109 | +The [Prisma schema file](./packages/prisma/prisma/schema.prisma) contains a single `Quotes` model and a `QuoteKind` enum. You can map this model to the database and create the corresponding `Quotes` table using the following command: |
| 110 | + |
| 111 | +```sh |
| 112 | +pnpm --filter @nextjs-starter-webpack-monorepo/prisma exec prisma migrate dev --name init |
| 113 | +``` |
| 114 | + |
| 115 | +You now have an empty `Quotes` table in your database. Next, run the [seed script](./packages/prisma/prisma/seed.ts) to create some sample records in the table: |
| 116 | + |
| 117 | +```sh |
| 118 | +pnpm --filter @nextjs-starter-webpack-monorepo/prisma exec prisma db seed |
| 119 | +``` |
| 120 | + |
| 121 | +### 4. Generate Prisma Client |
| 122 | + |
| 123 | +Run: |
| 124 | + |
| 125 | +``` |
| 126 | +pnpm --filter @nextjs-starter-webpack-monorepo/prisma exec prisma generate |
| 127 | +``` |
| 128 | + |
| 129 | +### 5. Build the Prisma package |
| 130 | + |
| 131 | +Run: |
| 132 | + |
| 133 | +```sh |
| 134 | +pnpm --filter @nextjs-starter-webpack-monorepo/prisma build |
| 135 | +``` |
| 136 | + |
| 137 | +### 6. Install the workspace dependencies in the Next.js app |
| 138 | + |
| 139 | +Run: |
| 140 | + |
| 141 | +```sh |
| 142 | +pnpm --filter @nextjs-starter-webpack-monorepo/next-app install |
| 143 | +``` |
| 144 | + |
| 145 | +### 7. Start the Next.js app |
| 146 | + |
| 147 | +You can run the app with the following command: |
| 148 | + |
| 149 | +``` |
| 150 | +pnpm --filter @nextjs-starter-webpack-monorepo/next-app dev |
| 151 | +``` |
| 152 | + |
| 153 | +## Resources |
| 154 | + |
| 155 | +- [Prisma Postgres documentation](https://www.prisma.io/docs/postgres) |
| 156 | +- Check out the [Prisma docs](https://www.prisma.io/docs) |
| 157 | +- [Join our community on Discord](https://pris.ly/discord?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) to share feedback and interact with other users. |
| 158 | +- [Subscribe to our YouTube channel](https://pris.ly/youtube?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for live demos and video tutorials. |
| 159 | +- [Follow us on X](https://pris.ly/x?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section) for the latest updates. |
| 160 | +- Report issues or ask [questions on GitHub](https://pris.ly/github?utm_source=github&utm_medium=prisma_examples&utm_content=next_steps_section). |
0 commit comments