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

Commit 8e0fd546 by Jan Aagaard Meier

fix(instance.get): Add options.raw. Closes #5815

1 parent 73b76500
...@@ -4,6 +4,7 @@ ...@@ -4,6 +4,7 @@
- [CHANGED] Throw if `dialect` is not provided to the constructor - [CHANGED] Throw if `dialect` is not provided to the constructor
- [CHANGED] Throw `bluebird.AggregateError` instead of array from `bulkCreate` when validation fails - [CHANGED] Throw `bluebird.AggregateError` instead of array from `bulkCreate` when validation fails
- [FIXED] `$notIn: []` is now converted to `NOT IN (NULL)` [#4859](https://github.com/sequelize/sequelize/issues/4859) - [FIXED] `$notIn: []` is now converted to `NOT IN (NULL)` [#4859](https://github.com/sequelize/sequelize/issues/4859)
- [FIXED] Add `raw` support to `instance.get()` [#5815](https://github.com/sequelize/sequelize/issues/5815)
## BC breaks: ## BC breaks:
- `hookValidate` removed in favor of `validate` with `hooks: true | false`. `validate` returns a promise which is rejected if validation fails - `hookValidate` removed in favor of `validate` with `hooks: true | false`. `validate` returns a promise which is rejected if validation fails
......
...@@ -171,9 +171,10 @@ Instance.prototype.setDataValue = function(key, value) { ...@@ -171,9 +171,10 @@ Instance.prototype.setDataValue = function(key, value) {
* *
* If key is given and a field or virtual getter is present for the key it will call that getter - else it will return the value for key. * If key is given and a field or virtual getter is present for the key it will call that getter - else it will return the value for key.
* *
* @param {String} [key] * @param {String} [key]
* @param {Object} [options] * @param {Object} [options]
* @param {Boolean} [options.plain=false] If set to true, included instances will be returned as plain objects * @param {Boolean} [options.plain=false] If set to true, included instances will be returned as plain objects
* @param {Boolean} [options.raw=false] If set to true, field and virtual setters will be ignored
* @return {Object|any} * @return {Object|any}
*/ */
Instance.prototype.get = function(key, options) { // testhint options:none Instance.prototype.get = function(key, options) { // testhint options:none
...@@ -182,11 +183,13 @@ Instance.prototype.get = function(key, options) { // testhint options:none ...@@ -182,11 +183,13 @@ Instance.prototype.get = function(key, options) { // testhint options:none
key = undefined; key = undefined;
} }
options = options || {};
if (key) { if (key) {
if (this._customGetters[key]) { if (this._customGetters[key] && !options.raw) {
return this._customGetters[key].call(this, key); return this._customGetters[key].call(this, key);
} }
if (options && 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(function (instance) { return this.dataValues[key].map(function (instance) {
return instance.get({plain: options.plain}); return instance.get({plain: options.plain});
...@@ -200,7 +203,7 @@ Instance.prototype.get = function(key, options) { // testhint options:none ...@@ -200,7 +203,7 @@ Instance.prototype.get = function(key, options) { // testhint options:none
return this.dataValues[key]; return this.dataValues[key];
} }
if (this._hasCustomGetters || (options && options.plain && this.$options.include) || (options && options.clone)) { if (this._hasCustomGetters || (options.plain && this.$options.include) || options.clone) {
var values = {} var values = {}
, _key; , _key;
......
'use strict';
/* jshint -W030 */
var chai = require('chai')
, sinon = require('sinon')
, expect = chai.expect
, Support = require(__dirname + '/../support')
, DataTypes = require(__dirname + '/../../../lib/data-types')
, current = Support.sequelize;
describe(Support.getTestDialectTeaser('Instance'), function() {
describe('get', function () {
beforeEach(function () {
this.getSpy = sinon.spy();
this.User = current.define('User', {
name: {
type: DataTypes.STRING,
get: this.getSpy
}
});
});
it('invokes getter if raw: false', function () {
this.User.build().get('name');
expect(this.getSpy).to.have.been.called;
});
it('does not invoke getter if raw: true', function () {
expect(this.getSpy, { raw: true }).not.to.have.been.called;
});
});
});
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!