C
C
chakaponi2019-09-18 14:58:28
JavaScript
chakaponi, 2019-09-18 14:58:28

How to export a collection instance so that queries can be made through it?

A well-known MongoDB problem: Everything is built on callbacks. What is the correct way to export a collection (on which methods like findOne are called directly) to another module so that the function does not have to be expanded through a heap of async/await and/or .then each time to use it?
I want something like this:
coll.findOne({});, not In other words, how to make the require of the collection return a function ready for use, and not a Promise. So far, the only semi-adequate option seems to be a simple wrapping of the entire code by opening a session. PS Of course, the right option would be to create an API for a request to the database, but isn't it shooting sparrows from a cannon? (await coll).findOne({});

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question