Skip to content

Instantly share code, notes, and snippets.

@unusualbob
Created December 14, 2012 21:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unusualbob/4288643 to your computer and use it in GitHub Desktop.
Save unusualbob/4288643 to your computer and use it in GitHub Desktop.
Mongoose Connection Deal
var mongoose = require('mongoose')
, Schema = mongoose.Schema;
//Define our schema(s)
var ConfigSchema = new Schema({
name: { type: String, required: true }
, USERID: { type: String, required: true }
, ROOMID: { type: String, required: true }
});
//Create our mongoose object based on that schema
var Config = mongoose.model('Config', ConfigSchema);
//Create db object
var db = mongoose.createConnection();
//Create error only db object
var errorObject = {
findOne : function() { //This is one of several calls, need to replicate for find, count, save, etc
return {
exec : function(next) {
next('No db dummy');
}
};
}
}
//Handle error
db.on('error', function (err) {
if (err) // couldn't connect
console.log('err'); //Log something
Config = errorObject; //Set our schema(s) to the error object
});
//Function that defines connecting
function connect () {
db.open('localhost', 'riddlebot');
}
//Try to connect
connect();
console.log('ok');
//Just did this so db would have enough time to init, connect should probably take a callback instead
setTimeout(doDb, 1000);
function doDb() {
Config.findOne().exec(function(err, config){
if (err){
console.log(err);
} else if (config){
console.log("config loaded");
} else {
console.log("No record found");
}
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment