Use Mongodb in Deno with Danet
Welcome ! Today’s article is here to help you add a MongoDB connection to your Danet’s API.
We assume that you already have a MongoDB database available, either locally (documentation here) or on a remote server/cloud provider, such as MongoDB Atlas which has an awesome free tier.
To connect to our mongodb database we will use the Denodrivers’ mongo driver : https://deno.land/x/mongo@v0.31.1
In most cases, the database’s connection is shared among all our services in our code, our MongoDBService
will then be a Singleton.
We also need to connect to our Database Server only once, when the app starts, and we need to close the connection when our app stops. In order to do this, we can use onAppBootstrap
and onAppShutdown
lifecycle hooks !
Let’s dive into the code now :
For simplicity’s sake, we are importing everything from urls. However, we recommend creating a deps.ts
file to import/export everything. Doing so result in a single point of truth and an easier maintenance.
Few key points :
-
The service implement
OnAppBoostrap
andOnAppShutdown
to use lifecycle hooks. -
The connection URI is created using
Deno.env.get
to get environment variables.
Now, we need to create a DatabaseModule
to keep our code clean :
Import this module in any module that need your newly created MongoDBService
, and use it wherever you need.
Here is a simplified code from of our Starter Repository that contains all the necessary code to work with MongoDB AND Postgres seamlessly:
export class MongodbRepository implements Repository<Todo> {
constructor(private dbService: MongoDBService) {
}
async getAll(): Promise<Todo[]> {
return this.dbService.getCollection<Todo>("Todo").find({}).toArray();
}
async getById(id: string) {
return this.dbService.getCollection<Todo>("Todo").findOne({
_id: new ObjectId(id),
});
}
async create(todo: Omit<Todo, "_id">) {
const insertedId = await this.dbService.getCollection<Omit<Todo, "id">>(
"Todo",
).insertOne(todo);
return {
_id: insertedId,
...todo,
};
}
async updateOne(todoId: string, todo: Todo) {
const objectId = new ObjectId(todoId);
const updated = await this.dbService.getCollection<Todo>("Todo").updateOne(
{ _id: objectId },
{ $set: { ...todo } },
);
return updated;
}
async deleteOne(todoId: string) {
return this.dbService.getCollection<Todo>("Todo").deleteOne({
_id: new ObjectId(todoId),
});
}
async deleteAll() {
return this.dbService.getCollection<Todo>("Todo").deleteMany({});
}
}
Well, that’s it folks ! It wasn’t hard, now you know !
To learn how to use Postgres in Danet, check this article out
To learn more about Danet, check out our documentation : https://savory.github.io/Danet/
The GitHub repository if you want to contribute : https://github.com/Savory/Danet