Search code examples
node.jsmongodbmongoskin

Mongoskin MongoClient.connect sync


In the module mongoskin when getting the database object mongoskin does this in a sync fashion, hence the code:

var db = mongoskin.db(url, {
    native_parser: true
});
var myCollection = db.collection('myCollection');

I am curious, how do they achieve this? I've been looking at the mongoskin code I however do not fully understand it.

And here, you can see mongoskin simplifys it :

https://github.com/kissjs/node-mongoskin#dbcollection-callback origin vs. mongoskin

Anyone know how mongoskin turned async code into sync fashion?


Solution

  • They say it in the beginning of the readme:

    We make some common use functions in promise mode, we call it SkinClass of a normal Class. And the API is almost same with official API.

    db.collection('myCollection'); returns SkinCollection - a wrapper for native Collection, which will use Collection when it is available.

    The magic lives here: https://github.com/kissjs/node-mongoskin/blob/master/lib/utils.js