-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathseed.js
More file actions
84 lines (72 loc) · 2.79 KB
/
Copy pathseed.js
File metadata and controls
84 lines (72 loc) · 2.79 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
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
require('dotenv').config();
require('./config/database');
const Course = require('./models/course')
const TeeDetail = require('./models/teeDetail')
// For better organization, the seed data is being stored in a separate data.js module
const data = require('./data');
// await needs an async function - use an async IIFE!
(async function() {
// Save the promises (or call right in the array if feeling frisky)
const resetCourse = Course.deleteMany({});
const resetTeeDetail = TeeDetail.deleteMany({});
// Promise.all will return a single promise that resolves
// only after all of the array's promises resolve
let results = await Promise.all([resetCourse, resetTeeDetail]);
// results will be an array of result objects!
console.log(results);
let course = new Course()
course.name = "Mount Prospect Golf Club"
course.parOut = data.coursePar[0]["Par"]
course.parIn = data.coursePar[1]["Par"]
course.parTotal = data.coursePar[2]["Par"]
course.holePars = data.holePars
course.holeIndexes = data.holeIndexes
await course.save()
console.log(course)
// let teeDetail = new TeeDetail()
// teeDetail.color = data.teeDetails[0]["color"]
// teeDetail.rating = data.teeDetails[0]["rating"]
// teeDetail.slope = data.teeDetails[0]["slope"]
// teeDetail.distanceOut = data.teeDetails[0]["out"]
// teeDetail.distanceIn = data.teeDetails[0]["in"]
// teeDetail.distanceTotal = data.teeDetails[0]["total"]
// teeDetail.holeDistances = data.teeDetails[0]["distances"]
// teeDetail.course = course._id
// await teeDetail.save()
// console.log(teeDetail)
async function saveTee(td) {
let teeDetail = new TeeDetail()
teeDetail.color = td["color"]
teeDetail.rating = td["rating"]
teeDetail.slope = td["slope"]
teeDetail.distanceOut = td["out"]
teeDetail.distanceIn = td["in"]
teeDetail.distanceTotal = td["total"]
teeDetail.holeDistances = td["distances"]
teeDetail.course = course._id
await teeDetail.save()
console.log(teeDetail)
}
await saveTee(data.teeDetails[0])
await saveTee(data.teeDetails[1])
await saveTee(data.teeDetails[2])
await saveTee(data.teeDetails[3])
// data.teeDetails.forEach(async (td) => {
// teeDetail.color = td["color"]
// teeDetail.rating = td["rating"]
// teeDetail.slope = td["slope"]
// teeDetail.distanceOut = td["out"]
// teeDetail.distanceIn = td["in"]
// teeDetail.distanceTotal = td["total"]
// teeDetail.holeDistances = td["distances"]
// teeDetail.course = course._id
// await teeDetail.save()
// console.log(teeDetail)
// })
// This time, provide the array of promises in-line
// results = await Promise.all([
// ]);
// console.log('Created golfers:', results[0])
// Lastly, use process.exit() to "cleanly" shut down the Node program
process.exit();
})();