Skip to content

Commit

Permalink
refact pageByLastId with query params
Browse files Browse the repository at this point in the history
  • Loading branch information
i5ting committed Aug 12, 2015
1 parent b6af57b commit 3824438
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 7 deletions.
6 changes: 5 additions & 1 deletion api.md
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,14 @@ way 2: 4 params
根据id直接返回,长度看第二个参数,下面的例子是50

Top.pageByLastId(one._id, 50, function(err, new_tops){

根据id直接返回,长度看第二个参数,下面的例子是50,不带带有排序条件

Top.pageByLastId(one._id, 100, {"username" : "fixture-user-41"}, function(err, new_tops){
根据id直接返回,长度看第二个参数,下面的例子是50,带有排序条件,created_at是升序

Top.pageByLastId(one._id, 100, {created_at:'asc'}, function(err, new_tops){
Top.pageByLastId(one._id, 100, {"username" : "fixture-user-41"}, {created_at:'asc'}, function(err, new_tops){

## count

Expand Down
16 changes: 14 additions & 2 deletions lib/dao.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,12 +152,24 @@ MongooseDao.prototype.pageByLastId = function(){
n = arguments[1];
cb = arguments[2];
}else if (arguments.length == 4) {
// pageByLastId(lid, count, sort, cb)
// pageByLastId(lid, count, query, cb)
var lid = arguments[0];
q = _get_q_by_last_id(lid);
n = arguments[1];
sort = arguments[2];

_extend(q, arguments[2]);

cb = arguments[3];
}else if (arguments.length == 5) {
// pageByLastId(lid, count, query, sort, cb)
var lid = arguments[0];
q = _get_q_by_last_id(lid);
n = arguments[1];

_extend(q, arguments[2]);

sort = arguments[3];
cb = arguments[4];
}else{
// pageByLastId(lid, cb)
var lid = arguments[0];
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "mongoosedao",
"version": "1.0.11",
"version": "1.0.12",
"description": "mongoose dao",
"main": "index.js",
"scripts": {
Expand Down
28 changes: 25 additions & 3 deletions test/top.js
Original file line number Diff line number Diff line change
Expand Up @@ -147,19 +147,41 @@ describe('MongooseDao', function(){
});
})

it('should return Top.pageByLastId(_id,50,sort).length == 50 when Top.pageByLastId(_id,5,{created_at:desc})', function(done){

it('should return Top.pageByLastId(_id,50,sort).length == 50 when Top.pageByLastId(_id,5,{"username" : "fixture-user-41"},{created_at:desc})', function(done){
Top.pagesize = 20;
Top.top(30, function(err, tops){
if(err){
console.dir(err);
}
var one = tops[0];

Top.pageByLastId(one._id, 100, {"username" : "fixture-user-41"}, {created_at:'desc'}, function(err, new_tops2){
// console.dir(new_tops2.length);
var last = new_tops2[0];

// console.dir(last)
assert.equal("fixture-user-41" == last.username, true);
done();
});

});
})


it('should return Top.pageByLastId(_id,50,sort).length == 50 when Top.pageByLastId(_id,5,{},{created_at:desc})', function(done){
Top.pagesize = 20;
Top.top(30, function(err, tops){
if(err){
console.dir(err);
}
var one = tops[0];

Top.pageByLastId(one._id, 100, {created_at:'asc'}, function(err, new_tops){
Top.pageByLastId(one._id, 100, {},{created_at:'asc'}, function(err, new_tops){
// console.dir(new_tops.length);
var first = new_tops[0];
// console.dir(first)
Top.pageByLastId(one._id, 100, {created_at:'desc'}, function(err, new_tops2){
Top.pageByLastId(one._id, 100, {}, {created_at:'desc'}, function(err, new_tops2){
// console.dir(new_tops2.length);
var last = new_tops2[new_tops.length - 1];

Expand Down

0 comments on commit 3824438

Please sign in to comment.