-
Notifications
You must be signed in to change notification settings - Fork 817
/
seed.js
120 lines (109 loc) · 4.27 KB
/
seed.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
const chalk = require('chalk');
const bcrypt = require('bcryptjs');
const mongoose = require('mongoose');
const { faker } = require('@faker-js/faker');
const setupDB = require('./db');
const { ROLES } = require('../constants');
const User = require('../models/user');
const Brand = require('../models/brand');
const Product = require('../models/product');
const Category = require('../models/category');
const args = process.argv.slice(2);
const email = args[0];
const password = args[1];
const NUM_PRODUCTS = 100;
const NUM_BRANDS = 10;
const NUM_CATEGORIES = 10;
const seedDB = async () => {
try {
let categories = [];
console.log(`${chalk.blue('✓')} ${chalk.blue('Seed database started')}`);
if (!email || !password) throw new Error('Missing arguments');
const existingUser = await User.findOne({ email });
if (!existingUser) {
console.log(`${chalk.yellow('!')} ${chalk.yellow('Seeding admin user...')}`);
const user = new User({
email,
password,
firstName: 'admin',
lastName: 'admin',
role: ROLES.Admin
});
const salt = await bcrypt.genSalt(10);
const hash = await bcrypt.hash(user.password, salt);
user.password = hash;
await user.save();
console.log(`${chalk.green('✓')} ${chalk.green('Admin user seeded.')}`);
} else {
console.log(`${chalk.yellow('!')} ${chalk.yellow('Admin user already exists, skipping seeding for admin user.')}`);
}
const categoriesCount = await Category.countDocuments();
if (categoriesCount >= NUM_CATEGORIES) {
console.log(`${chalk.yellow('!')} ${chalk.yellow('Sufficient number of categories already exist, skipping seeding for categories.')}`);
categories = await Category.find().select('_id');
} else {
for (let i = 0; i < NUM_CATEGORIES; i++) {
const category = new Category({
name: faker.commerce.department(),
description: faker.lorem.sentence(),
isActive: true
});
await category.save();
categories.push(category);
}
console.log(`${chalk.green('✓')} ${chalk.green('Categories seeded.')}`);
}
const brandsCount = await Brand.countDocuments();
if (brandsCount >= NUM_BRANDS) {
console.log(`${chalk.yellow('!')} ${chalk.yellow('Sufficient number of brands already exist, skipping seeding for brands.')}`);
} else {
for (let i = 0; i < NUM_BRANDS; i++) {
const brand = new Brand({
name: faker.company.name(),
description: faker.lorem.sentence(),
isActive: true
});
await brand.save();
}
console.log(`${chalk.green('✓')} ${chalk.green('Brands seeded.')}`);
}
const productsCount = await Product.countDocuments();
if (productsCount >= NUM_PRODUCTS) {
console.log(`${chalk.yellow('!')} ${chalk.yellow('Sufficient number of products already exist, skipping seeding for products.')}`);
} else {
const brands = await Brand.find().select('_id');
for (let i = 0; i < NUM_PRODUCTS; i++) {
const randomCategoryIndex = faker.number.int(categories.length - 1);
const product = new Product({
sku: faker.string.alphanumeric(10),
name: faker.commerce.productName(),
description: faker.lorem.sentence(),
quantity: faker.number.int({ min: 1, max: 100 }),
price: faker.commerce.price(),
taxable: faker.datatype.boolean(),
isActive: true,
brand: brands[faker.number.int(brands.length - 1)]._id,
category: categories[randomCategoryIndex]._id
});
await product.save();
await Category.updateOne({ _id: categories[randomCategoryIndex]._id }, { $push: { products: product._id } });
}
console.log(`${chalk.green('✓')} ${chalk.green('Products seeded and associated with categories.')}`);
}
} catch (error) {
console.log(`${chalk.red('x')} ${chalk.red('Error while seeding database')}`);
console.log(error);
return null;
} finally {
await mongoose.connection.close();
console.log(`${chalk.blue('✓')} ${chalk.blue('Database connection closed!')}`);
}
};
(async () => {
try {
await setupDB();
await seedDB();
} catch (error) {
console.error(`Error initializing database: ${error.message}`);
}
})();