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 |
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 |
To perform before run. |
|
public |
Get stats from seed results. |
|
public abstract |
async run() Run the seeder. |
|
public |
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:
Name | Type | Attribute | Description |
userSeederMethods | Object |
|
Object with the seeders method (e.g. run, shouldRun, beforeRun ...) |
Example:
Seeder.extends({
shouldRun: function() {
return User.countDocuments()
.exec()
.then(function(count) {
return count === 0;
});
},
run: function() {
return User.create(data);
}
});