Setup and tear down test fixtures with MongoDB. Use custom scripts to create indexes and more!
npm install node-mongodb-fixtures
For CLI use, it can be useful to install globally:
npm install node-mongodb-fixtures -g
The following example will load the example fixtures into a MongoDB database
- A valid MongoDB connection string
- node-mongodb-fixtures (This example assumes it is installed globally)
-
clone this repo to get the sample fixtures i.e.
./examples/fixtures
-
Execute
❯ mongodb-fixtures load -u mongodb://localhost:27017/mydb --path ./examples/fixtures
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures();
fixtures.connect('mongodb://localhost:27017/mydb').load() // load
See detailed programmatic usage below
❯ mongodb-fixtures load -u mongodb://localhost:27017/mydb'
- Choose a directory for your fixtures e.g.
./fixtures
- Create any mix of JSON (
.json
), JavaScript (.js
), or Typefiles (.ts
) files. - Each filename defines a MongoDB collection
JSON files should:
- Each contain a JSON Array of JSON objects. e.g.
- Each JSON object is loaded as a document in the collection.
[
{ "name": "Paul", "age": 36 },
{ "name": "Phoebe", "age": 26 }
]
JavaScript (or TypeScript) files should:
- Each return a JSON Array of JSON objects. e.g.
- Each JSON object is loaded as a document in the collection.
var ObjectId = require('mongodb').ObjectID;
module.exports = [
{ _id: ObjectId(), name: 'Paul', 'age': 36 },
{ _id: ObjectId(), name: 'Phoebe', 'age': 26 },
];
fixtures/
|-- people.js
|-- places.json
See ./examples/fixtures
"Collection scripts" enable you to inject your own custom logic in the fixture creation lifecycle. Each custom script is passed a reference to a MongoDB collection. You may use this reference to modify the collection however you like. For example, you can add indexes and more.
- Create a new JavaScript file with an underscore
_
suffix. e.g.people_.js
. - The
_
denotes a script. The text preceding it,people
, is the collection name. - Each script is passed a single argument, the collection.
- Each must return a
function
that takes acollection
and returns aPromise
.
// people_.js
module.exports = function(collection) {
// Write your custom logic and return a promise
return collection.createIndex( { "address.city": 1 }, { unique: false } );
}
fixtures/
|-- people_.js
|-- people.js
|-- places.json
Note: Custom scripts run after all fixtures have completed.
use the default fixtures directory,./fixtures
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures();
or specifiy the fixtures directory
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures({
dir: 'examples/fixtures'
});
Use the standard MongoDB URI connection scheme
fixtures.connect('mongodb://localhost:27017/mydb')
connect(uri, options, dbName)
arg | type | description |
---|---|---|
uri |
string (required) | MongoDB connection string |
options |
object (optional) | MongoDB connection options |
dbName |
string (optional) | identifies a database to switch to. Useful when the db in the connection string differs from the db you want to connect to |
See: ./examples/ex1.js
fixtures.load() // returns a promise
fixtures.unload() // returns a promise
fixtures.disconnect() // returns a promise
The following example does the following:
- connects to mongo
- then unloads all fixtures
- then load all fixtures
- then disconnects
const Fixtures = require('node-mongodb-fixtures');
const uri = 'mongodb://localhost/mydb'
const options = null;
const fixtures = new Fixtures({
dir: 'examples/fixtures'
});
fixtures
.connect('mongodb://localhost:27017/mydb')
.unload()
.then(() => fixtures.load())
.catch(e => console.error(e))
.finally(() => fixtures.disconnect());
❯ mongodb-fixtures
Usage: mongodb-fixtures [options] [command]
Options:
-V, --version output the version number
-u --url <url> mongo connection string
-s --ssl use SSL
-d --db_name <name> database name
-n --ssl_novlidate use SSL with no verification
-c --ssl_ca <base64> path to cert
-p --path <path> resource path. Default ./fixtures
-b --verbose verbose logs
-h, --help output usage information
Commands:
load
unload
rebuild
[info ] Using fixtures directory: /Users/dimascio/git/node-mongodb-fixtures/examples/fixtures
[info ] Using database mydb
[start] load people
[start] load places
[done ] load people
[done ] load places
[done ] *load all
[start] script people_.js
[done ] script people_.js
[done ] *script all