Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
VanThuan76 committed Dec 27, 2024
0 parents commit 39809c6
Show file tree
Hide file tree
Showing 117 changed files with 12,160 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .commitlintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"extends": ["@commitlint/config-conventional"]
}
6 changes: 6 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.github
.husky
node_modules
npm-debug.log
tests
**/__tests__
8 changes: 8 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
root = true

[*]
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
indent_style = space
indent_size = 2
4 changes: 4 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
DATABASE_URL=postgresql://user:password@localhost:5432/app
LOG_LEVEL=info
NODE_ENV=development
PORT=3000
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/dist/**
/coverage/**
20 changes: 20 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"parser": "@typescript-eslint/parser",
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"plugins": ["@typescript-eslint"],
"rules": {
"@typescript-eslint/no-unused-vars": ["error", { "argsIgnorePattern": "^_" }],
"@typescript-eslint/no-empty-interface": "off",
"no-console": "error"
},
"env": {
"node": true
}
}
45 changes: 45 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
name: CI

on:
pull_request:
push:
branches: [main]

jobs:
build:
runs-on: ubuntu-latest

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Setup node
uses: actions/setup-node@v3
with:
node-version-file: '.nvmrc'
cache: 'npm'

- name: Install dependencies
run: npm install

- name: Lint
run: npm run lint

- name: Create .env file
run: cp .env.example .env

- name: Start functional tests container
run: docker-compose up -d

- name: Test
run: npm test -- --coverage

- name: Stop functional tests container
if: always()
run: docker-compose -f "docker-compose.yml" down

- name: Publish code coverage to Code Climate
if: ${{ github.ref == 'refs/heads/main' }}
uses: paambaati/[email protected]
env:
CC_TEST_REPORTER_ID: ${{ secrets.CODE_CLIMATE_REPORTER_ID }}
10 changes: 10 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
node_modules/
dist/
coverage/
.npm
npm-debug.log*
.env

# Editor files
.idea/
.vscode/
1 change: 1 addition & 0 deletions .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
_
1 change: 1 addition & 0 deletions .husky/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
npx --no -- commitlint --edit $1
4 changes: 4 additions & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#!/bin/sh
. "$(dirname "$0")/_/husky.sh"

npx lint-staged
4 changes: 4 additions & 0 deletions .lintstagedrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"*.ts": ["prettier --write", "eslint --fix"],
"*": "prettier --write --ignore-unknown"
}
1 change: 1 addition & 0 deletions .nvmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
20.11.0
10 changes: 10 additions & 0 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"printWidth": 120,
"semi": true,
"singleQuote": true,
"trailingComma": "all",
"importOrder": ["<THIRD_PARTY_MODULES>", "^[./]"],
"importOrderSeparation": true,
"importOrderSortSpecifiers": true,
"plugins": ["@trivago/prettier-plugin-sort-imports"]
}
18 changes: 18 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
FROM node:20-alpine AS builder
WORKDIR /app
COPY . .
RUN npm pkg delete scripts.prepare
RUN npm install
RUN npm run build

FROM node:20-alpine
WORKDIR /app
ENV NODE_ENV=production
COPY package*.json ./
COPY --from=builder ./app/prisma ./
COPY --from=builder ./app/dist ./dist
RUN npm pkg delete scripts.prepare
RUN npm ci --only=production --quiet

EXPOSE 3000
CMD ["node", "./dist/web/api/server.js"]
76 changes: 76 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# Clean Architecture Template

[![License: MIT](https://img.shields.io/badge/License-MIT-green.svg)](https://github.com/JamieLivingstone/node-clean-architecture/blob/main/LICENSE)
[![Test Coverage](https://api.codeclimate.com/v1/badges/f2520d575fe754ae7032/test_coverage)](https://codeclimate.com/github/JamieLivingstone/node-clean-architecture/test_coverage)
[![Maintainability](https://api.codeclimate.com/v1/badges/f2520d575fe754ae7032/maintainability)](https://codeclimate.com/github/JamieLivingstone/node-clean-architecture/maintainability)
[![Known Vulnerabilities](https://snyk.io/test/github/JamieLivingstone/node-clean-architecture/badge.svg)](https://snyk.io/test/github/JamieLivingstone/node-clean-architecture)

A Node template that follows the Clean Architecture principles and encourages established practices.

## Features

- TypeScript first
- Dependency injection via Awilix
- CQRS (Command Query Responsibility Segregation)
- Automatic API documentation via Swagger
- Built with scalability and performance in mind by using Fastify (see [benchmarks](https://www.fastify.io/benchmarks))
- Built in logging with Pino
- Comprehensive testing including unit and functional tests
- Prisma ORM integration

## Getting Started

1. Install the latest [Node.js LTS](https://nodejs.org/en/)
2. Install Docker and ensure that it is running
3. Create .env file `cp .env.example .env`
4. Install project dependencies by running `npm install`
5. Start Docker (local Postgres instance) `docker-compose up -d`
6. Create database `npx prisma migrate deploy`
7. Start development server `npm run dev`
8. Navigate to Swagger ([http://localhost:3000/api-docs](http://localhost:3000/api-docs))

## Scripts

**Build production bundle**

```
npm run build
```

**Lint project (eslint)**

```
npm run lint
```

**Start development server**

```
npm run dev
```

**Run all tests**

```
npm test
```

**Run unit tests**

```
npm run test:unit
```

**Run functional tests (API tests)**

```
npm run test:functional
```

## License

[This project is licensed with the MIT license.](https://github.com/JamieLivingstone/node-clean-architecture/blob/main/LICENSE)

## Credits

The project's inspiration is [Jason Taylor's clean architecture template](https://github.com/jasontaylordev/CleanArchitecture) for C#.
12 changes: 12 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
version: '3.8'

services:
db:
image: postgres:16-alpine
restart: always
ports:
- '5432:5432'
environment:
POSTGRES_USER: user
POSTGRES_PASSWORD: password
POSTGRES_DB: app
33 changes: 33 additions & 0 deletions jest.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import { Config } from '@jest/types';
import dotenv from 'dotenv';
import { pathsToModuleNameMapper } from 'ts-jest';

import { compilerOptions } from './tsconfig.json';

dotenv.config();

const config: Config.InitialOptions = {
testEnvironment: 'node',
collectCoverageFrom: ['src/**/*.ts', '!src/**/__tests__/*.ts'],
projects: [
{
displayName: 'unit-tests',
testMatch: ['<rootDir>/src/**/*.test.ts'],
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
transform: {
'^.+\\.ts?$': 'ts-jest',
},
},
{
displayName: 'functional-tests',
testMatch: ['<rootDir>/tests/**/*.test.ts'],
testEnvironment: '<rootDir>/prisma/prisma-test-environment.ts',
moduleNameMapper: pathsToModuleNameMapper(compilerOptions.paths, { prefix: '<rootDir>/' }),
transform: {
'^.+\\.ts?$': 'ts-jest',
},
},
],
};

export default config;
Loading

0 comments on commit 39809c6

Please sign in to comment.