A REST API for loading and unloading MongoDB database fixtures.
Featured in the book Bootstrapping Microservices.
Don't run this on a production server, it is for testing only and provides unauthenticated access to your database!
Use this in your automated test process to load and unload your database for testing.
This builds upon node-mongodb-fixtures.
If you like this project, please star this repo and support my work
You must have Node.js installed to run this.
You need a MongoDB instance ready for testing.
src/index.js -> The JavaScript file that implements the REST API.
Dockerfile -> Allows you to package this application in a Docker image.
fixtures/ -> Database fixtures live under this directory.
example-json-fixture/ -> An example database fixture in JSON format.
example-js-fixture/ -> An example database fixture in JavaScript format.
This REST API exposes the following endpoints.
The default port number is 3555, but you can set this to any value as described in the section Use it in your project below.
HTTP GET http://localhost:3555/load-fixture?db=<database-name>&fix=<fixture-to-load>
HTTP GET http://localhost:3555/unload-fixture?db=<database-name>&ix=<fixture-to-unload>
HTTP GET http://localhost:3555/get-collection?db=<database-name>&col=<collection-to-get>
HTTP GET http://localhost:3555/drop-collection?db=<database-name>&col=<collection-to-drop>
Be careful to only drop a collection on dev or testing machines!
HTTP GET http://localhost:3555/drop-database?db=<database-name>
Be careful to only drop a database on dev or testing machines!
By default the db fixture REST API is executed using nodemon and is setup to watch the fixtures directory. This means you can update or add fixtures in order to restart the server.
Clone or download this repo. Open a command line and change directory to the repo then install dependencies:
cd db-fixture-rest-api
npm install
Now run the REST API:
npm start
Now use curl or your fav REST API client (eg ARC or Postman), hit the HTTP endpoints documented in the previous section to load and unload your database fixtures.
Two example database fixtures are included under the fixtures sub-directory.
You can load them using the names example-js-fixture and example-json-fixture.
For example, to load example-js-fixture hit http://localhost:3555/load-fixture?db=my-test-db&fix=example-json-fixture with a HTTP GET request.
After you have loaded a fixture, browse your database to check that the data has been loaded correctly. By default it is loaded under a database named my-test-database.
After that you can unload the fixture and then check that the data was removed from the database.
For example to unload example-js-fixture, hit http://localhost:3555/unload-fixture?db=my-test-db&fix=example-json-fixture with a HTTP GET request.
You can also directly drop any collection, for example to drop a collection called person, hit http://localhost:3555/drop-collection?db=my-test-db&col=person with a HTTP GET request.
In a similar way you can drop an entire database.
To use this REST API to help test your own project please customize it by setting the following environment variables:
- FIXTURES_DIR - Set the location of the fixtures directory (defaults to 'fixtures').
- PORT - Set the port number of the REST API (defaults to 3555).
- DBHOST - Set the host address for the MongoDB server instance (defaults to mongodb://localhost:27017)..
Doesn't do what you want? Please fork the code and hack it to your heart's content ;)
Please see node-mongodb-fixtures for more of what you can do with these database fixtures.