shortly-mvc[2021.11.18]

김정훈·2021년 11월 19일
0

스프린트

목록 보기
2/5

1. ORM설정

  • 사용할 데이터 베이스 생성
    • create database short
  • sequelize 및 sequelize-cli 설치
    • npm install --save sequelize
    • npm install --save-dev sequelize-cli
    • npx sequelize-cli init
      • init 처리를 하면 자동으로 config, models, migrations, seeders 파일 생성
  • config파일을 통해 생성한 데이터베이스 정보 등록

2. 모델 생성

  • npx sequelize-cli model:generate --name url --attributes url:string,title:string,visits:integer
    • 시퀄라이즈 cli를 통해 모델 생성(모델 이름 속성 부여)

3. 마이그레이션

  • npx sequelize-cli db:migrate
    • 해당 명령어를 통해 만들어진 모델을 베이터베이스에 이주

4. 라우터

  • app.js를 통해 endpoint확인
  • links를 통해 컨트롤러로 진입할 수 있게 엔드포인트 설정
  • GET /links
  • GET /links/:id
  • POST /links

5. 컨트롤러

  • 시퀄라이즈 메서드를 이용해 데이터베이스에서 자료 추출
  • findAll()
    • 전체 데이터 조회
const {url} = require('../../models')
get : async(req, res)=>{
        
   	  await url.findAll().then(data=>res.status(200).send(data))

}
//모델을 통해 findAll()메서드를 사용하여 데이터베이스의 모든 데이터 조회 가능
  • findOne()
    • 특정 데이터 조회
const {url} = require('../../models')
get : async(req, res)=>{
      const userId = req.params.id  
   	  await url.findOne(
      	{where:{id:userId}})
      .then(data=>res.status(200).send(data))

}
//모델을 통해 findOne()메서드를 사용하여 데이터베이스의 조건과 일치하는 데이터만 조회
  • create()
    • 데이터 추가
  • update()
    • 데이터 수정
profile
프론트엔드 개발자를 꿈꾸고 있습니다!

0개의 댓글