Giter VIP home page Giter VIP logo

node-mongodb-fixtures's Introduction

node-mongodb-fixtures

Setup and tear down test fixtures with MongoDB.

Use custom scripts to create indexes and more!

GitHub stars Twitter URL

Install

npm install node-mongodb-fixtures

CLI

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

npm install node-mongodb-fixtures -g

Usage

Programmatic

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

CLI

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

See detailed cli usage below


Try the Example

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)

Run the example

  1. Clone the repo
git clone https://github.com/cdimascio/node-mongodb-fixtures && cd node-mongodb-fixtures && npm install
  1. Load the fixtures into MongoDb
node bin/mongodb-fixtures load -u mongodb://localhost:27017/mydb --path ./examples/fixtures

Create fixtures

How

  1. Choose a directory for your fixtures e.g. ./fixtures
  2. Create any mix of JSON (.json), JavaScript (.js) files. (see file rules below)
  3. Each filename defines a MongoDB collection

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 },
];

Example Structure

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

See ./examples/fixtures

Collection Scripts: Indexes and more...

"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.

How

  1. Create a new JavaScript file with an underscore _ suffix. e.g. people_.js.
  2. The _ denotes a script. The text preceding it, people, is the collection name.
  3. Each script is passed a single argument, the collection.
  4. Each must return a function that takes a collection and returns a Promise.

Example

// 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.

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

Connect

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

Load

fixtures.load(); // returns a promise

Unload

fixtures.unload(); // returns a promise

Disconnect

fixtures.disconnect(); // returns a promise

Example

The following example does the following:

  • connects to mongo
  • then unloads all fixtures
  • then loads 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',
  filter: '.*',
});

fixtures
  .connect('mongodb://localhost:27017/mydb')
  .then(() => fixtures.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_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

Example Output

[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

Contributors are welcome!

Special thanks to those who have contributed:

License

MIT

Buy Me A Coffee

node-mongodb-fixtures's People

Contributors

cdimascio avatar darrenwest-allocate avatar dependabot[bot] avatar leeandher avatar mushketyk avatar myastr0 avatar mykolasmolis avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

node-mongodb-fixtures's Issues

MongoError: ns not found

After upgrading to latest version (2.3.3) I started getting this error when calling fixtures.unload():

UnhandledPromiseRejectionWarning: MongoError: ns not found

The problem gets fixed when downgrading to version 2.2.1

Invalid document id results in misleading error

Hey guys,

Ran into a confusing error today. I was accidentally trying to insert a document with an invalid ObjectId (1 character too short) and the error message I received as output was:

[error] no docs returned from file: "LendingFiles.ts". verify that a docs array was exported. note: if using .js files, use "module.exports", instead of "exports".

Took me a long time to pin down that there was a particular fixture that was failing the whole thing because of my bad ObjectId

How can you specify a Date type field from json?

for example.json convert the created_at and updated_at fields to fields of type Date

[
  {
    "name": "Foo",
    "created_at": "2020-11-07T12:56:59.301Z",
    "updated_at": "2014-11-07T12:56:59.301Z"
  }
]

Can I have multiple files for same collection

Can I have multiple files for the same collection? The documentation says that I need a file with the name of the collection like: users.js but I want to put them into separate files like:

users.registered.js
users.samples.js
users.expired.js

How to skip load more fixtures without removing existing entries.

Hello guys! Thanks a lot for a great job!

I have a question about how can I load some fixtures to my database without removing existing entries from the collection. My use case is that I have a list of categories in my app with some categories defined at the start by myself (these I want to load from fixtures) but users of the app can also create more categories. So what I want to do is to only add entries defined in fixtures but not removing existing ones. I did manage to do it by removing unload() function from my file but then I'm getting an error as it tries to add entries that already exist (the key name in the collection has to be unique).

My file looks like this:

require('dotenv').config();
const Fixtures = require('node-mongodb-fixtures');
const fixtures = new Fixtures({
  dir: './fixtures',
});

fixtures
  .connect(process.env.DB_CONNECTION_URL)
  .then(() => fixtures.load())
  .catch(e => console.error(e))
  .then(() => fixtures.disconnect());

As you can see I'm catching these errors so it works but I'm wondering if there is a better solution for my use case?

Thanks!

The unload drops all docs in my collections

Hi,

Is it possible to drop only the documents that are declared in the fixtures and keep the documents that are already present in the database (created by other means)

Thanks :-)

ReferenceError: file is not defined

If I don't export an array in my collection.js file, then I must be confronted with an error, instead I get this error:

(.../node-mongodb-fixtures/lib/index.js:171:66) ReferenceError: file is not defined

I took a look at the source code and looks like you are using a variable named file that is not defined in the function, so maybe use the collection name instead, or pass the file to it!

here is the part of code in lib/index.js:

function bulkImportDocuments(db, mute) {
  return collectionName => docs => {
    if (!Array.isArray(docs)) {
      // prettier-ignore
      throw new Error( '[error] no docs returned from file: "' + file + // <<------- HERE
          '". verify that a docs array was exported. note: if using .js files, use "module.exports", instead of "exports".'
      );
    }
...

Fixtures relationship

I just wonder how to relate two fixtures? lets say i have two schemas (users and comments). A comment document stores user_id as a referecen to user document. how this can be acheived through this lib?

I need a help to understand how node-mongodb-fixtures should work?!

Hi, first of all, I'm not familiar with node-mongodb-fixtures, that's why my question can be strange, but I try to add some tests to my REST API. I'm using jest and my API is based on node/express/MongoDB, also I'm using fakingoose to generate an entity from the db schema.
So, the node-mongodb-fixtures take a mock entity and insert it to the db collection and drop it after all tests are done, am I right understand its workflow?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    πŸ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. πŸ“ŠπŸ“ˆπŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❀️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.