Skip to content

Instantly share code, notes, and snippets.

@anhtran
Last active December 30, 2015 16:39
Show Gist options
  • Save anhtran/7856331 to your computer and use it in GitHub Desktop.
Save anhtran/7856331 to your computer and use it in GitHub Desktop.
Seperate models file for Express App + Mongooes
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
});
});
};
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