Blueprints
Next.js + Prisma - Blueprint
Next.js app on Prisma and PostgreSQL with a REST API and Swagger UI
A small full-stack starter built with Next.js, Prisma and PostgreSQL. It has a web UI with add, edit and delete, a REST API over the same data and a Swagger UI.
Tech stack:
| Layer | Technology |
|---|---|
| Framework | Next.js App Router |
| Database | PostgreSQL through Prisma |
| Migrations | Prisma Migrate, applied on start |
| API docs | Swagger UI from an OpenAPI spec |
| Styling | Tailwind CSS |
| Language | TypeScript |
The application runs on port 3000.
View on GitHub →Routes
| Path | What it does |
|---|---|
/ | List of records with add, edit and delete |
/targets/new | Form to add a record |
/targets/{id}/edit | Form to edit a record |
/swagger | Swagger UI for the API |
API
| Method | Path | What it does |
|---|---|---|
GET | /api/targets | List all records |
POST | /api/targets | Create a record |
GET | /api/targets/{id} | Get one record |
PATCH | /api/targets/{id} | Update a record |
DELETE | /api/targets/{id} | Delete a record |
GET | /api/health | Check the app and the database |
GET | /api/openapi.json | The OpenAPI spec |
Errors come back as JSON: 400 for invalid input, 403 in read-only mode, 404 for an unknown id and 409 for a name that is already taken.
Project Structure
app/ pages, server actions and API routes
components/ shared React components
lib/ Prisma client, queries, validation, OpenAPI spec
prisma/ schema, migrations and seed
scripts/prestart.mjs waits for the database, migrates and seeds- All database access goes through
lib/targets.ts. - Each time the container starts it waits for the database, applies pending migrations and seeds the table when it is empty. Only then does the server start.
Environment Variables
| Variable | Required | What it does |
|---|---|---|
DATABASE_URL | yes | PostgreSQL connection string. Rock8Cloud sets it for you. |
DEMO_READ_ONLY | no | Set to true to refuse every write. The default is off. |
Changing the Schema
- Edit
prisma/schema.prisma. - Create a migration:
npm run db:migrate -- --name what_changed. - Commit the migration and push. It is applied on the next start.
Running Locally
With Docker Compose
docker compose up --buildStarts the application together with a PostgreSQL instance.
With npm
docker compose up -d db
cp .env.example .env
npm install
npm run db:deploy
npm run dev