-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.js
52 lines (43 loc) · 1.04 KB
/
models.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
import db from './db.js'
const exerciseSchema = new db.Schema({
description: {
type: String,
required: true
},
duration: {
type: Number,
required: true
},
_date: {
type: Date,
required: true
},
user: {
type: db.Schema.Types.ObjectId,
ref: 'User'
}
});
exerciseSchema.virtual('date').get(function() {
return this._date.toDateString();
});
const Exercise = db.model("Exercise", exerciseSchema);
const userSchema = new db.Schema({
username: {
type: String,
required: true
}
});
userSchema.virtual('log', {
ref:'Exercise',
localField: '_id',
foreignField: 'user'
})
userSchema.virtual('count').get(function () {
return this.log?.length ?? 0;
});
userSchema.set('toObject', { virtuals: true });
userSchema.set('toJSON', { virtuals: true });
exerciseSchema.set('toObject', { virtuals: true });
exerciseSchema.set('toJSON', { virtuals: true });
const User = db.model("User", userSchema);
export { Exercise, User };