A NestJS-based backend application with Drizzle ORM and Better Auth authentication.
- Framework: NestJS
- Database ORM: Drizzle ORM
- Database: PostgreSQL
- Authentication: Better Auth
- Email Service: SendGrid
- File Storage: AWS S3
- API Documentation: Swagger/OpenAPI
- Language: TypeScript
- Code Quality: ESLint
- Git Hooks: Husky
$ npm installCreate a .env file in the root directory:
# Database Configuration
DB_HOST=localhost
DB_PORT=5432
DB_USER=postgres
DB_PASSWORD=admin
DB_NAME=paymentDb
# Application Configuration
PORT=3000
# Better Auth Configuration
BETTER_AUTH_SECRET=your-secret-key-here-change-in-production
BETTER_AUTH_URL=http://localhost:3000
# SendGrid Configuration
SENDGRID_API_KEY=your-sendgrid-api-key-here
SENDGRID_FROM_EMAIL=noreply@yourdomain.com
SENDGRID_FROM_NAME=Your App Name
# AWS S3 Configuration
S3_ACCESS_KEY_ID=your-aws-access-key-id
S3_SECRET_ACCESS_KEY=your-aws-secret-access-key
S3_AWS_REGION=us-east-1
S3_BUCKET_NAME=your-bucket-nameEnsure PostgreSQL is running and the database exists. Then run migrations:
# Generate migrations from schema
$ npm run db:generate
# Run migrations
$ npm run db:migrate
# Or push schema directly (development only)
$ npm run db:push# development
$ npm run start
# watch mode
$ npm run start:dev
# production mode
$ npm run start:prod# Generate migration from schema changes
$ npm run db:generate
# Run pending migrations
$ npm run db:migrate
# Push schema directly to database (dev only)
$ npm run db:push
# Open Drizzle Studio (visual database manager)
$ npm run db:studioThis project includes interactive API documentation powered by Swagger/OpenAPI.
Once the application is running, you can access the Swagger documentation at:
http://localhost:3000/api
The Swagger UI provides:
- Interactive API testing - Try out endpoints directly in the browser
- Request/Response schemas - View detailed data structures with examples
- Authentication flows - Test protected endpoints
- Automatic schema validation - See required fields and data types
- Full API endpoint documentation with examples
- Request and response body schemas with sample values
- Parameter descriptions and validation rules
- HTTP status code documentation
- Try-it-out functionality for testing endpoints
Note: For complete, interactive API documentation with request/response examples, visit the Swagger UI at
http://localhost:3000/api
All auth endpoints are available at /api/auth/*:
POST /api/auth/sign-up- Register new userPOST /api/auth/sign-in- LoginPOST /api/auth/sign-out- Logout- And more...
POST /product- Create a productGET /product- Get all productsGET /product/:id- Get product by IDPATCH /product/:id- Update productDELETE /product/:id- Delete product (hard delete)DELETE /product/:id/soft- Soft delete product
All product endpoints include full Swagger documentation with:
- Request body examples with sample data
- Response schemas with all fields
- Parameter validation rules
- HTTP status codes and error responses
The application includes email functionality powered by SendGrid:
- Transactional emails
- Template-based emails
- Email notifications
To use the email service:
- Sign up for a SendGrid account
- Create an API key in your SendGrid dashboard
- Add the API key and sender email to your
.envfile - Use the
EmailServicein your controllers and services
The application includes AWS S3 integration for file storage and management, powered by AWS SDK v3.
- File Upload: Upload single or multiple files to S3
- Public & Private Files: Support for both public and private file access
- Pre-signed URLs: Generate temporary signed URLs for private files (24-hour expiration)
- Metadata Storage: Store original filename, file size, and custom metadata
- UUID Naming: Automatic unique file naming using UUID v4
- Content Type Detection: Automatic MIME type handling
-
Create an AWS Account
- Sign up at AWS Console
-
Create an S3 Bucket
- Go to S3 service in AWS Console
- Click "Create bucket"
- Choose a unique bucket name
- Select your preferred region
- Configure bucket settings (public access, versioning, etc.)
-
Create IAM User with S3 Permissions
- Go to IAM service in AWS Console
- Create a new user with programmatic access
- Attach the
AmazonS3FullAccesspolicy (or create a custom policy with specific permissions) - Save the Access Key ID and Secret Access Key
-
Configure Environment Variables
- Add your AWS credentials to the
.envfile:
S3_ACCESS_KEY_ID=your-access-key-id S3_SECRET_ACCESS_KEY=your-secret-access-key S3_AWS_REGION=us-east-1 S3_BUCKET_NAME=your-bucket-name
- Add your AWS credentials to the
The S3Service provides methods for file operations:
import { S3Service } from '@/services/s3/s3.service';
// Upload a public file
const result = await s3Service.uploadSingle({
file: uploadedFile,
isPublic: true,
});
// Returns: { url, key, isPublic }
// Upload a private file
const result = await s3Service.uploadSingle({
file: uploadedFile,
isPublic: false,
});
// Returns: { url: presignedUrl, key, isPublic }
// Get public file URL
const url = s3Service.getFileUrl(fileKey);
// Get pre-signed URL for private file (valid for 24 hours)
const presignedUrl = await s3Service.getPresignedSignedUrl(fileKey);{
"url": "https://your-bucket.s3.amazonaws.com/uuid-key or presigned-url",
"key": "unique-uuid-v4-key",
"isPublic": true
}- Never commit AWS credentials to version control
- Use IAM roles with minimal required permissions
- Enable S3 bucket versioning for file recovery
- Configure CORS settings if accessing from frontend
- Use private buckets by default and generate pre-signed URLs when needed
- Regularly rotate access keys
- Enable S3 bucket logging for audit trails
# unit tests
$ npm run test
# e2e tests
$ npm run test:e2e
# test coverage
$ npm run test:covThis project uses ESLint for code linting and Husky for Git hooks to maintain code quality.
ESLint is configured to enforce code standards and catch potential issues:
# Run ESLint to check code
$ npm run lint
# Auto-fix ESLint issues
$ npm run lint:fixHusky is configured to run pre-commit and pre-push hooks automatically, ensuring:
- Code is linted before commits
- Tests pass before pushing
- Code quality standards are maintained across the team
The Git hooks are automatically installed when you run npm install.
When you're ready to deploy your NestJS application to production, there are some key steps you can take to ensure it runs as efficiently as possible. Check out the deployment documentation for more information.
If you are looking for a cloud-based platform to deploy your NestJS application, check out Mau, our official platform for deploying NestJS applications on AWS. Mau makes deployment straightforward and fast, requiring just a few simple steps:
$ npm install -g mau
$ mau deployWith Mau, you can deploy your application in just a few clicks, allowing you to focus on building features rather than managing infrastructure.
Check out a few resources that may come in handy when working with NestJS:
- Visit the NestJS Documentation to learn more about the framework.
- For questions and support, please visit our Discord channel.
- To dive deeper and get more hands-on experience, check out our official video courses.
- Deploy your application to AWS with the help of NestJS Mau in just a few clicks.
- Visualize your application graph and interact with the NestJS application in real-time using NestJS Devtools.
- Need help with your project (part-time to full-time)? Check out our official enterprise support.
- To stay in the loop and get updates, follow us on X and LinkedIn.
- Looking for a job, or have a job to offer? Check out our official Jobs board.
Nest is an MIT-licensed open source project. It can grow thanks to the sponsors and support by the amazing backers. If you'd like to join them, please read more here.
- Author - Kamil Myśliwiec
- Website - https://nestjs.com
- Twitter - @nestframework
Nest is MIT licensed.