seanemmer / mongoose-seed

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

Adding a test or example file would be really helpful #3

Closed nmcclay closed 8 years ago

nmcclay commented 8 years ago

I tried for about an hour to figure out how to get mongoose-seed to see my shema but for whatever reason it is refusing to work:

var mongoose = require('mongoose');
var Schema = mongoose.Schema;

module.exports = new Schema({
  front: 'string',
  back: 'string',
  category: { type: Schema.Types.ObjectId, ref: 'category' },
  order: 'number',
  flipped: 'boolean'
});

I'm assuming this is a different approach to setting up your mongoose model than you are using in your example, but I can't really tell because of the lack of samples or tests :(

seanemmer commented 8 years ago

Great idea - I'll put a more complete example together + some Chai tests when I have a minute. In the interim, can you show me the specific code you're using to invoke mongoose-seed?

Best, Sean

seanemmer commented 8 years ago

@nmcclay there's a pretty good example in the readme - was there anything else you were looking for?

alanhr commented 8 years ago

Hi guys, I had the same problem , but I added a little modification in the library and solved my problem. let me show you.

My schema export a function

module.exports = () => {

    var schema = Schema({
        name: {
            type: String,
            required: true
        }
    });

    return mongoose.model('ProfileType', schema);
};

in library I needed check if model was a function

Seeder.prototype.loadModels = function(modelPaths) {
    console.log(modelPaths);
    modelPaths.forEach(function(modelPath) {
        **var model = require(path.resolve(modelPath));
        if (typeof model === 'function') {
            model();
        }**
    });
};

What do you think about this solution ?

seanemmer commented 8 years ago

This looks great - submit a PR and we'll get it into the codebase!

seanemmer commented 8 years ago

Thanks to @alanhr for setting up some unit tests in pr #8 - closing issue