Last active
December 30, 2015 16:39
-
-
Save anhtran/7856331 to your computer and use it in GitHub Desktop.
Seperate models file for Express App + Mongooes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ProfileProvider = require('./profile_models').ProfileProvider; | |
var ProfileModel = new ProfileProvider(); | |
exports.all_profiles = function(req, res) { | |
ProfileModel.findAll(function(profiles){ | |
res.render('profiles', { | |
profile_list: profiles | |
}); | |
}); | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var mongoose = require('mongoose'); | |
mongoose.connect('mongodb://localhost/name_of_database'); | |
var Schema = mongoose.Schema, | |
ObjectId = Schema.ObjectId; | |
/******** SCHEMA DEFINATION *********/ | |
var ProfileSchema = new Schema({ | |
name: {type: String, index: true, require: true}, | |
birthday: Date, | |
localtion: String, | |
email: {type: String, require: true}, | |
joined_at: Date | |
}); | |
/******** SCHEMA MANAGER *********/ | |
mongoose.model('Profile', ProfileSchema); | |
var Profile = mongoose.model('Profile'); | |
ProfileProvider = function() {}; | |
// Create new | |
ProfileProvider.prototype.save = function(params, cb) { | |
var p = new Profile(params); | |
p.save(); | |
cb(p); | |
}; | |
// List all | |
ProfileProvider.prototype.findAll = function(cb) { | |
Profile.find() | |
.sort({name: 1, joined_at: -1}) | |
.exec(function(err, docs){ | |
cb(docs); | |
}); | |
}; | |
exports.ProfileProvider = ProfileProvider; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment