Skip to content
All essays
ArchitectureJanuary 20, 202513 min

PostgreSQL & Prisma: Modern Database Development

Master PostgreSQL with Prisma ORM. Learn schema design, migrations, querying, and advanced database patterns.

Ü
Ümit Uz
Mobile & Full Stack Developer

What is PostgreSQL?

PostgreSQL is a powerful, open-source relational database with advanced features.

Setting Up Prisma

bash
npm install prisma @prisma/client
npx prisma init

Schema Definition

prisma
// prisma/schema.prisma

datasource db {
  provider = "postgresql"
  url      = env("DATABASE_URL")
}

generator client {
  provider = "prisma-client-js"
}

model User {
  id        String   @id @default(uuid())
  email     String   @unique
  name      String?
  posts     Post[]
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

model Post {
  id        String   @id @default(uuid())
  title     String
  content   String?
  published Boolean  @default(false)
  author    User     @relation(fields: [authorId], references: [id])
  authorId  String
  createdAt DateTime @default(now())
  updatedAt DateTime @updatedAt
}

Migrations

bash
# Create migration
npx prisma migrate dev --name init

# Reset database
npx prisma migrate reset

# Deploy migrations
npx prisma migrate deploy

Prisma Client

javascript
import { PrismaClient } from '@prisma/client';

const prisma = new PrismaClient();

CRUD Operations

Create

javascript
const user = await prisma.user.create({
  data: {
    email: 'john@example.com',
    name: 'John Doe',
    posts: {
      create: {
        title: 'My First Post'
      }
    }
  }
});

Read

javascript
// Find many
const users = await prisma.user.findMany();

// Find unique
const user = await prisma.user.findUnique({
  where: { email: 'john@example.com' }
});

// Find first
const user = await prisma.user.findFirst({
  where: { name: { contains: 'John' } }
});

// Include relations
const user = await prisma.user.findUnique({
  where: { id: userId },
  include: { posts: true }
});

Update

javascript
const user = await prisma.user.update({
  where: { id: userId },
  data: { name: 'Jane Doe' }
});

Delete

javascript
await prisma.user.delete({
  where: { id: userId }
});

Advanced Queries

Filtering and Sorting

javascript
const users = await prisma.user.findMany({
  where: {
    email: { contains: 'example' },
    posts: {
      some: { published: true }
    }
  },
  orderBy: {
    createdAt: 'desc'
  },
  take: 10,
  skip: 20
});

Transactions

javascript
const result = await prisma.$transaction(async (tx) => {
  const user = await tx.user.create({
    data: { email: 'john@example.com' }
  });

  const post = await tx.post.create({
    data: {
      title: 'Hello',
      authorId: user.id
    }
  });

  return { user, post };
});

Best Practices

  1. 1Use transactions: For multi-step operations
  2. 2Optimize includes: Only select needed relations
  3. 3Use raw queries: For complex operations
  4. 4Handle connection pooling: Configure for production
  5. 5Use indexes: Speed up queries

Conclusion

Prisma with PostgreSQL provides a modern, type-safe approach to database development.

Related essays

Next essay
MongoDB & Mongoose: Complete NoSQL Database Guide