TIL
-
[DB] DB에서 특정한 형식으로 출력하기(시간, 문자열) (TO_CHAR()이용하기), 타임존 설정하기TIL 2024. 4. 5. 17:02
프로젝트를 진행하던 중 해당 게임의 위키 수정내역(히스토리) 목록을 보여줘야 했다. 각각의 히스토리들의 제목은 작성된 의 형태로 보여줘야했고, 이와 같은 형태로 API 내에서 값을 후처리해서 반환해줘야 했다. DB에서 SELECT 해온 값을 타임존설정하기, 특정한 시간 형식으로 바꾸거나 특정한 문자열 형식으로 변환해서 반환하는 법을 알아보았다. 1.특정한 시간형식으로 변경하기) TO_CHAR(CURRENT_DATE, 'YYYY-MM-DD HH24:MI:SS'); 첫번째 매개변수에 시간 값, 두번째 매개변수에 출력형식을 입력한다. 여기서 주의할점은 시는 HH24로 해줘야 24시간 형식으로 출력이되고, 분은 MI로 해줘야한다는것. 월(MM)과 분(MI)을 잘 분리해줘야한다 2.타임..
-
페이지네이션 & 무한스크롤 구현하기TIL 2024. 4. 3. 16:58
프로젝트를 진행하는 도중 필요한 기능이 생겼다. 서비스내에 있는 게임들을 가나다순으로 정렬하여 사용자들에게 보여줘야했다. 하지만 한번에 모든 게임목록들을 가져다 보여주는 것은 게임 수가 많아질 수록 효율성이 떨어진다. 사용자가 한번에 볼 수 있는 게임의 숫자를 적절히 정해서 필요한만큼만 그때그때 보여주는 것이 효율적이다. 게시판에 게시글목록을 볼때도 마찬가지. 그래서 게임목록을 가나다순으로 20개씩 끊어서 보여주기로했고, 페이지네이션을 이용하게되었다 FE는 게임목록을 요청할때 page를 1부터 2, 3, 4... 하나씩 올려가며 요청하면된다. BE는 전달받은 page에 X20을 해서 그만큼을 생략하고( OFFSET ), 추가 20개만 보여주면된다( LIMIT ) **DB의 OFFSET과 LIMIT를 이용..
-
이미지가 포함된 게시글 작성하기(게시글 이미지 업로드 기능구현하기)TIL 2024. 4. 3. 00:38
프로젝트 진행중 티스토리같이 게시글을 쓰는 공간에 유저들이 이미지까지 첨부하는 기능이 필요했다 게시글에 이미지를 업로드하는 기능을 구현해보았다. 이 기능은 일반적으로 프론트엔드로부터 html코드 자체를 넘겨받아 백엔드에서 통째로 저장한다. 이미지의 위치를 이미지 태그의 위치로 기억할 수 있기 때문이다. 백엔드는 게시글 내용을 반환해줄때도 html코드를 통째로 넘겨주면된다. 그리고 이미지 업로드API를 별도로 만들어서 유저가 게시글 작성도중 이미지를 업로드하면, 백엔드는 바로 이미지를S3(혹은 서버)에 저장하고, 그 url을 프론트엔드에 반환해주면된다. 여기서 이미지는 게시글에 업로드하는 순간 바로 S3에 저장되기 때문에 유저가 게시글에 작성도중 이미지를 첨부하고 뒤로가기등을 하여 나가버릴경우, 의미없는 ..
-
[DB] 트랜잭션 사용하기 in node.js + Error해결TIL 2024. 3. 26. 18:31
프로젝트 내에서 배너이미지 등록API를 개발하다가 트랜잭션이 필요해져서 구현하게되었다. 배너이미지 등록 API는 [기존 배너이미지 삭제]와 [새로운 배너이미지 등록] 작업이 DB에서 둘다 이루어져야 한다. 만약 [기존 배너이미지] 작업은 성공하고, [새로운 배너이미지 등록] 작업이 실패한다면, 등록된 배너이미지만 사라져 버리 는 문제가 발생한다. 2가지 작업 모두 실패하여 기존 이미지만 남아있게 하기, 2가지 작업 모두 성공하여 기존이미지 삭제후, 새로운 이미지로 대체하기 경우의 수를 이 2가지로 만들기 위해서 트랜잭션을 이용해야했다. 트랜잭션) pool.query('BEGIN'); //트랜잭션 시작지점설정 pool.query('COMMIT'); //트랜잭션 끝나는지점설정 pool.query('ROLLB..
-
[DB] Bulk Insert VS 단일 Insert 비교 실험TIL 2024. 3. 25. 22:26
프로젝트 알림기능을 구현하던중, 동시에 여러명에게 알림 데이터를 추가해야하는 상황이 발생했다. 원래 for문을 돌려서 insert문을 회원수만큼 돌리려고했으나 나중에 이것이 비효율적이라는 사실을 알게 되었다. (SELECT문으로 가져온 데이터의 개수만큼 반복문으로 다시 쿼리문을 실행하는 것은 이 작업이 얼마나 오래 걸릴지 가늠 조차할수 없을 정도로 큰 작업이 될 수 있기 때문에 반드시 제약해주는 장치가 있어야한다. Like Bulk Insert) 그러다 Bulk Insert문을 이용해서 한번에 데이터들을 삽입할 수 있다는 것을 알게되어 단일 Insert와 비교해보기로 했다. Bulk Insert) 한번의 Insert문 안에 VALUES를 여러개 넣어주면된다 INSERT INTO films (code, t..
-
[Error] The bucket does not allow ACLs - 이미지업로드하기TIL 2024. 3. 19. 18:06
이미지 업로드 기능을 구현하기위해서 IAM 계정 생성 - 액세스키 발급까지 하고나면 AWS에 접속은 되나 S3접속시 다음과 같은 에러가 발생했다. The bucket does not allow ACLs 해당버킷이 ALC을 허용하지않습니다 ACL이란 버킷이나 버킷 객체에 액세스를 허용하거나 제한하는 권한설정이다 다만 Bucket Policy와의 차이점은 bucket policy는 버킷에만 권한 설정이 가능하고, ACL은 버킷뿐만아니라, 버킷객체에 권한설정이 가능하다는 것이다. 그래서 ACL은 각 객체마다 다른 권한 설정이 필요할때 사용한다. 대신 버킷정책은 JSON을 통해 더 세밀하게 권한설정을 조정할 수 있지만, ACL은 그렇지 못하다. 해당 버킷에 ACL을 활성화 해주는법) S3 - 해당 버킷 - 권한..
-
[Error] AWS-SDK, multer - this.client.send is not a functionTIL 2024. 3. 19. 17:31
이미지를 업로드하는 기능을 추가하기위해서, multer와 sdk 모듈을 사용하다보면 에러가 발생한다. TypeError: this.client.send is not a function multer-s3는 3.x.x 버전이고, aws-sdk는 2.x.x버전일때 다음과같은에러가 발생한다 multer-s3를 다운그레이드하여 2.x.x로 맞춰주면 해결된다 multer-s3 삭제 npm uninstall multer-s3 multer-s3 재설치 npm install multer-s3@2.9.0
-
[AWS] AWS Access key와 Secret Access Key - 이미지업로드 기능TIL 2024. 3. 19. 15:48
프로젝트에서 게임 대표 이미지를 저장하고 불러오는 기능이 있다. 이미지를 저장할때 AWS-S3에 저장해야하는데 프로그램 내( AWS CLI, AWS-SDK )에서 AWS의 자원에 접근하기 위해서 는 Access key와 Secret Access Key가 필요하다. aws 웹 콘솔로 로그인을 할때 계정 ID,PW를 입력하는 것과같이, 프로그램내에서 aws서비스에 접속하기위해서는 같은 역 할을 해줄 것들이 필요한데, 그것이 액세스키와 시크릿 액세스키이다. Access key(액세스키) : 유저 번호에 해당하는 키Secret Access Key : 비밀번호에 해당하는 키 IAM 계정을 이용한다면 루트계정을 이용하여 만들 수 있다 IAM - 사용자 - 계정명(k1ju) - 보안자격증명 - 액세스키 생성..