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

Commit fdba0dd4 by Mick Hansen

only inject selected attributes into the model instance

1 parent b2cbb01f
...@@ -1438,16 +1438,8 @@ module.exports = (function() { ...@@ -1438,16 +1438,8 @@ module.exports = (function() {
tableNames[include.daoFactory.tableName] = true tableNames[include.daoFactory.tableName] = true
if (include.hasOwnProperty('attributes')) { if (include.hasOwnProperty('attributes')) {
var primaryKeys; include.originalAttributes = include.attributes;
if (include.daoFactory.hasPrimaryKeys) { include.attributes = include.attributes.concat(include.daoFactory.primaryKeyAttributes)
primaryKeys = []
for (var field in include.daoFactory.primaryKeys) {
primaryKeys.push(field)
}
} else {
primaryKeys = ['id']
}
include.attributes = include.attributes.concat(primaryKeys)
} else { } else {
include.attributes = Object.keys(include.daoFactory.attributes) include.attributes = Object.keys(include.daoFactory.attributes)
} }
......
...@@ -109,6 +109,9 @@ module.exports = (function() { ...@@ -109,6 +109,9 @@ module.exports = (function() {
DAO.prototype.set = function (key, value, options) { DAO.prototype.set = function (key, value, options) {
var values var values
, originalValue , originalValue
, keys
, i
, length
if (typeof key === "object") { if (typeof key === "object") {
values = key values = key
...@@ -121,7 +124,7 @@ module.exports = (function() { ...@@ -121,7 +124,7 @@ module.exports = (function() {
} }
// If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object // If raw, and we're not dealing with includes or special attributes, just set it straight on the dataValues object
if (options.raw && !(this.options && this.options.include) && !this.Model._hasBooleanAttributes && !this.Model._hasDateAttributes) { if (options.raw && !(this.options && this.options.include) && !(this.options && this.options.attributes) && !this.Model._hasBooleanAttributes && !this.Model._hasDateAttributes) {
if (Object.keys(this.dataValues).length) { if (Object.keys(this.dataValues).length) {
this.dataValues = _.extend(this.dataValues, values) this.dataValues = _.extend(this.dataValues, values)
} else { } else {
...@@ -131,8 +134,23 @@ module.exports = (function() { ...@@ -131,8 +134,23 @@ module.exports = (function() {
this._previousDataValues = _.clone(this.dataValues) this._previousDataValues = _.clone(this.dataValues)
} else { } else {
// Loop and call set // Loop and call set
for (key in values) {
this.set(key, values[key], options) if (this.options.attributes) {
keys = this.options.attributes
if (this.options && this.options.includeNames) {
keys = keys.concat(this.options.includeNames)
}
for (i = 0, length = keys.length; i < length; i++) {
if (values[keys[i]] !== undefined) {
this.set(keys[i], values[keys[i]], options)
}
}
} else {
for (key in values) {
this.set(key, values[key], options)
}
} }
if (options.raw) { if (options.raw) {
...@@ -231,7 +249,8 @@ module.exports = (function() { ...@@ -231,7 +249,8 @@ module.exports = (function() {
includeNames: include.includeNames, includeNames: include.includeNames,
includeMap: include.includeMap, includeMap: include.includeMap,
includeValidated: true, includeValidated: true,
raw: options.raw raw: options.raw,
attributes: include.originalAttributes
} }
} }
...@@ -621,16 +640,18 @@ module.exports = (function() { ...@@ -621,16 +640,18 @@ module.exports = (function() {
// private // private
var initValues = function(values, options) { var initValues = function(values, options) {
var defaults = {}, var defaults,
key; key;
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
defaults[this.Model.primaryKeyAttribute] = null;
values = values && _.clone(values) || {} values = values && _.clone(values) || {}
if (options.isNewRecord) { if (options.isNewRecord) {
defaults = {}
// set id to null if not passed as value, a newly created dao has no id
// removing this breaks bulkCreate
defaults[this.Model.primaryKeyAttribute] = null;
if (this.Model._hasDefaultValues) { if (this.Model._hasDefaultValues) {
Utils._.each(this.Model._defaultValues, function(valueFn, key) { Utils._.each(this.Model._defaultValues, function(valueFn, key) {
if (!defaults.hasOwnProperty(key)) { if (!defaults.hasOwnProperty(key)) {
...@@ -653,11 +674,12 @@ module.exports = (function() { ...@@ -653,11 +674,12 @@ module.exports = (function() {
this.dataValues[this.Model._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.deletedAt]); this.dataValues[this.Model._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.Model._timestampAttributes.deletedAt]);
delete defaults[this.Model._timestampAttributes.deletedAt]; delete defaults[this.Model._timestampAttributes.deletedAt];
} }
}
if (Object.keys(defaults).length) { if (Object.keys(defaults).length) {
for (key in defaults) { for (key in defaults) {
if (!values.hasOwnProperty(key)) { if (!values.hasOwnProperty(key)) {
values[key] = Utils.toDefaultValue(defaults[key]) values[key] = Utils.toDefaultValue(defaults[key])
}
} }
} }
} }
......
...@@ -245,6 +245,7 @@ module.exports = (function() { ...@@ -245,6 +245,7 @@ module.exports = (function() {
includeNames: this.options.includeNames, includeNames: this.options.includeNames,
includeMap: this.options.includeMap, includeMap: this.options.includeMap,
includeValidated: true, includeValidated: true,
attributes: this.options.attributes,
raw: true raw: true
}) })
} else if (this.options.hasJoinTableModel === true) { } else if (this.options.hasJoinTableModel === true) {
...@@ -268,7 +269,8 @@ module.exports = (function() { ...@@ -268,7 +269,8 @@ module.exports = (function() {
result = this.callee.bulkBuild(results, { result = this.callee.bulkBuild(results, {
isNewRecord: false, isNewRecord: false,
isDirty: false, isDirty: false,
raw: true raw: true,
attributes: this.options.attributes
}) })
} }
......
...@@ -717,7 +717,8 @@ module.exports = (function() { ...@@ -717,7 +717,8 @@ module.exports = (function() {
includeNames: options.includeNames, includeNames: options.includeNames,
includeMap: options.includeMap, includeMap: options.includeMap,
hasSingleAssociation: options.hasSingleAssociation, hasSingleAssociation: options.hasSingleAssociation,
hasMultiAssociation: options.hasMultiAssociation hasMultiAssociation: options.hasMultiAssociation,
attributes: options.attributes
}) })
return queryAndEmit.call(this, [sql, factory, queryOptions], 'select') return queryAndEmit.call(this, [sql, factory, queryOptions], 'select')
......
...@@ -7,6 +7,7 @@ var chai = require('chai') ...@@ -7,6 +7,7 @@ var chai = require('chai')
, DataTypes = require(__dirname + "/../lib/data-types") , DataTypes = require(__dirname + "/../lib/data-types")
, datetime = require('chai-datetime') , datetime = require('chai-datetime')
, async = require('async') , async = require('async')
, _ = require('lodash')
chai.use(datetime) chai.use(datetime)
chai.Assertion.includeStack = true chai.Assertion.includeStack = true
...@@ -516,6 +517,9 @@ describe(Support.getTestDialectTeaser("Include"), function () { ...@@ -516,6 +517,9 @@ describe(Support.getTestDialectTeaser("Include"), function () {
expect(tasks[0].title).to.equal('FooBar') expect(tasks[0].title).to.equal('FooBar')
expect(tasks[0].project.title).to.equal('BarFoo'); expect(tasks[0].project.title).to.equal('BarFoo');
expect(_.omit(tasks[0].get(), 'project')).to.deep.equal({ title: 'FooBar' })
expect(tasks[0].project.get()).to.deep.equal({ title: 'BarFoo'})
done() done()
}) })
}) })
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!