Skip to content

HBKEngineering/node-mongodb-fixtures

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

23 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

node-mongodb-fixtures

Setup and tear down test fixtures with MongoDB.

Install

npm install node-mongodb-fixtures

CLI

For CLI use, it can be useful to install globally:

npm install node-mongodb-fixtures -g

Try the Example

The following example will load the example fixtures into a MongoDB database

Prequisite

  • A valid MongoDB connection string
  • node-mongodb-fixtures (This example assumes it is installed globally)

Run

  • clone this repo to get the sample fixtures i.e. ./examples/fixtures

  • Execute

     ❯ mongodb-fixtures load -u mongodb://localhost:27017/mydb --path ./examples/fixtures

Usage

Programmatic

const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures(); 

fixtures.connect('mongodb://localhost:27017/mydb').load() // load

See detailed programmatic usage below

CLI

❯ mongodb-fixtures load -u mongodb://localhost:27017/mydb'

See detailed cli usage below

Create fixtures

How

  1. Choose a directory for your fixtures e.g. ./fixtures
  2. Create a number of JSON files.
    • Each JSON filename defines MongoDB collection.
    • Each file must contain a JSON Array of JSON objects. e.g.
[
  { "name": "Paul", "age": 36 }, 
  { "name": "Phoebe", "age": 26 }
]
  • Each JSON object is loaded as a document in the collection.

Example

fixtures/
|-- people.json
|-- places.json

Programmatic Usage

Init

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' 
}); 

Connect

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

Load

fixtures.load()

Unload

fixtures.unload()

Disconnect

fixtures.disconnect()

Example

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());

CLI Usage

❯ 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 

License

MIT

About

Setup and tear down test fixtures with MongoDB.

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%