【发布时间】:2020-10-28 20:56:53
【问题描述】:
我正在尝试在不使用 Sequelize 的情况下使用 node.js 和 mysql 制作具有多对多关系的产品和类别。还是我一定要使用它?
在连接到具有关系的数据库创建表时:
connection.connect((err) => {
if (err) {
return console.error("error: " + err.message);
} else {
console.log("Connected to the MySQL server.");
let createProducts = `CREATE TABLE IF NOT EXISTS products(
product_id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR (255) NOT NULL,
description VARCHAR (255),
price DECIMAL (19,2),
sku VARCHAR (255),
image VARCHAR (255),
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE now())`;
let createCategories = `CREATE TABLE IF NOT EXISTS categories(
category_id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
name VARCHAR (255) NOT NULL,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW())`;
let createProductsCategories = `CREATE TABLE IF NOT EXISTS products_categories(
id INT UNSIGNED PRIMARY KEY NOT NULL AUTO_INCREMENT,
product_id INT UNSIGNED NOT NULL,
category_id INT UNSIGNED NOT NULL,
FOREIGN KEY (product_id) REFERENCES products (product_id) ON DELETE CASCADE,
FOREIGN KEY (category_id) REFERENCES categories (category_id) ON DELETE CASCADE,
created_at TIMESTAMP NOT NULL DEFAULT NOW(),
updated_at TIMESTAMP NOT NULL DEFAULT NOW() ON UPDATE NOW())`;
connection.query(createProducts, function (err, results, fields) {
if (err) {
console.log(err.message);
}
});
connection.query(createCategories, function (err, results, fields) {
if (err) {
console.log(err.message);
}
});
connection.query(createProductsCategories, function (err, results, fields) {
if (err) {
console.log(err.message);
}
});
}
});
我还应该在产品和类别模型/控制器中包含哪些内容才能使它们之间的关系起作用?并且在创建具有类别的产品时会将数据插入到 products_categories 表中?
产品型号:
const Categories = require("category.model");
// constructor
const Product = function (product) {
this.name = product.name;
this.description = product.description;
this.price = product.price;
this.sku = product.sku;
this.image = product.image;
};
Product.create = (newProduct, result) => {
mysql.query("INSERT INTO products SET ?", newProduct, (err, res) => {
if (err) {
result(err, null);
return;
}
result(null, {id: res.insertId, ...newProduct});
});
};
产品控制器:
const Product = require("../models/product.model.js");
exports.create = (req, res) => {
if (!req.body) {
res.status(400).send({
message: "Content can not be empty!"
});
}
const product = new Product({
name: req.body.name,
description: req.body.description,
price: req.body.price
});
Product.create(product, (err, data) => {
if (err)
res.status(500).send({
message:
err.message || "Error occurred while creating the new Product."
});
else res.send(data);
});
};
产品路线:
module.exports = app => {
const products = require("../controllers/product.controller.js");
app.post("/products", products.create);
类别型号:
const Category = function (category) {
this.name = category.name;
};
Category.create = (newCategory, result) => {
mysql.query(`INSERT INTO categories SET ?`, newCategory, (err, res) => {
if (err) {
result(err, null);
return;
}
result(null, {id: res.insertId, ...newCategory});
});
};
【问题讨论】:
标签: mysql node.js many-to-many