客户希望我们在他的电子商务网站上使用 Mysql,就像我们在后端所做的那样。目前我正在使用 mysql,当要部署时我们将迁移到某个提供商
这是我们如何初始化数据库的序列片段
const { Sequelize } = require("sequelize");
// Create a Sequelize instance
const sequelize = new Sequelize("[db_name]", "[username]", "[password]", {
host: "localhost", // Replace with your MySQL host, e.g., '127.0.0.1'
dialect: "mysql", // Choose the database dialect (MySQL in this case)
port: 3306, // Default MySQL port is 3306
// Pool configuration used to manage connections
pool: {
max: 5, // Maximum number of connection in pool
min: 0, // Minimum number of connection in pool
acquire: 30000, // Maximum time (ms) that pool will try to get connection before throwing error
idle: 10000, // Maximum time (ms) that a connection can be idle before being released
},
// Optional logging configuration (can be a function or boolean)
logging: false, // Set to 'console.log' to see SQL queries
});
// Test the connection
sequelize
.authenticate()
.then(() => {
console.log("Connection has been established successfully.");
})
.catch((err) => {
console.error("Unable to connect to the database:", err);
});
module.exports = sequelize;
以前,我对硬编码插入某些模型中的偏好模型不感兴趣,只是为了制作模型并按应有的方式使用它们,但无论如何它都没有帮助,而且只有在某些模型中初始化的偏好模型没有创建
require("dotenv").config();
const sequelize = require("../config/database"); // Assuming you've created the Sequelize instance in config/database.js
const User = require("../models/user");
const Address = require("../models/address");
const Merchant = require("../models/merchant");
const Product = require("../models/product");
const Cart = require("../models/cart");
const CartItem = require("../models/cartitem");
const Order = require("../models/order");
const Review = require("../models/review");
const Wishlist = require("../models/wishlist");
const setupDB = async () => {
try {
// Test the MySQL connection using Sequelize
await sequelize.authenticate();
console.log("\x1b[32m%s\x1b[0m", "✓ MySQL Connected!"); // Green color for success message
// Sync the models with the database
await sequelize.sync({ alter: true }); // Change to { force: true } if you want to drop existing tables
console.log("\x1b[32m%s\x1b[0m", "✓ Database tables synced!"); // Success message for syncing
// Set up associations
User.hasMany(Address, { foreignKey: "userId", as: "addresses" });
Address.belongsTo(User, { foreignKey: "userId", as: "users" });
Merchant.hasMany(Product, { foreignKey: "merchantId", as: "products" });
Product.belongsTo(Merchant, { foreignKey: "merchantId", as: "merchants" });
User.hasMany(Cart, { foreignKey: "userId", as: "carts" });
Cart.belongsTo(User, { foreignKey: "userId", as: "users" });
Cart.hasMany(CartItem, { foreignKey: "cartId", as: "items" });
CartItem.belongsTo(Cart, { foreignKey: "cartId", as: "carts" });
CartItem.belongsTo(Product, { foreignKey: "productId", as: "products" });
User.hasMany(Order, { foreignKey: "userId", as: "orders" });
Order.belongsTo(User, { foreignKey: "userId", as: "users" });
Order.belongsTo(Cart, { foreignKey: "cartId", as: "carts" });
Product.hasMany(Review, { foreignKey: "productId", as: "reviews" });
Review.belongsTo(Product, { foreignKey: "productId", as: "products" });
User.hasMany(Review, { foreignKey: "userId", as: "reviews" });
Review.belongsTo(User, { foreignKey: "userId", as: "users" });
User.hasMany(Wishlist, { foreignKey: "userId", as: "wishlists" });
Wishlist.belongsTo(User, { foreignKey: "userId", as: "users" });
Wishlist.belongsTo(Product, { foreignKey: "productId", as: "products" });
} catch (error) {
console.log(
"\x1b[31m%s\x1b[0m",
"✗ Unable to connect to the database:",
error
); // Red color for error message
return null;
}
};
module.exports = setupDB;
我还需要一些帮助,因为我正在使用托管服务(我不想,但我的老板出于某种原因想使用托管服务)现在我想在线部署 mysql 数据库,那么我可以用它做什么?
如果您正在开发小型项目,请考虑使用 AWS。它提供 750 个免费小时,其成本效益使其成为绝佳选择。 AWS 链接