Batteries-included backend starter. Auth, users, audit logging, notifications, S3 uploads and MongoDB wired up so you don't set it up again for every new project.
| Area | Choice |
|---|---|
| Framework | NestJS on Fastify |
| Runtime | Bun |
| Database | MongoDB via Mongoose, with a generic MongoBaseService<T> CRUD base class |
| Validation | Zod 4 — DTOs are schemas + z.infer types, validated by Nest's standard-schema support |
| Auth | JWT access + refresh tokens bound to server-side sessions (max 3/user), argon2 hashing |
| Notifications | In-app feed + web push (VAPID) + SMS (Unifonic) + email (SMTP/@nestjs/mail, branded HTML) |
| Queue | RabbitMQ (@golevelup/nestjs-rabbitmq) with the delayed-message-exchange plugin |
| Storage | AWS S3 upload/cleanup helpers, used by the user avatar flow as a working example |
| Audit log | Append-only action log, queryable/exportable with AuditLog.* |
| Health | GET /health readiness (Mongo, heap, RabbitMQ when enabled) + GET /health/live |
| Docs | Swagger UI at /docs in development, generated from Zod schemas |
| Quality | Oxlint + Oxfmt (@imranbarbhuiya/oxc-config), Husky pre-commit, Vitest |
Authorization is role-based: every user has a role (Admin or User) and routes declare what they need with
@RequirePermissions(...). user.superAdmin passes every check. The catalog and role grants live in
src/lib/util/Permissions.ts; GET /roles and GET /roles/permissions expose them, and GET /users/me returns
the caller's effective permissions. Admin gets every permission; User gets none, so it can only read and
update its own profile through /users/me. Only super admins can update or delete a super admin account, and
changing another user's role also needs Users.Manage, not just Users.Update. PATCH /users/me only takes
name, email, phone and avatar, and changing the email needs currentPassword. Nobody, super admins included, can
change their own role or status.
Deleting a user is a soft delete (deleted: true), and the email stays reserved by the unique index: the same
address can't register again or be given to another account. Registering with it gets the usual "pending approval"
reply, but no account is created.
Responses are filtered through the route's response DTO: fields the DTO doesn't declare (_id, password,
sessions, push subscriptions) are stripped before they leave the API.
bun install
cp .env.example .env # fill in DB_URI, JWT_ACCESS_SECRET, JWT_REFRESH_SECRET at minimum
# Windows PowerShell
.\docker-setup.ps1
# Linux/Mac/WSL
./docker-setup.sh
bun devDocker starts MongoDB and RabbitMQ (with the delayed-message plugin) via docker-compose.yml. RabbitMQ is
optional: the queue module only loads when RMQ_URI is set, so leave it empty (and drop the rabbitmq service
from the compose file) if you don't need delayed jobs.
Self-registered accounts start as Requested and need an admin to approve them, so create the first account
from the command line. It's created as Accepted and can log in straight away:
bun scripts/createUser.ts --name "Jane Doe" --email jane@example.com --role Admin --super-admin| Flag | Required | Description |
|---|---|---|
--name |
yes | Display name |
--email |
yes | Login email (stored lowercase, must be unique) |
--password |
no | At least 8 characters. Omit it to be prompted, which keeps it out of history |
--role |
no | Admin or User (default User) |
--super-admin |
no | Grants super admin, which passes every permission check |
Run it from the project root so Bun loads DB_URI from .env.
Services after setup:
- MongoDB —
mongodb://localhost:27017 - RabbitMQ —
amqp://app:app123@localhost:5673 - RabbitMQ Management UI — http://localhost:15673 (user:
app, pass:app123)
| Task | Command |
|---|---|
| Dev | bun dev |
| Build | bun run build |
| Start | bun start |
| Lint | bun lint |
| Format | bun format |
| Typecheck | bun typecheck |
| Regenerate Zod DTOs from Mongoose schemas | bun sync |
| Unit tests | bun run test |
| E2E tests | bun run test:e2e |
| Everything — run before committing | bun run preflight |
| Docker: setup / restart / clean / logs / status | ./docker-setup.sh <action> (or .ps1 on Windows) |
scripts/
└── createUser.ts # Create a user from the CLI (first admin) — not part of the build
src/
├── app.module.ts # Wires every module together
├── main.ts # Fastify bootstrap, Swagger, @fastify/helmet, CORS
├── audit-log/ # Audit log module
├── auth/ # Register / login / refresh / logout / change password
├── health/ # GET /health (readiness) and /health/live, via @nestjs/terminus
├── user/ # Profile + admin user management
├── notification/ # In-app notification feed
├── push-notification/ # Web push VAPID key + subscribe routes
├── roles/ # Read-only roles + permission catalog routes
└── lib/
├── auth/ # JwtAuthModule (global JwtModule + AuthGuard)
├── decorators/ # @Auth(), @SuperAdmin(), @RequirePermissions(), @User(), @Swagger()
├── guard/ # AuthGuard, SuperAdminGuard, PermissionsGuard, RateLimitGuard
├── mail/ # MailService (SMTP) + HTML templates/ rendered into a shared layout
├── mongo/ # Schemas, generated Zod DTOs (mongo/zod), services, MongoBaseService
├── notifications/ # Web push + NotificationsService fan-out (in-app, push, SMS + email)
├── rabbitmq/ # Delayed notification queue
├── s3/ # Attachment upload/cleanup helpers
├── sms/ # SMS: swappable SmsProvider, Unifonic by default
├── scripts/ # bun sync — Mongoose schema -> Zod DTO generator
├── util/ # Snowflake IDs, sorting, xlsx export, etc.
└── zod/ # ZodValidationPipe, ZodSerializerInterceptor, Swagger converter
Copy the pattern of the existing modules:
- Add a Mongoose schema in
src/lib/mongo/schemas/<name>.schema.ts(seeuser.schema.ts). - Run
bun syncto generate the matching Zod DTO insrc/lib/mongo/zod/<name>.dto.ts. - Add a
Mongo<Name>Service extends MongoBaseService<I<Name>ZodSchema>undersrc/lib/mongo/<name>/, then register the schema + service indatabase.module.tsand export both fromsrc/lib/mongo/index.ts. - Create
src/<name>/with a controller, service, module, and request/response DTOs: a Zod schema (FooDtoSchema) and itsz.infertype (FooDto), wired in with@Body({ schema: FooDtoSchema }). Protect routes with@Auth(),@RequirePermissions(...)or@SuperAdmin().@Auth()always runs its guard first, so a@UseGuards(new RateLimitGuard(...))keys on the user in any decorator order. Pass the response DTO to@Swagger()/@SwaggerPaginated()— that's also what strips undeclared fields from the response. If the route takes asortquery, passparseSortQuerythe list of fields clients may sort by. - Import the new module in
src/app.module.ts.
AGENTS.md (referenced from CLAUDE.md) is the entry point for coding agents — architecture, the
module-adding checklist above in more detail, the auth/audit-log conventions, and pitfalls to avoid.
See .env.example for the full list and process-env.d.ts for types. Only DB_URI,
JWT_ACCESS_SECRET and JWT_REFRESH_SECRET are required to boot; S3, RabbitMQ, SMS, mail and web push
are only needed if you use those features.
CORS: src/main.ts only allows http://localhost:<any port> (CORS_ORIGINS). Add your frontend's origin(s)
there before deploying, or browsers will block its requests.
Request bodies are capped at 8 MB (bodyLimit in src/main.ts) and each attachment at 5 MB (attachment.dto.ts).
Attachments are sent base64-encoded in JSON, which adds about a third to the file size.
Run the API behind a gateway (nginx, Traefik, a load balancer) that sets X-Real-IP to the connecting address,
overwriting any value the client sent. Rate limits, request logs and audit entries read the client IP from that
header, and fall back to the socket address when it is missing.
docker build . produces a multi-stage image that runs dist/main.js as the non-root bun user, with only
production dependencies. The image does not set NODE_ENV or any other variable: supply them from the runtime
environment (--env-file, orchestrator secrets). NODE_ENV=production selects the production database name and
turns Swagger off. Docker's HEALTHCHECK polls /health/live.
- Docker services won't start — make sure Docker Desktop is running, then rerun the setup script;
check
docker compose logs -f/docker compose ps. - DB connection errors — confirm
DB_URImatches the running MongoDB container. - RabbitMQ connection errors — confirm
RMQ_URImatchesamqp://app:app123@localhost:5673/and the delayed-message plugin is enabled:docker exec app-rabbitmq-dev rabbitmq-plugins list | grep delayed. - Swagger not loading — check
NODE_ENV=developmentandDISABLE_SWAGGERisn'ttrue. - Push notifications throwing on boot — outside development,
PushNotificationServicefails fast whenVAPID_PUBLIC_KEY/VAPID_PRIVATE_KEY/VAPID_SUBJECTare missing or invalid; in development it only warns and disables push. Generate a key pair withnpx web-push generate-vapid-keys. - Emails not arriving — without
SMTP_HOST,MailServicelogs emails to the console in development and skips them elsewhere. With it, check the boot lineMail via SMTP host:port (…)and theMailModuleERROR line, which names the failing SMTP command, the reply code and the server's message. A server that doesn't offer STARTTLS is refused unlessSMTP_SECURE=true(port 465) or it's localhost.
MIT © Sarcaster