Skip to content

Commit

Permalink
Refactor lib/init to be function returning function
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Sep 21, 2016
1 parent 9df779b commit 0621d9d
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
1 change: 1 addition & 0 deletions changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -185,5 +185,6 @@ Now working on all Sequelize dialects.
* Support only Node v4 upwards
* Support Sequelize v4.x.x
* Refactor `lib/errors` to be function not object
* Refactor `lib/init` to be function returning function
* Refactor `lib/beforeFindHook` to be function returning function
* Drop testing on Travis CI for `mariadb` + `mssql` dialects
2 changes: 1 addition & 1 deletion lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module.exports = function(Sequelize) {
Sequelize.addHook('afterInit', afterInitHook);

// create initVirtualFields method
Sequelize.prototype.initVirtualFields = init;
Sequelize.prototype.initVirtualFields = init(Sequelize);

// replace Instance#get method
var instancePrototype = patches.instancePrototype;
Expand Down
49 changes: 25 additions & 24 deletions lib/init.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@ var shared = require('./shared'),
patchesFn = require('./patches');

// exports
module.exports = function(options) {
var sequelize = this,
Sequelize = sequelize.Sequelize,
patches = patchesFn(Sequelize);

// record options in sequelize
options = options || {};
options.active = true;
sequelize.options.virtualFields = options;

// parse virtual fields for all models
var models = sequelize.models;
_.forIn(models, function(model) {
_.forEach(model.attributes, function(field, fieldName) {
if (field.type instanceof Sequelize.VIRTUAL) parseField(field, fieldName, model);
module.exports = function(Sequelize) {
var patches = patchesFn(Sequelize);

// return `Sequelize.prototype.initVirtualFields()` method
return function(options) {
// record options in sequelize
options = options || {};
options.active = true;
this.options.virtualFields = options;

// parse virtual fields for all models
var models = this.models;
_.forIn(models, function(model) {
_.forEach(model.attributes, function(field, fieldName) {
if (field.type instanceof Sequelize.VIRTUAL) parseField(field, fieldName, model);
});
});
});

// inherit attributes, include and order where virtual fields refer to other virtual fields
inheritInclude();
inheritOrder();
// inherit attributes, include and order where virtual fields refer to other virtual fields
inheritInclude(models);
inheritOrder(models);

// return sequelize (for chaining)
return sequelize;
// return sequelize (for chaining)
return this;
};

// ----------
// functions
Expand Down Expand Up @@ -135,7 +136,7 @@ module.exports = function(options) {
}

if (_.isString(item.model)) {
var model = sequelize.models[item.model];
var model = parent.sequelize.models[item.model];
if (!model) throw new Sequelize.VirtualFieldsError(clauseType + " of virtual field '" + modelName + "'.'" + fieldName + "' points to unknown model '" + item.model + "'");
item.model = model;
} else if (!patches.isModel(item.model)) {
Expand All @@ -150,7 +151,7 @@ module.exports = function(options) {
return item;
}

function inheritInclude() {
function inheritInclude(models) {
// find virtual fields that refer to other virtual fields
var dependencies = [];
_.forIn(models, function(model, modelName) {
Expand Down Expand Up @@ -205,7 +206,7 @@ module.exports = function(options) {
});
}

function inheritOrder() {
function inheritOrder(models) {
// extend order clauses where refer to virtual fields
var dependencies = [];
_.forIn(models, function(model, modelName) {
Expand Down

0 comments on commit 0621d9d

Please sign in to comment.