Skip to content
This repository was archived by the owner on Sep 20, 2025. It is now read-only.

Commit dbf982a

Browse files
committed
Add demo-product seeders
1 parent 6b69860 commit dbf982a

File tree

3 files changed

+168
-0
lines changed

3 files changed

+168
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 < 15; i++) {
13+
insertArray.push({
14+
name: faker.internet.displayName(),
15+
createdAt: moment().utc().format(mysqlTimeFormat),
16+
updatedAt: moment().utc().format(mysqlTimeFormat),
17+
});
18+
}
19+
await queryInterface.bulkInsert(
20+
'manufacturers',
21+
insertArray,
22+
{ transaction },
23+
);
24+
await transaction.commit();
25+
} catch (err) {
26+
await transaction.rollback();
27+
throw err;
28+
}
29+
},
30+
down: async (queryInterface, Sequelize) => {
31+
const transaction = await queryInterface.sequelize.transaction();
32+
try {
33+
await queryInterface.bulkDelete('manufacturers', null, { transaction, });
34+
await transaction.commit();
35+
} catch (err) {
36+
await transaction.rollback();
37+
throw err;
38+
}
39+
}
40+
};
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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 < 15; i++) {
13+
insertArray.push({
14+
name: faker.internet.displayName(),
15+
createdAt: moment().utc().format(mysqlTimeFormat),
16+
updatedAt: moment().utc().format(mysqlTimeFormat),
17+
});
18+
}
19+
await queryInterface.bulkInsert(
20+
'categories',
21+
insertArray,
22+
{ transaction },
23+
);
24+
await transaction.commit();
25+
} catch (err) {
26+
await transaction.rollback();
27+
throw err;
28+
}
29+
},
30+
down: async (queryInterface, Sequelize) => {
31+
const transaction = await queryInterface.sequelize.transaction();
32+
try {
33+
await queryInterface.bulkDelete('categories', null, { transaction, });
34+
await transaction.commit();
35+
} catch (err) {
36+
await transaction.rollback();
37+
throw err;
38+
}
39+
}
40+
};
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
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

Comments
 (0)