2

I am building a component that is consumable by NPM. The application needs to connect to the database. If the user does not specify the details to the database uri in the config files, my application should spin up an instance of mongoDB from within the application itself.

I have seen many tutorials on how to integrate mongoDB into node.js, express, mocha and many other libraries, but all connect to an external database that's already running.

mongoDB server is being included as part of the dependencies so is installed with everything else, but I'm coming up short on material online on how to do boot up the database when the application starts.

Any help would be greatly appreciated. I suspect the information is out there, but I'm having a hard time finding it :(

Nicholas Mordecai
  • 859
  • 2
  • 12
  • 33
  • 1
    A database is logically a seperate application, it's actually nice to seperate your node application and the mongodb application, having them "loosely" coupled. Wouldn't it be an option create a new mongo db server for such a user where no config files are specified? You won't have to implement stuff that is already implemented better by the mongo db application. – levilime Nov 07 '16 at 21:32
  • Yes, I agree. How difficult is it to install and create a new mongodb server and dataset on demand with little to no user input at all? Just a flag in the configs to a custom DB and if it doesn't exist, then create a new server. Thanks – Nicholas Mordecai Nov 07 '16 at 23:02
  • Hey Nicholas I formulated an answer based on your requirements in the comments. Good luck! – levilime Nov 08 '16 at 07:41

2 Answers2

3

You can look into starting a subprocess. So letting Node run mongo as an external program. This way you could create a new mongodb instance for any user that fits that criteria. You can use for example the node "child process" library and use the spawn function to 'spawn' a new database instance by writing there the console command as already stated by Larry Turtis or here mongodb - multiple instances. It could look like this, to start the instance:

const spawn = require('child_process').spawn;
const pipe = spawn('mongod', ['--dbpath=<LOCATION>', '--port', '<PORT>'])

You can pipe console output to node with this:

    pipe.stdout.on('data', function (data) {
        printback(data.toString('utf8'));
    });

    pipe.stderr.on('data', (data) => {
        printback(data.toString('utf8'));
    });

    pipe.on('close', (code) => {
        callback('Process exited with code: '+ code);
    });

And kill the mongodb running instance by keeping the pipe reference and then doing this:

static end_pipe(pipe) {
    pipe.kill('SIGINT');
}
levilime
  • 362
  • 1
  • 9
  • Having error with this aproach: `Error: spawn mongod ENOENT` , should mongo be given a specific stdio ? (https://nodejs.org/api/child_process.html#child_process_options_stdio) – user1767316 Mar 31 '20 at 15:23
2

The command to boot mongoDB is available on the NPM mongodb page. Adding --fork and --logpath because I assume you want it to start in the background.

mongod --dbpath=/data --port 27017 --fork --logpath /var/log/mongod.log

You could just include this as part of your npm start script in package.json:

"scripts": {"start": "mongod --dbpath=/data --port 27017 --fork --logpath /var/log/mongod.log"}
Larry Turtis
  • 1,907
  • 13
  • 26