comments 3

External MongoDB Connections With MeteorJS

At the very beginning of, I was completely unfamiliar with server side java script programming, even with node-js. There were just a few hear-says in my mind and a huge code base to solve how it works. Then I’ve started to look at great resources of meteor to learn it. Discover meteor book was really helpful, furthermore there are some great videos on Udemy to dive deeps.  Anyways, there wasn’t any good resource to learn how to connect mongodb through meteor, and of course this was the first approach to start. As you know there are a lot of mongodb drivers which are officially supported, you can access the whole list here. Unluckily meteor hasn’t been supported by mongodb guys yet, but node-js. And luckily meteor-js uses node-js on server-side.

By default meteor comes with its own database which is mongodb, furthermore it provides great flexibility to use it. Defining collections, doing CRUD operations on them is like finger snaps. Unfortunately, there’s no chance to use it to connect an external mongodb. That was the first hassle that I encountered in a short time of beginning. Therefore, I decided to learn node-js mongodb driver and node-js mongodb driver API page was my only and lovely friend at this point, it has been really helpful through the way of development. Although all of these great features there was a problem, how would I be able to use node-js in meteor. Yes meteor’s server-side is written in node-js, but it provides a completely different syntax, usage and disables the node-js usage by default. (As I heard, they are planing to add node-js packages support in meteor 1.3.0)

And at this point, I found this great project that provides usability of NPM packages in meteor-js. The best part of this package is, it really helps you to use node-js packages easily without struggling much, since the worst part is, NPM packages can’t be updated through meteor update command which updates every meteor packages to the latest version. It puts a packages.json file into your project’s main directory that you can type NPM packages with versions, as you want them to install on first startup of project. Here’s my packages.json you can keep tracking this file on my github page to see which version is currently being used.

Sadly, this wasn’t the worst part. As you can know, all node-js methods are asynchronous by default. I needed them to work as synchronous, since I cant trust users on waiting until a query execution finishes. And here’s the Async package that perfectly suits my needs.  Here’s an example method:

This method connects an external mongodb and returns the collection list as a string array. As you can see, Async.runSync is my savior here. It provides a done callback which takes two arguments. These are error and result. It returns result object as well as error after method execution synchronously.

I’m not the man who can put principles on programming yet. But I know a thing well, to avoid shitty situations don’t be scared to put logs on necessary places, especially at exceptions and unexpected situations. They can be the most helpful approaches in your code.

I will be writing about design choices in my next blog post.

Thanks !


Leave a Reply

Your email address will not be published. Required fields are marked *