Support Online
Skip to main content

REST API Development Guide with Prisma and PostgreSQL

What Will You Learn in This Guide?

In this guide, you will learn the basic logic of Prisma ORM.
You will develop a REST API with TypeScript and PostgreSQL.
You will set up an isolated database environment using Docker.

🧠 Technical Summary

Main topic: Prisma ORM, TypeScript and PostgreSQL integration.
Solved problem: Type-safe database access without writing complex SQL.
Steps followed: Project setup, PostgreSQL with Docker, Prisma schema and API endpoints.


1. Preparing the TypeScript Project

This step creates the Node.js-based project skeleton.

mkdir my-blog && cd my-blog
npm init -y
npm install typescript ts-node @types/node --save-dev

  1. This configuration defines TypeScript build settings:

{
"compilerOptions": {
"sourceMap": true,
"outDir": "dist",
"strict": true,
"lib": ["esnext"],
"esModuleInterop": true
}
}

2. Installing PostgreSQL and Prisma with Docker

  1. This step runs PostgreSQL in an isolated environment.

version: "3.8"
services:
postgres:
image: postgres:10.3
restart: always
environment:
POSTGRES_USER: genixnode
POSTGRES_PASSWORD: guclu_sifre
ports:
- "5432:5432"

  • This command initializes the database:

docker-compose up -d

  • This command creates the Prisma configuration:

npx prisma init

  • This variable allows Prisma to connect to the database:

DATABASE_URL="postgresql://genixnode:guclu_sifre@localhost:5432/my-blog?schema=public"

3. Defining the Data Model

  1. This step determines the table and relationships.

model User {
id Int @id @default(autoincrement())
email String @unique
name String?
posts Post[]
}

model Post {
id Int @id @default(autoincrement())
title String
author User? @relation(fields: [authorId], references: [id])
authorId Int?
}

  • This command creates tables and generates the client:

npx prisma migrate dev --name init

4. REST API Endpoints with Express

  1. This step defines the API routes.

npm install express
npm install @types/express --save-dev

  • Sample route that brings all users:

app.get("/users", async (req, res) => {
const users = await prisma.user.findMany()
res.json(users)
})

  • This command starts the API server:

npx ts-node src/index.ts

❓ Frequently Asked Questions (FAQ)

1. Why should Prisma be preferred? Provides type safety and automatic query generation.

2. Is it mandatory to use Docker? No, but it offers a quick and clean installation.

3. What does Prisma Studio do? It allows you to manage the database via the browser.


🎯 Result

With this guide, you have developed a modern REST API. Thanks to Prisma, database operations have become simpler. A more secure backend was obtained with TypeScript.

You can safely run this API structure on high-performance Linux servers on the GenixNode infrastructure.