JSPM

  • Created
  • Published
  • Downloads 16850
  • Score
    100M100P100Q140216F
  • License MIT

The ultimate Node.js library for populating your MongoDB database.

Package Exports

  • mongo-seeding

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongo-seeding) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

Mongo Seeding

Mongo Seeding

npm version Build Status Codacy Badge Codacy Badge David David

The ultimate solution for populating your MongoDB database. Define the data in JSON or JavaScript. Import collections and documents!

Installation

To install the app, run the following command:

npm install mongo-seeding --save

Usage

  1. Follow the tutorial to define documents and collections to import.

  2. Import the seedDatabase function:

    const { seedDatabase } = require('mongo-seeding');
  3. Define a partial configuration object. The object will be merged with the default config object (see Configuration section). Therefore, you can specify only properties, which should override the default values, for example:

    const path = require('path');
    
    const config = {
      database: {
        host: '127.0.0.1',
        port: 27017,
        name: 'mydatabase',
      },
      inputPath: path.resolve(__dirname, '../data'),
      dropDatabase: true,
    };
  4. Seed your database:

  • with async/await, i.e.:

    (async () => {
      try {
        await seedDatabase(config);
      } catch (err) {
        // Handle errors
      }
      // Do whatever you want after successful import
    })()    
  • with raw promises:

    seedDatabase(config).then(() => {
      // Do whatever you want after successful import
    }).catch(err => {
      // Handle errors
    });

Configuration

You can override any configuration property by passing partial config object to seedDatabase function. The object is merged with the default configuration object. You can pass {} to use all default settings.

In order to configure database connection, override needed properties of database object or specify databaseConnectionUri. The database object is ignored, if the databaseConnectionUri string is defined (e.g. mongodb://127.0.0.1:27017/testing).

Default configuration object:

const config = {
  database: {
    protocol: 'mongodb',
    host: '127.0.0.1',
    port: 27017,
    name: 'database',
    username: undefined,
    password: undefined,
  },
  databaseConnectionUri: undefined, // if defined, it will be used for DB connection instead of `database` object
  dropCollection: false, // drops every collection that is being imported
  inputPath: resolve(__dirname, '../../data'), // input directory with import data structure
  dropDatabase: false, // drops database before import
  replaceIdWithUnderscoreId: false, // rewrites `id` property to `_id` for every document; useful for ORMs
  supportedExtensions: ['json', 'js'], // file extensions that should be read
  reconnectTimeoutInSeconds: 10, // maximum time of waiting for successful connection with MongoDB
};

Debug output

In order to see debug output, set environmental variable DEBUG to value mongo-seeding before starting your Node.js app:

DEBUG=mongo-seeding node yourapp/index.js

You can also set it programmatically before requiring mongo-seeding:

process.env.DEBUG = 'mongo-seeding';
const { seedDatabase } = require('mongo-seeding');