Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Datastore.findOpts(query, opts, callback) #489

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 57 additions & 0 deletions lib/datastore.js
Original file line number Diff line number Diff line change
Expand Up @@ -493,6 +493,63 @@ Datastore.prototype.find = function (query, projection, callback) {
}
};

/**
* Same as find above, only that the second param is now an object of config objects.
* It will never return a cursor. This keeps this find nodeback function promisifyable.
*
* @param {Object} query MongoDB-style query
* @param {Object} opts
* @param {Object} opts.fields MongoDB-style projection
* @param {Object} opts.sort MongoDB-style sort
* @param {Object} opts.paging
* @param {Number} opts.paging.skip skip value for paging
* @param {Number} opts.paging.limit limit value for paging
*/
Datastore.prototype.findOpts = function (query, opts, callback) {

switch (arguments.length) {
case 1:
opts = {};
// callback is undefined, will return a cursor
break;
case 2:
if (typeof opts === 'function') {
callback = opts;
opts = {};
} // If not assume projection is an object and callback undefined
break;
}

var cursor = new Cursor(this, query, function(err, docs, callback) {
var res = [], i;

if (err) { return callback(err); }

for (i = 0; i < docs.length; i += 1) {
res.push(model.deepCopy(docs[i]));
}
return callback(null, res);
});

const projections = opts.fields||{};

cursor.projection(projections);

//load up the cursor depending on opts
let rollingCursor = cursor;

if(opts.paging){
rollingCursor = rollingCursor.skip(opts.paging.skip).limit(opts.paging.limit);
}

if (opts.sort){
rollingCursor = rollingCursor.sort(opts.sort)
}

rollingCursor.exec(callback);

};


/**
* Find one document matching the query
Expand Down
7 changes: 4 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
{
"name": "nedb",
"version": "1.8.0",
"name": "nedb-jollof",
"version": "1.0.0",
"author": {
"name": "Louis Chatriot",
"email": "louis.chatriot@gmail.com"
},
"contributors": [
"Louis Chatriot"
"Louis Chatriot",
"Iyobo Eki"
],
"description": "File-based embedded data store for node.js",
"keywords": [
Expand Down