Refactor code structure for improved readability and maintainability

This commit is contained in:
Boris D
2025-09-09 13:28:39 +03:00
parent 5572bf48b7
commit afb1f343e0
23 changed files with 4239 additions and 0 deletions

21
.dockerignore Normal file
View File

@ -0,0 +1,21 @@
node_modules
npm-debug.log
yarn-error.log
yarn-debug.log
dist
.git
.gitignore
README.md
.env
.env.*
!.env.example
Dockerfile
.dockerignore
docker-compose*.yml
coverage
.nyc_output
.cache
.vscode
.idea
*.log
logs

15
.env example.docker Normal file
View File

@ -0,0 +1,15 @@
# Docker Environment Configuration
NODE_ENV=development
# Application Configuration
APP_PORT=3000
# Database Configuration
DB_HOST=mariadb
DB_PORT=3306
DB_USERNAME=app_user
DB_PASSWORD=app_password
DB_DATABASE=low_code_engine
DB_ROOT_PASSWORD=rootpassword
DB_PORT=3309

9
.env.example Normal file
View File

@ -0,0 +1,9 @@
# Database Configuration
DB_HOST=localhost
DB_PORT=3306
DB_USERNAME=root
DB_PASSWORD=your_password_here
DB_DATABASE=low_code_engine
# Application Configuration
NODE_ENV=development

58
.eslintignore Normal file
View File

@ -0,0 +1,58 @@
# Dependencies
node_modules/
# Build outputs
dist/
build/
# Environment files
.env
.env.local
.env.production
.env.test
# Logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Coverage directory used by tools like istanbul
coverage/
# nyc test coverage
.nyc_output
# ESLint cache
.eslintcache
# Optional npm cache directory
.npm
# Optional REPL history
.node_repl_history
# Output of 'npm pack'
*.tgz
# Yarn Integrity file
.yarn-integrity
# dotenv environment variables file
.env
# IDE files
.vscode/
.idea/
*.swp
*.swo
*~
# OS generated files
.DS_Store
.DS_Store?
._*
.Spotlight-V100
.Trashes
ehthumbs.db
Thumbs.db

28
.eslintrc.js Normal file
View File

@ -0,0 +1,28 @@
module.exports = {
parser: "@typescript-eslint/parser",
parserOptions: {
project: "tsconfig.json",
tsconfigRootDir: __dirname,
sourceType: "module",
},
plugins: ["@typescript-eslint/eslint-plugin"],
extends: [
"eslint:recommended",
"plugin:@typescript-eslint/recommended",
"plugin:prettier/recommended",
],
root: true,
env: {
node: true,
jest: true,
},
ignorePatterns: [".eslintrc.js"],
rules: {
"@typescript-eslint/interface-name-prefix": "off",
"@typescript-eslint/explicit-function-return-type": "off",
"@typescript-eslint/explicit-module-boundary-types": "off",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-unused-vars": ["error", { argsIgnorePattern: "^_" }],
"@typescript-eslint/no-inferrable-types": "off",
},
};

36
.gitignore vendored Normal file
View File

@ -0,0 +1,36 @@
node_modules
# Environment variables
.env
.env.local
.env.development.local
.env.test.local
.env.production.local
# Database
*.db
*.sqlite
# Docker
.env.docker
docker-compose.override.yml
# Logs
logs
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# Runtime data
pids
*.pid
*.seed
*.pid.lock
# Coverage directory used by tools like istanbul
coverage
# Build outputs
dist/
build/

3
.vscode/extensions.json vendored Normal file
View File

@ -0,0 +1,3 @@
{
"recommendations": ["dbaeumer.vscode-eslint", "esbenp.prettier-vscode"]
}

22
.vscode/settings.json vendored Normal file
View File

@ -0,0 +1,22 @@
{
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
},
"editor.formatOnSave": true,
"editor.defaultFormatter": "esbenp.prettier-vscode",
"[typescript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
},
"[javascript]": {
"editor.defaultFormatter": "esbenp.prettier-vscode",
"editor.codeActionsOnSave": {
"source.fixAll.eslint": "explicit"
}
},
"eslint.validate": ["javascript", "typescript"],
"eslint.format.enable": true,
"eslint.lintTask.enable": true
}

64
Dockerfile Normal file
View File

@ -0,0 +1,64 @@
# Multi-stage build for production optimization
FROM node:18-alpine AS development
# Set working directory
WORKDIR /usr/src/app
# Copy package files
COPY package*.json yarn.lock ./
# Install dependencies
RUN yarn install --frozen-lockfile
# Copy source code
COPY . .
# Expose port
EXPOSE 3000
# Development command
CMD ["yarn", "start:dev"]
# Production build stage
FROM node:18-alpine AS build
WORKDIR /usr/src/app
# Copy package files
COPY package*.json yarn.lock ./
# Install dependencies
RUN yarn install --frozen-lockfile
# Copy source code
COPY . .
# Build the application
RUN yarn build
# Production stage
FROM node:18-alpine AS production
WORKDIR /usr/src/app
# Copy package files
COPY package*.json yarn.lock ./
# Install only production dependencies
RUN yarn install --frozen-lockfile --production && yarn cache clean
# Copy built application
COPY --from=build /usr/src/app/dist ./dist
# Create non-root user
RUN addgroup -g 1001 -S nodejs
RUN adduser -S nestjs -u 1001
# Change ownership of the app directory
USER nestjs
# Expose port
EXPOSE 3000
# Production command
CMD ["node", "dist/main"]

49
MIGRATIONS.md Normal file
View File

@ -0,0 +1,49 @@
# TypeORM Migrations Guide
This project uses TypeORM migrations for database schema management in production environments.
## Migration Commands
### Generate Migration
Generate a migration file based on entity changes:
```bash
# Start the database first
yarn docker:dev:detached
# Generate migration from entity changes
yarn migration:generate src/migrations/YourMigrationName
```
### Create Empty Migration
Create an empty migration file for custom SQL:
```bash
yarn migration:create src/migrations/YourMigrationName
```
### Run Migrations
Apply pending migrations to the database:
```bash
yarn migration:run
```
### Revert Migration
Revert the last applied migration:
```bash
yarn migration:revert
```
### Show Migration Status
Show which migrations have been applied:
```bash
yarn migration:show
```

70
docker-compose.yml Normal file
View File

@ -0,0 +1,70 @@
version: "3.8"
services:
# MariaDB Database
mariadb:
image: mariadb:10.11
container_name: low-code-engine-mariadb
restart: unless-stopped
environment:
MYSQL_ROOT_PASSWORD: ${DB_ROOT_PASSWORD:-rootpassword}
MYSQL_DATABASE: ${DB_DATABASE:-low_code_engine}
MYSQL_USER: ${DB_USERNAME:-app_user}
MYSQL_PASSWORD: ${DB_PASSWORD:-app_password}
ports:
- "${DB_PORT:-3306}:3306"
volumes:
- mariadb_data:/var/lib/mysql
- ./docker/mariadb/init:/docker-entrypoint-initdb.d
networks:
- app-network
healthcheck:
test:
[
"CMD",
"mysqladmin",
"ping",
"-h",
"localhost",
"-u",
"root",
"-p${DB_ROOT_PASSWORD:-rootpassword}",
]
timeout: 5s
retries: 10
interval: 10s
# NestJS Application
app:
build:
context: .
dockerfile: Dockerfile
target: development
container_name: low-code-engine-app
restart: unless-stopped
environment:
NODE_ENV: ${NODE_ENV:-development}
DB_HOST: mariadb
DB_PORT: 3306
DB_USERNAME: ${DB_USERNAME:-app_user}
DB_PASSWORD: ${DB_PASSWORD:-app_password}
DB_DATABASE: ${DB_DATABASE:-low_code_engine}
ports:
- "${APP_PORT:-3000}:3000"
volumes:
- .:/usr/src/app
- /usr/src/app/node_modules
networks:
- app-network
depends_on:
mariadb:
condition: service_healthy
command: yarn start:dev
volumes:
mariadb_data:
driver: local
networks:
app-network:
driver: bridge

View File

@ -0,0 +1,12 @@
-- Initial database setup for Low Code Engine
-- This script runs when the MariaDB container starts for the first time
-- Ensure the database exists
CREATE DATABASE IF NOT EXISTS low_code_engine CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
-- Grant privileges to the application user
GRANT ALL PRIVILEGES ON low_code_engine.* TO 'app_user'@'%';
FLUSH PRIVILEGES;
-- You can add initial data here if needed
-- INSERT INTO users (email, firstName, lastName) VALUES ('admin@example.com', 'Admin', 'User');

8
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
}
}

60
package.json Normal file
View File

@ -0,0 +1,60 @@
{
"name": "low-code-engine",
"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",
"lint:check": "eslint \"{src,apps,libs,test}/**/*.ts\"",
"typeorm": "typeorm-ts-node-commonjs",
"migration:generate": "npm run typeorm -- -d src/data-source.ts migration:generate",
"migration:create": "npm run typeorm -- migration:create",
"migration:run": "npm run typeorm -- -d src/data-source.ts migration:run",
"migration:revert": "npm run typeorm -- -d src/data-source.ts migration:revert",
"migration:show": "npm run typeorm -- -d src/data-source.ts migration:show",
"docker:dev": "./docker.sh dev",
"docker:dev:detached": "./docker.sh dev-detached",
"docker:prod": "./docker.sh prod",
"docker:stop": "./docker.sh stop",
"docker:clean": "./docker.sh clean",
"docker:logs": "./docker.sh logs",
"docker:shell": "./docker.sh shell"
},
"dependencies": {
"@nestjs/common": "^9.0.0",
"@nestjs/config": "^4.0.2",
"@nestjs/core": "^9.0.0",
"@nestjs/platform-express": "^9.0.0",
"@nestjs/typeorm": "^11.0.0",
"mariadb": "^3.4.5",
"mysql": "^2.18.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.2.0",
"typeorm": "^0.3.26"
},
"devDependencies": {
"@nestjs/cli": "^9.0.0",
"@nestjs/schematics": "^9.0.0",
"@types/express": "^4.17.13",
"@types/node": "^18.0.0",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"eslint": "^8.0.1",
"eslint-config-prettier": "^8.3.0",
"eslint-plugin-prettier": "^4.0.0",
"prettier": "^2.3.2",
"source-map-support": "^0.5.20",
"ts-loader": "^9.2.3",
"ts-node": "^10.0.0",
"tsconfig-paths": "^4.1.0",
"typescript": "^4.7.4"
}
}

31
src/app/app.module.ts Normal file
View File

@ -0,0 +1,31 @@
import { Module } from "@nestjs/common";
import { TypeOrmModule } from "@nestjs/typeorm";
import { ConfigModule } from "@nestjs/config";
import { User } from "../entities/user.entity";
import { UserService } from "../services/user.service";
@Module({
imports: [
ConfigModule.forRoot({
isGlobal: true,
envFilePath: ".env",
}),
TypeOrmModule.forRoot({
type: "mariadb",
host: process.env.DB_HOST || "localhost",
port: parseInt(process.env.DB_PORT) || 3306,
username: process.env.DB_USERNAME || "root",
password: process.env.DB_PASSWORD || "",
database: process.env.DB_DATABASE || "low_code_engine",
entities: [__dirname + "/../**/*.entity{.ts,.js}"],
migrations: [__dirname + "/../migrations/*{.ts,.js}"],
synchronize: process.env.NODE_ENV === "development",
migrationsRun: process.env.NODE_ENV === "production",
autoLoadEntities: true,
}),
TypeOrmModule.forFeature([User]),
],
controllers: [],
providers: [UserService],
})
export class AppModule {}

18
src/data-source.ts Normal file
View File

@ -0,0 +1,18 @@
import { DataSource } from "typeorm";
import { config } from "dotenv";
// Load environment variables
config();
export default new DataSource({
type: "mariadb",
host: process.env.DB_HOST || "localhost",
port: parseInt(process.env.DB_PORT) || 3306,
username: process.env.DB_USERNAME || "root",
password: process.env.DB_PASSWORD || "",
database: process.env.DB_DATABASE || "low_code_engine",
entities: ["src/**/*.entity{.ts,.js}"],
migrations: ["src/migrations/*{.ts,.js}"],
synchronize: false,
logging: false,
});

View File

@ -0,0 +1,31 @@
import {
Entity,
PrimaryGeneratedColumn,
Column,
CreateDateColumn,
UpdateDateColumn,
} from "typeorm";
@Entity("users")
export class User {
@PrimaryGeneratedColumn()
id: number;
@Column({ unique: true })
email: string;
@Column()
firstName: string;
@Column()
lastName: string;
@Column({ default: true })
isActive: boolean;
@CreateDateColumn()
createdAt: Date;
@UpdateDateColumn()
updatedAt: Date;
}

9
src/main.ts Normal file
View File

@ -0,0 +1,9 @@
import { NestFactory } from "@nestjs/core";
import { AppModule } from "./app/app.module";
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(3000);
}
bootstrap();

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class Init1757413588655 implements MigrationInterface {
name = 'Init1757413588655'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`CREATE TABLE \`users\` (\`id\` int NOT NULL AUTO_INCREMENT, \`email\` varchar(255) NOT NULL, \`firstName\` varchar(255) NOT NULL, \`lastName\` varchar(255) NOT NULL, \`isActive\` tinyint NOT NULL DEFAULT 1, \`createdAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6), \`updatedAt\` datetime(6) NOT NULL DEFAULT CURRENT_TIMESTAMP(6) ON UPDATE CURRENT_TIMESTAMP(6), UNIQUE INDEX \`IDX_97672ac88f789774dd47f7c8be\` (\`email\`), PRIMARY KEY (\`id\`)) ENGINE=InnoDB`);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX \`IDX_97672ac88f789774dd47f7c8be\` ON \`users\``);
await queryRunner.query(`DROP TABLE \`users\``);
}
}

View File

@ -0,0 +1,34 @@
import { Injectable } from "@nestjs/common";
import { InjectRepository } from "@nestjs/typeorm";
import { Repository } from "typeorm";
import { User } from "../entities/user.entity";
@Injectable()
export class UserService {
constructor(
@InjectRepository(User)
private userRepository: Repository<User>
) {}
async findAll(): Promise<User[]> {
return this.userRepository.find();
}
async findOne(id: number): Promise<User> {
return this.userRepository.findOne({ where: { id } });
}
async create(userData: Partial<User>): Promise<User> {
const user = this.userRepository.create(userData);
return this.userRepository.save(user);
}
async update(id: number, userData: Partial<User>): Promise<User> {
await this.userRepository.update(id, userData);
return this.findOne(id);
}
async remove(id: number): Promise<void> {
await this.userRepository.delete(id);
}
}

11
tsconfig.build.json Normal file
View File

@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"outDir": "./dist",
"module": "commonjs",
"types": ["node"],
"allowJs": true
},
"include": ["src/**/*"],
"exclude": ["node_modules", "dist", "test"]
}

21
tsconfig.json Normal file
View File

@ -0,0 +1,21 @@
{
"compilerOptions": {
"module": "commonjs",
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"allowSyntheticDefaultImports": true,
"target": "ES2021",
"sourceMap": true,
"outDir": "./dist",
"baseUrl": "./",
"incremental": true,
"skipLibCheck": true,
"strictNullChecks": false,
"noImplicitAny": false,
"strictBindCallApply": false,
"forceConsistentCasingInFileNames": false,
"noFallthroughCasesInSwitch": false
}
}

3614
yarn.lock Normal file

File diff suppressed because it is too large Load Diff