-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
81 lines (65 loc) · 2.33 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
require('dotenv').config();
const mongoose = require('mongoose');
const bcrypt = require('bcryptjs');
const User = require('./models/user');
const Movie = require('./models/movie')
const seedUsers = async () => {
try {
await User.deleteMany();
const mockUsers = [
{
firstName: 'User',
lastName: 'Smith',
email: '[email protected]',
password: await bcrypt.hash('user123',10),
role: 'user',
profilePicture: 'https://images.unsplash.com/photo-1535713875002-d1d0cf377fde?ixlib=rb-4.0.3&ixid=M3wxMjA3fDB8MHxzZWFyY2h8Mnx8dXNlciUyMHByb2ZpbGV8ZW58MHx8MHx8fDA%3D&w=1000&q=80',
},
{
firstName: 'Admin',
lastName: 'Smith',
email: '[email protected]',
password: await bcrypt.hash('admin123',10),
role: 'admin',
profilePicture: 'https://pixinvent.com/materialize-material-design-admin-template/laravel/demo-4/images/user/12.jpg',
}
];
await User.create(mockUsers);
console.log('Mockup users created successfully');
} catch(error) {
console.log('Error occured while seeding Users : ',error)
}
};
const seedMovies = async () => {
try {
await Movie.deleteMany();
const seedMoviesJson = require('./data/seedMoviesVideos.json');
await Movie.create(seedMoviesJson);
console.log('Seed movies added successfully');
} catch(error) {
console.log('Error occured while seeding movies to database', error);
}
};
const seedAll = async () => {
// Guard
const arguments = process.argv;
if (!arguments.includes('i-am-a-pro')) {
console.log('WARNING!!');
console.log('You are about to replace all the data in your database');
console.log('with mockup / seed data ! This operation is ireversable !!');
console.log('If you know what you are doing, add "i-am-a-pro" argument.');
process.exit(1);
};
// Connect to MongoDB
await mongoose.connect(process.env.MONGO_URI, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
// Run the seed functions
await seedUsers();
await seedMovies();
// Finish up
console.log('Done seeding');
process.exit(0);
}
seedAll();