mongo-control
Advanced tools
Comparing version 1.0.2 to 1.0.3
@@ -13,10 +13,12 @@ var MongoClient = require('mongodb').MongoClient; | ||
if (params.query && (typeof params.query == "string")) { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
if (params.query) { | ||
if (typeof params.query == "string") { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
} | ||
} | ||
} | ||
} else params.query = {}; | ||
@@ -41,12 +43,14 @@ MongoClient.connect(params.db, function(e, db) { | ||
if (params.query && (typeof params.query == "string")) { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
if (params.query) { | ||
if (typeof params.query == "string") { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
} | ||
} | ||
} | ||
} else params.query = {}; | ||
if(params.limit) params.limit = parseInt(params.limit, 10); | ||
if (params.limit) params.limit = parseInt(params.limit, 10); | ||
else params.limit = 0; | ||
@@ -104,2 +108,50 @@ | ||
MC.update = function(params) { | ||
return new Promise(function(res, err) { | ||
if (!params.db || !params.collection || !params.query || !params.update) return err("!params.db || !params.collection || !params.id || !params.update"); | ||
if (params.query) { | ||
if (typeof params.query == "string") { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
} | ||
} | ||
} else params.query = {}; | ||
if (typeof params.update == "string") { | ||
try { | ||
params.update = JSON.parse(params.update); | ||
for (var key in params.update) { | ||
var item = params.update[key]; | ||
if (reJsStrData.test(item)) params.update[key] = new Date(item); | ||
} | ||
} catch (e) { | ||
return err(e); | ||
} | ||
} | ||
var updObj = { | ||
"$set": params.update | ||
}; | ||
MongoClient.connect(params.db, function(e, db) { | ||
if (e) return err(e); | ||
db.collection(params.collection).updateMany(params.query, updObj, function(e, r) { | ||
if (e) return err(e); | ||
res(r); | ||
db.close(); | ||
}); | ||
}); | ||
}); | ||
}; | ||
MC.updateById = function(params) { | ||
@@ -254,10 +306,12 @@ | ||
if (params.query && (typeof params.query == "string")) { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
if (params.query) { | ||
if (typeof params.query == "string") { | ||
try { | ||
params.query = JSON.parse(params.query); | ||
} catch (e) { | ||
params.query = {}; | ||
err(e); | ||
} | ||
} | ||
} | ||
} else params.query = {}; | ||
@@ -317,3 +371,3 @@ MongoClient.connect(params.db, function(e, db) { | ||
if (params.query) { | ||
if(typeof params.query == "string") { | ||
if (typeof params.query == "string") { | ||
try { | ||
@@ -320,0 +374,0 @@ params.query = JSON.parse(params.query); |
var express = require('express'); | ||
var bodyParser = require('body-parser'); | ||
var MH = require('../mongo.helpers'); | ||
var MC = require('../mongo.helpers'); | ||
@@ -13,3 +13,3 @@ var databaseUri = 'mongodb://localhost:27017/coverage'; //don't forget to change when copy-paste | ||
var i = 0; | ||
var helperNames = Object.keys(MH); | ||
var helperNames = Object.keys(MC); | ||
var l = helperNames.length; | ||
@@ -19,3 +19,3 @@ | ||
var funcName = helperNames[i]; | ||
var func = MH[funcName]; | ||
var func = MC[funcName]; | ||
var route = "/" + funcName.toLowerCase(); | ||
@@ -22,0 +22,0 @@ |
{ | ||
"name": "mongo-control", | ||
"version": "1.0.2", | ||
"version": "1.0.3", | ||
"description": "Simple functions to controll mongo", | ||
@@ -5,0 +5,0 @@ "main": "mongo.control.js", |
12663
381