Skip to content

Commit d76fa8c

Browse files
authored
Initial commit
0 parents  commit d76fa8c

36 files changed

+6572
-0
lines changed

.env.example

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
NODE_ENV="development"
2+
3+
POSTGRES_HOST="localhost"
4+
POSTGRES_PORT="5432" # Needs to match the port in docker-compose.yml
5+
POSTGRES_NAME="mydb"
6+
POSTGRES_USER="username"
7+
POSTGRES_PASSWORD="password"
8+
9+
DATABASE_URL="postgresql://${POSTGRES_USER}:${POSTGRES_PASSWORD}@${POSTGRES_HOST}:${POSTGRES_PORT}/${POSTGRES_NAME}?schema=public"
10+
11+
REDIS_HOST="localhost"
12+
REDIS_PORT="6379" # Needs to match the port in docker-compose.yml
13+
REDIS_USERNAME=""
14+
REDIS_PASSWORD=""
15+
REDIS_URL="redis://${REDIS_USERNAME}:${REDIS_PASSWORD}@${REDIS_HOST}:${REDIS_PORT}"

.eslintrc.js

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
module.exports = {
2+
parser: '@typescript-eslint/parser',
3+
parserOptions: {
4+
project: 'tsconfig.json',
5+
tsconfigRootDir: __dirname,
6+
sourceType: 'module',
7+
},
8+
plugins: ['@typescript-eslint/eslint-plugin'],
9+
extends: [
10+
'plugin:@typescript-eslint/recommended',
11+
'plugin:prettier/recommended',
12+
],
13+
root: true,
14+
env: {
15+
node: true,
16+
jest: true,
17+
},
18+
ignorePatterns: ['.eslintrc.js'],
19+
rules: {
20+
'@typescript-eslint/interface-name-prefix': 'off',
21+
'@typescript-eslint/explicit-function-return-type': 'off',
22+
'@typescript-eslint/explicit-module-boundary-types': 'off',
23+
'@typescript-eslint/no-explicit-any': 'off',
24+
},
25+
};

.github/actions/build/action.yml

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
name: 'Build'
2+
description: 'Sets up repository'
3+
runs:
4+
using: 'composite'
5+
steps:
6+
- name: Check out source code
7+
uses: actions/checkout@v3
8+
- name: Install Node.js
9+
uses: actions/setup-node@v3
10+
- name: Set up pnpm
11+
uses: pnpm/action-setup@v2
12+
with:
13+
version: latest
14+
- name: Install dependencies
15+
shell: bash
16+
run: pnpm install

.github/workflows/deploy.yml

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# name: Deploy
2+
3+
# on:
4+
# workflow_run:
5+
# workflows: ['Tests']
6+
# types:
7+
# - completed
8+
9+
# jobs:
10+
# deploy:
11+
# if: ${{ github.event.workflow_run.conclusion == 'success' && github.ref == 'refs/heads/main' }}
12+
# runs-on: ubuntu-latest
13+
# steps:
14+
# - uses: actions/checkout@v3
15+
# - uses: ./.github/actions/build
16+
# - name: Apply database migrations
17+
# run: pnpx prisma migrate deploy
18+
# env:
19+
# DATABASE_URL: ${{ secrets.DATABASE_URL }}
20+
# - name: Install Railway CLI
21+
# run: pnpm add --global @railway/cli
22+
# - name: Deploy to Railway
23+
# run: railway up --service="nestjs-starter"
24+
# env:
25+
# RAILWAY_TOKEN: ${{ secrets.RAILWAY_TOKEN }}
26+
# REDIS_URL: ${{ secrets.REDIS_URL }}

.github/workflows/test.yml

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
name: Tests
2+
on:
3+
push:
4+
branches:
5+
- main
6+
pull_request:
7+
branches: ['*']
8+
9+
jobs:
10+
unit-tests:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v3
14+
- uses: ./.github/actions/build
15+
- name: Run tests
16+
run: pnpm test
17+
18+
# e2e-tests:
19+
# runs-on: ubuntu-latest
20+
# steps:
21+
# - uses: actions/checkout@v3
22+
# - uses: ./.github/actions/build
23+
# - name: Run tests
24+
# run: pnpm test:e2e
25+
# env:
26+
# POSTGRES_USER: username
27+
# POSTGRES_PASSWORD: password

.gitignore

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
/build
5+
6+
# Logs
7+
logs
8+
*.log
9+
npm-debug.log*
10+
pnpm-debug.log*
11+
yarn-debug.log*
12+
yarn-error.log*
13+
lerna-debug.log*
14+
15+
# OS
16+
.DS_Store
17+
18+
# Tests
19+
/coverage
20+
/.nyc_output
21+
22+
# IDEs and editors
23+
/.idea
24+
.project
25+
.classpath
26+
.c9/
27+
*.launch
28+
.settings/
29+
*.sublime-workspace
30+
31+
# IDE - VSCode
32+
.vscode/*
33+
!.vscode/settings.json
34+
!.vscode/tasks.json
35+
!.vscode/launch.json
36+
!.vscode/extensions.json
37+
38+
# dotenv environment variable files
39+
.env
40+
.env.test
41+
.env.development.local
42+
.env.test.local
43+
.env.production.local
44+
.env.local
45+
46+
# temp directory
47+
.temp
48+
.tmp
49+
50+
# Runtime data
51+
pids
52+
*.pid
53+
*.seed
54+
*.pid.lock
55+
56+
# Diagnostic reports (https://nodejs.org/api/report.html)
57+
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

.prettierrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"singleQuote": true,
3+
"trailingComma": "all"
4+
}

README.md

+140
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
# Starter Kit API Project
2+
3+
This Starter Kit API project serves as a robust foundation for building scalable and maintainable backend applications. It incorporates best practices, essential modules, and configurations to jumpstart your development process.
4+
5+
## Features
6+
7+
- **Enhanced TypeScript Configuration**: Improved `tsconfig.json` for stricter type checking and better developer experience.
8+
- **Environment Configuration**: Utilizes `ConfigModule` for managing environment variables, including Jest setup.
9+
- **Consistent HTTP Responses**: Enforces a standardized structure for all HTTP responses.
10+
- **Basic HTTP Security**: Implements essential security measures to protect your application.
11+
- **Request Validation**: Incorporates whitelisted validation for incoming requests.
12+
- **Advanced Logging**: Utilizes Winston for comprehensive logging capabilities.
13+
- **Database Setup**: Docker Compose configuration for PostgreSQL and Redis.
14+
- **ORM Integration**: Prisma setup for efficient database interactions.
15+
- **Caching Solution**: Redis integration with a CacheService for improved performance.
16+
- **Testing Framework**: Jest configuration with environment variable support.
17+
- **CI Pipeline**: GitHub Actions setup for continuous integration.
18+
19+
## Core Module
20+
21+
The Core Module (`CoreModule`) serves as the central hub for essential application-wide services and configurations. It includes:
22+
23+
- Global exception filters
24+
- Interceptors for response transformation
25+
- Logging service
26+
- Caching service
27+
- Database module
28+
29+
When extending core functionality, consider adding services or providers to this module if they are required application-wide.
30+
31+
## Logging
32+
33+
This starter uses Winston for advanced logging capabilities. The `LoggerService` in the Core Module provides:
34+
35+
- Customizable log formats for different environments
36+
- Log levels (error, warn, info, debug, verbose)
37+
- Context-based logging
38+
- Metadata support for detailed log entries
39+
40+
To use the logger in your services or controllers, inject the `LoggerService` and utilize its methods for consistent logging across your application.
41+
42+
## Getting Started
43+
44+
1. Go to the [Starter Kit API](https://github.com/jfjuanmiguel/starter-kit-api) Github repo:
45+
2. Press the "Use this template" button to create a new repository.
46+
3. Follow the steps to create a new Github repo from the template.
47+
4. Clone the repository on your local machine:
48+
```
49+
git clone https://github.com/YOUR_USERNAME/YOUR_REPO_NAME.git
50+
```
51+
5. Install dependencies:
52+
```
53+
pnpm install
54+
```
55+
6. Set up your environment variables:
56+
Copy `.env.example` to `.env` and fill in the required values.
57+
7. Start the development server:
58+
```
59+
pnpm start:dev
60+
```
61+
62+
The Starter Kit API project has 2 Docker Compose files: In both files, you need to update the name of the containers and networks where it says `# Needs updating`, for example `starter_kit_api` to the name of your project.
63+
64+
```
65+
services:
66+
postgres_starter_kit_api: # Needs updating
67+
image: postgres:alpine
68+
container_name: postgres_starter_kit_api # Needs updating
69+
restart: always
70+
env_file:
71+
- .env
72+
environment:
73+
- POSTGRES_USER=${POSTGRES_USER}
74+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
75+
ports:
76+
- '5432:5432'
77+
volumes:
78+
- postgres_data:/var/lib/postgresql/data
79+
80+
redis_starter_kit_api: # Needs updating
81+
image: redis:alpine
82+
container_name: redis_starter_kit_api # Needs updating
83+
ports:
84+
- '6379:6379'
85+
volumes:
86+
- redis_data:/data
87+
88+
networks:
89+
default:
90+
name: starter_kit_api # Needs updating
91+
92+
volumes:
93+
postgres_data:
94+
redis_data:
95+
```
96+
97+
Make sure you remember to also update the `docker-compose-test.yml` file!
98+
99+
This repo comes with a default `User` model out of the box defined in the `/src/database/schema.prisma` file:
100+
101+
```
102+
model User {
103+
id Int @id @default(autoincrement())
104+
email String @unique
105+
createdAt DateTime @default(now())
106+
updatedAt DateTime @updatedAt
107+
}
108+
```
109+
110+
Before you can run the local server, you need to apply this migration to your local Postgres database.
111+
112+
Make sure on your local machine you don't have any existing Docker containers running that would cause a conflict.
113+
114+
Then spin up the local Postgres and Redis containers:
115+
116+
```
117+
pnpm docker:start
118+
```
119+
120+
Then run this script to apply the migration to your local Postgres database:
121+
122+
```
123+
pnpm db:migrate:dev
124+
```
125+
126+
And you're ready to go!
127+
128+
You can now start the local server:
129+
130+
```
131+
pnpm start:dev
132+
```
133+
134+
## Testing
135+
136+
This project uses Jest for testing. To run the tests, use the following command:
137+
138+
```
139+
pnpm test
140+
```

docker-compose-test.yml

+35
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
services:
2+
postgres_starter_kit_api_test: # Needs updating
3+
image: postgres:alpine
4+
container_name: postgres_starter_kit_api_test # Needs updating
5+
restart: 'no'
6+
env_file:
7+
- .env.test
8+
environment:
9+
- POSTGRES_USER=${POSTGRES_USER}
10+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
11+
ports:
12+
- '5444:5432' # Different port to avoid conflict with the development environment
13+
volumes:
14+
- postgres_data_test:/var/lib/postgresql/data
15+
healthcheck:
16+
test: ['CMD-SHELL', 'pg_isready -U $POSTGRES_USER']
17+
interval: 10s
18+
timeout: 5s
19+
retries: 5
20+
21+
redis_starter_kit_api_test: # Needs updating
22+
image: redis:alpine
23+
container_name: redis_starter_kit_api_test # Needs updating
24+
ports:
25+
- '6380:6379' # Different port to avoid conflict with the development environment
26+
volumes:
27+
- redis_data_test:/data
28+
29+
networks:
30+
default:
31+
name: starter_kit_api_test # Needs updating
32+
33+
volumes:
34+
postgres_data_test:
35+
redis_data_test:

docker-compose.yml

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
services:
2+
postgres_starter_kit_api: # Needs updating
3+
image: postgres:alpine
4+
container_name: postgres_starter_kit_api # Needs updating
5+
restart: always
6+
env_file:
7+
- .env
8+
environment:
9+
- POSTGRES_USER=${POSTGRES_USER}
10+
- POSTGRES_PASSWORD=${POSTGRES_PASSWORD}
11+
ports:
12+
- '5432:5432'
13+
volumes:
14+
- postgres_data:/var/lib/postgresql/data
15+
16+
redis_starter_kit_api: # Needs updating
17+
image: redis:alpine
18+
container_name: redis_starter_kit_api # Needs updating
19+
ports:
20+
- '6379:6379'
21+
volumes:
22+
- redis_data:/data
23+
24+
networks:
25+
default:
26+
name: starter_kit_api # Needs updating
27+
28+
volumes:
29+
postgres_data:
30+
redis_data:

nest-cli.json

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"$schema": "https://json.schemastore.org/nest-cli",
3+
"collection": "@nestjs/schematics",
4+
"sourceRoot": "src",
5+
"compilerOptions": {
6+
"deleteOutDir": true
7+
}
8+
}

0 commit comments

Comments
 (0)