2017-04-01 21 views
3

현재 Sequelize.js로 데이터를 시드하고 연결 ID에 하드 코딩 된 값을 사용하고 있습니다. 이것은 동적으로 올바르게 할 수 있어야하기 때문에 이상적이지 않습니다. 예를 들어, 사용자 및 프로필을 "has one"및 "belongs to"연결과 연관시킵니다. 반드시 하드 코딩 된 profileId을 사용자에게 시드하고 싶지는 않습니다. 프로필을 만든 후에 프로필 씨드에서 오히려 그렇게하고 싶습니다. 프로필이 생성되면 동적으로 사용자에게 profileId을 추가합니다. Sequelize.js로 작업 할 때 이것이 가능한가? 또는 Sequelize로 시드 할 때 하드 코드 연결 ID가 더 일반적입니까?동적 시딩을 속임수로 지정

아마도 나는 잘못 먹는 것에 대해 이야기 할 것입니까? Sequelize를 사용하여 마이그레이션 파일이있는 일대일 시드 파일을 갖춰야합니까? Rails에는 일반적으로 시드 파일이 1 개만 있습니다. 원하는 경우 여러 파일로 분할 할 수 있습니다.

일반적으로 여기서 안내 및 조언을 구합니다.

users.js

// User seeds 

'use strict'; 

module.exports = { 
    up: function (queryInterface, Sequelize) { 
    /* 
     Add altering commands here. 
     Return a promise to correctly handle asynchronicity. 

     Example: 
     return queryInterface.bulkInsert('Person', [{ 
     name: 'John Doe', 
     isBetaMember: false 
     }], {}); 
    */ 

    var users = []; 
    for (let i = 0; i < 10; i++) { 
     users.push({ 
     fname: "Foo", 
     lname: "Bar", 
     username: `foobar${i}`, 
     email: `foobar${i}@gmail.com`, 
     profileId: i + 1 
     }); 
    } 
    return queryInterface.bulkInsert('Users', users); 
    }, 

    down: function (queryInterface, Sequelize) { 
    /* 
     Add reverting commands here. 
     Return a promise to correctly handle asynchronicity. 

     Example: 
     return queryInterface.bulkDelete('Person', null, {}); 
    */ 
    return queryInterface.bulkDelete('Users', null, {}); 
    } 
}; 

profiles.js

// Profile seeds 

'use strict'; 
var models = require('./../models'); 
var User = models.User; 
var Profile = models.Profile; 


module.exports = { 
    up: function (queryInterface, Sequelize) { 
    /* 
     Add altering commands here. 
     Return a promise to correctly handle asynchronicity. 

     Example: 
     return queryInterface.bulkInsert('Person', [{ 
     name: 'John Doe', 
     isBetaMember: false 
     }], {}); 
    */ 

    var profiles = []; 
    var genders = ['m', 'f']; 
    for (let i = 0; i < 10; i++) { 
     profiles.push({ 
     birthday: new Date(), 
     gender: genders[Math.round(Math.random())], 
     occupation: 'Dev', 
     description: 'Cool yo', 
     userId: i + 1 
     }); 
    } 
    return queryInterface.bulkInsert('Profiles', profiles); 
    }, 

    down: function (queryInterface, Sequelize) { 
    /* 
     Add reverting commands here. 
     Return a promise to correctly handle asynchronicity. 

     Example: 
     return queryInterface.bulkDelete('Person', null, {}); 
    */ 
    return queryInterface.bulkDelete('Profiles', null, {}); 
    } 
}; 

당신은 그냥 모두 하드 코딩 for 루프 (좋지 않은)을 사용하고 볼 수 있듯이

:이 내 파일입니다.

답변

2

사용자 및 프로필에 다른 시드를 사용하는 대신 create-with-association 기능을 사용하여 하나의 파일에 함께 시드 할 수 있습니다.

그리고 create() 시리즈를 사용할 때는 시드 인터페이스에서 Promise를 반환 값으로 사용하기 때문에 Promise.all()에 그 값을 래핑해야합니다.

up: function (queryInterface, Sequelize) { 
    return Promise.all([ 
    models.Profile.create({ 
     data: 'profile stuff', 
     users: [{ 
      name: "name", 
      ... 
     }, { 
      name: 'another user', 
      ... 
     }]}, { 
     include: [ model.users] 
     } 
    ), 
    models.Profile.create({ 
     data: 'another profile', 
     users: [{ 
     name: "more users", 
     ... 
     }, { 
     name: 'another user', 
     ... 
     }]}, { 
     include: [ model.users] 
     } 
    ) 
    ]) 
} 

실제로 이것이 가장 좋은 해결책인지는 확실하지 않지만 파일을 시드하는 데 외래 키를 유지하는 방법은 확실하지 않습니다.

+0

감사합니다. @ simon.ro! 이것은 큰 도움과 내가 이걸로 어려움을 겪은 유일한 사람이 아니라는 것을 아는 것이 좋다. – bideowego