Research
Security News
Malicious npm Packages Inject SSH Backdoors via Typosquatted Libraries
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
cagination
Advanced tools
A Mongoose helper to simplify the pagination process.
var currentPage = data.currentPage;
var perPage = data.perPage;
var options = {
actorId: nicCage._id
};
Film.find(options).select('name actorId').populate({
path: 'actorId',
select: 'lastName'
}).sort({
name: -1
}).skip((currentPage - 1) * perPage).limit(perPage).exec(function(err, films) {
Film.count(options).exec(function(err, count) {
var totalPages = Math.ceil(count / perPage);
// return films and totalPages
});
});
NOT THE PAGES! NOT THE PAGES! AGWHGHAHGHAA THEY'RE CLOGGING MY CODE!
Of course this is quite a mess, and painful to rewrite for each query you need paginated. Although the .count() procedure is pretty quick, to make this really efficient, you would need to also utilize async to perform that operation in parallel with .find(). But then you have a real disaster on your hands for such a simple task.
Caginate aims to be robust but reproducible. Like your standard find() query, Caginate can accept the same options for select, populate, and sort. However, Caginate handles all of the bothersome math and manual skipping/limiting needed to get a paginated return. Additionally, it asynchronously grabs the document count in order to provide you with the total number of pages.
var currentPage = data.currentPage;
var perPage = data.perPage;
var options = {
firstName: 'Nicolas'
};
caginate(Film, {
options: options,
select: 'name actorId',
populate: {
path: 'actorId',
select: 'lastName'
},
sort: {
name: -1
},
perPage: perPage,
currentPage: currentPage
}).exec(function(err, films, count, totalPages) {
// return films and totalPages
});
Get the source from GitHub or install via NPM
npm install cagination --save
Make sure to add:
var caginate = require('caginate').find;
and you should be set. At any time you want paginated results, replace your Mongoose Model.find(query, callback)
with caginate(Model, query, callback)
.
The Caginate query is an object consisting of:
The Caginate callback will look something like:
caginate(Model, {
perPage: 10,
currentPage: 1
}, function(err, documents, count, totalPages) {
if(err) {
// handle the Mongoose error
}
// documents - documents matching query but limited by perPage
// count - the total number of documents matching your query
// totalPages - pages needed to fit all of the documents at 10 per page
});
FAQs
A Mongoose helper to simplify the pagination process.
The npm package cagination receives a total of 0 weekly downloads. As such, cagination popularity was classified as not popular.
We found that cagination demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Research
Security News
Socket’s threat research team has detected six malicious npm packages typosquatting popular libraries to insert SSH backdoors.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.