Database Setup for NestJS API
This guide explains how to set up and use Drizzle ORM with PostgreSQL in your NestJS API.
Database Setup for NestJS API
This guide explains how to set up and use Drizzle ORM with PostgreSQL in your NestJS API.
Environment Variables
Create a .env file in the apps/api directory with the following variables:
env
# Database Configuration
DATABASE_URL=postgresql://user:password@localhost:5432/database_name
# Application Port
PORT=3003
# CORS Configuration (optional)
CORS_ORIGIN=http://localhost:3000Starting the Database
From the monorepo root, run:
# Start PostgreSQL with Docker
pnpm db:start
# Or watch logs
pnpm db:watchDatabase Migrations
# Generate migrations from schema changes
pnpm db:generate
# Push schema changes to database (development)
pnpm db:push
# Run migrations (production)
pnpm db:migrate
# Open Drizzle Studio (database GUI)
pnpm db:studioUsing the Database in Services
The database is available through dependency injection:
import { Inject, Injectable } from "@nestjs/common";
import { eq } from "drizzle-orm";
import { user } from "@nomor/db";
import { DATABASE, type Database } from "../../database/database.module";
@Injectable()
export class MyService {
constructor(@Inject(DATABASE) private readonly db: Database) {}
async findUser(id: string) {
const [foundUser] = await this.db
.select()
.from(user)
.where(eq(user.id, id));
return foundUser;
}
}Available Tables
user- User accountssession- User sessionsaccount- OAuth accountsverification- Email/phone verification tokens
API Documentation
Once the server is running, access Swagger documentation at:
http://localhost:3003/api/api-docsExample Usage
Create a User
curl -X POST http://localhost:3003/api/v2/user \
-H "Content-Type: application/json" \
-d '{
"name": "John Doe",
"email": "john@example.com"
}'Get All Users
curl http://localhost:3003/api/v2/userGet User by ID
curl http://localhost:3003/api/v2/user/{user-id}Starter Nest Backend Guidelines
This document explains how the `apps/api` service is structured, how to extend it safely, and how to write and test new backend code with NestJS. It is intentionally beginner-friendly—treat it as the playbook for everyday backend work in this repo.
Role-Based Permissions System Setup
This document summarizes the role-based permissions system that has been implemented.