-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworkout.js
60 lines (55 loc) · 1.22 KB
/
workout.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
const mongoose = require("mongoose");
const Schema = mongoose.Schema;
const workoutSchema = new Schema(
{
day: {
type: Date,
required: true
},
exercises: [
{
type: {
type: String,
trim: true,
required: "Enter an exercise type"
},
name: {
type: String,
trim: true,
required: "Enter an exercise name"
},
duration: {
type: Number,
required: "Enter an exercise duration in minutes"
},
weight: {
type: Number
},
reps: {
type: Number
},
sets: {
type: Number
},
distance: {
type: Number
}
}
]
},
{
toJSON: {
// include any virtual properties when data is requested
virtuals: true
}
}
);
// adds a dynamically-created property to schema
workoutSchema.virtual("totalDuration").get(function() {
// "reduce" array of exercises down to just the sum of their durations
return this.exercises.reduce((total, exercise) => {
return total + exercise.duration;
}, 0);
});
const Workout = mongoose.model("Workout", workoutSchema);
module.exports = Workout;