Skip to content

Commit

Permalink
fix leanQuery with flat option (#21)
Browse files Browse the repository at this point in the history
* fix leanQuery with flat option
* several smaller fixes and promise tests
* boolean conversion allows numbers (0=false, !0 = true) and undefined (=true)
* fix: count promise resolves now same count object than callback
* add promise tests which validate same thing than callback tests
* disconnect mongoose after tests
  • Loading branch information
jupe committed Oct 19, 2017
1 parent 8a6e8fb commit b4e9bea
Show file tree
Hide file tree
Showing 5 changed files with 252 additions and 129 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ model.find({group: "users"}).select("name").skip(1).limit(5).populate('name')

|versio|Changes|
|------|-------|
|0.4.0|Fix lean query with flatten + couple other and add promise tests|
|0.3.0|Big refactoring, see more from release note.. e.g. mongoose 4.x support|
|0.2.1|added oid support, fixed aggregate and support mongoose => 3.8.1
|0.2.0|replace underscore with lodash, possible to return promise when no callback in use|
Expand Down
20 changes: 10 additions & 10 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ module.exports = function QueryPlugin (schema, options) {
break;
case('count'):
if (!callback) {
return this.count(q.q);
return this.count(q.q).exec().then(count => ({count}));
}
this.count(q.q, (error,count) => {
if(error) callback(error);
Expand Down Expand Up @@ -107,28 +107,28 @@ module.exports = function QueryPlugin (schema, options) {
return query.findOne(callback);
}
} else {
if( q.fl ) {
if (q.fl) {
if (callback) {
query.find((error, docs) => {
if(error) callback(error);
if (error) callback(error);
else {
let arr = [];
const arr = [];
docs.forEach((doc) => {
let json = doc.toJSON({virtuals: true});
arr.push( flatten(json));
const json = opt.lean ? doc : doc.toJSON({virtuals: true});
arr.push(flatten(json));
});
callback(error, arr);
}
});
} else {
return new Promise((resolve, reject) => {
query.find((error, docs) => {
if(error) reject(error);
if (error) reject(error);
else {
let arr = [];
const arr = [];
docs.forEach((doc) => {
let json = doc.toJSON({virtuals: true});
arr.push( flatten(json));
const json = opt.lean ? doc : doc.toJSON({virtuals: true});
arr.push(flatten(json));
});
resolve(arr);
}
Expand Down
12 changes: 9 additions & 3 deletions lib/tools.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const util = require('util')
, _ = require('lodash')
, logger = require('./logger');

module.exports.toJSON = function(str){
Expand All @@ -10,8 +11,13 @@ module.exports.toJSON = function(str){
json = {};
}
return json;
}
};
module.exports.toBool = function (str) {
if (_.isUndefined(str))
return true;
if (_.isNumber(str)) {
return str === 0 ? false : true;
}
if (str.toLowerCase() === "true" ||
str.toLowerCase() === "yes" ){
return true;
Expand All @@ -22,7 +28,7 @@ module.exports.toBool = function (str) {
} else {
return -1;
}
}
};
function parseDate(str) {
//31/2/2010
var m = str.match(/^(\d{1,2})[\/\s\.\-\,](\d{1,2})[\/\s\.\-\,](\d{4})$/);
Expand All @@ -41,4 +47,4 @@ module.exports.isStringValidDate = function(str){
if(isDate(parseDate(str)))return true;
if(isDate(parseDate2(str)))return true;
return false;
}
};
26 changes: 15 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,33 +1,37 @@
{
"name": "mongoose-query",
"description": "mongoose Query lib",
"keywords": ["mongoose", "query", "mongodb"],
"version": "0.3.0",
"keywords": [
"mongoose",
"query",
"mongodb"
],
"version": "0.4.0",
"homepage": "https://github.com/jupe/mongoose-query",
"author": "Jussi Vatjus-Anttila <jussiva@gmail.com>)",
"main": "lib/index",
"bugs" : {
"url" : "https://github.com/jupe/mongoose-query/issues"
"bugs": {
"url": "https://github.com/jupe/mongoose-query/issues"
},
"repository": {
"type": "git",
"url": "https://github.com/jupe/mongoose-query"
"type": "git",
"url": "https://github.com/jupe/mongoose-query"
},
"licenses" : "MIT",
"license": "MIT",
"dependencies": {
"mongoose": "^4.11.4",
"flat": "*",
"lodash": "*"
},
"devDependencies": {
"mocha": "3.5.3",
"chai": "*",
"request": "*"
"chai": "*",
"mocha": "^4.0.1",
"request": "*"
},
"contributors": [
"Jussi Vatjus-Anttila <jussiva@gmail.com>"
],
"scripts": {
"test": "mocha -R list"
"test": "mocha -R list"
}
}
Loading

0 comments on commit b4e9bea

Please sign in to comment.