gpt4 book ai didi

model-view-controller - 使用 ORM 时在 Controller 上请求数据库数据是一个好习惯吗?

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

使用 ORM 时,在 Controller 上执行诸如 model-instance.findAll() 之类的事情是一个好习惯吗?

最佳答案

是的,在 Controller 中使用 Sequelize ORM 将被认为是很好的做法,假设它实现正确 - 错误处理等。

示例:

// `models` should contain model definitions

function MyController(app) {
const controller = this;

controller.getRecord = async (id, options) => {
try {
const records = await models.ModelName.findById(
where: {
id,
},
});
// do more things
return records;
} catch (err) {
// handle the error
return err;
}
};

return controller;
}

module.exports = MyController;

关于model-view-controller - 使用 ORM 时在 Controller 上请求数据库数据是一个好习惯吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52562339/

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