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

Commit b7c8e85a by Mick Hansen

feat(model/attributes): return type and dependency fields for VIRTUAL

1 parent ad7f882b
...@@ -658,13 +658,29 @@ UUIDV4.prototype.validate = function(value) { ...@@ -658,13 +658,29 @@ UUIDV4.prototype.validate = function(value) {
* } * }
* }) * })
* ``` * ```
*
* VIRTUAL also takes a return type and dependency fields as arguments
* If a virtual attribute is present in `attributes` it will automatically pull in the extra fields aswell.
* Return type is mostly usefull for setups that rely on types like GraphQL.
* ```js
* {
* active: {
* type: new DataTypes.VIRTUAL(DataTypes.BOOLEAN, ['createdAt']),
* get: function() {
* return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000)
* }
* }
* }
*
* In the above code the password is stored plainly in the password field so it can be validated, but is never stored in the DB. * In the above code the password is stored plainly in the password field so it can be validated, but is never stored in the DB.
* @property VIRTUAL * @property VIRTUAL
* @alias NONE * @alias NONE
*/ */
var VIRTUAL = function() { var VIRTUAL = function(returnType, fields) {
if (!(this instanceof VIRTUAL)) return new VIRTUAL(); if (!(this instanceof VIRTUAL)) return new VIRTUAL(returnType, fields);
ABSTRACT.apply(this, arguments);
this.returnType = returnType;
this.fields = fields;
}; };
util.inherits(VIRTUAL, ABSTRACT); util.inherits(VIRTUAL, ABSTRACT);
......
...@@ -1326,7 +1326,16 @@ Model.prototype.findAll = function(options) { ...@@ -1326,7 +1326,16 @@ Model.prototype.findAll = function(options) {
if (options.attributes === undefined) { if (options.attributes === undefined) {
options.attributes = Object.keys(this.tableAttributes); options.attributes = Object.keys(this.tableAttributes);
} else if (this._hasVirtualAttributes) {
options.attributes.forEach(function (attribute) {
if (this._isVirtualAttribute(attribute) && this.rawAttributes[attribute].type.fields) {
options.attributes = options.attributes.concat(this.rawAttributes[attribute].type.fields);
}
}.bind(this));
options.attributes = _.without.apply(this, [options.attributes].concat(this._virtualAttributes));
options.attributes = _.unique(options.attributes);
} }
Utils.mapOptionFieldNames(options, this); Utils.mapOptionFieldNames(options, this);
options = paranoidClause(this, options); options = paranoidClause(this, options);
......
...@@ -1185,6 +1185,30 @@ describe(Support.getTestDialectTeaser('Model'), function() { ...@@ -1185,6 +1185,30 @@ describe(Support.getTestDialectTeaser('Model'), function() {
}); });
}); });
}); });
it('should pull in dependent fields for a VIRTUAL', function () {
var User = this.sequelize.define('User', {
active: {
type: new Sequelize.VIRTUAL(Sequelize.BOOLEAN, ['createdAt']),
get: function() {
return this.get('createdAt') > Date.now() - (7 * 24 * 60 * 60 * 1000);
}
}
}, {
timestamps: true
});
return User.create().then(function () {
return User.findAll({
attributes: ['active']
}).then(function (users) {
users.forEach(function (user) {
expect(user.get('createdAt')).to.be.ok;
expect(user.get('active')).to.equal(true);
});
});
});
});
}); });
}); });
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!