Small Node.js + TypeScript project that demonstrates hexagonal architecture with a Postgres-backed User module. It exposes a simple CRUD API for users and keeps domain logic isolated from infrastructure.
- Node.js, Express
- TypeScript, ts-node
- Postgres (pg)
- Jest, Supertest
src/main.ts: Express app bootstrapsrc/lib/Shared: cross-cutting concerns (env, service container)src/lib/User/domain: entities and value objectssrc/lib/User/application: use casessrc/lib/User/infrastructure: Express controller/router + Postgres repository
- Node.js
- Postgres database
Create a .env file at the project root:
DATABASE_URL=postgres://user:password@localhost:5432/hexagonalThe app expects a users table with a created_at default because inserts do not set it explicitly:
CREATE TABLE users (
id uuid PRIMARY KEY,
name text NOT NULL,
email text NOT NULL,
created_at timestamp NOT NULL DEFAULT now()
);npm installnpm run devThe server listens on http://localhost:3000.
npm run build
npm startnpm testNote: integration tests for PostgresUserRepository require DATABASE_URL and a running database.
All endpoints accept and return JSON.
Returns an array of users.
Returns a single user or 404 if not found.
Creates a user. Body:
{
"id": "9b9b4be2-8e6c-4a01-9f84-ff1cf7b2f7a2",
"name": "Ada",
"email": "ada@example.com",
"createdAt": "2024-01-01T10:00:00.000Z"
}Updates a user. Body is the same as create. Returns 204 or 404 if not found.
Deletes a user. Returns 204 or 404 if not found.
idmust be at least 5 charactersnamemust be at least 3 charactersemailmust contain@and.createdAtmust be in the past