Total Complexity | 2 |
Complexity/F | 2 |
Lines of Code | 41 |
Function Count | 1 |
Duplicated Lines | 0 |
Ratio | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | 'use strict'; |
||
2 | |||
3 | const mongoose = require('mongoose'); |
||
4 | const Schema = mongoose.Schema; |
||
5 | |||
6 | // Author Schema definition |
||
7 | |||
8 | const AuthorSchema = new Schema({ |
||
9 | name: { |
||
10 | type: String, |
||
11 | required: true |
||
12 | }, |
||
13 | birth_year: { |
||
14 | type: Number, |
||
15 | required: true, |
||
16 | }, |
||
17 | bio: { |
||
18 | type: String |
||
19 | }, |
||
20 | country: { |
||
21 | type: String |
||
22 | }, |
||
23 | createdAt: { |
||
24 | type: Date, |
||
25 | default: Date.now |
||
26 | } |
||
27 | }, { |
||
28 | versionKey: false |
||
29 | }); |
||
30 | |||
31 | // Set the createdAt parameter equal to the current time |
||
32 | AuthorSchema.pre('save', next => { |
||
33 | const now = new Date(); |
||
34 | if(!this.createdAt) { |
||
35 | this.createdAt = now; |
||
36 | } |
||
37 | next(); |
||
38 | }); |
||
39 | |||
40 | // Exports the AuthorSchema for use elsewhere |
||
41 | module.exports = mongoose.model('author', AuthorSchema); |