Created
January 16, 2013 22:12
-
-
Save comster/4551436 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// | |
// # Simple Mongo Collection API Endpoint | |
// | |
var ObjectID = mongo.ObjectID; | |
(exports = module.exports = function(house, options){ | |
// This endpoint requires a data source | |
var ds = options.ds; | |
var col = options.collection; | |
var handleReq = function(req, res, next) { | |
/*if(!req.session.data.user) { | |
res.writeHead(403); | |
res.end('{}'); | |
return; | |
}*/ | |
var path = req.hasOwnProperty('urlRouted') ? req.urlRouted : req.url; | |
var findQuery = function(query) { | |
// accept "id" (backbone.js) but convert it to mongo object _id's | |
if(query.id) { | |
query._id = query.id; | |
delete query.id; | |
} | |
if(query.hasOwnProperty('_id') && typeof query._id == 'string') { | |
try { | |
query._id = new ObjectID(query._id); | |
} catch(e) { | |
console.log('bad object id'); | |
} | |
} | |
// query mongo id's | |
if(query.hasOwnProperty('id')) { | |
query._id = new ObjectID(query.id); | |
delete query.id; | |
} | |
ds.find(col, query, function(err, data){ | |
if(err) { | |
house.log.err(err); | |
} else if(data) { | |
res.data(data); | |
} else { | |
house.log.err(new Error('no data from mongo')); | |
} | |
}); | |
} | |
var docId; | |
if(path.length > 1 && path.indexOf('/') === 0) { | |
docId = new ObjectID(path.substr(1)); | |
} | |
if(req.method == 'GET') { | |
var query = {}; | |
if(docId) { | |
query._id = docId; | |
findQuery(query); | |
} else { | |
if(req.query) { | |
query = req.query; | |
findQuery(query); | |
} | |
} | |
} else if(req.method == 'HEAD') { | |
var query = {}; | |
if(docId) { | |
query._id = docId; | |
findQuery(query); | |
} else { | |
if(req.query) { | |
query = req.query; | |
} | |
ds.count(col, query, function(err, data){ | |
if(err) { | |
house.log.err(err); | |
} else { | |
res.setHeader('X-Count', data); | |
res.data({}); | |
} | |
}); | |
} | |
} else if(req.method == 'POST') { | |
if(path == '') { | |
ds.insert(col, req.fields, function(err, data){ | |
ds.find(col, {_id: data.id}, function(err, docs) { | |
if (err) { | |
house.log.err(err); | |
} else { | |
res.data(_.first(docs)); | |
} | |
}); | |
}); | |
} | |
} else if(req.method == 'PUT') { | |
var query = {}; | |
if(docId) { | |
query._id = docId; | |
var putDoc = req.fields; | |
ds.update(col, query, putDoc, function(err, data){ | |
if(err) { | |
house.log.err(err); | |
res.end('error'); | |
} else { | |
ds.find(col, query, function(err, data) { | |
res.data(_.first(data)); | |
}); | |
} | |
}); | |
} | |
} else if(req.method == 'DELETE') { | |
var query = {}; | |
if(docId) { | |
query._id = docId; | |
ds.find(col, query, function(err, data) { | |
var doc = _.first(data); | |
ds.remove(col, query, function(err, data){ | |
if(err) { | |
house.log.err(err); | |
res.end('error'); | |
} else { | |
res.data(data); | |
} | |
}); | |
}); | |
} | |
} else if(req.method == 'OPTIONS') { | |
} | |
} | |
return handleReq; | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment