-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
40 lines (38 loc) · 1.14 KB
/
seed.js
File metadata and controls
40 lines (38 loc) · 1.14 KB
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
const restaurantsData = require('./sampleData.js');
const reviewsList = require('./sample_reviews.js');
const mongoose = require('mongoose');
const db = require('./db/mongodb.js');
const getReviews = () => {
// random length for how many reviews will render with minimum of 3 reviews
const randomLength = Math.ceil(Math.random() * 10) + 3;
const reviewsArray = [];
// looping up to the random length
for (let i = 1; i < randomLength; i++) {
// random index generator
const randomIdx = Math.floor(Math.random() * 100);
// push random comments into array using randomIdx
reviewsArray.push(reviewsList[randomIdx]);
}
// returns array of reviews
return reviewsArray;
};
let counter = 0;
restaurantsData.forEach((restaurant) => {
// for a restaurant, use schema to set values
const eachRestaurant = {
restaurantId: restaurant.id,
restaurantName: restaurant.name,
restaurantReviews: getReviews(),
};
// save each one into db
db.insertOne(eachRestaurant, (error) => {
if (error) {
throw error;
} else {
counter++;
if (counter === 119) {
mongoose.disconnect();
}
}
});
});