2017-04-20 16 views
0

사양 :Sequelize : 인스턴스 메소드 getParticipants()에서 'findAll'? 내 회의 인스턴스 방법

getParticipants() : 약속 -> 참가자 배열

회의 모델 :

return sequelize.define('conference', { 

    id: { 
     type: Sequelize.UUID, 
     defaultValue: Sequelize.UUIDV4, 
     primaryKey: true 
    }, 

    name: { 
     type: Sequelize.STRING, 
     allowNull: false, 
     unique: true 
    }, 

    maxParticipants: { 
     type: Sequelize.INTEGER, 
     allowNull: false 
    }, 

    fileShareSession: { 
     type: Sequelize.STRING, 
     defaultValue: null, 
     allowNull: true 
    }, 

    startDate: { 
     type: Sequelize.DATE, 
     defaultValue: null, 
     allowNull: true 
    }, 

    endDate: { 
     type: Sequelize.DATE, 
     defaultValue: null, 
     allowNull: true 
    }, 

    state: { 
     type: Sequelize.ENUM(
      ConferenceState.new, 
      ConferenceState.starting, 
      .. 
     ), 
     defaultValue: ConferenceState.new, 
     required: true, 
     allowNull: false 
    } 

참가자 모델 :

return sequelize.define('participant', { 

    id: { 
     type: Sequelize.UUID, 
     defaultValue: Sequelize.UUIDV4, 
     primaryKey: true 
    }, 

    displayName: { 
     type: Sequelize.STRING, 
     defaultValue: null, 
     allowNull: true 
    }, 

    mediaResourceId: { 
     type: Sequelize.STRING, 
     defaultValue: null, 
     allowNull: true 
    }, 

    screenSharingId: { 
     type: Sequelize.STRING, 
     defaultValue: null, 
     allowNull: true 
    }, 

    mediaType: { 
     type: Sequelize.ENUM(
      MediaType.AUDIO_VIDEO), 
     defaultValue: MediaType.AUDIO_VIDEO, 
     allowNull: false 
    }, 

    state: { 
     type: Sequelize.ENUM(
      ParticipantState.new, 
      ParticipantState.joining, 
      .. 
     ), 
     defaultValue: ParticipantState.new, 
     required: true, 
     allowNull: false 
    } 

질문 :

그래서 나는 내 회의 인스턴스 모델 participant.findAll 여부를 할 수 있습니까? 그렇다면 findAll을 사용하여 Array를 다시 얻을 수 있습니까?

나는 그런 식으로 일을 한 것입니다 : 당신은 테이블 간의 관계를 할 때

// getParticipants() : Promise -> Participant array 
     getParticipants() { 
      return new Promise((resolve, reject) => { 
       var Participant = sequelize.models.participant; 
       Participant.findAll({ 
        where: { 
         id: id 
        } 
       }).then(function(participant) { 
        if (_.isObject(participant)) { 
         resolve(participant); 
        } else { 
         throw new ResourceNotFound(conference.name, {id: id}); 
        } 
       }).catch(function(err) { 
        reject(err); 
       }); 
      }); 
     }, 

답변

2

LAZY 로딩 sequelize에 의해 구현됩니다. 당신이 당신의 데이터베이스처럼 쿼리 할 때

var Conference = sequelize.define('conference', { ... }); 

var Participant = sequelize.define('participant', { ... }); 

Conference.belongsToMany(Participant, { through: 'ConferenceParticipants'}); 

Participant.belongsToMany(Conference, { through: 'ConferenceParticipants'}); 

그런 다음 당신이 열망로드 기능을 구현 할 수 있습니다 : 다음과 같이 관계를 만들 수는 LAZY 로딩을 사용하려면

// Obtain the participant list included in the original object (EAGER) 
var conference = 
Conference.findOne({ 
    attributes: ['field1', 'field2', ...], 
    where: {title: 'Conference A'}, 
    includes: [{ 
     attributes: ['field1', 'field2', ...], 
     model: Participant, 
     through: { model: 'ConferenceParticipants'} // You have to name the join table 
    }] 
    }) 
    .then(function(conference) { 
     // Here you will have the conference with the list of participants 

    }); 

이 sequelize 당신을 위해 그것을 구현, 당신 단지 방법을 아래에 호출 할 필요가 :

// Obtain the participant LAZY 
conference.getParticipants().then(function(participants) { 
    // participants is an array of participant 
}) 

// You can also pass filters to the getter method. 
// They are equal to the options you can pass to a usual finder method. 
conference.getParticipants({ where: 'id > 10' }).then(function(participants) { 
    // participants with an id greater than 10 :) 
}) 

// You can also only retrieve certain fields of a associated object. 
conference.getParticipants({attributes: ['title']}).then(function(participants) { 
    // retrieve participants with the attributes "title" and "id" 
}) 

document에 관계 구현을 sequelize에 대한 참조를 얻을 수 있습니다.