mongo-cursor-pagination
Advanced tools
Comparing version
{ | ||
"name": "mongo-cursor-pagination", | ||
"version": "4.1.0", | ||
"version": "4.1.1", | ||
"description": "Make it easy to return cursor-paginated results from a Mongo collection", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -49,2 +49,3 @@ # mongo-cursor-pagination | ||
3. Immutable. If the value changes between paged queries, it could appear twice. | ||
4. Complete. A value must exist for all documents. | ||
The default is to use the Mongo built-in '_id' field, which satisfies the above criteria. | ||
@@ -253,2 +254,4 @@ The only reason to NOT use the Mongo _id field is if you chose to implement your own ids. | ||
* 4.1.1 Fixed bug that would overwrite `$or` in queries passed in. | ||
* 4.1.0 Adds `sortAscending` option to sort by the `paginatedField` ascending. Defaults to false (existing behavior). | ||
@@ -255,0 +258,0 @@ |
@@ -393,2 +393,30 @@ var paging = require('../'); | ||
}); | ||
it('should not overwrite $or used in a query', () => { | ||
// First page of 2 | ||
var res = sync.await(paging.find(db.collection('test_paging_custom_fields'), { | ||
query: { $or: [{ counter: { $gt: 3 } }] }, | ||
limit: 2, | ||
paginatedField: 'timestamp' | ||
}, sync.defer())); | ||
expect(res.results.length).toBe(2); | ||
expect(res.results[0].counter).toBe(6); | ||
expect(res.results[1].counter).toBe(5); | ||
expect(res.hasPrevious).toBe(false); | ||
expect(res.hasNext).toBe(true); | ||
// Go forward 2 | ||
res = sync.await(paging.find(db.collection('test_paging_custom_fields'), { | ||
query: { $or: [{ counter: { $gt: 3 } }] }, | ||
limit: 2, | ||
paginatedField: 'timestamp', | ||
next: res.next | ||
}, sync.defer())); | ||
expect(res.results.length).toBe(1); | ||
expect(res.results[0].counter).toBe(4); | ||
expect(res.hasPrevious).toBe(true); | ||
expect(res.hasNext).toBe(false); | ||
}); | ||
}); | ||
@@ -632,2 +660,2 @@ | ||
}); | ||
}); | ||
}); |
@@ -37,2 +37,4 @@ var _ = require('underscore'); | ||
var queries = [params.query]; | ||
if (params.limit < 1) params.limit = 1; | ||
@@ -66,37 +68,45 @@ if (params.limit > config.MAX_LIMIT) params.limit = config.MAX_LIMIT; | ||
if (shouldSecondarySortOnId) { | ||
params.query.$or = [{ | ||
queries.push({ | ||
$or: [{ | ||
[params.paginatedField]: { | ||
[comparisonOp]: params.next[0] | ||
} | ||
}, { | ||
[params.paginatedField]: { | ||
$eq: params.next[0] | ||
}, | ||
_id: { | ||
[comparisonOp]: params.next[1] | ||
} | ||
}] | ||
}); | ||
} else { | ||
queries.push({ | ||
[params.paginatedField]: { | ||
[comparisonOp]: params.next[0] | ||
[comparisonOp]: params.next | ||
} | ||
}, { | ||
[params.paginatedField]: { | ||
$eq: params.next[0] | ||
}, | ||
_id: { | ||
[comparisonOp]: params.next[1] | ||
} | ||
}]; | ||
} else { | ||
params.query[params.paginatedField] = { | ||
[comparisonOp]: params.next | ||
}; | ||
}); | ||
} | ||
} else if (params.previous) { | ||
if (shouldSecondarySortOnId) { | ||
params.query.$or = [{ | ||
queries.push({ | ||
$or: [{ | ||
[params.paginatedField]: { | ||
[comparisonOp]: params.previous[0] | ||
} | ||
}, { | ||
[params.paginatedField]: { | ||
$eq: params.previous[0] | ||
}, | ||
_id: { | ||
[comparisonOp]: params.previous[1] | ||
} | ||
}] | ||
}); | ||
} else { | ||
queries.push({ | ||
[params.paginatedField]: { | ||
[comparisonOp]: params.previous[0] | ||
[comparisonOp]: params.previous | ||
} | ||
}, { | ||
[params.paginatedField]: { | ||
$eq: params.previous[0] | ||
}, | ||
_id: { | ||
[comparisonOp]: params.previous[1] | ||
} | ||
}]; | ||
} else { | ||
params.query[params.paginatedField] = { | ||
[comparisonOp]: params.previous | ||
}; | ||
}); | ||
} | ||
@@ -119,3 +129,3 @@ } | ||
collection | ||
.find(params.query, fields) | ||
.find({ $and: queries }, fields) | ||
.sort(sort) | ||
@@ -169,2 +179,2 @@ .limit(params.limit + 1) // Query one more element to see if there's another page. | ||
}); | ||
}; | ||
}; |
64553
1.97%1535
2.33%276
1.1%