@@ -1257,6 +1257,8 @@ var pluginManager = function pluginManager() {
1257
1257
}
1258
1258
} ) ;
1259
1259
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
+
1260
1262
countlyDb . s = { } ;
1261
1263
countlyDb . _collection_cache = { } ;
1262
1264
//overwrite some methods
@@ -1338,14 +1340,22 @@ var pluginManager = function pluginManager() {
1338
1340
1339
1341
ob . _findAndModify = ob . findAndModify ;
1340
1342
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
+
1341
1351
mngr . dispatch ( "/db/readAndUpdate" , {
1342
1352
db : db_name ,
1343
1353
operation : "findAndModify" ,
1344
1354
collection : collection ,
1345
1355
query : query ,
1346
1356
sort : sort ,
1347
1357
update : doc ,
1348
- options : typeof options === "function" ? { } : options
1358
+ options : options
1349
1359
} ) ;
1350
1360
var e ;
1351
1361
var args = arguments ;
@@ -1354,41 +1364,41 @@ var pluginManager = function pluginManager() {
1354
1364
e = new Error ( ) ;
1355
1365
at += e . stack . replace ( / \r \n | \r | \n / g, "\n" ) . split ( "\n" ) [ 2 ] ;
1356
1366
}
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" ) ) ) ;
1362
1378
}
1363
1379
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" ) ) ) ;
1379
1381
}
1380
1382
} ;
1381
1383
1382
1384
var overwriteRetryWrite = function ( obj , name ) {
1383
1385
obj [ "_" + name ] = obj [ name ] ;
1384
1386
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
+
1385
1395
mngr . dispatch ( "/db/update" , {
1386
1396
db : db_name ,
1387
1397
operation : name ,
1388
1398
collection : collection ,
1389
1399
query : selector ,
1390
1400
update : doc ,
1391
- options : typeof options === "function" ? { } : options
1401
+ options : options
1392
1402
} ) ;
1393
1403
var args = arguments ;
1394
1404
var e ;
@@ -1397,36 +1407,29 @@ var pluginManager = function pluginManager() {
1397
1407
e = new Error ( ) ;
1398
1408
at += e . stack . replace ( / \r \n | \r | \n / g, "\n" ) . split ( "\n" ) [ 2 ] ;
1399
1409
}
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 ) ) ) ;
1405
1421
}
1406
1422
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 ) ) ) ;
1423
1424
}
1424
1425
} ;
1425
1426
} ;
1426
1427
1427
1428
overwriteRetryWrite ( ob , "updateOne" ) ;
1428
1429
overwriteRetryWrite ( ob , "updateMany" ) ;
1429
1430
overwriteRetryWrite ( ob , "replaceOne" ) ;
1431
+ overwriteRetryWrite ( ob , "findOneAndUpdate" ) ;
1432
+ overwriteRetryWrite ( ob , "findOneAndReplace" ) ;
1430
1433
1431
1434
//overwrite with write logging
1432
1435
var logForWrites = function ( callback , e , data ) {
@@ -1469,31 +1472,31 @@ var pluginManager = function pluginManager() {
1469
1472
var overwriteDefaultWrite = function ( obj , name ) {
1470
1473
obj [ "_" + name ] = obj [ name ] ;
1471
1474
obj [ name ] = function ( selector , options , callback ) {
1475
+ if ( typeof options === "function" ) {
1476
+ callback = options ;
1477
+ options = { } ;
1478
+ }
1479
+ else {
1480
+ options = options || { } ;
1481
+ }
1482
+
1472
1483
mngr . dispatch ( "/db/write" , {
1473
1484
db : db_name ,
1474
1485
operation : name ,
1475
1486
collection : collection ,
1476
1487
query : selector ,
1477
- options : typeof options === "function" ? { } : options
1488
+ options : options
1478
1489
} ) ;
1479
1490
var e ;
1480
1491
var at = "" ;
1481
1492
if ( log . getLevel ( "db" ) === "debug" || log . getLevel ( "db" ) === "info" ) {
1482
1493
e = new Error ( ) ;
1483
1494
at += e . stack . replace ( / \r \n | \r | \n / g, "\n" ) . split ( "\n" ) [ 2 ] ;
1484
1495
}
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 ) ) ) ;
1497
1500
} ;
1498
1501
} ;
1499
1502
overwriteDefaultWrite ( ob , "deleteOne" ) ;
@@ -1537,44 +1540,46 @@ var pluginManager = function pluginManager() {
1537
1540
var overwriteDefaultRead = function ( obj , name ) {
1538
1541
obj [ "_" + name ] = obj [ name ] ;
1539
1542
obj [ name ] = function ( query , options , callback ) {
1543
+ if ( typeof options === "function" ) {
1544
+ callback = options ;
1545
+ options = { } ;
1546
+ }
1547
+ else {
1548
+ options = options || { } ;
1549
+ }
1550
+
1540
1551
mngr . dispatch ( "/db/read" , {
1541
1552
db : db_name ,
1542
1553
operation : name ,
1543
1554
collection : collection ,
1544
1555
query : query ,
1545
- options : typeof options === "function" ? { } : options
1556
+ options : options
1546
1557
} ) ;
1547
1558
var e ;
1548
1559
var at = "" ;
1549
1560
if ( log . getLevel ( "db" ) === "debug" || log . getLevel ( "db" ) === "info" ) {
1550
1561
e = new Error ( ) ;
1551
1562
at += e . stack . replace ( / \r \n | \r | \n / g, "\n" ) . split ( "\n" ) [ 2 ] ;
1552
1563
}
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 } ;
1568
1573
}
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 ) ) ) ;
1573
1574
}
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 ) ) ) ;
1574
1578
} ;
1575
1579
} ;
1576
1580
1577
1581
overwriteDefaultRead ( ob , "findOne" ) ;
1582
+ overwriteDefaultRead ( ob , "findOneAndDelete" ) ;
1578
1583
overwriteDefaultRead ( ob , "aggregate" ) ;
1579
1584
1580
1585
ob . _find = ob . find ;
@@ -1588,16 +1593,19 @@ var pluginManager = function pluginManager() {
1588
1593
options . projection = options . fields ;
1589
1594
delete options . fields ;
1590
1595
}
1591
- else if ( Object . keys ( options ) . length ) {
1596
+ else if ( findOptions . indexOf ( Object . keys ( options ) [ 0 ] ) === - 1 ) {
1592
1597
options = { projection : options } ;
1593
1598
}
1594
1599
}
1600
+ else {
1601
+ options = options || { } ;
1602
+ }
1595
1603
mngr . dispatch ( "/db/read" , {
1596
1604
db : db_name ,
1597
1605
operation : "find" ,
1598
1606
collection : collection ,
1599
1607
query : query ,
1600
- options : options || { }
1608
+ options : options
1601
1609
} ) ;
1602
1610
if ( log . getLevel ( "db" ) === "debug" || log . getLevel ( "db" ) === "info" ) {
1603
1611
e = new Error ( ) ;
0 commit comments