mongo-seeding
The ultimate Node.js library for populating your MongoDB database.
Last updated 4 months ago by pkosiec .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install mongo-seeding 
SYNC missed versions from official npm registry.

Mongo Seeding

Mongo Seeding

npm version Build Status Codacy Badge Codacy Badge David David install size

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

Table of contents

Installation

To install the app, run the following command:

npm install mongo-seeding --save

Usage

  1. Import the Seeder class:

    const { Seeder } = require('mongo-seeding');
    
  2. 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 config = {
      database: {
        host: '127.0.0.1',
        port: 27017,
        name: 'mydatabase',
      },
      dropDatabase: true,
    };
    

    Instead of database configuration object, you can also provide database connection URI to the database property:

    const config = {
      database: 'mongodb://127.0.0.1:27017/mydatabase',
      dropDatabase: true,
    };
    
  3. Instantiate Seeder class:

    const seeder = new Seeder(config);
    
  4. (OPTIONAL) To read MongoDB collections from disk, firstly follow the tutorial in order to define documents and collections to import. Next, read them using readCollectionsFromPath method:

    const path = require('path');
    const collections = seeder.readCollectionsFromPath(path.resolve("./your/path"));
    
  5. Seed your database:

    • with async/await, for example:

      try {
        await seeder.import(collections);
      } catch (err) {
        // Handle errors
      }
      // Do whatever you want after successful import
      
    • with raw promises:

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

See an import data example for a sample Node.js application utilizing the library.

API description

The Seeder class contains the following methods.

constructor(partialConfig?)

Creates a new Seeder object and loads configuration for data import.

Configuration

You can override any default configuration property by passing partial config object to the Seeder constructor. The object is merged with the default configuration object. To use all default settings, simply omit the constructor argument (new Seeder()).

The following snippet represents the type definition of Seeder config with all available properties:

export interface SeederConfig {
  database: string | SeederDatabaseConfigObject; // database connection URI or configuration object
  databaseReconnectTimeout: number; // maximum time of waiting for successful MongoDB connection in milliseconds
  dropDatabase: boolean; // drops entire database before import
  dropCollections: boolean; // drops every collection which is being imported
}

export interface SeederDatabaseConfigObject {
  protocol: string;
  host: string;
  port: number;
  name: string;
  username?: string;
  password?: string;
}

In order to configure database connection, specify connection URI for database property or assign a partial SeederDatabaseConfigObject object, overriding necessary properties.

Default configuration:

The default configuration object is as follows:

const defaultConfig = {
  database: {
    protocol: 'mongodb',
    host: '127.0.0.1',
    port: 27017,
    name: 'database',
    username: undefined,
    password: undefined, 
  },
  databaseReconnectTimeout: 10000,
  dropDatabase: false,
  dropCollections: false,
};

readCollectionsFromPath(path, partialOptions?)

This method populates collections and their documents from given path. The path has to contain import data structure described here.

Options

You can specify an optional partial options object for this method, which will be merged with default configuration object. One of properties of the configuration object is array of transform functions:

const collectionReadingOptions = {
  extensions: ['ts', 'js', 'json'],
  transformers: [
    Seeder.Transformers.replaceDocumentIdWithUnderscoreId,
  ]
}

const collections = seeder.readCollectionsFromPath(
  path.resolve('./your/path'),
  collectionReadingOptions
);

Transform function is a simple function in a form of (collection: SeederCollection) => SeederCollection. It means that you can manipulate collections after reading them from disk. SeederCollection is defined as follows:

interface SeederCollection {
  name: string;
  documents: object[];
}

There is one built-in transform function: Seeder.Transformers.replaceDocumentIdWithUnderscoreId, which replaces id field with _id property for every document in collection.

Default options

The default options object is as follows:

const defaultCollectionReadingConfig: SeederCollectionReadingConfig = {
  extensions: ['json', 'js'],
  transformers: [],
};

import(collections, partialConfig?)

This method connects to a database and imports all provided collections. collections argument type is an array of SeederCollection type, which is defined as follows:

interface SeederCollection {
  name: string;
  documents: object[];
}

Configuration

You can provide additional partialConfig argument in a form of Seeder partial configuration object - the same used in the constructor. It is an easy way to change the configuration for one single data import. The configuration object will be merged with provided configuration from constructor and default config.

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 { Seeder } = require('mongo-seeding');

Current Tags

  • 3.1.0                                ...           latest (4 months ago)

25 Versions

  • 3.1.0                                ...           4 months ago
  • 3.0.0                                ...           6 months ago
  • 3.0.0-2                                ...           7 months ago
  • 3.0.0-1                                ...           7 months ago
  • 3.0.0-0                                ...           7 months ago
  • 2.2.0                                ...           9 months ago
  • 2.1.0                                ...           10 months ago
  • 2.0.5                                ...           10 months ago
  • 2.0.4                                ...           a year ago
  • 2.0.3                                ...           a year ago
  • 2.0.2                                ...           a year ago
  • 2.0.1                                ...           a year ago
  • 2.0.0                                ...           a year ago
  • 1.1.0                                ...           a year ago
  • 1.0.2                                ...           a year ago
  • 1.0.1                                ...           a year ago
  • 1.0.0                                ...           a year ago
  • 1.0.0-1                                ...           a year ago
  • 1.0.0-0                                ...           a year ago
  • 0.1.4                                ...           a year ago
  • 0.1.3                                ...           a year ago
  • 0.1.2                                ...           a year ago
  • 0.1.1                                ...           a year ago
  • 0.1.0                                ...           a year ago
  • 0.1.0-1                                ...           a year ago
Maintainers (1)
Downloads
Today 1
This Week 1
This Month 27
Last Day 0
Last Week 0
Last Month 43
Dependencies (3)
Dev Dependencies (10)

Copyright 2014 - 2016 © taobao.org |