-
Notifications
You must be signed in to change notification settings - Fork 0
/
seed.js
61 lines (54 loc) · 1.7 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
const { Genre } = require("./models/genre");
const { Movie } = require("./models/movie");
const mongoose = require("mongoose");
const config = require("config");
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 }
]
}
];
async function seed() {
await mongoose.connect(config.get("db"),{useNewUrlParser: true ,useUnifiedTopology: true});
await Movie.deleteMany({});
await Genre.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);
}
mongoose.disconnect();
console.info("Done!");
}
seed();