mongodoki
v5.0.0
Published
A tool to on-the-fly run MongoDB as a Docker Container, data restoring included
Downloads
84
Readme
Mongodoki
A little library and command-line tool to on-the-fly run MongoDB as a Docker Container.
Prerequisites
Docker must be installed and running on your machine, Mongodoki does the rest. Thus, no need to natively install MongoDB on your machine.
Node.js version 7.10.0 or 8.x.x
Quick Start as library
npm install mongodoki
then:
const Mongodoki = require('mongodoki').Mongodoki;
const md = new Mongodoki();
//create and start a mongo:latest Docker container and return a MongoDB Db instance, default port: 27017, db name: local, container name: mongodoki-container
//db is a mongodb#MongoClient instance.
const db = await md.getDB();
//use Mongo Db instance through official mongodb drivers
const collections = await db.collections();
...
Quick Start as command line tool
npm install -g mongodoki
$ mongodoki --help
;)
Example:
$ mongodoki start -n awesomedb
$ mongodoki stop awesomedb
API
Instantiating Mongodoki
const md = new Mongodoki([config]);
config
object is optional; when missing, it defaults to:
{ tag: 'latest', containerName: 'mongodoki', hostPort: 27017, reuse: false }
where:
tag
- is the preferred tag for the official Docker Image;
containerName
- is the name of the container to create;
port
- is the MongoDB port at which dockerized mongod will listen;
reuse
- boolean, if true it will try to reuse an existing container avoiding building a new one;
volume
- optional, is an object with two properties:
{hostDir: <abs_path>, containerDir: <abs_path>'}
when volume
is specified it allows to bind a directory with path in hostDir
in the local host machine to a container volume path specified by the containerDir
property. All paths are absolute and hostDir
directory must be included in File Sharing Docker Preferences.
Specifying a volume
binding allows to locally persist the database data in the host machine, preserving it between containers start/stop/create/destroy lifecycle.
Example:
let md = new Mongodoki( {containerName: 'myMongo', reuse: false, volume: {hostDir: '/Users/diego/temp', containerDir: '/data/db'}} );
Mongodoki Methods
All methods return a Promise.
Create the Container (eventually import a data dump) and connect to the DB
getDB(dbName = 'local' [, timeout = 60000] [, dbDumpPath] )
Returns a Promise with a connected MongoClient object in case of success;
Parameters:
dbName
- name of the dockerized DB to connect to;
timeout
- optional, max time to wait for the container/db startup, in milliseconds (default: 60000);
dbDumpPath
- optional, root directory path of a db dump (previously created by MongoDB mongodump
tool); in case of a successful import the created database on the container will contain the restored data.
Manage the Container
stop();
remove();
stopAndRemove();
respectively, they stop, remove and stop+remove the Docker container.
Removing a container also removes all the created (and unused) Docker Volumes on the host machine (same as running docker volume prune
command).
License - "MIT License"
Copyright (c) 2017 Antonio Pintus, Vivocha S.p.A.
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.