|
| 1 | +'use strict'; |
| 2 | +const moment = require("moment-timezone"); |
| 3 | +const { faker, } = require('@faker-js/faker'); |
| 4 | +const { mysqlTimeFormat, } = require("../../utils/time"); |
| 5 | + |
| 6 | +/** @type {import('sequelize-cli').Migration} */ |
| 7 | +module.exports = { |
| 8 | + up: async (queryInterface, Sequelize) => { |
| 9 | + const transaction = await queryInterface.sequelize.transaction(); |
| 10 | + try { |
| 11 | + const insertArray = []; |
| 12 | + for (let i = 0; i < 30; i++) { |
| 13 | + const category = await queryInterface.sequelize.query( |
| 14 | + "SELECT id FROM categories ORDER BY rand() LIMIT 1", |
| 15 | + { type: Sequelize.QueryTypes.SELECT }, |
| 16 | + ); |
| 17 | + |
| 18 | + if (0 === category.length) { |
| 19 | + throw new Error("No categories are available to select."); |
| 20 | + } |
| 21 | + |
| 22 | + const manufacturer = await queryInterface.sequelize.query( |
| 23 | + "SELECT id FROM manufacturers ORDER BY rand() LIMIT 1", |
| 24 | + { type: Sequelize.QueryTypes.SELECT }, |
| 25 | + ); |
| 26 | + |
| 27 | + if (0 === manufacturer.length) { |
| 28 | + throw new Error("No manufacturers are available to select."); |
| 29 | + } |
| 30 | + |
| 31 | + const productInsertResult = await queryInterface.sequelize.query( |
| 32 | + `INSERT INTO products(name, units, weight, categoriesId, price, description, manufacturersId, createdAt, updatedAt) |
| 33 | + VALUES (:name, :units, :weight, :categoriesId, :price, :description, :manufacturersId, :createdAt, :updatedAt)`, |
| 34 | + { |
| 35 | + replacements: { |
| 36 | + name: faker.commerce.productName(), |
| 37 | + units: faker.number.int({ min: 0, max: 1000, }), |
| 38 | + weight: faker.number.int({ min: 0, max: 100, }) + " kg", |
| 39 | + categoriesId: category[0].id, |
| 40 | + price: faker.number.float({ min: 0.1, max: 200, }), |
| 41 | + description: faker.number.int({ min: 0, max: 1, }) == 1 ? |
| 42 | + faker.lorem.paragraphs({ min: 1, max: 1, }, "\n") : |
| 43 | + null, |
| 44 | + manufacturersId: manufacturer[0].id, |
| 45 | + createdAt: moment().utc().format(mysqlTimeFormat), |
| 46 | + updatedAt: moment().utc().format(mysqlTimeFormat), |
| 47 | + }, |
| 48 | + type: Sequelize.QueryTypes.INSERT, |
| 49 | + } |
| 50 | + ); |
| 51 | + const productsId = productInsertResult[0]; |
| 52 | + for (let k = 0; k < faker.number.int({ min: 1, max: 5, }); k++) { |
| 53 | + const photo = "image string"; |
| 54 | + await queryInterface.sequelize.query( |
| 55 | + `INSERT INTO productPhotos(productsId, name, photo, type, createdAt, updatedAt) |
| 56 | + VALUES (:productsId, :name, :photo, :type, :createdAt, :updatedAt)`, |
| 57 | + { |
| 58 | + replacements: { |
| 59 | + name: faker.commerce.productName(), |
| 60 | + photo: photo, |
| 61 | + type: "image/png", |
| 62 | + createdAt: moment().utc().format(mysqlTimeFormat), |
| 63 | + updatedAt: moment().utc().format(mysqlTimeFormat), |
| 64 | + productsId, |
| 65 | + }, |
| 66 | + type: Sequelize.QueryTypes.INSERT, |
| 67 | + } |
| 68 | + ); |
| 69 | + } |
| 70 | + } |
| 71 | + await transaction.commit(); |
| 72 | + } catch (err) { |
| 73 | + await transaction.rollback(); |
| 74 | + throw err; |
| 75 | + } |
| 76 | + }, |
| 77 | + down: async (queryInterface, Sequelize) => { |
| 78 | + const transaction = await queryInterface.sequelize.transaction(); |
| 79 | + try { |
| 80 | + await queryInterface.bulkDelete('productPhotos', null, { transaction, }); |
| 81 | + await queryInterface.bulkDelete('products', null, { transaction, }); |
| 82 | + await transaction.commit(); |
| 83 | + } catch (err) { |
| 84 | + await transaction.rollback(); |
| 85 | + throw err; |
| 86 | + } |
| 87 | + } |
| 88 | +}; |
0 commit comments