-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListingSchema.js
More file actions
executable file
·36 lines (31 loc) · 981 Bytes
/
ListingSchema.js
File metadata and controls
executable file
·36 lines (31 loc) · 981 Bytes
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
/* 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({
/* your code here */
code: {type: String, required: true, unique: 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) {
/* your code here */
var currentDate = new Date();
this.updated_at = currentDate;
if(!this.created_at)
{
this.created_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;