Init project. Create simple backend. Add postgres in docker

This commit is contained in:
2025-03-16 11:24:14 +04:00
commit bcc5d1daf4
44 changed files with 18464 additions and 0 deletions

6
backend/.env.example Normal file
View File

@@ -0,0 +1,6 @@
DATABASE_TYPE=postgres
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_USER=root
DATABASE_PASSWORD=root
DATABASE_DATABASE=test

56
backend/.gitignore vendored Normal file
View File

@@ -0,0 +1,56 @@
# compiled output
/dist
/node_modules
/build
# Logs
logs
*.log
npm-debug.log*
pnpm-debug.log*
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
# OS
.DS_Store
# Tests
/coverage
/.nyc_output
# IDEs and editors
/.idea
.project
.classpath
.c9/
*.launch
.settings/
*.sublime-workspace
# IDE - VSCode
.vscode/*
!.vscode/settings.json
!.vscode/tasks.json
!.vscode/launch.json
!.vscode/extensions.json
# dotenv environment variable files
.env
.env.development.local
.env.test.local
.env.production.local
.env.local
# temp directory
.temp
.tmp
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Diagnostic reports (https://nodejs.org/api/report.html)
report.[0-9]*.[0-9]*.[0-9]*.[0-9]*.json

5
backend/.prettierrc Normal file
View File

@@ -0,0 +1,5 @@
{
"singleQuote": true,
"trailingComma": "all",
"endOfLine": "auto"
}

36
backend/eslint.config.mjs Normal file
View File

@@ -0,0 +1,36 @@
// @ts-check
import eslint from '@eslint/js';
import eslintPluginPrettierRecommended from 'eslint-plugin-prettier/recommended';
import globals from 'globals';
import tseslint from 'typescript-eslint';
export default tseslint.config(
{
ignores: ['eslint.config.mjs'],
},
eslint.configs.recommended,
...tseslint.configs.recommendedTypeChecked,
eslintPluginPrettierRecommended,
{
languageOptions: {
globals: {
...globals.node,
...globals.jest,
},
ecmaVersion: 5,
sourceType: 'module',
parserOptions: {
projectService: true,
tsconfigRootDir: import.meta.dirname,
},
},
},
{
rules: {
'@typescript-eslint/no-explicit-any': 'off',
'@typescript-eslint/no-floating-promises': 'warn',
'@typescript-eslint/no-unsafe-argument': 'warn',
'prettier/prettier': 0,
},
},
);

8
backend/nest-cli.json Normal file
View File

@@ -0,0 +1,8 @@
{
"$schema": "https://json.schemastore.org/nest-cli",
"collection": "@nestjs/schematics",
"sourceRoot": "src",
"compilerOptions": {
"deleteOutDir": true
}
}

12016
backend/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

82
backend/package.json Normal file
View File

@@ -0,0 +1,82 @@
{
"name": "chat",
"version": "0.0.1",
"description": "",
"author": "",
"private": true,
"license": "UNLICENSED",
"scripts": {
"build": "nest build",
"format": "prettier --write \"src/**/*.ts\" \"test/**/*.ts\"",
"start": "nest start",
"start:dev": "nest start --watch",
"start:debug": "nest start --debug --watch",
"start:prod": "node dist/main",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
"test:cov": "jest --coverage",
"test:debug": "node --inspect-brk -r tsconfig-paths/register -r ts-node/register node_modules/.bin/jest --runInBand",
"test:e2e": "jest --config ./test/jest-e2e.json"
},
"dependencies": {
"@nestjs/common": "^11.0.1",
"@nestjs/config": "^4.0.1",
"@nestjs/core": "^11.0.1",
"@nestjs/platform-express": "^11.0.1",
"@nestjs/swagger": "^11.0.6",
"@nestjs/typeorm": "^11.0.0",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"mysql2": "^3.13.0",
"pg": "^8.14.0",
"reflect-metadata": "^0.2.2",
"rxjs": "^7.8.1",
"swagger-ui-express": "^5.0.1",
"typeorm": "^0.3.21"
},
"devDependencies": {
"@eslint/eslintrc": "^3.2.0",
"@eslint/js": "^9.18.0",
"@nestjs/cli": "^11.0.0",
"@nestjs/schematics": "^11.0.0",
"@nestjs/testing": "^11.0.1",
"@swc/cli": "^0.6.0",
"@swc/core": "^1.10.7",
"@types/express": "^5.0.0",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.7",
"@types/supertest": "^6.0.2",
"eslint": "^9.18.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-prettier": "^5.2.2",
"globals": "^16.0.0",
"jest": "^29.7.0",
"prettier": "^3.4.2",
"source-map-support": "^0.5.21",
"supertest": "^7.0.0",
"ts-jest": "^29.2.5",
"ts-loader": "^9.5.2",
"ts-node": "^10.9.2",
"tsconfig-paths": "^4.2.0",
"typescript": "^5.7.3",
"typescript-eslint": "^8.20.0"
},
"jest": {
"moduleFileExtensions": [
"js",
"json",
"ts"
],
"rootDir": "src",
"testRegex": ".*\\.spec\\.ts$",
"transform": {
"^.+\\.(t|j)s$": "ts-jest"
},
"collectCoverageFrom": [
"**/*.(t|j)s"
],
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}

View File

@@ -0,0 +1,11 @@
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
import { IDatabaseConfigKey } from './database';
const config = {};
export type IConfig = typeof config & {
[k in IDatabaseConfigKey]: TypeOrmModuleOptions;
};
const getConfig = () => config;
export default getConfig;

View File

@@ -0,0 +1,19 @@
import { registerAs } from '@nestjs/config';
import { TypeOrmModuleOptions } from '@nestjs/typeorm';
export const DatabaseConfigKey = 'database';
export type IDatabaseConfigKey = typeof DatabaseConfigKey;
export default registerAs(
DatabaseConfigKey,
(): TypeOrmModuleOptions => ({
type: 'postgres',
host: process.env.DATABASE_HOST,
port: +(process.env.DATABASE_PORT ?? 5432),
username: process.env.DATABASE_USER,
password: process.env.DATABASE_PASSWORD,
database: process.env.DATABASE_DATABASE,
synchronize: process.env.NODE_ENV !== 'production',
autoLoadEntities: true,
}),
);

View File

@@ -0,0 +1,3 @@
import { MessageController } from './message.controller';
export default MessageController;

View File

@@ -0,0 +1,18 @@
import Message, { CreateMessageDTO } from '@/entities/message';
import MessageService from '@/shared/services/message';
import { Body, Controller, Get, Post } from '@nestjs/common';
@Controller('message')
export class MessageController {
constructor(private messageService: MessageService) {}
@Get()
async getTopOfHistory(): Promise<Message[]> {
return await this.messageService.getTopOfHistory();
}
@Post()
async send(@Body() message: CreateMessageDTO): Promise<Message> {
return await this.messageService.addMessage(message);
}
}

View File

@@ -0,0 +1,4 @@
import { Message } from './message';
export default Message;
export { CreateMessageDTO } from './types';

View File

@@ -0,0 +1,13 @@
import { Entity, Column, PrimaryGeneratedColumn } from 'typeorm';
@Entity()
export class Message {
@PrimaryGeneratedColumn()
id: number;
@Column()
timeOfSend: string;
@Column()
sender: string;
}

View File

@@ -0,0 +1,12 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsNotEmpty } from 'class-validator';
export class CreateMessageDTO {
@ApiProperty()
@IsNotEmpty()
public timeOfSend: string;
@ApiProperty()
@IsNotEmpty()
public sender: string;
}

22
backend/src/main.ts Normal file
View File

@@ -0,0 +1,22 @@
import { NestFactory } from '@nestjs/core';
import { SwaggerModule, DocumentBuilder } from '@nestjs/swagger';
import { ValidationPipe } from '@nestjs/common';
import AppModule from './modules/app';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useGlobalPipes(new ValidationPipe());
const options = new DocumentBuilder()
.setTitle('Chat')
.setDescription(
'Chat - Asynchronous chat on WebSockets written on Next+Nest',
)
.build();
const document = SwaggerModule.createDocument(app, options);
SwaggerModule.setup('docs', app, document);
await app.listen(process.env.PORT ?? 8000);
}
bootstrap();

View File

@@ -0,0 +1,20 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { ConfigModule } from '@nestjs/config';
import config from '@/configuration/configuration';
import databaseConfig from '@/configuration/database';
import MessageModule from '../message';
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
load: [config],
}),
TypeOrmModule.forRoot(databaseConfig()),
MessageModule,
],
})
export class AppModule {}

View File

@@ -0,0 +1,3 @@
import { AppModule } from './app.module';
export default AppModule;

View File

@@ -0,0 +1,3 @@
import { MessageModule } from './message.module';
export default MessageModule;

View File

@@ -0,0 +1,12 @@
import MessageController from '@/controllers/message';
import Message from '@/entities/message';
import MessageService from '@/shared/services/message';
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
@Module({
imports: [TypeOrmModule.forFeature([Message])],
controllers: [MessageController],
providers: [MessageService],
})
export class MessageModule {}

View File

@@ -0,0 +1,3 @@
import { MessageService } from './message.service';
export default MessageService;

View File

@@ -0,0 +1,22 @@
import Message, { CreateMessageDTO } from '@/entities/message';
import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
@Injectable()
export class MessageService {
constructor(
@InjectRepository(Message)
private messageRepository: Repository<Message>,
) {}
getTopOfHistory = () =>
this.messageRepository
.createQueryBuilder('message')
.orderBy('message.id', 'DESC')
.limit(20)
.getMany();
addMessage = (message: CreateMessageDTO): Promise<Message> =>
this.messageRepository.save(message);
}

View File

@@ -0,0 +1,4 @@
{
"extends": "./tsconfig.json",
"exclude": ["node_modules", "test", "dist", "**/*spec.ts"]
}

24
backend/tsconfig.json Normal file
View File

@@ -0,0 +1,24 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2023",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noImplicitAny": false,
"strictBindCallApply": false,
"noFallthroughCasesInSwitch": false,
"paths": {
"@/*": ["src/*"],
}
}
}