-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListingSchema.js
executable file
·39 lines (30 loc) · 1.04 KB
/
ListingSchema.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
/* Import mongoose and define any variables needed to create the schema */
var mongoose = require('mongoose'),
Schema = mongoose.Schema;
/* Create your schema */
var listingSchema = new Schema({
code: { type: String, required: true },
name: { type: String, required: true },
coordinates: {
latitude: Number,
longitude: Number
},
address: String,
created_at: Date,
updated_at: Date
});
/* create a 'pre' function that adds the updated_at (and created_at if not already there) property */
listingSchema.pre('save', function (next) {
var currentDate = new Date();
//if the created_at date of the listing object does not exits, sets it to the current date
if (!this.created_at) {
this.created_at = currentDate;
}
//sets updated_at date to current date
this.updated_at = currentDate;
next();
});
/* Use your schema to instantiate a Mongoose model */
var Listing = mongoose.model('Listing', listingSchema);
/* Export the model to make it avaiable to other parts of your Node application */
module.exports = Listing;