keystonejs / storage

Storage.js is a Javascript library that gives you an easy and standardised access to any provider of your choice.
MIT License
60 stars 12 forks source link

MongoClient - database connection #5

Closed grabbou closed 10 years ago

grabbou commented 10 years ago

Scripts using e.g. MongoDB are not exiting because connection with the database is still open in the background. As closing and opening it every Storage.get() is inefficient, we need a better way of handling that. We can't force users to call any extra method manually to clean-up connection so it has to play nicely with built-in NodeJS capabilities.

It's not an issue if we are using express-based apps (like Keystone) as they are running all the time.

grabbou commented 10 years ago

Probably the best way is to attach process.exit at the end of your script to clean-up MongoDB connection after your script executes. In Keystone and other express apps - it's not an issue.

Sample snippet:

Storage.get('mongo', function (err, client) {
    async.waterfall([
        function uploadFile(callback) {
            client.upload('../LICENSE', 'testing/license.md', function (err) {
                callback(err);
            });
        },
        function downloadFile(callback) {
            client.download('testing/license.md', 'license2.md', function (err) {
                callback(err);
            });
        },
    ], function (err) {
        if (err) {
            console.log('Error - ', err);
            process.exit(1);
        } else {
            console.log('Successful');
            process.exit(0);
        }
    });
});

It's going to play nicely with #8.