npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

paginate-response

v1.0.2

Published

this is a package for getting response data as a pagination in expressjs with mongodb only

Downloads

44

Readme

paginate-response

npm package for converting response data to a paginated response in Node.js with Express.js and MongoDB

Installation

Install paginate-response with npm

  npm install paginate-response

Usage/Examples

// put where model schema code is

import paginate from "paginate-response";

...

const adminSchema = new mongoose.Schema({
    ...
})

adminSchema.plugin(paginate);

const Admin = mongoose.model("Admin", adminSchema);

export default Admin;

// create of paginate function

//admin.service.ts

import QueryString from "qs";
...

const queryAdmins = async (
  filter: Pick<QueryString.ParsedQs, "name">,
  options: Pick<QueryString.ParsedQs, "sortBy" | "limit" | "page" | "search">
) => {
  const admins = await Admin.paginate(filter, options);
  return admins;
};

...

// usage of paginate function

// admin.controller.ts

import adminService from "./../services/admin.service";
...

const getAdmin = catchAsync(async (req: Request, res: Response) => {
  const filter = pick(req.query, ["name"]);
  const options = pick(req.query, ["sortBy", "limit", "page", "search"]);
  const result = await adminService.queryAdmins(filter, options);
  res.status(httpStatus.OK).send(result);
});

...

Output

{
    "results": [
        {
            ...
        },
        ...
    ],
    "page": 1,
    "limit": 10,
    "totalPages": 1,
    "totalResults": 2
}

Usage in Api

in api we can pass options as a query

in this case we can search by name because we pass name in filter, if we want to more filter like email, alias,etc then we pass all parameter in filter.

now, option is for pagination,

  • a sortBy option is for sorting data assending or descending order.
const res = fetch("BASE_URL/admin?sortBy=asc", { method: "GET" });
  • a limit is for how much data comes in one page
const res = fetch("BASE_URL/admin?limit=10", { method: "GET" });
  • a page option is a represent current page if we want to go to 2nd page then
const res = fetch("BASE_URL/admin?page=2", { method: "GET" });
  • a search option is for we can filter data by field which we set in filter
const res = fetch("BASE_URL/admin?search=xyz", { method: "GET" });

Tech Stack

Server: Node, Express

Databse: MongoDB

Authors

License

MIT