Skip to content

Instantly start a MongoDB dev server and load database fixtures.

License

Notifications You must be signed in to change notification settings

ashleydavis/insta-mongo

Repository files navigation

insta-mongo

Instantly start a MongoDB dev server and load database fixtures.

You need Node.js installed to use insta-mongo.

Support the developer

Click here to support the developer.

An example

See here for an example of insta-mongo in a simple Node.js/Express app.

Install it

Install globally:

npm install -g insta-mongo

Install locally to use as a development database for your Node.js project:

npm install --save-dev insta-mongo

Run it

In your terminal, run it globally:

insta-mongo

Or run it locally:

npx insta-mongo

You now have a MongoDB database server with a REST API to load database fixtures*.

* A database fixture is a particular named set of test data for your application.

Integrate it in your Node.js project

To create a dev database that runs while your Node.js application is running use concurrently.

Your start and start:dev script in your package.json will look something like this:

{
    "scripts": {
        "start": "node index.js",
        "start-db": "insta-mongo",
        "start:dev": "concurrently \"npm run start-db\" \"npm start\" --kill-others"
    }
}

Using the flag --kill-others will stop the database server when your Node.js application terminates.

Usage

[npx] insta-mongo [options]
Options:

 --db-port=<port-no>            Sets the port for the database server.
                                Defaults to 5001.
 --rest-port=<port-no>          Sets the port for the REST API      
                                Defaults to 5000.
 --db=<database-name>           Sets the database into which to load 
                                the initial fixture.
 --load=<fixture-name>          Loads an initial named database fixture.
 --fixtures=<path-to-fixtures>  Sets the path that contains database fixtures.
                                Defaults to ./fixtures.

Database fixtures:

Place your database fixtures under ./fixtures like this:

./fixtures/
    fixture-1/
        collection1.json
        collection2.json
    fixture-2/
        another-collection.js

Each JavaScript or JSON file is loaded into it's own collection that is the same as the name of the file.

See example JavaScript collection here.

See example JSON collection here.

REST API end points

Hit the following endpoints to load and unload your database fixtures.

Loads a fixture:

HTTP GET http://localhost:5000/load-fixture?db=<db-name>&fix=<your-fixture-name>

Unloads a fixture:

HTTP GET http://localhost:5000/unload-fixture?db=<db-name>&fix=<your-fixture-name>

Drops a named database collection:

HTTP GET http://localhost:5000/drop-collection?db=<db-name>&col=<collection-name>

Gets the contents of a collection.

HTTP GET http://localhost:5000/get-collection?db=<db-name>&col=<collection-name>

See a VS Code REST Client script that exercises these REST APIs.

Acknowledgements

Thanks so much to the developers of mongodb-memory-server, without this it wouldn't be possible.

https://github.com/nodkz/mongodb-memory-server

Thanks also to my earlier project that has provided the REST API for insta-mongo and has now been superseded by insta-mongo:

https://github.com/ashleydavis/db-fixture-rest-api

db-fixture-rest-api you served me well, now you can rest ;)

About

Instantly start a MongoDB dev server and load database fixtures.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published