Skip to content

Commit e80c9d4

Browse files
authored
feat(prisma-client): new nextjs-starter-webpack-monorepo example (#8186)
* feat(prisma-client): new nextjs-starter-webpack-monorepo example * feat: rename prisma-postgres -> generator-prisma-client, rename .generated -> generated, adjust README, adjust imports --------- Co-authored-by: jkomyno <[email protected]>
1 parent 7353c37 commit e80c9d4

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+11552
-0
lines changed
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
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).
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
{
2+
"name": "generator-prisma-client-nextjs-starter-webpack-monorepo",
3+
"version": "1.0.0",
4+
"description": "",
5+
"keywords": [],
6+
"author": "Alberto Schiabel <[email protected]> (https://github.com/jkomyno)",
7+
"license": "MIT",
8+
"packageManager": "[email protected]+sha512.499434c9d8fdd1a2794ebf4552b3b25c0a633abcee5bb15e7b5de90f32f47b513aca98cd5cfd001c31f0db454bc3804edccd578501e4ca293a6816166bbd9f81",
9+
"scripts": {
10+
"build": "pnpm -r build",
11+
"clean": "pnpm -r clean"
12+
}
13+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
DATABASE_URL="prisma+postgres://..."
2+
DIRECT_URL="postgres://..."
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"extends": "next/core-web-vitals"
3+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
2+
3+
# dependencies
4+
/node_modules
5+
/.pnp
6+
.pnp.js
7+
8+
.env
9+
10+
# testing
11+
/coverage
12+
13+
# next.js
14+
/.next/
15+
/out/
16+
17+
# production
18+
/build
19+
20+
# misc
21+
.DS_Store
22+
*.pem
23+
24+
# debug
25+
npm-debug.log*
26+
yarn-debug.log*
27+
yarn-error.log*
28+
29+
# local env files
30+
.env*.local
31+
32+
# vercel
33+
.vercel
34+
35+
# typescript
36+
*.tsbuildinfo
37+
next-env.d.ts
38+
package-lock.json
39+
40+
# generated Prisma client
41+
lib/.generated
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
MIT License Copyright (c) 2025 Prisma
2+
3+
Permission is hereby granted, free of
4+
charge, to any person obtaining a copy of this software and associated
5+
documentation files (the "Software"), to deal in the Software without
6+
restriction, including without limitation the rights to use, copy, modify, merge,
7+
publish, distribute, sublicense, and/or sell copies of the Software, and to
8+
permit persons to whom the Software is furnished to do so, subject to the
9+
following conditions:
10+
11+
The above copyright notice and this permission notice
12+
(including the next paragraph) shall be included in all copies or substantial
13+
portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
16+
ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO
18+
EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19+
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20+
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21+
THE SOFTWARE.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { getQuotes } from '@/lib/utils/query'
2+
3+
// disabling caching
4+
export const fetchCache = 'force-no-store'
5+
export const revalidate = 0
6+
7+
export async function GET(_request: Request) {
8+
const data = await getQuotes()
9+
10+
return new Response(JSON.stringify(data), {
11+
headers: { 'Content-Type': 'application/json' },
12+
status: 200,
13+
})
14+
}
25.3 KB
Binary file not shown.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
@tailwind base;
2+
@tailwind components;
3+
@tailwind utilities;
4+
5+
:root {
6+
--foreground-rgb: 0, 0, 0;
7+
--background-start-rgb: 214, 219, 220;
8+
--background-end-rgb: 255, 255, 255;
9+
}
10+
11+
@media (prefers-color-scheme: dark) {
12+
:root {
13+
--foreground-rgb: 255, 255, 255;
14+
--background-start-rgb: 0, 0, 0;
15+
--background-end-rgb: 0, 0, 0;
16+
}
17+
}
18+
19+
body {
20+
color: rgb(var(--foreground-rgb));
21+
background: linear-gradient(
22+
to bottom,
23+
transparent,
24+
rgb(var(--background-end-rgb))
25+
)
26+
rgb(var(--background-start-rgb));
27+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import './globals.css'
2+
import type { Metadata } from 'next'
3+
import { Inter } from 'next/font/google'
4+
5+
const inter = Inter({ subsets: ['latin'] })
6+
7+
export const metadata: Metadata = {
8+
title: 'Accelerated Quotes',
9+
description: 'Accelerate starter',
10+
}
11+
12+
export default function RootLayout({
13+
children,
14+
}: {
15+
children: React.ReactNode
16+
}) {
17+
return (
18+
<html lang="en">
19+
<body className={inter.className}>{children}</body>
20+
</html>
21+
)
22+
}

0 commit comments

Comments
 (0)