Skip to content

Commit 2e27a29

Browse files
committed
[plugins] fix passing options for db operations
1 parent 3f389da commit 2e27a29

File tree

1 file changed

+86
-78
lines changed

1 file changed

+86
-78
lines changed

plugins/pluginManager.js

+86-78
Original file line numberDiff line numberDiff line change
@@ -1257,6 +1257,8 @@ var pluginManager = function pluginManager() {
12571257
}
12581258
});
12591259

1260+
var findOptions = ["limit", "sort", "projection", "skip", "hint", "explain", "snapshot", "timeout", "tailable", "batchSize", "returnKey", "maxScan", "min", "max", "showDiskLoc", "comment", "raw", "promoteLongs", "promoteValues", "promoteBuffers", "readPreference", "partial", "maxTimeMS", "collation", "session"];
1261+
12601262
countlyDb.s = {};
12611263
countlyDb._collection_cache = {};
12621264
//overwrite some methods
@@ -1338,14 +1340,22 @@ var pluginManager = function pluginManager() {
13381340

13391341
ob._findAndModify = ob.findAndModify;
13401342
ob.findAndModify = function(query, sort, doc, options, callback) {
1343+
if (typeof options === "function") {
1344+
callback = options;
1345+
options = {};
1346+
}
1347+
else {
1348+
options = options || {};
1349+
}
1350+
13411351
mngr.dispatch("/db/readAndUpdate", {
13421352
db: db_name,
13431353
operation: "findAndModify",
13441354
collection: collection,
13451355
query: query,
13461356
sort: sort,
13471357
update: doc,
1348-
options: typeof options === "function" ? {} : options
1358+
options: options
13491359
});
13501360
var e;
13511361
var args = arguments;
@@ -1354,41 +1364,41 @@ var pluginManager = function pluginManager() {
13541364
e = new Error();
13551365
at += e.stack.replace(/\r\n|\r|\n/g, "\n").split("\n")[2];
13561366
}
1357-
if (typeof options === "function") {
1358-
//options was not passed, we have callback
1359-
logDbWrite.d("findAndModify " + collection + " %j %j %j" + at, query, sort, doc);
1360-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1361-
return this._findAndModify(query, sort, doc, retryifNeeded(options, null, e, copyArguments(arguments, "findAndModify")));
1367+
1368+
logDbWrite.d("findAndModify " + collection + " %j %j %j %j" + at, query, sort, doc, options);
1369+
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1370+
if (options.upsert) {
1371+
var self = this;
1372+
1373+
return this._findAndModify(query, sort, doc, options, retryifNeeded(callback, function() {
1374+
logDbWrite.d("retrying findAndModify " + collection + " %j %j %j %j" + at, query, sort, doc, options);
1375+
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1376+
self._findAndModify(query, sort, doc, options, retryifNeeded(callback, null, e, copyArguments(args, "findAndModify")));
1377+
}, e, copyArguments(arguments, "findAndModify")));
13621378
}
13631379
else {
1364-
//we have options
1365-
logDbWrite.d("findAndModify " + collection + " %j %j %j %j" + at, query, sort, doc, options);
1366-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1367-
if (options.upsert) {
1368-
var self = this;
1369-
1370-
return this._findAndModify(query, sort, doc, options, retryifNeeded(callback, function() {
1371-
logDbWrite.d("retrying findAndModify " + collection + " %j %j %j %j" + at, query, sort, doc, options);
1372-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1373-
self._findAndModify(query, sort, doc, options, retryifNeeded(callback, null, e, copyArguments(args, "findAndModify")));
1374-
}, e, copyArguments(arguments, "findAndModify")));
1375-
}
1376-
else {
1377-
return this._findAndModify(query, sort, doc, options, retryifNeeded(callback, null, e, copyArguments(arguments, "findAndModify")));
1378-
}
1380+
return this._findAndModify(query, sort, doc, options, retryifNeeded(callback, null, e, copyArguments(arguments, "findAndModify")));
13791381
}
13801382
};
13811383

13821384
var overwriteRetryWrite = function(obj, name) {
13831385
obj["_" + name] = obj[name];
13841386
obj[name] = function(selector, doc, options, callback) {
1387+
if (typeof options === "function") {
1388+
callback = options;
1389+
options = {};
1390+
}
1391+
else {
1392+
options = options || {};
1393+
}
1394+
13851395
mngr.dispatch("/db/update", {
13861396
db: db_name,
13871397
operation: name,
13881398
collection: collection,
13891399
query: selector,
13901400
update: doc,
1391-
options: typeof options === "function" ? {} : options
1401+
options: options
13921402
});
13931403
var args = arguments;
13941404
var e;
@@ -1397,36 +1407,29 @@ var pluginManager = function pluginManager() {
13971407
e = new Error();
13981408
at += e.stack.replace(/\r\n|\r|\n/g, "\n").split("\n")[2];
13991409
}
1400-
if (typeof options === "function") {
1401-
//options was not passed, we have callback
1402-
logDbWrite.d(name + " " + collection + " %j %j" + at, selector, doc);
1403-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1404-
return this["_" + name](selector, doc, retryifNeeded(options, null, e, copyArguments(arguments, name)));
1410+
1411+
logDbWrite.d(name + " " + collection + " %j %j %j" + at, selector, doc, options);
1412+
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1413+
if (options.upsert) {
1414+
var self = this;
1415+
1416+
return this["_" + name](selector, doc, options, retryifNeeded(callback, function() {
1417+
logDbWrite.d("retrying " + name + " " + collection + " %j %j %j" + at, selector, doc, options);
1418+
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1419+
self["_" + name](selector, doc, options, retryifNeeded(callback, null, e, copyArguments(args, name)));
1420+
}, e, copyArguments(arguments, name)));
14051421
}
14061422
else {
1407-
options = options || {};
1408-
//we have options
1409-
logDbWrite.d(name + " " + collection + " %j %j %j" + at, selector, doc, options);
1410-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1411-
if (options.upsert) {
1412-
var self = this;
1413-
1414-
return this["_" + name](selector, doc, options, retryifNeeded(callback, function() {
1415-
logDbWrite.d("retrying " + name + " " + collection + " %j %j %j" + at, selector, doc, options);
1416-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1417-
self["_" + name](selector, doc, options, retryifNeeded(callback, null, e, copyArguments(args, name)));
1418-
}, e, copyArguments(arguments, name)));
1419-
}
1420-
else {
1421-
return this["_" + name](selector, doc, options, retryifNeeded(callback, null, e, copyArguments(arguments, name)));
1422-
}
1423+
return this["_" + name](selector, doc, options, retryifNeeded(callback, null, e, copyArguments(arguments, name)));
14231424
}
14241425
};
14251426
};
14261427

14271428
overwriteRetryWrite(ob, "updateOne");
14281429
overwriteRetryWrite(ob, "updateMany");
14291430
overwriteRetryWrite(ob, "replaceOne");
1431+
overwriteRetryWrite(ob, "findOneAndUpdate");
1432+
overwriteRetryWrite(ob, "findOneAndReplace");
14301433

14311434
//overwrite with write logging
14321435
var logForWrites = function(callback, e, data) {
@@ -1469,31 +1472,31 @@ var pluginManager = function pluginManager() {
14691472
var overwriteDefaultWrite = function(obj, name) {
14701473
obj["_" + name] = obj[name];
14711474
obj[name] = function(selector, options, callback) {
1475+
if (typeof options === "function") {
1476+
callback = options;
1477+
options = {};
1478+
}
1479+
else {
1480+
options = options || {};
1481+
}
1482+
14721483
mngr.dispatch("/db/write", {
14731484
db: db_name,
14741485
operation: name,
14751486
collection: collection,
14761487
query: selector,
1477-
options: typeof options === "function" ? {} : options
1488+
options: options
14781489
});
14791490
var e;
14801491
var at = "";
14811492
if (log.getLevel("db") === "debug" || log.getLevel("db") === "info") {
14821493
e = new Error();
14831494
at += e.stack.replace(/\r\n|\r|\n/g, "\n").split("\n")[2];
14841495
}
1485-
if (typeof options === "function") {
1486-
//options was not passed, we have callback
1487-
logDbWrite.d(name + " " + collection + " %j" + at, selector);
1488-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1489-
return this["_" + name](selector, logForWrites(options, e, copyArguments(arguments, name)));
1490-
}
1491-
else {
1492-
//we have options
1493-
logDbWrite.d(name + " " + collection + " %j %j" + at, selector, options);
1494-
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1495-
return this["_" + name](selector, options, logForWrites(callback, e, copyArguments(arguments, name)));
1496-
}
1496+
1497+
logDbWrite.d(name + " " + collection + " %j %j" + at, selector, options);
1498+
logDbWrite.d("From connection %j", countlyDb._cly_debug);
1499+
return this["_" + name](selector, options, logForWrites(callback, e, copyArguments(arguments, name)));
14971500
};
14981501
};
14991502
overwriteDefaultWrite(ob, "deleteOne");
@@ -1537,44 +1540,46 @@ var pluginManager = function pluginManager() {
15371540
var overwriteDefaultRead = function(obj, name) {
15381541
obj["_" + name] = obj[name];
15391542
obj[name] = function(query, options, callback) {
1543+
if (typeof options === "function") {
1544+
callback = options;
1545+
options = {};
1546+
}
1547+
else {
1548+
options = options || {};
1549+
}
1550+
15401551
mngr.dispatch("/db/read", {
15411552
db: db_name,
15421553
operation: name,
15431554
collection: collection,
15441555
query: query,
1545-
options: typeof options === "function" ? {} : options
1556+
options: options
15461557
});
15471558
var e;
15481559
var at = "";
15491560
if (log.getLevel("db") === "debug" || log.getLevel("db") === "info") {
15501561
e = new Error();
15511562
at += e.stack.replace(/\r\n|\r|\n/g, "\n").split("\n")[2];
15521563
}
1553-
if (typeof options === "function") {
1554-
//options was not passed, we have callback
1555-
logDbRead.d(name + " " + collection + " %j" + at, query);
1556-
logDbRead.d("From connection %j", countlyDb._cly_debug);
1557-
return this["_" + name](query, logForReads(options, e, copyArguments(arguments, name)));
1558-
}
1559-
else {
1560-
if (name === "findOne" && options && !options.projection) {
1561-
if (options.fields) {
1562-
options.projection = options.fields;
1563-
delete options.fields;
1564-
}
1565-
else {
1566-
options = {projection: options};
1567-
}
1564+
1565+
if (name === "findOne" && options && !options.projection) {
1566+
if (options.fields) {
1567+
options.projection = options.fields;
1568+
delete options.fields;
1569+
}
1570+
else if (findOptions.indexOf(Object.keys(options)[0]) === -1) {
1571+
1572+
options = {projection: options};
15681573
}
1569-
//we have options
1570-
logDbRead.d(name + " " + collection + " %j %j" + at, query, options);
1571-
logDbRead.d("From connection %j", countlyDb._cly_debug);
1572-
return this["_" + name](query, options, logForReads(callback, e, copyArguments(arguments, name)));
15731574
}
1575+
logDbRead.d(name + " " + collection + " %j %j" + at, query, options);
1576+
logDbRead.d("From connection %j", countlyDb._cly_debug);
1577+
return this["_" + name](query, options, logForReads(callback, e, copyArguments(arguments, name)));
15741578
};
15751579
};
15761580

15771581
overwriteDefaultRead(ob, "findOne");
1582+
overwriteDefaultRead(ob, "findOneAndDelete");
15781583
overwriteDefaultRead(ob, "aggregate");
15791584

15801585
ob._find = ob.find;
@@ -1588,16 +1593,19 @@ var pluginManager = function pluginManager() {
15881593
options.projection = options.fields;
15891594
delete options.fields;
15901595
}
1591-
else if (Object.keys(options).length) {
1596+
else if (findOptions.indexOf(Object.keys(options)[0]) === -1) {
15921597
options = {projection: options};
15931598
}
15941599
}
1600+
else {
1601+
options = options || {};
1602+
}
15951603
mngr.dispatch("/db/read", {
15961604
db: db_name,
15971605
operation: "find",
15981606
collection: collection,
15991607
query: query,
1600-
options: options || {}
1608+
options: options
16011609
});
16021610
if (log.getLevel("db") === "debug" || log.getLevel("db") === "info") {
16031611
e = new Error();

0 commit comments

Comments
 (0)