Sindbad~EG File Manager
const Sequelize = require("sequelize");
// const { User, Order, Product } = require("./models"); // Import your Sequelize models
const db = require("../models");
const createOrderWithProducts = async () => {
const transaction = await db.transaction();
try {
// Perform database operations within the transaction
const user = await db.User.create({ name: "John Doe" }, { transaction });
const order = await db.Order.create(
{ userId: user.id, totalAmount: 100 },
{ transaction }
);
const product1 = await Product.create(
{ name: "Product 1", price: 50 },
{ transaction }
);
const product2 = await db.Product.create(
{ name: "Product 2", price: 30 },
{ transaction }
);
// Associate products with the order
await order.addProducts([product1, product2], { transaction });
// Commit the transaction if everything is successful
await transaction.commit();
console.log("Order created successfully!");
} catch (error) {
// Rollback the transaction if an error occurs
await transaction.rollback();
console.error("Transaction failed:", error);
}
};
createOrderWithProducts();
Sindbad File Manager Version 1.0, Coded By Sindbad EG ~ The Terrorists