【问题标题】:React / Express / MySQL - Where to put MySQL transactions?React / Express / MySQL - 在哪里放置 MySQL 事务?
【发布时间】:2020-06-10 20:57:38
【问题描述】:

我正在使用 MySql 开发一个 React 前端 + Express 后端 API。

我在搞清楚在哪里进行 MySql 事务时遇到了一些麻烦。

我的 Express API 文件夹结构是这样的:

  • API
    • 型号/
    • 控制器/
    • 路线/
    • app.js


Models/ 文件夹包含将 MySql 表的骨架保存为类的文件,其中调用数据库的方法作为静态方法。 models/ 文件夹中的文件之一(Student.js)如下所示:

// models/Student.js

const sql = require('./db'); // DB connection in this file

class Student {
    constructor(student) {
        this.id_student = student.id_student;
        this.name = student.name;
        this.surname = student.surname;
        this.email = student.email;
        this.current_year = student.current_year;
        this.id_section = student.id_section;
    }

    static getAllStudents(callback) {
        sql.query('SELECT * FROM student', (error, results) => {
            if (error) {
                console.log(`Error : ${error}`);
                callback(null, error);
                return;
            }
            console.log(`Elèves : ${JSON.stringify(results)}`);
            callback(null, results);
        });
    };
}


在 controllers/ 文件夹中,student.js 文件如下所示:

// controllers/student.js

const Student = require('../models/Student');

exports.getAllStudents = (req, res) => {
    Student.getAllStudents((error, students) => {
        if (error) {
            res.status(400).send(`Error retriving students - getAllStudents from controller/student.js - : ${error}`);
        }
        else {
            res.status(200).json({ students });
        }
    })
};


在 routes/ 文件夹中,我只是调用相应的控制器,并根据路由使用它的方法。


关键是,我的模型/文件夹中有另一个文件(看起来像模型/Student.js):Section.js

我需要做一个事务来将值插入到学生表和部分表中(每个都表示为模型/文件夹中的单独文件,如解释的那样)。


问题:

  • 我应该在哪里进行此交易?在模型/文件夹中的哪个文件中? Student.js 还是 Section.js ?还是另一种文件?如果是这样,在哪个文件夹中以及如何?


非常感谢您花时间帮助我


----- 更新 ------

这是表格关系的图片:

Table relations

如您所见,Student表与Section有关系(其实我所说的Section表叫做section_relations表,是一个“链接表”)。

创建学生时,我首先检查学生表中的 id_section_group 和 id_section_promo 是否存在于 section_relations 表中。

如果没有,我想先创建部分(归档所有需要的 FK id)然后创建学生。这应该在一个事务中,将 FK id 插入到部分表中以创建部分,然后创建学生。

这应该在事务中完成,因为如果 2 个插入中的一个失败,它可以回滚。

非常感谢

【问题讨论】:

    标签: javascript mysql node.js reactjs express


    【解决方案1】:

    这取决于Student和Section的关系,有一点很清楚,Student模型应该只对student表和Section 模型应该只在 section 表上做事务,那么你需要在控制器中处理调用这些事务的逻辑,哪个控制器?我怎么说,这取决于关系和你想达到什么目标:

    • 如果学生更新了一个部分,那么你应该在学生中进行 控制器
    • 如果部分更新学生,则使用部分控制器
    • 如果他们没有关系,那么你处理他们中的每一个 相应的控制器。

    【讨论】:

    • Student 和 Section 表有 3 个关系(实际上 models/Student.js 文件中的 2 个 mores 字段链接到 Section 表)。我想做的是,在提交创建学生的表单时,如果学生部分不存在,则创建它然后创建学生。这应该在事务中完成,以免弄乱我的数据库表,因为我有可能回滚。问题是,在这种情况下,我应该在哪个文件中进行交易?感谢您的宝贵时间
    • 你应该在student控制器中创建事务并调用里面的section控制器进行其他操作,事务是一个promise,你可以使用then提交,catch进行回滚。
    • 你的意思是在学生模型中进行交易(因为我所有的查询都在模型/文件夹中)然后从学生模型调用函数到它的控制器?至于控制器,可以在一个控制器中调用几个不同的模型吗?非常感谢(你可以在我的帖子中看到表格+关系的更新和图片)
    猜你喜欢
    • 1970-01-01
    • 2015-03-12
    • 1970-01-01
    • 2011-03-24
    • 1970-01-01
    • 2011-03-22
    • 2012-04-01
    • 2013-07-30
    • 2014-02-02
    相关资源
    最近更新 更多