A REST API for managing users and groups built with NestJS, Prisma, and PostgreSQL.
- Node.js 18+
- Docker and Docker Compose
- Redis (optional, falls back to in-memory cache)
- Start the database and Redis:
docker-compose up -d- Install dependencies and set up the database:
npm install
npx prisma db push
npx ts-node prisma/seed.ts- Start the server:
npm run start:devThe API is available at http://localhost:3000/api/v1.
API documentation: http://localhost:3000/docs
Copy .env and adjust values as needed:
| Variable | Description | Default |
|---|---|---|
DATABASE_URL |
PostgreSQL connection string | postgresql://user:password@localhost:5432/postgres |
REDIS_HOST |
Redis host | localhost |
REDIS_PORT |
Redis port | 6379 |
PORT |
Server port | 3000 |
NODE_ENV |
Environment | development |
CORS_ORIGIN |
Allowed origins | * |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/users |
Get users with offset pagination |
| GET | /api/v1/users/cursor |
Get users with cursor pagination |
| PATCH | /api/v1/users/statuses |
Bulk update user statuses |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/groups |
Get groups with pagination |
| DELETE | /api/v1/groups/:groupId/users/:userId |
Remove user from group |
| Method | Endpoint | Description |
|---|---|---|
| GET | /api/v1/health |
Health check |
GET /api/v1/users?limit=20&offset=0
Response:
{
"data": [...],
"meta": {
"limit": 20,
"offset": 0,
"total": 100
}
}Better for large datasets. Uses the last item's ID as cursor.
GET /api/v1/users/cursor?limit=20
GET /api/v1/users/cursor?cursor=25&limit=20
Response:
{
"data": [...],
"meta": {
"nextCursor": 45,
"hasNext": true
}
}Update up to 500 users in a single request. The operation is atomic.
PATCH /api/v1/users/statuses
Content-Type: application/json
{
"updates": [
{ "id": 1, "status": "active" },
{ "id": 2, "status": "blocked" }
]
}
Valid statuses: pending, active, blocked
When removing the last user from a group, the group status is automatically updated to empty.
DELETE /api/v1/groups/1/users/5
Returns 204 No Content on success.
All errors follow this format:
{
"error": {
"code": "VALIDATION_ERROR",
"message": "Invalid request",
"details": [
{ "field": "limit", "reason": "limit must not be greater than 100" }
]
}
}Error codes: VALIDATION_ERROR, NOT_FOUND, CONFLICT, INTERNAL_ERROR
| Endpoint | Limit |
|---|---|
| General | 10/sec, 100/min |
| Bulk status update | 5/sec, 20/min |
Returns 429 Too Many Requests when exceeded.
List endpoints are cached in Redis with a 30-second TTL. Cache is invalidated on mutations.
# Run e2e tests
npm run test:e2e
# Run with coverage
npm run test:covsrc/
common/ # Shared DTOs, filters, interceptors
database/ # Prisma service and module
cache/ # Redis cache service
users/ # Users module (controller, service, DTOs)
groups/ # Groups module (controller, service, DTOs)
health/ # Health check module
prisma/
schema.prisma # Database schema
seed.ts # Seed data
test/
*.e2e-spec.ts # End-to-end tests
| Command | Description |
|---|---|
npm run start:dev |
Start in development mode with hot reload |
npm run start:prod |
Start production build |
npm run build |
Build for production |
npm run test:e2e |
Run end-to-end tests |
npm run lint |
Lint and fix code |
model User {
id Int @id @default(autoincrement())
username String
status UserStatus @default(pending)
groupId Int?
group Group? @relation(...)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
model Group {
id Int @id @default(autoincrement())
name String
status GroupStatus @default(empty)
users User[]
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
}
enum UserStatus { pending, active, blocked }
enum GroupStatus { empty, notEmpty }MIT