sghribi / loopback-fixtures

ISC License
18 stars 21 forks source link

model.destroyAll is not a function #10

Open albsierra opened 7 years ago

albsierra commented 7 years ago

I have an Email model whose base model isn't PersistedModel. Therefore, when I try to load fixtures it return an error "model.destroyAll is not a function".

I modified load-fixtures.js to filter models derived from PersistedModel and sending models instead of app.models. It works for me.

var app, argv, fixtureLoader, optimist, run, _;

require('coffee-script/register');

optimist = require('optimist');

app = require('../../../server/server');

fixtureLoader = require('./fixtures-loader');

_ = require('lodash');

run = function() {
  var append, fixturePath, models;
  append = argv.append;
  fixturePath = argv.fixturePath;
  models = [];

  _.each(app.models, function(model){
    if(typeof model.destroyAll == 'function') {
      models[model.modelName] = model;
    }
  });

  if (!append) {
    return fixtureLoader.purgeDatabase(models).then(function() {
      console.log('Data purged');
      return fixtureLoader.loadFixtures(models, fixturePath);
    }).then(function() {
      console.log('Data successfully loaded');
      return process.exit(0);
    })["catch"](function(err) {
      console.log('Error on fixtures loading:', err);
      return process.exit(1);
    });
  } else {
    return fixtureLoader.loadFixtures(models, fixturePath).then(function() {
      console.log('Data successfully loaded');
      return process.exit(0);
    })["catch"](function(err) {
      console.log('Error on fixtures loading:', err);
      return process.exit(1);
    });
  }
};
albsierra commented 7 years ago

Sorry.

It was easier to check if destroyAll method exist in model, in lib/fixtures-loader.js, than the previous solution.

  purgeDatabase: function(models) {
    var purgeModelPromises;
    purgeModelPromises = [];
    _.forEach(models, (function(_this) {
      return function(model) {
        if(model.destroyAll)
          return purgeModelPromises.push(_this.purgeModel(model));
      };
    })(this));
    return Promise.all(purgeModelPromises);
  },