
Security News
ESLint Adds Official Support for Linting HTML
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
node-mongodb-fixtures
Advanced tools
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
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures();
fixtures
.connect('mongodb://localhost:27017/mydb')
.then(() => fixtures.unload())
.then(() => fixtures.load())
.then(() => fixtures.disconnect());
See detailed programmatic usage below
❯ mongodb-fixtures load -u mongodb://localhost:27017/mydb
The following example will load the example fixtures into a locally running MongoDB. To use another DB modify the the connection url accordingly (in step 2)
git clone https://github.com/cdimascio/node-mongodb-fixtures && cd node-mongodb-fixtures && npm install
node bin/mongodb-fixtures load -u mongodb://localhost:27017/mydb --path ./examples/fixtures
./fixtures
.json
), JavaScript (.js
) files. (see file rules below)JSON Files
The name of the JSON file is/will be the collection name. Each JSON file must contain a 'JSON Array of JSON objects'. Each JSON object is loaded as a document in the collection.
JSON files are useful when you can represent all of your documents as JSON.
[
{ "name": "Paul", "age": 36 },
{ "name": "Phoebe", "age": 26 }
]
JavaScript Files
The name of the JSON file is/will be the collection name. Each .js
file must return a 'JSON Array of JSON objects'. Each JSON object is loaded as a document in the collection.
JavaScript files are useful when you require code to represent your documents.
const { ObjectID: ObjectId } = require('mongodb');
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.
_
suffix. e.g. people_.js
._
denotes a script. The text preceding it, people
, is the collection name.function
that takes a collection
and returns a Promise
.// 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',
mute: false, // do not mute the log output
});
or filter the fixtures present in the directory with a Regex pattern
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures({
dir: 'examples/fixtures',
filter: 'people.*',
});
Use the standard MongoDB URI connection scheme
fixtures.connect('mongodb://localhost:27017/mydb'); // returns a promise
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:
const Fixtures = require('node-mongodb-fixtures');
const uri = 'mongodb://localhost/mydb';
const options = null;
const fixtures = new Fixtures({
dir: 'examples/fixtures',
filter: '.*',
});
fixtures
.connect('mongodb://localhost:27017/mydb')
.then(() => fixtures.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_novalidate use SSL with no verification
-c --ssl_ca </path/to/cert> path to cert
-p --path <path> resource path. Default ./fixtures
-f --filter <pattern> regex pattern to filter fixture names
-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
[info ] No filtering in use
[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
Contributors are welcome!
Special thanks to those who have contributed:
FAQs
A package and CLI for MongoDB fixtures
The npm package node-mongodb-fixtures receives a total of 2,319 weekly downloads. As such, node-mongodb-fixtures popularity was classified as popular.
We found that node-mongodb-fixtures demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
ESLint now supports HTML linting with 48 new rules, expanding its language plugin system to cover more of the modern web development stack.
Security News
CISA is discontinuing official RSS support for KEV and cybersecurity alerts, shifting updates to email and social media, disrupting automation workflows.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.