Skip to content

Commit

Permalink
feat: add mysql support
Browse files Browse the repository at this point in the history
  • Loading branch information
khuongln-1346 committed Nov 21, 2024
1 parent f540fbd commit 6ea356c
Show file tree
Hide file tree
Showing 46 changed files with 1,716 additions and 63 deletions.
65 changes: 65 additions & 0 deletions .docker/docker-compose.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
services:
db:
container_name: nestjs-turbo-postgres
image: postgres:17
restart: always
volumes:
- postgres_data:/var/lib/postgresql/data
environment:
POSTGRES_DB: nestjs_turbo
POSTGRES_USER: postgres
POSTGRES_PASSWORD: postgres
TZ: "UTC"
ports:
- "25432:5432"
networks:
- nestjs-turbo-network

redis:
image: redis/redis-stack:latest
restart: always
ports:
- "6379:6379"
- "8001:8001"
volumes:
- redis_data:/data
healthcheck:
test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
environment:
REDIS_ARGS: "--requirepass redispass"
networks:
- nestjs-turbo-network

maildev:
build:
context: .
dockerfile: maildev.Dockerfile
ports:
- ${MAIL_CLIENT_PORT}:1080
- ${MAIL_PORT}:1025
networks:
- nestjs-turbo-network

pgadmin:
container_name: pgadmin
image: dpage/pgadmin4
ports:
- "18080:80"
volumes:
- pgadmin_data:/root/.pgadmin
environment:
PGADMIN_DEFAULT_EMAIL: [email protected]
PGADMIN_DEFAULT_PASSWORD: 12345678
PGADMIN_CONFIG_WTF_CSRF_ENABLED: "False"
PGADMIN_CONFIG_ENHANCED_COOKIE_PROTECTION: "False"
networks:
- nestjs-turbo-network

volumes:
postgres_data:
pgadmin_data:
redis_data:

networks:
nestjs-turbo-network:
driver: bridge
47 changes: 47 additions & 0 deletions .docker/docker-compose.mysql.local.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
services:
db:
container_name: nestjs-turbo-mysql
image: mysql
restart: always
volumes:
- mysql_data:/var/lib/mysql
environment:
MYSQL_DATABASE: realworld_api
MYSQL_ROOT_PASSWORD: 12345678
ports:
- "13306:3306"
networks:
- nestjs-turbo-network

redis:
image: redis/redis-stack:latest
restart: always
ports:
- "6379:6379"
- "8001:8001"
volumes:
- redis_data:/data
healthcheck:
test: [ "CMD", "redis-cli", "--raw", "incr", "ping" ]
environment:
REDIS_ARGS: "--requirepass redispass"
networks:
- nestjs-turbo-network

maildev:
build:
context: .
dockerfile: maildev.Dockerfile
ports:
- ${MAIL_CLIENT_PORT}:1080
- ${MAIL_PORT}:1025
networks:
- nestjs-turbo-network

volumes:
mysql_data:
redis_data:

networks:
nestjs-turbo-network:
driver: bridge
2 changes: 1 addition & 1 deletion apps/realworld-api/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
NODE_ENV=development

##== Application
APP_NAME="Admin API"
APP_NAME="Realworld API"
APP_URL=http://localhost:3000
APP_PORT=3000
APP_DEBUG=false
Expand Down
3 changes: 1 addition & 2 deletions apps/realworld-api/src/api/profile/profile.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Module } from '@nestjs/common';
import { TypeOrmModule } from '@nestjs/typeorm';
import { UserEntity } from '@repo/database-typeorm';
import { UserFollowsEntity } from '@repo/database-typeorm/entities/user-follows.entity';
import { UserEntity, UserFollowsEntity } from '@repo/database-typeorm';
import { ProfileController } from './profile.controller';
import { ProfileService } from './profile.service';

Expand Down
3 changes: 1 addition & 2 deletions apps/realworld-api/src/api/profile/profile.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ import { ErrorCode } from '@/constants/error-code.constant';
import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { ValidationException } from '@repo/api';
import { UserEntity } from '@repo/database-typeorm';
import { UserFollowsEntity } from '@repo/database-typeorm/entities/user-follows.entity';
import { UserEntity, UserFollowsEntity } from '@repo/database-typeorm';
import { Repository } from 'typeorm';
import { ProfileDto, ProfileResDto } from './dto/profile.dto';

Expand Down
1 change: 1 addition & 0 deletions apps/realworld-api/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import authConfig from './api/auth/config/auth.config';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AllConfigType } from './config/config.type';
// import { TypeOrmConfigService } from './database/mysql-typeorm-config.service'; // Uncomment this line if you are using MySQL
import { TypeOrmConfigService } from './database/typeorm-config.service';

const configModule = ConfigModule.forRoot({
Expand Down
51 changes: 51 additions & 0 deletions apps/realworld-api/src/database/mysql-typeorm-config.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { AllConfigType } from '@/config/config.type';
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { TypeOrmModuleOptions, TypeOrmOptionsFactory } from '@nestjs/typeorm';
import { join } from 'path';

@Injectable()
export class TypeOrmConfigService implements TypeOrmOptionsFactory {
constructor(private configService: ConfigService<AllConfigType>) {}

createTypeOrmOptions(): TypeOrmModuleOptions {
const modulePath = require.resolve('@repo/mysql-typeorm');
const nodeModulesDir = join(modulePath, '..', '..');

return {
type: this.configService.get('database.type', { infer: true }),
host: this.configService.get('database.host', { infer: true }),
port: this.configService.get('database.port', { infer: true }),
username: this.configService.get('database.username', { infer: true }),
password: this.configService.get('database.password', { infer: true }),
database: this.configService.get('database.name', { infer: true }),
synchronize: this.configService.get('database.synchronize', {
infer: true,
}),
dropSchema: false,
keepConnectionAlive: true,
logger: 'debug',
entities: [join(nodeModulesDir, 'dist', '**', '*.entity.{ts,js}')],
poolSize: this.configService.get('database.maxConnections', {
infer: true,
}),
ssl: this.configService.get('database.sslEnabled', { infer: true })
? {
rejectUnauthorized: this.configService.get(
'database.rejectUnauthorized',
{ infer: true },
),
ca:
this.configService.get('database.ca', { infer: true }) ??
undefined,
key:
this.configService.get('database.key', { infer: true }) ??
undefined,
cert:
this.configService.get('database.cert', { infer: true }) ??
undefined,
}
: undefined,
} as TypeOrmModuleOptions;
}
}
2 changes: 1 addition & 1 deletion apps/realworld-api/src/database/typeorm-config.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,6 @@ export class TypeOrmConfigService implements TypeOrmOptionsFactory {
undefined,
}
: undefined,
} as TypeOrmModuleOptions;
} as unknown as TypeOrmModuleOptions;
}
}
1 change: 1 addition & 0 deletions apps/realworld-api/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"@/guards/*": ["src/guards/*"],
"@/interceptors/*": ["src/interceptors/*"],
"@/utils/*": ["src/utils/*"]
// "@repo/database-typeorm": ["node_modules/@repo/mysql-typeorm"], // Uncomment this line if you are using MySQL
}
}
}
17 changes: 17 additions & 0 deletions packages/mysql-typeorm/.env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
##== Environment
NODE_ENV=development

##== Database
DATABASE_TYPE=mysql
DATABASE_HOST=localhost
DATABASE_PORT=3306
DATABASE_USERNAME=root
DATABASE_PASSWORD=12345678
DATABASE_LOGGING=true
DATABASE_SYNCHRONIZE=false
DATABASE_MAX_CONNECTIONS=100
DATABASE_SSL_ENABLED=false
DATABASE_REJECT_UNAUTHORIZED=false
DATABASE_CA=
DATABASE_KEY=
DATABASE_CERT=
3 changes: 3 additions & 0 deletions packages/mysql-typeorm/.prettierrc.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import prettier from '@repo/eslint-config/prettier-base.config.mjs';

export default { ...prettier };
3 changes: 3 additions & 0 deletions packages/mysql-typeorm/eslint.config.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import nest from '@repo/eslint-config/eslint-nest.config.mjs';

export default [...nest];
63 changes: 63 additions & 0 deletions packages/mysql-typeorm/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
{
"name": "@repo/mysql-typeorm",
"version": "0.0.0",
"private": true,
"license": "MIT",
"scripts": {
"clean": "rm -rf dist",
"dev": "pnpm build --watch",
"build": "tsc -b -v",
"lint": "eslint \"{src,test}/**/*.ts\"",
"typeorm": "pnpm clean && env-cmd typeorm-ts-node-commonjs -d src/data-source.ts",
"migration:up": "pnpm typeorm migration:run",
"migration:down": "pnpm typeorm migration:revert",
"migration:show": "pnpm typeorm migration:show",
"migration:create": "typeorm migration:create",
"migration:generate": "pnpm typeorm migration:generate --pretty",
"typeorm-ex": "pnpm clean && pnpm build && env-cmd typeorm-extension",
"db:create": "pnpm typeorm-ex db:create",
"db:drop": "pnpm typeorm-ex db:drop",
"seed:run": "pnpm typeorm-ex seed:run",
"seed:create": "pnpm typeorm-ex seed:create"
},
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": [
"./dist/**",
"!./dist/factories",
"!./dist/migrations",
"!./dist/seeds"
],
"publishConfig": {
"access": "public"
},
"exports": {
".": {
"types": "./dist/index.d.ts",
"default": "./dist/index.js"
},
"./*": {
"types": "./dist/*.d.ts",
"default": "./dist/*.js"
}
},
"dependencies": {
"@nestjs/config": "^3.3.0",
"@nestjs/mapped-types": "*",
"@repo/nest-common": "workspace:*",
"class-transformer": "^0.5.1",
"class-validator": "^0.14.1",
"env-cmd": "^10.1.0",
"pg": "^8.13.1",
"typeorm": "^0.3.20",
"typeorm-extension": "^3.6.3"
},
"devDependencies": {
"@repo/eslint-config": "workspace:*",
"@repo/typescript-config": "workspace:*",
"@types/node": "^20.17.2",
"ts-loader": "^9.5.1",
"ts-node": "^10.9.2",
"typescript": "^5.6.3"
}
}
16 changes: 16 additions & 0 deletions packages/mysql-typeorm/src/config/database-config.type.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
export type DatabaseConfig = {
type: string;
host: string;
port: number;
password: string;
name: string;
username: string;
logging: boolean;
synchronize: boolean;
maxConnections: number;
sslEnabled: boolean;
rejectUnauthorized: boolean;
ca?: string;
key?: string;
cert?: string;
};
Loading

0 comments on commit 6ea356c

Please sign in to comment.