Skip to content
This repository has been archived by the owner on Jul 20, 2019. It is now read-only.

Commit

Permalink
Moved functions from rest to util.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ricard Aspeljung committed Apr 2, 2013
1 parent fe2b619 commit c13d2fa
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 24 deletions.
31 changes: 7 additions & 24 deletions lib/rest.js
Expand Up @@ -15,23 +15,6 @@ var MongoClient = require("mongodb").MongoClient,

debug("rest.js is loaded");

function connectionURL(dbName) {
var auth = "";
if (config.db.username && config.db.password) {
auth = config.db.username + ":" + config.db.password + "@";
}
return "mongodb://" + auth + config.db.host + ":" + config.db.port + "/" + dbName;// + "?maxPoolSize=20";
}

function parseJSON(data, next) {
var json;
try {
json = JSON.parse(data);
} catch (e) {
return next(new restify.InvalidArgumentError("Not valid JSON data."));
}
return json;
}

/**
* Query
Expand All @@ -45,7 +28,7 @@ function handleGet(req, res, next) {
'_id': new BSON.ObjectID(req.params.id)
};
} else {
query = req.query.query ? parseJSON(req.query.query, next) : {};
query = req.query.query ? util.parseJSON(req.query.query, next, restify) : {};
}
var options = req.params.options || {};

Expand All @@ -61,14 +44,14 @@ function handleGet(req, res, next) {
if (req.body.toString().length > 0) {
var body = req.body.split(",");
if (body[0]) {
query = parseJSON(body[0], next);
query = util.parseJSON(body[0], next);
}
if (body[1]) {
options = parseJSON(body[1], next);
options = util.parseJSON(body[1], next);
}
}

MongoClient.connect(connectionURL(req.params.db), function (err, db) {
MongoClient.connect(util.connectionURL(req.params.db, config), function (err, db) {
db.collection(req.params.collection, function (err, collection) {
collection.find(query, options, function (err, cursor) {
cursor.toArray(function (err, docs) {
Expand Down Expand Up @@ -103,7 +86,7 @@ server.get('/:db/:collection', handleGet);
server.post('/:db/:collection', function (req, res) {
debug("POST-request recieved");
if (req.params) {
MongoClient.connect(connectionURL(req.params.db), function (err, db) {
MongoClient.connect(util.connectionURL(req.params.db, config), function (err, db) {
var collection = db.collection(req.params.collection);
// We only support inserting one document at a time
collection.insert(Array.isArray(req.params) ? util.cleanParams(req.params[0]) : util.cleanParams(req.params), function (err, docs) {
Expand All @@ -127,7 +110,7 @@ server.put('/:db/:collection/:id', function (req, res) {
var spec = {
'_id': new BSON.ObjectID(req.params.id)
};
MongoClient.connect(connectionURL(req.params.db), function (err, db) {
MongoClient.connect(util.connectionURL(req.params.db, config), function (err, db) {
db.collection(req.params.collection, function (err, collection) {
collection.update(spec, util.cleanParams(req.params), true, function (err, docs) {
res.set('content-type', 'application/json; charset=utf-8');
Expand All @@ -145,7 +128,7 @@ server.del('/:db/:collection/:id', function (req, res) {
var spec = {
'_id': new BSON.ObjectID(req.params.id)
};
MongoClient.connect(connectionURL(req.params.db), function (err, db) {
MongoClient.connect(util.connectionURL(req.params.db, config), function (err, db) {
db.collection(req.params.collection, function (err, collection) {
collection.remove(spec, function (err, docs) {
res.set('content-type', 'application/json; charset=utf-8');
Expand Down
16 changes: 16 additions & 0 deletions lib/util.js
Expand Up @@ -43,5 +43,21 @@ module.exports.util = {
delete clean.collection;
}
return clean;
},
parseJSON: function (data, next, restify) {
var json;
try {
json = JSON.parse(data);
} catch (e) {
return next(new restify.InvalidArgumentError("Not valid JSON data."));
}
return json;
},
connectionURL: function (dbName, config) {
var auth = "";
if (config.db.username && config.db.password) {
auth = config.db.username + ":" + config.db.password + "@";
}
return "mongodb://" + auth + config.db.host + ":" + config.db.port + "/" + dbName; // + "?maxPoolSize=20";
}
};

0 comments on commit c13d2fa

Please sign in to comment.