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