Skip to content

Instantly share code, notes, and snippets.

@r01010010
Last active August 29, 2015 14:09
Show Gist options
  • Save r01010010/ca68f0f4feaa0f742355 to your computer and use it in GitHub Desktop.
Save r01010010/ca68f0f4feaa0f742355 to your computer and use it in GitHub Desktop.
/**
* Dependencies
*/
fs = require('fs');
/**
* DB Initializer
*/
var i_mongoose = {
mongoose: null,
db: null,
/**
* Connects to mongodb, sets handlers, inits models.
* @param mongoose:mongoose
* @param db:config.db
* @returns {i_mongoose}
*/
init: function(mongoose, db){
this.mongoose = mongoose;
this.db = db;
this.connect(db);
this.set_handlers();
this.init_models();
return this;
},
/**
* Connects to the given mongodb database
* @param [db:config.db]
* @returns {i_mongoose}
*/
connect: function(db){
var options = {
server: {
socketOptions: {
keepAlive: 1
}
}
};
this.mongoose.connect(db || this.db, options);
return this;
},
/**
* Initializes model classes
* @returns {i_mongoose}
*/
init_models: function(){
// todo: /server/models to config
var models_path = global.base_dir+'/server/models';
// Bootstrap models
fs.readdirSync(models_path).forEach(function requireModels(file) {
if (~file.indexOf('.js')) require(models_path + '/' + file);
});
return this;
},
/**
* Sets error and other event handlers for mongodb's connection
* @returns {i_mongoose}
*/
set_handlers: function(){
// Error handler
this.mongoose.connection.on('error', function (err) {
console.log(err);
});
// Reconnect when closed
this.mongoose.connection.once('disconnected', function () {
this.connect();
});
return this;
}
}
module.exports = i_mongoose;
var mongoose = require('mongoose'),
VTor = require('validator'),
Schema = mongoose.Schema;
/**
* Activity Schema
*/
var PostSchema = new Schema({
email: { type: String },
ts_creation: { type: Date, default: Date.now() },
description: { type: String },
town: { type: String }
});
/**
* Methods
*/
PostSchema.methods = {
savePost: function (cb) {
var self = this
this.validate(function (err) {
// If error
if (err) return cb(err);
// Save
self.save(cb);
})
}
}
/**
* Middleware
*/
PostSchema.pre('validate', function(next){
this.schema.paths['email'] = VTor.normalizeEmail(this.schema.paths['email'], null);
next();
});
/**
* Statics
*/
PostSchema.statics = {
/**
* Find activity by id
*
* @param {ObjectId} id
* @param {Function} cb
* @api private
*/
load: function (id, cb) {
this.findOne({ _id : id })
.exec(cb)
}
}
module.exports = mongoose.model('Post', PostSchema);
/**
* Module dependencies.
*/
var express = require('express')
, fs = require('fs')
, passport = require('passport')
, env = process.env.NODE_ENV || 'development'
, config = require('./config/config')[env]
, mongoose = require('mongoose');
/**
* Globals
*/
g = global;
g.wizco = {};
g.wizco.base_dir = __dirname;
// Initializers
// DB Mongoose ORM and Model classes initialitation
require('./inits/i-mongoose').init(mongoose, config.db);
var post = mongoose.model('post');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment