Developing the MongoDB Repository

Learn how to use MongoClient to develop a repository and use it in an application.

We'll cover the following

In this lesson, we’ll learn how to use the MongoClient package to develop a repository.

Previously, we learned how to connect to the database and create the handlers for the user’s collection that we want. We know that our repository needs to have access to this handler so that it can interact with the collection.

Again, we could create the MongoDB client directly inside the repository, but that would make it impossible for us to test that repository without trying to connect to MongoDB.

Because we want the dependencies to be injected into the modules as much as possible, we’ll pass the MongoDB client into our repository via its constructor, which is something very similar to what we did in other parts of the code.

Let’s go back to our MongoDB repository and do this by following these steps:

  1. Create the constructor method inside the MongoDB repository.

Make sure it receives an object with a property named storage of the Database type, which is exported by the deno-mongo package:

Get hands-on with 1200+ tech skills courses.