issue # nestjs 로 초기화

This commit is contained in:
geonhee-min
2025-11-21 15:18:37 +09:00
parent ce50a53256
commit 0170421d16
57 changed files with 2483 additions and 143 deletions

View File

@@ -0,0 +1,22 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AppController } from './app.controller';
import { AppService } from './app.service';
describe('AppController', () => {
let appController: AppController;
beforeEach(async () => {
const app: TestingModule = await Test.createTestingModule({
controllers: [AppController],
providers: [AppService],
}).compile();
appController = app.get<AppController>(AppController);
});
describe('root', () => {
it('should return "Hello World!"', () => {
expect(appController.getHello()).toBe('Hello World!');
});
});
});

12
src/app.controller.ts Normal file
View File

@@ -0,0 +1,12 @@
import { Controller, Get } from '@nestjs/common';
import { AppService } from './app.service';
@Controller()
export class AppController {
constructor(private readonly appService: AppService) {}
@Get()
getHello(): string {
return this.appService.getHello();
}
}

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

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { DbModule } from './db/db.module';
import { RedisModule } from './redis/redis.module';
@Module({
imports: [DbModule, RedisModule],
controllers: [AppController],
providers: [AppService],
})
export class AppModule {}

8
src/app.service.ts Normal file
View File

@@ -0,0 +1,8 @@
import { Injectable } from '@nestjs/common';
@Injectable()
export class AppService {
getHello(): string {
return 'Hello World!';
}
}

View File

@@ -1,17 +0,0 @@
import Fastify from 'fastify';
import fastifyPostgres from 'fastify-postgres';
import { registerSwagger } from './utils/plugin/swagger';
import dotenv from 'dotenv';
dotenv.config();
export async function buildApp() {
const app = Fastify();
await app.register(fastifyPostgres, {
connectionString: `postgresql://${process.env.PGUSER}:${process.env.PGPASSWORD}@${process.env.PGHOST}:${process.env.PGPORT}/${process.env.PGDATABASE}`
})
await registerSwagger(app);
return app;
}

22
src/db/db.module.ts Normal file
View File

@@ -0,0 +1,22 @@
import { Global, Module } from "@nestjs/common";
import { Pool } from "pg";
import { drizzle, NodePgDatabase } from "drizzle-orm/node-postgres";
import * as schema from '../../drizzle/schema';
@Global()
@Module({
providers: [
{
provide: "DRIZZLE",
useFactory: (): NodePgDatabase<typeof schema> => {
const pool = new Pool({
connectionString: process.env.PG_DATABASE_URL
});
return drizzle(pool, { schema: schema });
}
}
],
exports: ["DRIZZLE"]
})
export class DbModule {}

8
src/main.ts Normal file
View File

@@ -0,0 +1,8 @@
import { NestFactory } from '@nestjs/core';
import { AppModule } from './app.module';
async function bootstrap() {
const app = await NestFactory.create(AppModule);
await app.listen(process.env.PORT ?? 3000);
}
bootstrap();

View File

@@ -0,0 +1,14 @@
import { Controller, Get, Post, Query } from "@nestjs/common";
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
import { AccountService } from "./account.service";
@Controller('account')
export class AccountController {
constructor(private readonly accountService: AccountService) {}
@Get('check-duplication')
async checkDuplication(@Query() query: CheckDuplicationRequestDto): Promise<CheckDuplicationResponseDto> {
return this.accountService.checkDuplication(query);
}
}

View File

@@ -0,0 +1,10 @@
import { Module } from "@nestjs/common";
import { AccountController } from "./account.controller";
import { AccountRepo } from "./account.repo";
import { AccountService } from "./account.service";
@Module({
controllers: [AccountController],
providers: [AccountService, AccountRepo]
})
export class AccountModule {}

View File

@@ -0,0 +1,21 @@
import { Inject, Injectable } from "@nestjs/common";
import * as schema from "drizzle/schema";
import { countDistinct, and, eq } from 'drizzle-orm';
import { NodePgDatabase } from "drizzle-orm/node-postgres";
@Injectable()
export class AccountRepo {
constructor(@Inject('DRIZZLE') private readonly db: NodePgDatabase<typeof schema>) {}
async checkDuplication(type: 'email' | 'accountId', value: string) {
const result = await this
.db
.select({ count: countDistinct(schema.account[type])})
.from(schema.account)
.where(
eq(schema.account[type], value)
);
return result[0].count;
}
}

View File

@@ -0,0 +1,15 @@
import { Injectable } from "@nestjs/common";
import { AccountRepo } from "./account.repo";
import { CheckDuplicationRequestDto } from "./dto/checkDuplication/check-duplication-request.dto";
import { CheckDuplicationResponseDto } from "./dto/checkDuplication/check-duplication-response.dto";
@Injectable()
export class AccountService {
constructor(private readonly accountRepo: AccountRepo) {}
async checkDuplication(data: CheckDuplicationRequestDto): Promise<CheckDuplicationResponseDto> {
const count = await this.accountRepo.checkDuplication(data.type, data.value);
return { isDuplicated: count > 0 };
}
}

View File

@@ -0,0 +1,9 @@
import { IsString, IsIn } from '@nestjs/class-validator'
export class CheckDuplicationRequestDto {
@IsIn(['email', 'accountId'])
type: 'email' | 'accountId';
@IsString()
value: string;
}

View File

@@ -0,0 +1,3 @@
export class CheckDuplicationResponseDto {
isDuplicated: boolean;
}

19
src/redis/redis.module.ts Normal file
View File

@@ -0,0 +1,19 @@
import { Global, Module } from "@nestjs/common";
import Redis from "ioredis";
@Global()
@Module({
providers: [
{
provide: "REDIS",
useFactory: () => {
return new Redis({
host: process.env.RD_HOST!,
port: Number(process.env.RD_PORT || 6779)
});
}
},
],
exports: ["REDIS"]
})
export class RedisModule{}

View File

@@ -1,14 +0,0 @@
import { buildApp } from './app';
const start = async () => {
try {
const app = await buildApp();
await app.listen({ port: 3000 });
console.log(`Server running on port ${process.env.PORT}`);
} catch (err) {
console.error(err);
process.exit(1);
}
}
start();

View File

@@ -1,33 +0,0 @@
import { FastifyInstance } from "fastify";
import fastifySwagger from "@fastify/swagger";
import fastifySwaggerUi from "@fastify/swagger-ui";
export async function registerSwagger(fastify: FastifyInstance) {
await fastify.register(fastifySwagger, {
openapi: {
info: {
title: "API Docs",
description: "API documentation for Fastify server",
version: "1.0.0"
},
servers: [
{
url: "http://localhost:3000",
description: "Local Development",
},
{
url: "https://api.scheduler.bkdhome.p-e.kr",
description: "Production Server"
}
]
}
});
await fastify.register(fastifySwaggerUi, {
routePrefix: "/docs",
uiConfig: {
docExpansion: "none",
deepLinking: false
}
});
}