hash-bang / Gulp-Mongoose-Scenario

Gulp wrapper for mongoose-scenario
MIT License
1 stars 0 forks source link

Clear usage examples #1

Open adamski opened 9 years ago

adamski commented 9 years ago

I've been trying this out for our project but run into an issue.

I have a JSON file tests/scenarios/topics.json:

{
    "Topic": 
        [
        {
            "name": "Astronomy",
            "slug": "astronomy",
            "createdBy": "admin.user.ref"
        },
        {
            "name": "Maths",
            "slug": "maths",
            "createdBy": "admin.user.ref"
        }
    ]
}

In my gulpfile.js task I have

    var db = mongoose.connection;

    db.on('error', console.error.bind(console, 'connection error:'));
    db.on('open', function (next) {
        // Slurp in all the .json files located in models/scenarios and run them though mongoose-scenario  
        var scenario = require('gulp-mongoose-scenario');
        console.log('import mongoose model scenarios:');

        gulp.src('tests/scenarios/*.json')
        .pipe(scenario({connection: db, nuke: true}))
        .on('end', function(err) {
            console.log('finished importing');
            if (err) { console.log(err); return next(err) };
            next();
        });
    });

But the topics collection is not created. However, a similarly formatted JSON file for users does go in.

I tried with a root value of "topics" but get the error

Error: Collection "topics" not found in Mongoose schema. Did you forget to load it?

What do the scenario .json files look like?

hash-bang commented 9 years ago

@adamski Scenario requires all your models to be loaded before it can populate them. If the example you posted is the full contents of the gulp.task() definition then you need to include something along the lines of require('./models/tropics') beforehand so that Mongoose is aware of your model definition.

Better yet saving the below as ./models/index.js allows you to load all of your models at once using require('./models'):

var requireDir = require('require-dir');
requireDir('./');

Hope this helps.

adamski commented 9 years ago

Sorry, It was not the full gulp task I posted.

At the top of my gulp task I have:

    var modelsFolderPath    = __dirname + '/models/';
    //...
    fs.readdirSync(modelsFolderPath).forEach(function (modelFile) {
        require(modelsFolderPath+modelFile);
    });
hash-bang commented 9 years ago

If you've included the model files it should be fine then.

My guess would be that the naming convention used in your model file doesn't match the one you are giving to Scenario.

It looks like you're trying to populate the Topic collection in your Scenario schema. Does this match the name you are passing to mongoose.model(NAME, SCHEMA)?

Other than that, all I can think off the top of my head is that Mongoo is perhaps rewriting the model names. I know for example that all collections are usually automatically lower cased (i.e. try topics instead of Topics).