Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Moved functions from rest to util.

  • Loading branch information...
commit c13d2fa04c5a915cc15cbf34cb18e107676336a2 1 parent fe2b619
Ricard Aspeljung authored
Showing with 23 additions and 24 deletions.
  1. +7 −24 lib/rest.js
  2. +16 −0 lib/util.js
View
31 lib/rest.js
@@ -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
@@ -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 || {};
@@ -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) {
@@ -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) {
@@ -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');
@@ -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');
View
16 lib/util.js
@@ -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";
}
};
Please sign in to comment.
Something went wrong with that request. Please try again.