seanemmer / mongoose-seed

Seed data population for Mongoose
MIT License
52 stars 33 forks source link

Model Args #40

Open vkiranmaniya opened 3 years ago

vkiranmaniya commented 3 years ago

Added possibility to pass the model function arguments in the case of the model function accepts an argument. Conder the usecase.

Mongoose model definition in feathers.js and feathers-mongoose

module.exports = function(app) {
  const modelName = 'category';
  const mongooseClient = app.get('mongooseClient');
  const schema = new mongooseClient.Schema({
    name: String,
  }, {
    timestamps: true,
  });
  if (mongooseClient.modelNames().includes(modelName)) {
    mongooseClient.deleteModel(modelName);
  }
  return mongooseClient.model(modelName, schema);
};

The plugin code, which will actually initiate the seeder

const path = require('path');
const seeder = require('mongoose-seed');
const seedable = require('./seeder/seed');
const logger = require('./logger');

module.exports = function(app) {
    seeder.connect(app.get('mongodb'), function() {

        // requires the app argument to load the model
        seeder.loadModels([
          path.join(__dirname, 'models/category.model.js')
        ], app);

        seeder.clearModels(['category'], function() {
          seeder.populateModels(seedable, function() {
            seeder.disconnect();
          });
        });
    });
  }
};