-
항해 99 5기 TIL_67항해 99 2022. 3. 18. 03:39
▶ Today I Learned
<실전 프로젝트>
[뱃지 기능 구현]
선착순 100명에게만 지급되는 특별 뱃지를 위해 만든 조건
// 100번째 까지 모두 지급되었는지 확인 const oneHundred = await user.getMyBadges({ where: { userId: 100, }, }) // User-badge 테이블에서 user의 id가 100까지 차있다는 건 카카오나 로컬 중 100명까지 모두 지급되었다는 것 // ch: 100번이라는 숫자와 비교하는 것으로 식을 짜면 mysql의 특성상 1 ~ 100번 사이의 유저가 탈퇴했다고해도 그 다음 번호의 사람에게 뱃지를 주지는 않는다.
이렇게 Id만을 조회하게 되면 테이블을 별도로 join하거나 기존의 join된 테이블에서 카카오나 로컬로 로그인한 유저들의 아이디를 모두 가져와 array.length > 100 같은 것을 할 필요는 없다.
그저 조건에 충족하는 row가 있는지만 확인하면 끝이므로 작업 처리도 빠를 것이다.
100번 아래 번호 중 하나가 삭제되었다고해서 그다음 생성되는 유저 아이디가 해당 번호를 메꾸지않고 101번으로 등록된다는 mysql의 특성때문에 초기 가입 유저들의 빈자리가 메꿔지지 않았다.
이를 위해 위 방식보다는 해당 뱃지의 자리가 몇개 남았는지를 보여주는 column을 새로 만들기로 했다.
그 전에 알아야 할 명령어들
sequelize-cli <명령> 명령: sequelize-cli db:migrate Run pending migrations sequelize-cli db:migrate:schema:timestamps:add Update migration table to have timestamps sequelize-cli db:migrate:status List the status of all migrations sequelize-cli db:migrate:undo Reverts a migration sequelize-cli db:migrate:undo:all Revert all migrations ran sequelize-cli db:seed Run specified seeder sequelize-cli db:seed:undo Deletes data from the database sequelize-cli db:seed:all Run every seeder sequelize-cli db:seed:undo:all Deletes data from the database sequelize-cli db:create Create database specified by configuration sequelize-cli db:drop Drop database specified by configuration sequelize-cli init Initializes project sequelize-cli init:config Initializes configuration sequelize-cli init:migrations Initializes migrations sequelize-cli init:models Initializes models sequelize-cli init:seeders Initializes seeders sequelize-cli migration:generate Generates a new migration file sequelize-cli migration:create Generates a new migration file sequelize-cli model:generate Generates a model and its migration sequelize-cli model:create Generates a model and its migration sequelize-cli seed:generate Generates a new seed file sequelize-cli seed:create Generates a new seed file 옵션: --version 버전 넘버를 보여줍니다 [여부] --help 도움말을 보여줍니다
vscode terminal에서 실행 시에는 npx를 앞에 붙여서 실행해주어야 한다.
ex) npx sequelize-cli db:seed:all
특정 Column을 지워주는 방법
down: (queryInterface, Sequelize) => { return Promise.all([ queryInterface.removeColumn('tableName', 'columnName1'), queryInterface.removeColumn('tableName', 'columnName2') ]); }
출처:
https://stackoverflow.com/questions/46357533/how-to-add-delete-new-columns-in-sequelize-cli
하지만 어째서인지 지워지지 않았다.
그러던 중 아래의 링크에서 해당 구문이 down이 아닌 up에서 쓰인 것을 발견하였다.
https://riverpark94.github.io/devlog/2021/09/10/NB-TIL8/
이유를 아래의 링크에서 찾아보니
up은 실제 DB에 적용되는 영역이며, down은 작업을 취소한 경우라고 한다.
https://velog.io/@milkyway/Sequelize-ORM-%ED%94%84%EB%A0%88%EC%9E%84%EC%9B%8C%ED%81%AC
이렇게 또 하나 알아간다.
▶ 느낀 점
생각보다 중간중간 막히는 부분들이 많은 것 같다. 나 뿐만이 아니라 팀원분들도 마찬가지인듯 하다.
하지만 다들 어떻게든 하나씩 해내고 있다.
오늘 마케팅 전략 준비, 회의, 코딩으로 벌써 하루가 다 갔지만
내일 발표 준비와 코딩 나머지 부분을 완벽히 마무리 지어야겠다.
오늘 하루도 고생많으셨습니다 모두들! ;)
▶ 공부 시 참고 링크들
https://any-ting.tistory.com/50
https://crispypotato.tistory.com/156
https://sequelize.org/v5/class/lib/query-interface.js~QueryInterface.html
'항해 99' 카테고리의 다른 글
항해 99 5기 TIL_69 (0) 2022.03.20 항해 99 5기 TIL_68 (0) 2022.03.18 항해 99 5기 TIL_66 (0) 2022.03.17 항해 99 5기 TIL_65 (0) 2022.03.16 항해 99 5기 TIL_64 (0) 2022.03.15