不要怂,就是干,撸起袖子干!

Commit 9089c3c9 by Jozef Hartinger Committed by Jan Aagaard Meier

#7404 make it possible to pass parameters to getter functions (#7435)

https://github.com/sequelize/sequelize/issues/7404
1 parent 2d45cbe5
...@@ -60,6 +60,7 @@ ...@@ -60,6 +60,7 @@
- [FIXED] Deleted paranoid records can be queried in the same second. [#7204](https://github.com/sequelize/sequelize/issues/7204)/[#7332](https://github.com/sequelize/sequelize/pull/7332) - [FIXED] Deleted paranoid records can be queried in the same second. [#7204](https://github.com/sequelize/sequelize/issues/7204)/[#7332](https://github.com/sequelize/sequelize/pull/7332)
- [FIXED] `removeAttribute('id')` results in `undefined: null` data value [#7318](https://github.com/sequelize/sequelize/issues/7318) - [FIXED] `removeAttribute('id')` results in `undefined: null` data value [#7318](https://github.com/sequelize/sequelize/issues/7318)
- [FIXED] `bulkCreate` now runs in O(N) time instead of O(N^2) time. [#4247](https://github.com/sequelize/sequelize/issues/4247) - [FIXED] `bulkCreate` now runs in O(N) time instead of O(N^2) time. [#4247](https://github.com/sequelize/sequelize/issues/4247)
- [FIXED] Passing parameters to model getters [#7404](https://github.com/sequelize/sequelize/issues/7404)
## BC breaks: ## BC breaks:
- `DATEONLY` now returns string in `YYYY-MM-DD` format rather than `Date` type - `DATEONLY` now returns string in `YYYY-MM-DD` format rather than `Date` type
......
...@@ -3043,13 +3043,13 @@ class Model { ...@@ -3043,13 +3043,13 @@ class Model {
if (key) { if (key) {
if (this._customGetters[key] && !options.raw) { if (this._customGetters[key] && !options.raw) {
return this._customGetters[key].call(this, key); return this._customGetters[key].call(this, key, options);
} }
if (options.plain && this._options.include && this._options.includeNames.indexOf(key) !== -1) { if (options.plain && this._options.include && this._options.includeNames.indexOf(key) !== -1) {
if (Array.isArray(this.dataValues[key])) { if (Array.isArray(this.dataValues[key])) {
return this.dataValues[key].map(instance => instance.get({plain: options.plain})); return this.dataValues[key].map(instance => instance.get(options));
} else if (this.dataValues[key] instanceof Model) { } else if (this.dataValues[key] instanceof Model) {
return this.dataValues[key].get({plain: options.plain}); return this.dataValues[key].get(options);
} else { } else {
return this.dataValues[key]; return this.dataValues[key];
} }
......
...@@ -372,6 +372,67 @@ describe(Support.getTestDialectTeaser('DAO'), function() { ...@@ -372,6 +372,67 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
expect(product.get({clone: true}).title).to.be.ok; expect(product.get({clone: true}).title).to.be.ok;
}); });
}); });
it('can pass parameters to getters', function () {
var Product = this.sequelize.define('product', {
title: Sequelize.STRING
}, {
getterMethods: {
rating: function (key, options) {
if (options.apiVersion > 1) {
return 100;
}
return 5;
}
}
});
var User = this.sequelize.define('user', {
first_name: Sequelize.STRING,
last_name: Sequelize.STRING
}, {
getterMethods: {
height: function (key, options) {
if (options.apiVersion > 1) {
return 185; // cm
}
return 6.06; // ft
}
}
});
Product.belongsTo(User);
var product = Product.build({}, {
include: [
User
]
});
product.set({
id: 1,
title: 'Chair',
user: {
id: 1,
first_name: 'Jozef',
last_name: 'Hartinger'
}
});
expect(product.get('rating')).to.equal(5);
expect(product.get('rating', {apiVersion: 2})).to.equal(100);
expect(product.get({plain: true})).to.have.property('rating', 5);
expect(product.get({plain: true}).user).to.have.property('height', 6.06);
expect(product.get({plain: true, apiVersion: 1})).to.have.property('rating', 5);
expect(product.get({plain: true, apiVersion: 1}).user).to.have.property('height', 6.06);
expect(product.get({plain: true, apiVersion: 2})).to.have.property('rating', 100);
expect(product.get({plain: true, apiVersion: 2}).user).to.have.property('height', 185);
expect(product.get('user').get('height', {apiVersion: 2})).to.equal(185);
});
}); });
describe('changed', function() { describe('changed', function() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!