Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SequelizeMock as a drop-in replacement #70

Open
fantactuka opened this issue Mar 18, 2019 · 0 comments
Open

SequelizeMock as a drop-in replacement #70

fantactuka opened this issue Mar 18, 2019 · 0 comments

Comments

@fantactuka
Copy link

fantactuka commented Mar 18, 2019

I'm trying to use sequelize-mock as a drop-in replacement of regular sequelize. Following snippet is from models/index.js that is using either sequelize-mock or sequelize based on environment

const Sequelize = isTest ? require('sequelize-mock') : require('sequelize');
const sequelize = new Sequelize(dbConfig);

loadFiles(modelsDir).forEach(file => {
  const model = sequelize.import(file);
  db[model.name] = model;
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

All works great except that Sequelize.define from sequelize-mock behaves different. It using its second argument as columns default values, while the one from sequelize using second argument as columns configuration. So given model's code is

Sequelize.define('Class', {
 name: {
   type: Sequelize.STRING,
   field: 'class_name'
 },
 ...
});

then sequelize-mock will be using { type: Sequelize.STRING, field: 'class_name' } as a default value for name column.

Question: Is it something that never supposed to work this way or am I missing something?

P.S. Since apart from that it works for me I've overridden define method to reset _defaults field that there're no default values for columns:

const { define } = SequelizeMock.prototype;
SequelizeMock.prototype.define = (...args) => {
  const model = define(...args);
  model._defaults = {}; // or going through model attributes and using `defaultValue` if it exists
  return model;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant