Compatibility tests when switching from MongoDB to CosmoDB with Mongo API
We are using MongoDB 3.6 beacuse its one of the version where LTS expiry is still to be defined: https://www.mongodb.com/support-policy
- MongoDB 3.4 has LTS till 1 Jan 2020.
- MongoDB 3.2 LTS expired on 2019.
git clone https://github.com/conplementAG/mongodb-cosmosdb-compatibility
- Docker
- Docker Compose (Included in Docker Desktop)
First you need to create a CosmoDB instance in Azure.
If you are not logged in Azure use az login from the cli.
Be sure to set the right subscription using az account set --subscription xxxxxxxxx.
Now you can change the parameter and run the script set-cosmosdb.sh that sits on the scripts folder.
This will create cosmodb instance and output the neccessary information that you will need to follow along.
Note: Aggregation pipelines by default are disabled. The set-cosmosdb.sh enables them but if you create the CosmoDB manually go to the CosmoDB account under Preview Features and enable Aggregation Pipeline.
Download and install CosmoDB emulator from https://aka.ms/cosmosdb-emulator.
If you want to use docker for running the cosmodb emulator you will need to enable windows containers, currently it dosent run on Linux Containers.
For our use case we will install the emulator on the host machine.
By default CosmoDB emulator starts only the SQL endpoint enabled so you need to enable the MongoDB endpoint manually.
To enable MongoDB endpoint start the emulator from CMD with Administrative rights using the option /EnableMongoDbEndpoint
E.g. C:\Programs\Azure Cosmos DB Emulator /EnableMongoDbEndpoint.
From the root folder build the Docker Image
docker build . -t poc:latest
Docker compose will spin up three containers for our application + the mongo db instance.
Each of our app containers will need their .env files namely:
app.cosmos.envused to hold the credentials of the cosmos db instance in azureapp.localcosmos.envused to hold the credentials of the local cosmos db instnaceapp.mongo.envused to hold the credentials of the local mongodb instancemongo.envcontains the init username and password configuration, template ismongo.env.template
After creating and filling the env files from the root folder you can start everything using
docker-compose up
When installing the azure cosmos emulator in the host machine the url to access the locahost from the container is host.docker.internal which points to the ip address of the host machine.
One alternative is to run the container which accesses the emulator using network_mode=host but be aware that Docker Desktop for Mac and Windows dosent support it. It works only on Linux.
That means in the app.localcosmos.env file the DB_CONNSTR contains the value mongodb://host.docker.internal:10255/dbName?ssl=true to reference the host machine.
If you mount the ./src folder into the container so that while the container is running you can change the code be sure that from your host machine you didnt install nodes_modules first. Some of the modules when installed for Windows will download command scripts that wont work in Linux.
If you install the node_modules from the container then it will work when you change the code locally (but not when run also locally).
If you want to test each of the connections you can log into the respective containers using
docker exec -it xxxcontainer_idxxx "/bin/bash"
The application has been configured with script commands which can be run using npm run command_name
testruns all the tests once (Default)twatchruns the tests on watch mode, helpful when mounting the.\srcfolder and connection to the container interactivly.test:debugopens the possibility to debug the tests remotly usingchrome://inspect(dont forget to port map in the docker-compose.yaml)startwhich starts nodemon index.js. Use it to keep the container alive (e.g. to connect to the container). You can override it in docker-compose file. The tests are written to test basic CRUD operations, Aggregations and more complex Queuries.