Skip to content

Instantly share code, notes, and snippets.

@popeating
Created November 3, 2023 13:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save popeating/e93af05634d258ad807ad59c78edd266 to your computer and use it in GitHub Desktop.
Save popeating/e93af05634d258ad807ad59c78edd266 to your computer and use it in GitHub Desktop.
export async function contactList(params) {
try {
const limit = 5;
const page = params.page || 1;
const skip = limit * (page - 1);
const contacts = await Contact.find()
.sort({ createdAt: -1 })
.sort({ updatedAt: -1 })
.limit(limit)
.skip(skip);
const count = await Contact.find().count();
const pages = Math.ceil(count / limit);
const newContact = contacts.map((contact) => ({
...contact._doc,
_id: contact._doc._id.toString(),
}));
return { contacts: newContact, pages: pages };
} catch (e) {
return { error: e.message };
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment