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

Commit aa39f96e by Jozef Hartinger Committed by Sushant

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

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

https://github.com/sequelize/sequelize/issues/7404

* [ci-skip] Changelog
1 parent 85412bf0
# Future
- [FIXED] Passing parameters to model getters [#7404](https://github.com/sequelize/sequelize/issues/7404)
# 3.30.3
- [ADDED] Ability to run transactions on a read-replica by marking transactions as read only [#7323](https://github.com/sequelize/sequelize/issues/7323)
- [FIXED] Add quotes around column names for unique constraints in sqlite [#4407](https://github.com/sequelize/sequelize/issues/4407)
......
......@@ -182,17 +182,19 @@ Instance.prototype.get = function(key, options) { // testhint options:none
key = undefined;
}
options = options || {};
if (key) {
if (this._customGetters[key]) {
return this._customGetters[key].call(this, key);
return this._customGetters[key].call(this, key, options);
}
if (options && options.plain && this.$options.include && this.$options.includeNames.indexOf(key) !== -1) {
if (Array.isArray(this.dataValues[key])) {
return this.dataValues[key].map(function (instance) {
return instance.get({plain: options.plain});
return instance.get(options);
});
} else if (this.dataValues[key] instanceof Instance) {
return this.dataValues[key].get({plain: options.plain});
return this.dataValues[key].get(options);
} else {
return this.dataValues[key];
}
......
......@@ -372,6 +372,67 @@ describe(Support.getTestDialectTeaser('DAO'), function() {
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() {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!