TIL
-
의존성 주입(Dependency Injection)TIL 2024. 6. 10. 17:02
의존성 주입을 쓰는이유 1. 클래스간 결합도를 낮출 수 있다 -> 결합도를 낮춘다는 것(의존성을 줄인다는 것)은 변화에 강하다는것 객체의 생성과 사용을 분리한다 다른 객체를 직접 생성하지않고 외부에서 필요한 객체를 주입받기 때문에 각 역할을 분리할 수 있다 2. 테스트가 가능하다* -> 테스트코드를 작성하는 것은 별도의 모의 객체를 생성하고 그 객체의 함수가 어떤 값을 반환한다고 가정한다(모킹) -> 이것도 의존성을 낮춰줘서 가능한일. 의존성 주입을 하지않으면 모의 객체의 함수를 모킹하는것이 아니라 실제 클래스의 함수를 가져와야하는데, 그럼 문제가 발생한다. 실제로 동작하면 안되는 것들이 동작하게 될 수 있다. 실제객체가 아닌 모의객체를 이용한 테스트가 가능해진다 (예시1 : db연결이 실제..
-
[nest.js] swagger 적용하기TIL 2024. 5. 29. 16:04
왜 swagger를 쓰는가?) swagger 를 사용하면 api명세서를 코드내에서 자동으로 쓸 수 있게해준다 api가 많아질경우 api명세서를 쓰는 시간을 훨씬 더 단축시킬 수 있고, 백엔드 작업을 하다보면 놓친부분이나, api명세서 변경이 꽤 많이 일어난다. 그럴때마다 코드와 api명세서를 둘다 수정하는 것은 시간도 오래걸리고, 만약 코드만 수정하고, api명세서 수정하는 것을 깜빡하면 프론트엔드쪽에서는 잘못된 명세서를 많은 시간을 낭비하게된다. swagger 설치하기 npm install --save @nestjs/swagger main.ts에 swagger 적용하기) main.tsimport { NestFactory } from '@nestjs/core';import { Swag..
-
[ERROR] ReferenceError: exports is not defined in ES module scopeTIL 2024. 5. 28. 12:26
nest project를 실행할때 에러가 발생했다. ReferenceError: exports is not defined in ES module scopeThis file is being treated as an ES module because it has a '.js' file extension and '/Users/kiju/dev/study/crime-city/package.json' contains "type": "module". To treat it as a CommonJS script, rename it to use the '.cjs' file extension. package.json에 "type" : "module" 을 지웠더니 해결되었다 참고)https://stackoverflo..
-
Express staticTIL 2024. 5. 25. 15:05
Express static 미들웨어 express 에서 기본적으로 제공하는 미들웨어이고, express 객체안에서 바로 꺼내 쓸 수 있다정적파일인 이미지, css, javascript등 파일을 쓸때 사용한다 지정된 디렉토리에서 파일을 찾아서 클라이언트에게 제공한다 이 미들웨어를 통해 정적파일에 restful한 방식으로 접근할 수 있다. 사용법 예시1)app.use('요청경로', express.static('실제경로'));app.use('/', express.static(path.join(__dirname, 'public'))); app.use('/', express.static(path.join(__dirname, 'public')));ㄴ 현재 위치한 파일에 대한 절대경로 + '/public' => 이..
-
[DB] 트랜잭션 동시에 실행해서 성능올리기 ( confilct serializable한 nonserial schedule을 허용하기 )TIL 2024. 4. 8. 17:25
- nonserial schedule(논시리얼 스케쥴) : 트랜잭션이 순차적이지않고 겹쳐서 실행되는 스케쥴 장점) 트랜잭션이 겹침 동시성이 올라간다 (성능이 향상된다) I/O작업마다 다음 트랜잭션 동작 실행한다 (I/O 입출력 작업은 CPU가 유휴상태로 있는 작업이다. 그래서 CPU가 유휴상태로있는 시간동안 다른 트랜잭션의 동작을 실행함으로써 CPU의 유휴시간이 줄어들고 성능이 좋아진다.) 단점) 결과가 이상해질 수 있다 (동시에 트랜잭션을 수행하다보니 잘못된 값을 읽거나 써서 결과가 이상해질 수 있다) - serial schedule(시리얼 스케쥴) : 트랜잭션이 순차적으로 수행되는 스케쥴 장점) 결과가 이상해질 가능성이 없다 (하나의 트랜잭션이 완전히 끝나고 다음 트랜잭션이 수행되기때문에 결과가 이상..
-
[DB] soft delete 논리삭제TIL 2024. 4. 5. 22:32
프로젝트를 진행하던 중, 실제 서비스에서 쓰이는 바와 같이 soft delete를 사용하자는 의견이 나왔다. db에 저장된 내용을 물리적으로 삭제(hard delete)하는 것이아니라, 논리적으로만 삭제함으로써, 데이터 삭제요청이 들어오더라도, 그 요청을 서비스 내에서 수행함과 동시에, 실제 DB에는 과거에 생겼던 데이터 목록들 도 볼 수 있게된다. 그 결과 문제가 생겼을때, 데이터 복원이 가능하고, 데이터 변경이력, 데이터 삭제시간 등을 확인할 수 있는 장점이 있다. 구현이 어렵지도 않기때문에 바로 기획단계에서 바로 프로젝트에 도입했다. DB에 테이블을 만들때 삭제여부를 확인할 수 있는 데이터속성 1개만 추가하면된다 (is_deleted) 데이터 속성 1개 만으로 데이터의 삭제여부, 삭제시간을 모두 알..
-
N+1 문제 해결하기TIL 2024. 4. 5. 22:06
프로젝트를 진행하던 중, 위키를 수정할때마다 기존에 수정한 이력이 있는 사용자들 모두에게 알림을 보내는 기능을 구현 해야했다. 기존에는 SELECT문으로 해당 위키를 수정한 사람들의 목록을 출력하고 그 숫자만큼 반복문으로, 알림을 추가 하는 Insert 쿼리를 돌려주었다. 그렇게 N+1 문제를 안게되었다. N+1 문제란 SELECT문으로 나온 데이터의 개수만큼 다른 쿼리문(update, insert, delete)을 반복하는 경우를 말한다. 그래서 실제로는 1+N에 더 가깝다고 볼 수 있다. 문제가 되는 이유) 그래서 이게 왜 문제가되냐면, DB에 연결하는 작업은 프로그램에서 가장 느린 작업이다. 그런데 SELECT문으로 가져온 데이터의 결과수 만큼 새로운 쿼리문을 반복한다는 것은 가져온 데이터의 수가 ..
-
3계층구조)TIL 2024. 4. 5. 17:47
(Req.DTO) / (Res.DTO) --- 1계층 : 컨트롤러 (HTTP통신, 라우터등과 같이 통신을 담당하는 부분 ) --- BO(비즈니스 오브젝트) 2계층 : 서비스 (하나의 비즈니스로직) ㄴ 하나의 비즈니스 로직을 이행하기위해서 유저정보, 결제 정보등 데이터조회가 필요하므로 뒷단의 데이터계층에서 데이터를 조회하거나 참조하는 기능도 수행한다 ㄴ트랜잭션 계층, 비즈니스 로직계층 이라고도한다 --- 엔티티 3계층 : 레포지토리( db작업을 담당하는 부분) ㄴ 하나의 비즈니스로직에 필요한 데이터 조회,입력,수정등을 지원하는 단계 비즈니스 로직을 수행할때, 데이터 기입이나 변동이 필요할때, 이에대한 작업을 지원하고, 정보를 보관하는 계층 이렇게 3계층 구조로 나누는 이유는 뭘까?) ->응집도를 높이고 결..