2016-07-20 98 views
0

我有一個模型將使用節點和sequalize將人員返回給我。這個模型看起來像這樣節點Js和Sequalize模型從兩個表中獲取數據

class DbModel implements IModelMaker { 

    static _model: IDatabaseModel = null; 

    getModel(db: ISequelize): IDatabaseModel { 
    if (DbModel._model) 
     return DbModel._model; 
    const options = helper.getDatabaseDefaultOptions('people'); 
    const entity: any = {}; 
    entity['people_id'] = {type: Sequelize.UUID, primaryKey: true, 'defaultValue': Sequelize.UUIDV4}; 
    entity['sexes_id'] = helper.getForeignKey('sexes_id', sexes.getModel(db)); 
    entity['marital_states_id'] = helper.getForeignKey('marital_states_id', marital_states.getModel(db)); 
    entity['employment_states_id'] = helper.getForeignKey('employment_states_id', employment_states.getModel(db)); 
    entity['linkedins_id'] = helper.getForeignKey('linkedins_id', linkedins.getModel(db)); 
    entity['facebooks_id'] = helper.getForeignKey('facebooks_id', facebooks.getModel(db)); 
    entity['first_names'] = Sequelize.TEXT; 
    entity['surnames'] = Sequelize.TEXT; 
    entity['name_to_call_user'] = Sequelize.TEXT; 
    entity['id_number'] = Sequelize.TEXT; 
    entity['passport_number'] = Sequelize.TEXT; 
    entity['birth_date'] = Sequelize.DATEONLY; 
    entity['tax_number'] = Sequelize.TEXT; 
    entity['vat_number'] = Sequelize.TEXT; 
    entity['message_to_show_on_user_invoice'] = Sequelize.TEXT; 
    entity['workplace_name'] = Sequelize.TEXT; 
    entity['is_verified'] = Sequelize.BOOLEAN; 
    entity['has_private_profile_enabled'] = Sequelize.BOOLEAN; 
    entity['mobile_phone'] = Sequelize.TEXT; 
    entity['occupations_id'] = Sequelize.UUID; 
    entity['profile_photo_url'] = Sequelize.TEXT; 
    entity['private_profile_url'] = Sequelize.TEXT; 
    entity['public_profile_url'] = Sequelize.TEXT; 
    entity['description'] = Sequelize.TEXT; 
    entity['created_at'] = Sequelize.DATE; 
    const result = db.define('dbo.people', entity, options); 
    result.belongsTo(sexes.getModel(db), {foreignKey: 'sexes_id'}); 
    result.belongsTo(marital_states.getModel(db), {foreignKey: 'marital_states_id'}); 
    result.belongsTo(employment_states.getModel(db), {foreignKey: 'employment_states_id'}); 
    result.belongsTo(linkedins.getModel(db), {foreignKey: 'linkedins_id'}); 
    result.belongsTo(facebooks.getModel(db), {foreignKey: 'facebooks_id'}); 
    DbModel._model = result; 
    return result; 
    } 

這讓我被存儲在dbo.people表這是偉大的,因爲這就是我想要的結果和這個模型完全適用於這一點。 現在我想要做的就是添加到這個模型並從另一個表中調用數據。所以我想從這個模型中調用dbo.users_references的數據。

我的問題是,有沒有辦法連接語句添加到就像是一個上面創建的模型,或者我將需要一個新的模式並調用作爲一個單獨的函數。第二種方式將在我的情況下工作,但我想保持它的簡單,所以如果我能得到這一切在一個模型的話,我會很高興,否則我一定只是與其他選項輕度幸福

回答

1

你需要defineusers_references一個模型,它以同樣的方式,你與sexesmarital_states做聯想等將在查詢時指定什麼include實際發生的加入。

用例:

DbModel.findAll({ 
    where: ..., 
    include: [ 
    { model: employment_states.getModel(db) }, 
    { model: users_references.getModel(db) } 
    ] 
}); 

我道歉,如果這似乎有點手波浪 - 它是,因爲我收集有很多幫手邏輯在你的項目,這不是在後。您可能需要查看the docs,但不可否認的是,他們對如何從查詢中的相關表中實際獲取數據有點朦朧,但它們應該覆蓋您所描述的內容。