Skip to content

Commit 0938c7b

Browse files
ExcaliburExcalibur
Excalibur
authored and
Excalibur
committed
first commit
0 parents  commit 0938c7b

26 files changed

+5573
-0
lines changed

.env.template

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
ENVIRONMENT
2+
DATABASE_NAME
3+
DATABASE_PASSWORD
4+
DATABASE_HOST
5+
DATABASE_PORT
6+
DATABASE_USERNAME
7+
SYNC

.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+
};

.gitignore

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# compiled output
2+
/dist
3+
/node_modules
4+
5+
# Logs
6+
logs
7+
*.log
8+
npm-debug.log*
9+
pnpm-debug.log*
10+
yarn-debug.log*
11+
yarn-error.log*
12+
lerna-debug.log*
13+
14+
# OS
15+
.DS_Store
16+
17+
# Tests
18+
/coverage
19+
/.nyc_output
20+
21+
# IDEs and editors
22+
/.idea
23+
.project
24+
.classpath
25+
.c9/
26+
*.launch
27+
.settings/
28+
*.sublime-workspace
29+
30+
# IDE - VSCode
31+
.vscode/*
32+
!.vscode/settings.json
33+
!.vscode/tasks.json
34+
!.vscode/launch.json
35+
!.vscode/extensions.json
36+
37+
# environments
38+
.env
39+
.env.development

.prettierrc

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

README.md

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<p align="center">
2+
<a href="http://nestjs.com/" target="blank"><img src="https://nestjs.com/img/logo-small.svg" width="200" alt="Nest Logo" /></a>
3+
</p>
4+
5+
## Description
6+
7+
## Installation
8+
9+
```bash
10+
$ yarn install
11+
```
12+
13+
## Running the DB
14+
15+
```bash
16+
# to set up the database
17+
docker-compose up -d
18+
```
19+
20+
## Running the app
21+
22+
```bash
23+
# development
24+
$ yarn start
25+
26+
# watch mode
27+
$ yarn start:dev
28+
```

docker-compose.yml

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
version: '3'
2+
3+
services:
4+
db:
5+
image: postgres:14.3
6+
restart: always
7+
env_file: ./.env
8+
ports:
9+
- "5432:5432"
10+
environment:
11+
POSTGRES_PASSWORD: ${DATABASE_PASSWORD}
12+
POSTGRES_DB: ${DATABASE_NAME}
13+
container_name: BSL

nest-cli.json

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

package.json

+79
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
{
2+
"name": "beestore",
3+
"version": "0.0.1",
4+
"description": "",
5+
"author": "",
6+
"private": true,
7+
"license": "UNLICENSED",
8+
"scripts": {
9+
"prebuild": "rimraf dist",
10+
"build": "nest build",
11+
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
12+
"start": "nest start",
13+
"start:dev": "nest start --watch",
14+
"start:debug": "nest start --debug --watch",
15+
"start:prod": "node dist/main",
16+
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
17+
"test": "jest",
18+
"test:watch": "jest --watch",
19+
"test:cov": "jest --coverage",
20+
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
21+
"test:e2e": "jest --config ./test/jest-e2e.json"
22+
},
23+
"dependencies": {
24+
"@nestjs/common": "^9.0.0",
25+
"@nestjs/config": "^2.2.0",
26+
"@nestjs/core": "^9.0.0",
27+
"@nestjs/mapped-types": "*",
28+
"@nestjs/platform-express": "^9.0.0",
29+
"@nestjs/typeorm": "^9.0.0",
30+
"class-transformer": "^0.5.1",
31+
"class-validator": "^0.13.2",
32+
"joi": "^17.6.0",
33+
"pg": "^8.7.3",
34+
"reflect-metadata": "^0.1.13",
35+
"rimraf": "^3.0.2",
36+
"rxjs": "^7.2.0",
37+
"typeorm": "^0.3.7"
38+
},
39+
"devDependencies": {
40+
"@nestjs/cli": "^9.0.0",
41+
"@nestjs/schematics": "^9.0.0",
42+
"@nestjs/testing": "^9.0.0",
43+
"@types/express": "^4.17.13",
44+
"@types/jest": "28.1.4",
45+
"@types/node": "^16.0.0",
46+
"@types/supertest": "^2.0.11",
47+
"@typescript-eslint/eslint-plugin": "^5.0.0",
48+
"@typescript-eslint/parser": "^5.0.0",
49+
"eslint": "^8.0.1",
50+
"eslint-config-prettier": "^8.3.0",
51+
"eslint-plugin-prettier": "^4.0.0",
52+
"jest": "28.1.2",
53+
"prettier": "^2.3.2",
54+
"source-map-support": "^0.5.20",
55+
"supertest": "^6.1.3",
56+
"ts-jest": "28.0.5",
57+
"ts-loader": "^9.2.3",
58+
"ts-node": "^10.0.0",
59+
"tsconfig-paths": "4.0.0",
60+
"typescript": "^4.3.5"
61+
},
62+
"jest": {
63+
"moduleFileExtensions": [
64+
"js",
65+
"json",
66+
"ts"
67+
],
68+
"rootDir": "src",
69+
"testRegex": ".*\\.spec\\.ts$",
70+
"transform": {
71+
"^.+\\.(t|j)s$": "ts-jest"
72+
},
73+
"collectCoverageFrom": [
74+
"**/*.(t|j)s"
75+
],
76+
"coverageDirectory": "../coverage",
77+
"testEnvironment": "node"
78+
}
79+
}

src/app.controller.spec.ts

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { Test, TestingModule } from '@nestjs/testing';
2+
import { AppController } from './app.controller';
3+
import { AppService } from './app.service';
4+
5+
describe('AppController', () => {
6+
let appController: AppController;
7+
8+
beforeEach(async () => {
9+
const app: TestingModule = await Test.createTestingModule({
10+
controllers: [AppController],
11+
providers: [AppService],
12+
}).compile();
13+
14+
appController = app.get<AppController>(AppController);
15+
});
16+
17+
describe('root', () => {
18+
it('should return "Hello World!"', () => {
19+
expect(appController.getHello()).toBe('Hello World!');
20+
});
21+
});
22+
});

src/app.controller.ts

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import { Controller, Get } from '@nestjs/common';
2+
import { AppService } from './app.service';
3+
4+
@Controller()
5+
export class AppController {
6+
constructor(private readonly appService: AppService) {}
7+
8+
@Get()
9+
getHello(): string {
10+
return this.appService.getHello();
11+
}
12+
}

src/app.module.ts

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import * as Joi from 'joi';
2+
import { Module } from '@nestjs/common';
3+
import { ConfigModule, ConfigService } from '@nestjs/config';
4+
import { TypeOrmModule } from '@nestjs/typeorm';
5+
6+
import { AppController } from './app.controller';
7+
import { AppService } from './app.service';
8+
import { ProductsModule } from './products/products.module';
9+
10+
@Module({
11+
imports: [
12+
ConfigModule.forRoot({
13+
isGlobal: true,
14+
envFilePath: [
15+
'.env',
16+
'.env.development',
17+
'.env.staging',
18+
'.env.test',
19+
'.env.production',
20+
],
21+
validationSchema: Joi.object({
22+
NODE_ENV: Joi.string()
23+
.valid('development', 'production', 'test', 'staging')
24+
.default('development'),
25+
ENVIRONMENT: Joi.string(),
26+
PORT: Joi.number().default(3000),
27+
}),
28+
}),
29+
TypeOrmModule.forRootAsync({
30+
imports: [ConfigModule],
31+
inject: [ConfigService],
32+
useFactory: async (configService: ConfigService) => ({
33+
name: 'default',
34+
type: 'postgres',
35+
host: configService.get('DATABASE_HOST'),
36+
port: configService.get<number>('DATABASE_PORT'),
37+
database: configService.get('DATABASE_NAME'),
38+
username: configService.get('DATABASE_USERNAME'),
39+
password: configService.get('DATABASE_PASSWORD'),
40+
autoLoadEntities: true,
41+
synchronize: configService.get<boolean>('SYNC'),
42+
logging: configService.get('ENVIRONMENT') !== 'production',
43+
}),
44+
}),
45+
ProductsModule,
46+
],
47+
controllers: [AppController],
48+
providers: [AppService],
49+
})
50+
export class AppModule {}

src/app.service.ts

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { Injectable } from '@nestjs/common';
2+
3+
@Injectable()
4+
export class AppService {
5+
getHello(): string {
6+
return 'Hello World!';
7+
}
8+
}

src/config/env.config.ts

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { plainToClass } from 'class-transformer';
2+
import {
3+
IsBoolean,
4+
IsEnum,
5+
IsNumber,
6+
IsString,
7+
validateSync,
8+
} from 'class-validator';
9+
10+
import { Environment } from 'src/utils/enums/environments';
11+
12+
class EnvironmentVariables {
13+
@IsEnum(Environment)
14+
NODE_ENV: Environment;
15+
16+
@IsString()
17+
ENVIRONMENT: string;
18+
@IsNumber()
19+
DATABASE_PORT: number;
20+
@IsString()
21+
DATABASE_NAME: string;
22+
@IsString()
23+
DATABASE_USERNAME: string;
24+
@IsString()
25+
DATABASE_PASSWORD: string;
26+
@IsBoolean()
27+
SYNC: boolean;
28+
}
29+
30+
export function validate(config: Record<string, unknown>) {
31+
const validateConfig = plainToClass(EnvironmentVariables, config);
32+
const errors = validateSync(validateConfig, { skipMissingProperties: false });
33+
34+
if (errors.length > 0) {
35+
throw new Error(errors.toString());
36+
}
37+
return validateConfig;
38+
}

src/main.ts

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { ValidationPipe } from '@nestjs/common';
2+
import { NestFactory } from '@nestjs/core';
3+
import { AppModule } from './app.module';
4+
5+
async function bootstrap() {
6+
const app = await NestFactory.create(AppModule);
7+
8+
app.setGlobalPrefix('api');
9+
10+
app.useGlobalPipes(
11+
new ValidationPipe({
12+
whitelist: true,
13+
forbidNonWhitelisted: true,
14+
}),
15+
);
16+
17+
await app.listen(3000);
18+
}
19+
bootstrap();

0 commit comments

Comments
 (0)