DEV Community

Cover image for Cosmos DB SQL API for NestJS and Dynamic Module
Anzhari Purnomo
Anzhari Purnomo

Posted on

Cosmos DB SQL API for NestJS and Dynamic Module

So recently, I need to use Azure Cosmos DB with their Core SQL API as my data persistence layer for my NestJS application. I looked around NestJS documentation, and the ORM we can use for NestJS (TypeORM, Mongoose, Sequelize, and Prisma) only support either PostgreSQL, MySQL/MariaDB, MongoDB, and other database engines, but not Cosmos DB SQL API.

Thanks to the beauty of open-source software, usually within a 30 minutes search, I would find a module that would satisfy my needs. So, I searched far and wide and found @nest/azure-database.

I tried it and asked a question on Stackoverflow on using the module for my use case, and finally, I decided that at its current state, I am not confident enough to use it in my application.

I noticed that Cosmos DB provided us with a Javascript NodeJS SDK, so I am thinking, is it possible to use the SDK API on NestJS directly?

So this question leads me to this Reddit post, this dev.to post and this Stackoverflow post, which indicates that it is possible and to make it happen, we need to use NestJS Dynamic Module.

To quote the explanation from the docs:

… dynamic modules provide an API for importing one module into another, and customizing the properties and behavior of that module when it is imported, as opposed to using the static bindings we've seen so far.

Also, there are two sample implementations you can refer to by Jay McDoniel where the author is using dynamic module with NodeJS pg module and apparently in NestJS sample directory where the sample is using NodeJS Mongoose module without using the NestJS' version.

So, for now, I will see if I can utilize NestJS' dynamic module for my use case and will share my sample implementation in my future post.

Top comments (0)