Model view controller 在使用ORM时,在控制器上请求db数据是一种好的做法吗?

Model view controller 在使用ORM时,在控制器上请求db数据是一种好的做法吗?,model-view-controller,sequelize.js,Model View Controller,Sequelize.js,当使用ORM时,在控制器上执行诸如model instance.findAll()之类的操作是否是一种好的做法?是的,在控制器中使用Sequelize ORM将被视为一种好的做法,假设它实现正确-错误处理等 示例: // `models` should contain model definitions function MyController(app) { const controller = this; controller.getRecord = async (id, opt

当使用ORM时,在控制器上执行诸如model instance.findAll()之类的操作是否是一种好的做法?

是的,在控制器中使用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;

视情况而定。你能给我们举一个你打算什么时候使用它的例子吗?