2017-04-20 1 views
0

Spécification pour ma méthode d'instance de conférence:Sequelize: 'findAll' dans la méthode d'instance getParticipants()?

getParticipants(): Promise -> array des participants

modèle de la conférence:

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 
    } 

modèle Participant:

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 
    } 

Question:

je peux donc faire une participant.findAll dans mon modèle d'instance de conférence ou non? Quand est-ce que je reçois un tableau avec un findAll?

je l'aurais fait comme ça:

// 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); 
       }); 
      }); 
     }, 

Répondre

2

lazy loading est mis en œuvre par sequelize lorsque vous faites des relations entre les tables. Vous pourriez faire une relation comme suit:

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

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

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

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

Ensuite, vous pouvez mettre en œuvre le chargement EAGER lorsque vous interrogez votre base de données comme:

// 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 

    }); 

Si vous souhaitez utiliser le chargement LAZY, sequelize mettre en œuvre pour vous, vous juste besoin d'appeler les méthodes ci-dessous:

// 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" 
}) 

Vous pouvez obtenir une référence à la mise en œuvre sequelize de la relation dans le prochain document.