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

Commit cfdd4f3f by Mick Hansen

add new bulkBuild method, provides performance benefits by not building option o…

…bjects multiple times, in time perhaps more benefits
1 parent 861fd2d8
...@@ -666,6 +666,9 @@ module.exports = (function() { ...@@ -666,6 +666,9 @@ module.exports = (function() {
} }
DAOFactory.prototype.build = function(values, options) { DAOFactory.prototype.build = function(values, options) {
if (Array.isArray(values)) {
return this.bulkBuild(values, options)
}
options = options || { isNewRecord: true, isDirty: true } options = options || { isNewRecord: true, isDirty: true }
if (options.hasOwnProperty('include') && options.include && !options.includeValidated) { if (options.hasOwnProperty('include') && options.include && !options.includeValidated) {
...@@ -675,6 +678,12 @@ module.exports = (function() { ...@@ -675,6 +678,12 @@ module.exports = (function() {
return new this.DAO(values, options) return new this.DAO(values, options)
} }
DAOFactory.prototype.bulkBuild = function(valueSets, options) {
return valueSets.map(function (values) {
return this.build(values, options)
}.bind(this))
}
DAOFactory.prototype.create = function(values, fieldsOrOptions) { DAOFactory.prototype.create = function(values, fieldsOrOptions) {
Utils.validateParameter(values, Object, { optional: true }) Utils.validateParameter(values, Object, { optional: true })
Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true, index: 2, method: 'DAOFactory#create' }) Utils.validateParameter(fieldsOrOptions, Object, { deprecated: Array, optional: true, index: 2, method: 'DAOFactory#create' })
......
...@@ -224,12 +224,7 @@ module.exports = (function() { ...@@ -224,12 +224,7 @@ module.exports = (function() {
, self = this , self = this
var accessor = Utils._.camelize(key) var accessor = Utils._.camelize(key)
var childOptions = {
// downcase the first char
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1)
value.forEach(function(data) {
var daoInstance = include.daoFactory.build(data, {
isNewRecord: false, isNewRecord: false,
isDirty: false, isDirty: false,
include: include.include, include: include.include,
...@@ -237,7 +232,13 @@ module.exports = (function() { ...@@ -237,7 +232,13 @@ module.exports = (function() {
includeMap: include.includeMap, includeMap: include.includeMap,
includeValidated: true, includeValidated: true,
raw: options.raw raw: options.raw
}) }
// downcase the first char
accessor = accessor.slice(0,1).toLowerCase() + accessor.slice(1)
value.forEach(function(data) {
var daoInstance = include.daoFactory.build(data, childOptions)
, isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers) , isEmpty = !Utils.firstValueOfHash(daoInstance.identifiers)
if (association.isSingleAssociation) { if (association.isSingleAssociation) {
......
...@@ -238,8 +238,7 @@ module.exports = (function() { ...@@ -238,8 +238,7 @@ module.exports = (function() {
checkExisting: this.options.hasMultiAssociation checkExisting: this.options.hasMultiAssociation
}) })
result = results.map(function(result) { result = this.callee.bulkBuild(results, {
return this.callee.build(result, {
isNewRecord: false, isNewRecord: false,
isDirty: false, isDirty: false,
include:this.options.include, include:this.options.include,
...@@ -248,7 +247,6 @@ module.exports = (function() { ...@@ -248,7 +247,6 @@ module.exports = (function() {
includeValidated: true, includeValidated: true,
raw: true raw: true
}) })
}.bind(this))
} else if (this.options.hasJoinTableModel === true) { } else if (this.options.hasJoinTableModel === true) {
result = results.map(function(result) { result = results.map(function(result) {
result = Dot.transform(result) result = Dot.transform(result)
...@@ -267,9 +265,11 @@ module.exports = (function() { ...@@ -267,9 +265,11 @@ module.exports = (function() {
// Regular queries // Regular queries
} else { } else {
result = results.map(function(result) { result = this.callee.bulkBuild(results, {
return this.callee.build(result, { isNewRecord: false, isDirty: false, raw: true }) isNewRecord: false,
}.bind(this)) isDirty: false,
raw: true
})
} }
// return the first real model instance if options.plain is set (e.g. Model.find) // return the first real model instance if options.plain is set (e.g. Model.find)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!