-
Notifications
You must be signed in to change notification settings - Fork 102
/
db.js
39 lines (33 loc) · 857 Bytes
/
db.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
var uri = 'mongodb://localhost:27017/test';
var _ = require('lodash');
var mongoose = require('mongoose');
mongoose.connect(uri);
var db = mongoose.connection;
db.on('error', console.error.bind(console, 'connection error:'));
db.once('open', function (callback) {
console.log('db connected');
});
var userSchema = mongoose.Schema({
username: String,
gender: String,
name: {
title: String,
first: String,
last: String
},
location: {
street: String,
city: String,
state: String,
zip: Number
}
});
userSchema.virtual('name.full').get(function () {
return _.startCase(this.name.first + ' ' + this.name.last);
});
userSchema.virtual('name.full').set(function (value) {
var bits = value.split(' ');
this.name.first = bits[0];
this.name.last = bits[1];
});
exports.User = mongoose.model('User', userSchema);