Skip to Content
Rock8Cloud
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:

LayerTechnology
FrameworkNext.js App Router
DatabasePostgreSQL through Prisma
MigrationsPrisma Migrate, applied on start
API docsSwagger UI from an OpenAPI spec
StylingTailwind CSS
LanguageTypeScript

The application runs on port 3000.

View on GitHub →

Routes

PathWhat it does
/List of records with add, edit and delete
/targets/newForm to add a record
/targets/{id}/editForm to edit a record
/swaggerSwagger UI for the API

API

MethodPathWhat it does
GET/api/targetsList all records
POST/api/targetsCreate 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/healthCheck the app and the database
GET/api/openapi.jsonThe 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

VariableRequiredWhat it does
DATABASE_URLyesPostgreSQL connection string. Rock8Cloud sets it for you.
DEMO_READ_ONLYnoSet to true to refuse every write. The default is off.

Changing the Schema

  1. Edit prisma/schema.prisma.
  2. Create a migration: npm run db:migrate -- --name what_changed.
  3. Commit the migration and push. It is applied on the next start.

Running Locally

With Docker Compose

docker compose up --build

Starts 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

On this page