gpt4 book ai didi

node.js - Node Js 和 Sequelize 模型从两个表中获取数据

转载 作者:行者123 更新时间:2023-12-03 22:28:39 25 4
gpt4 key购买 nike

我有一个模型,可以使用 node 和 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 中获取要使用此模型调用的数据。

我的问题是,有没有办法将连接语句添加到像上面那样创建的模型中,或者我是否需要创建一个新模型并将其作为单独的函数调用。第二种方法适用于我的情况,但我想保持简单,所以如果我可以在一个模型中获得所有内容,那么我会很高兴,否则我必须对另一种选择感到满意

最佳答案

您将需要 define users_references 的模型,并以与 sexesmarital_states 等相同的方式将其关联。当指定 include 的内容时,连接实际上会在查询时发生。

用法示例:

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

如果这看起来有点笨拙,我深表歉意 - 确实如此,因为我认为您的项目中有很多辅助逻辑不在帖子中。您可能想查看 the docs ,但不可否认,他们对如何从查询中的相关表中实际获取数据有点模糊,但它们应该足以涵盖您所描述的内容。

关于node.js - Node Js 和 Sequelize 模型从两个表中获取数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38483547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com