Home Reference Source Test
public class | source

Seeder

Base class for seeders to extend.

Seeder is an Abstract base class in order to use Seeder you need to extend Seeder into your own class and implement async run() method

Example:

import { Seeder } from 'mongoose-data-seed';
import { User } from '../server/models';

const data = [
  {
    email: 'user1@gmail.com',
    password: '123123',
    passwordConfirmation: '123123',
    isAdmin: true,
  },
  {
    email: 'user2@gmail.com',
    password: '123123',
    passwordConfirmation: '123123',
    isAdmin: false,
  },
];

class UsersSeeder extends Seeder {
  async shouldRun() {
    const count = await User.countDocuments().exec();

    return count === 0;
  }

  async run() {
    return User.create(data);
  }
}

export default UsersSeeder;

Static Method Summary

Static Public Methods
public static

extend(userSeederMethods: Object): Seeder

Creates a new seeder by extending the base seeder.

Constructor Summary

Public Constructor
public abstract

Abstract class can not be constructed.

Method Summary

Public Methods
public abstract

async beforeRun(): Promise

To perform before run.

public

getStats(results: Array): Object

Get stats from seed results.

public abstract

async run()

Run the seeder.

public

async seed(): Promise

Seed the data.

public abstract

Should run

Static Public Methods

public static extend(userSeederMethods: Object): Seeder source

Creates a new seeder by extending the base seeder. Useful when not using old javascript

Params:

NameTypeAttributeDescription
userSeederMethods Object
  • optional
  • default: {}

Object with the seeders method (e.g. run, shouldRun, beforeRun ...)

Return:

Seeder

Example:

Seeder.extends({
  shouldRun: function() {
    return User.countDocuments()
      .exec()
      .then(function(count) {
        return count === 0;
      });
  },
  run: function() {
    return User.create(data);
  }
});

Public Constructors

public abstract constructor() source

Abstract class can not be constructed. Seeder class should be extended.

Throw:

TypeError

when creating an instance of the abstract class.

TypeError

when the run method is not implemented.

Public Methods

public abstract async beforeRun(): Promise source

To perform before run.

Return:

Promise

public getStats(results: Array): Object source

Get stats from seed results.

Params:

NameTypeAttributeDescription
results Array
  • optional

Seed results.

Return:

Object

Return Properties:

NameTypeAttributeDescription
run boolean

Did the seeder run?

created number

Amount of records created by the seeder.

public abstract async run() source

Run the seeder.

public async seed(): Promise source

Seed the data.

Return:

Promise

Stats about the save.

public abstract async shouldRun(): Promise<boolean> source

Should run

Return:

Promise<boolean>

Indication if should run