Created
November 3, 2023 13:25
-
-
Save popeating/e93af05634d258ad807ad59c78edd266 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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