-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseedData.js
84 lines (76 loc) · 2.56 KB
/
seedData.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
const dotenv = require('dotenv');
const fs = require('fs');
const colors = require('colors');
const db = require('./config/db');
const faker = require('faker');
// Carregar variáveis ENV
dotenv.config({ path: './config/config.env' });
// Carregar models
const Product = require('./models/Product');
const Category = require('./models/Category');
const User = require('./models/User');
const Order = require('./models/Order');
// Conectar ao MongoDB
db().then();
// Ler os arquivos JSON
const products = JSON.parse(fs.readFileSync(`${__dirname}/_seedData/products.json`, 'utf-8'));
const users = JSON.parse(fs.readFileSync(`${__dirname}/_seedData/user.json`, 'utf-8'));
const orders = JSON.parse(fs.readFileSync(`${__dirname}/_seedData/order.json`, 'utf-8'));
const categories = JSON.parse(fs.readFileSync(`${__dirname}/_seedData/category.json`, 'utf-8'));
// Importar dados de amostra
const importData = async () => {
try {
await Product.create(products);
await Order.create(orders);
await User.create(users);
await Category.create(categories);
console.log('Dados importados com sucesso'.green.inverse);
process.exit();
} catch (error) {
console.log(error);
}
}
const fakerDataGenerator = async () => {
try {
for (let i = 0; i < 50; i++) {
await Product.create({
"title": faker.commerce.productName(),
"price": faker.commerce.price(),
"description": faker.commerce.productDescription()
});
await Category.create({
"title": faker.commerce.department()
});
await User.create({
"first_name": faker.name.firstName(),
"last_name": faker.name.lastName(),
"email": faker.internet.email(),
"password": faker.internet.password()
});
}
console.log('Dados gerados com sucesso'.green.inverse);
process.exit();
} catch (error) {
console.log(error);
}
}
// Delete os dados do DB
const deleteData = async () => {
try {
await Product.deleteMany();
await User.deleteMany();
await Category.deleteMany();
await Order.deleteMany();
console.log('Dados deletados com sucesso'.red.inverse);
process.exit();
} catch (error) {
console.log(error);
}
}
if (process.argv[2] === '-i') {
importData().then();
} else if (process.argv[2] === '-f') {
fakerDataGenerator().then();
} else if (process.argv[2] === '-d') {
deleteData().then();
}