5 |
6 | Robust backend boilerplate designed for scalability, flexibility, and ease of development. It's packed with modern technologies and best practices to kickstart your next backend project.
7 |
8 |
9 |
10 |
11 | ## Prerequisites
12 |
13 | Before you get started, make sure you have the following installed on your machine:
14 |
15 | - **Docker + Docker Compose**
16 | - **PNPM**
17 | - **Node.js 20+ (LTS)**
18 |
19 | ## How to Run
20 |
21 | 1. **Set up Docker Services**:
22 |
23 | - Run the following command to start MongoDB and Redis instances locally:
24 | ```sh
25 | docker compose up -d
26 | ```
27 | 2. **Install Dependencies**:
28 |
29 | - Use pnpm to install all the necessary dependencies:
30 | ```sh
31 | pnpm i
32 | ```
33 | 3. **Configure Environment Variables**:
34 |
35 | - Create a `.env` file in the root directory.
36 | - Use the provided `.env.sample` as a template to enter all the required environment variables.
37 |
38 | ## What's Included
39 |
40 | - **OpenAPI Autogenerated Swagger Docs** : Automatically generated Swagger docs through MagicRouter API and Zod, accessible at `/api-docs`.
41 | - **Auth Module**: Includes Google Sign-In support for easy authentication.
42 | - **User Management**: Comprehensive user management functionality.
43 | - **File Upload**: Handles file uploads with Multer and Amazon S3.
44 | - **Data Validation & Serialization**: Zod is used for validation and serialization of data.
45 | - **Configuration Management**: Managed using dotenv-cli and validated with Zod for accuracy and safety.
46 | - **Middlewares**:
47 | - **Authorization**: Built-in authorization middleware.
48 | - **Zod Schema Validation**: Ensures your API inputs are correctly validated.
49 | - **JWT Extraction**: Easily extract and verify JWT tokens.
50 | - **Type-safe Email Handling**: Emails are managed using React Email and Mailgun for dynamic and flexible email handling.
51 | - **Queues**: Powered by BullMQ with Redis for handling background jobs.
52 | - **ESLint Setup**: Pre-configured ESLint setup for consistent code quality.
53 | ```sh
54 | pnpm run lint
55 | ```
56 | - **Development Server**: Run the server in development mode using ts-node-dev:
57 | ```sh
58 | pnpm run dev
59 | ```
60 | - **Build Process**: Efficiently bundle your project using tsup:
61 | ```sh
62 | pnpm run build
63 | ```
64 | - **PM2 Support**: Out-of-the-box support for PM2 to manage your production processes.
65 |
66 | ## Folder Structure
67 | ```plaintext
68 | ├── build.ts
69 | ├── docker-compose.yml
70 | ├── docs
71 | │ └── EMAIL.md
72 | ├── ecosystem.config.js
73 | ├── eslint.config.mjs
74 | ├── LICENSE
75 | ├── logo.webp
76 | ├── modules.d.ts
77 | ├── package.json
78 | ├── pnpm-lock.yaml
79 | ├── public
80 | │ ├── index.html
81 | │ ├── logo.webp
82 | │ ├── script.js
83 | │ └── styles.css
84 | ├── README.md
85 | ├── src
86 | │ ├── common
87 | │ │ ├── common.schema.ts
88 | │ │ └── common.utils.ts
89 | │ ├── config
90 | │ │ └── config.service.ts
91 | │ ├── email
92 | │ │ ├── email.service.ts
93 | │ │ └── templates
94 | │ │ └── ResetPassword.tsx
95 | │ ├── enums.ts
96 | │ ├── healthcheck
97 | │ │ ├── healthcheck.controller.ts
98 | │ │ └── healthcheck.routes.ts
99 | │ ├── lib
100 | │ │ ├── aws.service.ts
101 | │ │ ├── common.schema.ts
102 | │ │ ├── database.ts
103 | │ │ ├── email.server.ts
104 | │ │ ├── logger.service.ts
105 | │ │ ├── mailgun.server.ts
106 | │ │ ├── queue.server.ts
107 | │ │ ├── realtime.server.ts
108 | │ │ ├── redis.server.ts
109 | │ │ └── session.store.ts
110 | │ ├── main.ts
111 | │ ├── middlewares
112 | │ │ ├── can-access.middleware.ts
113 | │ │ ├── extract-jwt-schema.middleware.ts
114 | │ │ ├── multer-s3.middleware.ts
115 | │ │ └── validate-zod-schema.middleware.ts
116 | │ ├── modules
117 | │ │ ├── auth
118 | │ │ │ ├── auth.constants.ts
119 | │ │ │ ├── auth.controller.ts
120 | │ │ │ ├── auth.router.ts
121 | │ │ │ ├── auth.schema.ts
122 | │ │ │ └── auth.service.ts
123 | │ │ └── user
124 | │ │ ├── user.controller.ts
125 | │ │ ├── user.dto.ts
126 | │ │ ├── user.model.ts
127 | │ │ ├── user.router.ts
128 | │ │ ├── user.schema.ts
129 | │ │ └── user.services.ts
130 | │ ├── openapi
131 | │ │ ├── magic-router.ts
132 | │ │ ├── openapi.utils.ts
133 | │ │ ├── swagger-doc-generator.ts
134 | │ │ ├── swagger-instance.ts
135 | │ │ └── zod-extend.ts
136 | │ ├── queues
137 | │ │ └── email.queue.ts
138 | │ ├── routes
139 | │ │ └── routes.ts
140 | │ ├── types.ts
141 | │ ├── upload
142 | │ │ ├── upload.controller.ts
143 | │ │ └── upload.router.ts
144 | │ └── utils
145 | │ ├── api.utils.ts
146 | │ ├── auth.utils.ts
147 | │ ├── common.utils.ts
148 | │ ├── email.utils.ts
149 | │ ├── getPaginator.ts
150 | │ ├── globalErrorHandler.ts
151 | │ ├── isUsername.ts
152 | │ └── responseInterceptor.ts
153 | └── tsconfig.json
154 | ```
155 |
156 | ## Roadmap
157 |
158 | - **Socket.io Support:** Adding support for Redis adapter and a chat module.
159 | - **Notification Infrastructure**: Notifications via FCM and Novu.
160 | - **Ansible Playbook** : Create an Ansible playbook for server configuration to set up a basic environment quickly and consistently.
161 | - **AWS CDK Support** : Integrate AWS CDK for infrastructure management, making it easier to deploy and manage cloud resources.
162 | - **Monorepo Support** : Implement monorepo architecture using Turborepo and Pnpm for better project organization and scalability.
163 | - **AWS Lambda Support** : Add support for deploying serverless functions on AWS Lambda.
164 | - **Cloudflare Workers Support** : Enable Cloudflare Workers support for edge computing and faster request handling.
165 | - **Containerization with Docker** : Implement containerization to ensure the project can be easily deployed to any environment using Docker.
166 | - **Kubernetes Support** : Integrate Kubernetes for container orchestration, enabling scalable and automated deployment of the application.
167 | - **CI/CD with GitHub Actions** : Implement a CI/CD pipeline using GitHub Actions to automate testing, building, and deployment processes.
168 | - **Testing with Jest**: Add support for unit and integration testing using Jest to ensure code reliability and maintainability.
169 |
170 | ## Contributions
171 |
172 | Feel free to contribute to this project by submitting issues or pull requests. Let's build something amazing together!
173 |
174 | ## **License**
175 |
176 | This project is licensed under the MIT License - see the [LICENSE](./LICENSE) file for details.
177 |
--------------------------------------------------------------------------------
/build.ts:
--------------------------------------------------------------------------------
1 | import { defineConfig } from 'tsup';
2 |
3 | export default defineConfig({
4 | entry: ['src/main.ts'],
5 | splitting: false,
6 | sourcemap: true,
7 | clean: true,
8 | bundle: true,
9 | minify: true,
10 | platform: 'node',
11 | tsconfig: 'tsconfig.json',
12 | keepNames: true,
13 | });
14 |
--------------------------------------------------------------------------------
/docker-compose.yml:
--------------------------------------------------------------------------------
1 | services:
2 | mongo:
3 | image: mongo:5.0.2
4 | restart: 'unless-stopped'
5 | ports:
6 | - '27017:27017'
7 | environment:
8 | MONGO_INITDB_ROOT_USERNAME: root
9 | MONGO_INITDB_ROOT_PASSWORD: example
10 | volumes:
11 | - mongodb_ts_toolkit:/data/db
12 |
13 | redis:
14 | image: redis:latest
15 | ports:
16 | - 6380:6379
17 | volumes:
18 | - redis_ts_toolkit:/data
19 |
20 | volumes:
21 | mongodb_ts_toolkit:
22 | redis_ts_toolkit:
23 |
--------------------------------------------------------------------------------
/docs/EMAIL.md:
--------------------------------------------------------------------------------
1 | # Email Service Documentation
2 |
3 | This document outlines the email service implementation using React Email for templating and Mailgun for delivery.
4 |
5 | ## Overview
6 |
7 | The email service provides a robust, type-safe way to send transactional emails using:
8 | - [React Email](https://react.email/) for building and maintaining email templates
9 | - [Mailgun](https://www.mailgun.com/) for reliable email delivery
10 | - TypeScript for type safety and better developer experience
11 |
12 | ## Configuration
13 |
14 | ### Environment Variables
15 |
16 | Add the following variables to your `.env` file:
17 |
18 | ```env
19 | MAILGUN_API_KEY="your-mailgun-api-key"
20 | MAILGUN_DOMAIN="your-mailgun-domain"
21 | MAILGUN_FROM_EMAIL="noreply@yourdomain.com"
22 | ```
23 |
24 | ## Email Templates
25 |
26 | Email templates are built using React Email components and are located in `src/email/templates/`. Each template is a React component that accepts typed props for the dynamic content.
27 |
28 | ### Available Templates
29 |
30 | 1. **Reset Password Email** (`ResetPassword.tsx`)
31 | ```typescript
32 | interface ResetPasswordEmailProps {
33 | userName: string;
34 | resetLink: string;
35 | }
36 | ```
37 |
38 | ## Usage
39 |
40 | ### Sending Reset Password Email
41 |
42 | ```typescript
43 | import { sendResetPasswordEmail } from '../email/email.service';
44 |
45 | await sendResetPasswordEmail({
46 | email: 'user@example.com',
47 | userName: 'John Doe',
48 | resetLink: 'https://yourdomain.com/reset-password?token=xyz'
49 | });
50 | ```
51 |
52 | ### Creating New Email Templates
53 |
54 | 1. Create a new template in `src/email/templates/`
55 | 2. Use React Email components for consistent styling
56 | 3. Export the template component with proper TypeScript interfaces
57 | 4. Add a new method in `EmailService` class to send the email
58 |
59 | Example:
60 | ```typescript
61 | // src/email/templates/WelcomeEmail.tsx
62 | import * as React from 'react';
63 | import { Button, Container, Head, Html, Preview, Text } from '@react-email/components';
64 |
65 | interface WelcomeEmailProps {
66 | userName: string;
67 | }
68 |
69 | export const WelcomeEmail = ({ userName }: WelcomeEmailProps) => (
70 |
71 |
12 |
13 |
26 |
27 |
28 |
29 |
30 |
31 |
TypeScript Backend Toolkit
32 |
33 | A robust backend boilerplate designed for scalability, flexibility, and ease of development.
34 | Packed with modern technologies and best practices to kickstart your next backend project.
35 |
# Start MongoDB and Redis
55 | docker compose up -d
56 |
57 | # Install dependencies
58 | pnpm install
59 |
60 | # Start development server
61 | pnpm run dev
62 |
63 | 🚀 Server running at http://localhost:3000
64 | 📚 API Docs: http://localhost:3000/api-docs
65 | 📊 Queue Dashboard: http://localhost:3000/admin/queues
66 |
67 |
68 |
69 |
70 |
71 |
What's Included
72 |
73 |
74 |
75 |
OpenAPI Docs
76 |
Auto-generated Swagger documentation through MagicRouter API and Zod for perfect type safety
77 |
78 |
79 |
80 |
Auth Module
81 |
Complete authentication system with Google Sign-In support and JWT handling
82 |
83 |
84 |
85 |
User Management
86 |
Comprehensive user management with role-based access control and profile handling
87 |
88 |
89 |
90 |
File Upload
91 |
Seamless file uploads with Multer and Amazon S3 integration for scalable storage
92 |
93 |
94 |
95 |
Data Validation
96 |
Type-safe data validation and serialization powered by Zod
97 |
98 |
99 |
100 |
Config Management
101 |
Environment configuration with dotenv-cli and Zod validation for type safety
102 |
103 |
104 |
105 |
106 |
107 |
Before You Start
108 |
109 | To ensure a smooth development experience, make sure you have these essential tools installed.
110 | They form the foundation of your development environment and are crucial for running the project efficiently.
111 |
112 |
113 |
114 |
115 |
Docker + Docker Compose
116 |
Required for running MongoDB and Redis services in isolated containers
117 |
118 |
119 |
120 |
PNPM
121 |
Fast, disk space efficient package manager for managing dependencies
122 |
123 |
124 |
125 |
Node.js 20+ (LTS)
126 |
Latest LTS version for optimal performance and security
127 |
128 |
129 |
130 |
131 |
132 |
What's Next
133 |
134 | Our development roadmap outlines exciting features and improvements planned for future releases.
135 | These additions will enhance scalability, developer experience, and deployment options.
136 |
137 |
138 |
139 |
140 |
Real-time Support
141 |
Socket.io integration with Redis adapter for scalable real-time communication
142 |
143 |
144 |
145 |
Advanced Notifications
146 |
FCM & Novu integration for powerful, multi-channel notifications
147 |
148 |
149 |
150 |
Server Automation
151 |
Ansible playbooks for automated server provisioning and configuration
152 |
153 |
154 |
155 |
Cloud Infrastructure
156 |
AWS CDK support for infrastructure as code and easy cloud deployment
157 |
158 |
159 |
160 |
Monorepo Structure
161 |
Turborepo integration for efficient monorepo management
162 |
163 |
164 |
165 |
Serverless Ready
166 |
Support for AWS Lambda and Cloudflare Workers deployment