Skip to content

Commit

Permalink
Merge pull request #39 from MarkMurillo/master
Browse files Browse the repository at this point in the history
MongoDB integration
  • Loading branch information
MarkMurillo committed Mar 21, 2013
2 parents a223933 + 3e37202 commit 20a2def
Show file tree
Hide file tree
Showing 95 changed files with 36,156 additions and 3 deletions.
76 changes: 76 additions & 0 deletions DemaciaTV/accountprovider-mongodb.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
var Db = require('mongodb').Db;
var Connection = require('mongodb').Connection;
var Server = require('mongodb').Server;
var BSON = require('mongodb').BSON;
var ObjectID = require('mongodb').ObjectID;

AccountProvider = function(host, port) {
this.db = new Db('DemaciaTV', new Server(host, port, {auto_reconnect: true}, {}));
this.db.open(function(){});
};

AccountProvider.prototype.getCollection = function(callback){
this.db.collection('accounts', function(error, account_collection){
if(error) callback(error);
else callback (null, account_collection);
});
};

AccountProvider.prototype.clearData = function(callback){
this.getCollection(function(error, account_collection){
if(error) callback(error);
else{
account_collection.remove();
}
});
};

AccountProvider.prototype.findAll = function(callback){
this.getCollection(function(error, account_collection){
if(error) callback(error);
else{
account_collection.find().toArray(function(error, results){
if(error) callback(error);
else callback(null, results);
});
}
});
};

AccountProvider.prototype.findById = function(id, callback) {
this.getCollection(function(error, account_collection) {
if( error ) callback(error);
else {
account_collection.findOne({_id: account_collection.db.bson_serializer.ObjectID.createFromHexString(id)}, function(error, result) {
if( error ) callback(error);
else callback(null, result);
});
}
});
};

AccountProvider.prototype.findByName = function(n, callback) {
this.getCollection(function(error, account_collection) {
if( error ) callback(error);
else {
account_collection.findOne({name: n}, function(error, result) {
if( error ) callback(error);
else callback(null, result);
});
}
});
};

AccountProvider.prototype.save = function(account, callback) {
this.getCollection(function(error, account_collection) {
if( error ) callback(error)
else {
account_collection.insert(account, function() {
callback(null, account);
});
}
});
};

exports.AccountProvider = AccountProvider;

37 changes: 35 additions & 2 deletions DemaciaTV/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,15 @@ var express = require('express')
, http = require('http')
, path = require('path')
, stylus = require('stylus')
, nib = require('nib');
, nib = require('nib')
, AccountProvider = require('./accountprovider-mongodb').AccountProvider;

var app = express();

var accountProvider = new AccountProvider('localhost', 27017);

app.configure(function(){
app.set('port', process.env.PORT || 80);
app.set('port', process.env.PORT || 3000);
app.set('views', __dirname + '/views');
app.set('view engine', 'jade');
app.use(express.favicon(__dirname + '/public/images/favicon.ico', { maxAge: 2592000000 }));
Expand All @@ -40,9 +43,39 @@ app.configure('development', function(){
app.use(express.errorHandler());
});



app.get('/', routes.index);
app.get('/get', function(req, res){
accountProvider.findAll(function(error,result){
res.render('get.jade', {title: "DB Contents", accounts: result});
});
});

app.post('/get', function(req,res){
accountProvider.clearData();
res.redirect('/post');
});

app.get('/post', function(req, res) {
res.render('post.jade', {title: "DB Input"});
});

app.post('/post', function(req, res){
accountProvider.save({
name: req.param('input')
}, function( error, docs) {
res.redirect('/get')
});
});


app.get('/users', user.list);





http.createServer(app).listen(app.get('port'), function(){
console.log("Express server listening on port " + app.get('port'));
});
5 changes: 5 additions & 0 deletions DemaciaTV/node_modules/mongodb/.travis.yml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

23 changes: 23 additions & 0 deletions DemaciaTV/node_modules/mongodb/CONTRIBUTING.md

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 28 additions & 0 deletions DemaciaTV/node_modules/mongodb/Makefile

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 20a2def

Please sign in to comment.