REST API Development Guide with Prisma and PostgreSQL
REST API Development with Prisma and PostgreSQL
What will you learn in this guide?
In this guide, you will develop a REST API connected to a PostgreSQL database using Prisma ORM.
You will build a modern API architecture with TypeScript, Docker and Express.
You will create a real blog API by implementing CRUD operations end-to-end.
🧠 Technical Summary
Subject: REST API development with Prisma + PostgreSQL
Purpose: To provide safe and type-supported data access without writing SQL
Method: PostgreSQL, Prisma ORM, Express and TypeScript with Docker
Prerequisites
- Node.js 14 or above
- A development environment with Docker installed
- Basic familiarity with TypeScript and REST API logic
1️⃣ Creating the TypeScript Project
First create an empty project directory:
mkdir my-blog
cd my-blog
npm init -y
- This command creates a basic package.json file.
Install TypeScript tools:
npm install typescript ts-node @types/node --save-dev
- These packages provide TypeScript compilation and execution.
Add TypeScript configuration:
nano tsconfig.json
{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}
- These settings provide strict type checking and modern JavaScript support.
2️⃣ Installing PostgreSQL and Prisma with Docker
- Add Prisma CLI to the project:
npm install prisma --save-dev
- This tool provides schema and migration management.
Create Docker configuration for PostgreSQL:
nano docker-compose.yml
version: '3.8'
services:
postgres:
image: postgres:10.3
restart: always
environment:
- POSTGRES_USER=genixnode
- POSTGRES_PASSWORD=guclu_sifre
ports:
- "5432:5432"
volumes:
- postgres:/var/lib/postgresql/data
volumes:
postgres:
- This configuration runs PostgreSQL in an isolated environment.
Start the database:
docker-compose up -d
- This command runs the PostgreSQL container in the background.
Launch Prisma:
npx prisma init
- This command creates schema.prisma and .env files.
Edit the .env file:
DATABASE_URL="postgresql://genixnode:guclu_sifre@localhost:5432/my-blog?schema=public"
- This variable allows Prisma to connect to the database.
3️⃣ Defining the Data Model and Migration
- Open the Prisma diagram:
nano prisma/schema.prisma
model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}
model Post {
id Int @id @default(autoincrement())
title String
content String?
published Boolean @default(false)
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}
- These models define the relationship between user and post.
Create the database tables:
npx prisma migrate dev --name init
- This command produces a migration file and creates the table.
4️⃣ Data Transactions with Prisma Client
- Install Prisma Client:
npm install @prisma/client
- This package provides type-safe queries.
Create source directory:
mkdir src
nano src/index.ts
import { PrismaClient } from '@prisma/client'
const prisma = new PrismaClient()
async function main() {
const user = await prisma.user.create({
data: {
email: 'ali@ornek.com',
name: 'Ali'
}
})
console.log(user)
}
main()
.catch(console.error)
.finally(() => prisma.$disconnect())
- This example adds users to the database.
5️⃣ REST API Setup with Express
- Install Express:
npm install express
npm install @types/express --save-dev
- These packages enable HTTP API development.
Type the server code:
import express from 'express'
import { PrismaClient } from '@prisma/client'
const app = express()
const prisma = new PrismaClient()
app.use(express.json())
app.get('/users', async (req, res) => {
const users = await prisma.user.findMany()
res.json(users)
})
app.listen(3000, () => {
console.log('API http://localhost:3000 adresinde çalışıyor')
})
- This code lists users via the /users endpoint.
6️⃣ CRUD API Routes
- Bring published articles
app.get('/feed', async (req, res) => {
const posts = await prisma.post.findMany({
where: { published: true },
include: { author: true }
})
res.json(posts)
})
Create user
app.post('/user', async (req, res) => {
const user = await prisma.user.create({
data: req.body
})
res.json(user)
})
Publish article
app.put('/post/publish/:id', async (req, res) => {
const post = await prisma.post.update({
where: { id: Number(req.params.id) },
data: { published: true }
})
res.json(post)
})
Delete text
app.delete('/post/:id', async (req, res) => {
const post = await prisma.post.delete({
where: { id: Number(req.params.id) }
})
res.json(post)
})
- These endpoints provide a full CRUD API.
❓ Frequently Asked Questions (FAQ)
1. Why is Prisma preferred? It produces type-safe queries without writing SQL.
2. Is Docker mandatory? No, but it provides environment consistency.
3. Is this API suitable for production? Yes, it can scale with the right configuration.
4. Is Prisma high-performance? Yes, the query engine works very optimized.
Result
In this guide, you developed a modern REST API with Prisma and PostgreSQL. Thanks to TypeScript, you caught errors early. This architecture can be used safely in real projects.
You can run this structure on the GenixNode infrastructure in minutes. 🚀

