简体   繁体   English

Node Js和Sequalize模型从两个表中获取数据

[英]Node Js and Sequalize model to get data from two tables

I have a model that will return people to me using node and sequalize. 我有一个模型,该模型将使用节点和均衡使人们回到我身边。 The model looks like this 模型看起来像这样

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;
  }

This gets me the results that are stored in the dbo.people table which is great because that is what I want and this model works perfectly for that. 这使我得到了存储在dbo.people表中的结果,这很棒,因为这正是我想要的,并且该模型对此非常有效。 Now what I want to do is to add to this model and call data from another table. 现在,我要做的就是添加到该模型中并从另一个表中调用数据。 So I want to get the data from dbo.users_references to be called with this model. 因此,我想从dbo.users_references获取数据以使用此模型进行调用。

My question is, is there a way to add a join statement to a model that is created like the one above, or will I need to make a new model and call that as a separate function. 我的问题是,有没有一种方法可以将join语句添加到上面创建的模型中,或者我需要创建一个新模型并将其作为单独的函数调用。 The second way will work in my case, but I want to keep it simple, so if I can get it all in one model, then I would be happy, else I must just be mildly happy with the other option 第二种方法适用于我的情况,但是我想保持简单,因此,如果我可以将其全部集成到一个模型中,那么我会很高兴,否则我只能对另一个选择感到满意

You will need to define a model for users_references and associate it in the same way as you are doing with sexes , marital_states , etc. The join will actually occur at query time when specify what to include . 您将需要为users_references define一个模型,并以与处理sexesmarital_states等相同的方式将其关联。当指定要include时, marital_states实际上将在查询时发生。

Usage example: 用法示例:

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

I apologize if this seems a little hand-wavy - it is, since I gather there's a lot of helper logic in you project that's not in the post. 如果这看起来有点麻烦,我很抱歉-是的,因为我收集到您的项目中有很多辅助逻辑,这些逻辑不在发布中。 You might want to review the docs , but admittedly they're a little hazy on how to actually get the data from related tables in the queries, but they should cover enough for what you've described. 您可能想要查看这些文档 ,但是坦白地说,它们对于如何从查询中的相关表中实际获取数据有些困惑,但是对于您所描述的内容,它们应该足够介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM