-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
75 lines (67 loc) · 1.89 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
const { Genre } = require("./models/genre");
const { Movie } = require("./models/movie");
const { User } = require("./models/user");
const mongoose = require("mongoose");
const config = require("config");
const db = config.get("db");
const data = [
{
name: "Comedy",
movies: [
{ title: "Airplane", numberInStock: 5, dailyRentalRate: 2 },
{ title: "The Hangover", numberInStock: 10, dailyRentalRate: 2 },
{ title: "Wedding Crashers", numberInStock: 15, dailyRentalRate: 2 },
],
},
{
name: "Action",
movies: [
{ title: "Die Hard", numberInStock: 5, dailyRentalRate: 2 },
{ title: "Terminator", numberInStock: 10, dailyRentalRate: 2 },
{ title: "The Avengers", numberInStock: 15, dailyRentalRate: 2 },
],
},
{
name: "Romance",
movies: [
{ title: "The Notebook", numberInStock: 5, dailyRentalRate: 2 },
{ title: "When Harry Met Sally", numberInStock: 10, dailyRentalRate: 2 },
{ title: "Pretty Woman", numberInStock: 15, dailyRentalRate: 2 },
],
},
{
name: "Thriller",
movies: [
{ title: "The Sixth Sense", numberInStock: 5, dailyRentalRate: 2 },
{ title: "Gone Girl", numberInStock: 10, dailyRentalRate: 2 },
{ title: "The Others", numberInStock: 15, dailyRentalRate: 2 },
],
},
];
const dataUsers = [
{ name: "Seed User", email: "[email protected]", password: "password" },
];
async function seed() {
await mongoose.connect(db);
await Movie.deleteMany({});
await Genre.deleteMany({});
await User.deleteMany({});
for (let genre of data) {
const { _id: genreId } = await new Genre({ name: genre.name }).save();
const movies = genre.movies.map((movie) => ({
...movie,
genre: { _id: genreId, name: genre.name },
}));
await Movie.insertMany(movies);
}
for (let user of dataUsers) {
await new User({
name: user.name,
email: user.email,
password: user.password,
}).save();
}
mongoose.disconnect();
console.info("Done!");
}
seed();