Seed Data

박은지·2022년 2월 13일
0

Express

목록 보기
8/10

💡 테이블에 가장 처음 넣는 데이터를 seed 데이터라고 부른다.

1. sequelize-cli 명령 실행

seed data도 sequelize-cli 명령어를 통해 테이블에 넣을 수 있다.

터미널에 npx sequelize seed:generate --name initialMembers 명령을 입력하고 실행하면
seeders라는 디렉토리에 새로운 js파일이 생성된다.

2. seeders 디렉토리 안의 js파일 수정

// seeders > 20220213113837-initialMembers.js

'use strict';

module.exports = {
  async up (queryInterface, Sequelize) {
    /**
     * Add seed commands here.
     *
     * Example:
     * await queryInterface.bulkInsert('People', [{
     *   name: 'John Doe',
     *   isBetaMember: false
     * }], {});
    */

    // Members 테이블에 배열 안의 직원 정보 삽입
    await queryInterface.bulkInsert('Members', [
      {
        id: 1,
        name: 'Alex',
        team: 'engineering',
        position: 'Server Developer',
        emailAddress: 'alex@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2018/12/10',
        birthday: '1994/11/08',
        profileImage: 'profile1.png',
      },
      {
        id: 2,
        name: 'Benjamin',
        team: 'engineering',
        position: 'Server Developer',
        emailAddress: 'benjamin@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2021/01/20',
        birthday: '1992/03/26',
        profileImage: 'profile2.png',
      },
      {
        id: 3,
        name: 'Charles',
        team: 'engineering',
        position: 'Android Developer',
        emailAddress: 'charles@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2018/10/09',
        birthday: '1994/09/08',
        profileImage: 'profile3.png',
      },
      {
        id: 4,
        name: 'Eric',
        team: 'engineering',
        position: 'Web Frontend Developer',
        emailAddress: 'eric@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2020/04/07',
        birthday: '1995/04/10',
        profileImage: 'profile4.png',
      },
      {
        id: 5,
        name: 'Danial',
        team: 'marketing',
        position: 'Marketing Manager',
        emailAddress: 'danial@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2021/04/21',
        birthday: '1991/07/12',
        profileImage: 'profile5.png',
      },
      {
        id: 6,
        name: 'George',
        team: 'marketing',
        position: 'Marketing Staff',
        emailAddress: 'george@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2020/01/06',
        birthday: '1997/02/09',
        profileImage: 'profile6.png',
      },
      {
        id: 7,
        name: 'Henry',
        team: 'marketing',
        position: 'Marketing Staff',
        emailAddress: 'henry@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2020/04/03',
        birthday: '1997/08/18',
        profileImage: 'profile7.png',
      },
      {
        id: 8,
        name: 'James',
        team: 'sales',
        position: 'Sales Manager',
        emailAddress: 'james@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2020/11/26',
        birthday: '1993/05/22',
        profileImage: 'profile8.png',
      },
      {
        id: 9,
        name: 'Kevin',
        team: 'sales',
        position: 'Sales Staff',
        emailAddress: 'kevin@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2020/06/19',
        birthday: '1989/06/10',
        profileImage: 'profile9.png',
      },
      {
        id: 10,
        name: 'Michael',
        team: 'sales',
        position: 'Sales Staff',
        emailAddress: 'michael@google.com',
        phoneNumber: '010-xxxx-xxxx',
        admissionDate: '2019/11/12',
        birthday: '1992/09/17',
        profileImage: 'profile10.png',
      },
    ], {});
  },

  async down (queryInterface, Sequelize) {
    /**
     * Add commands to revert seed here.
     *
     * Example:
     * await queryInterface.bulkDelete('People', null, {});
     */
    // Members 테이블에 있는 모든 row 삭제
    await queryInterface.bulkDelete('Members', null, {});
  }
};

3. seeder 파일 적용, seed 데이터 삽입

터미널에서 npx sequelize db:seed:all 명령을 실행하여
seeders 디렉토리 안에 있는 모든 seeder 파일이 적용되도록 한다.

이제 seeders 파일이 적용되었기 때문에 Members 테이블 안에는 직원 정보가 들어있다.

0개의 댓글