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

Commit 1e1a10b6 by Overlook Motel

include all

1 parent cf0fc069
Showing with 122 additions and 17 deletions
......@@ -1348,8 +1348,32 @@ module.exports = (function() {
options.include = [options.include]
}
options.include = options.include.map(function(include) {
include = validateIncludedElement.call(this, include, tableNames)
// convert all included elements to { daoFactory: Model } form
var includes = options.include = options.include.map(function(include) {
if (include instanceof DAOFactory) {
return { daoFactory: include }
} else if (typeof include !== 'object') {
throw new Error('Include unexpected. Element has to be either an instance of DAOFactory or an object.')
} else if (include.hasOwnProperty('model')) {
include.daoFactory = include.model
delete include.model
}
return include
})
// validate all included elements
for (var index = 0; index < includes.length; index++) {
var include = includes[index]
if (include.all) {
includes.splice(index, 1)
index--
validateIncludedAllElement.call(this, includes, include)
continue
}
include = includes[index] = validateIncludedElement.call(this, include, tableNames)
options.includeMap[include.as] = include
options.includeNames.push(include.as)
......@@ -1360,24 +1384,11 @@ module.exports = (function() {
options.hasIncludeWhere = options.hasIncludeWhere || include.hasIncludeWhere || !!include.where
options.hasIncludeRequired = options.hasIncludeRequired || include.hasIncludeRequired || !!include.required
return include
}.bind(this))
}
}
var validateIncludedElement = function(include, tableNames) {
if (include instanceof DAOFactory) {
include = { daoFactory: include }
}
if (typeof include !== 'object') {
throw new Error('Include unexpected. Element has to be either an instance of DAOFactory or an object.')
}
if (include.hasOwnProperty('model')) {
include.daoFactory = include.model
delete include.model
} else if (!include.hasOwnProperty('daoFactory')) {
if (!include.hasOwnProperty('daoFactory')) {
throw new Error('Include malformed. Expected attributes: daoFactory, as!')
}
......@@ -1448,6 +1459,100 @@ module.exports = (function() {
}
}
var validateIncludedAllElement = function(includes, include) {
// check 'all' attribute provided is valid
var all = include.all
delete include.all
if (all !== true) {
if (!Array.isArray(all)) {
all = [all]
}
var validTypes = {
BelongsTo: true,
HasOne: true,
HasMany: true,
One: ['BelongsTo', 'HasOne'],
Has: ['HasOne', 'HasMany'],
Many: ['HasMany']
}
for (var i = 0; i < all.length; i++) {
var type = all[i]
if (type == 'All') {
all = true
break
}
var types = validTypes[type]
if (!types) {
throw new Error('include all \'' + type + '\' is not valid - must be BelongsTo, HasOne, HasMany, One, Has, Many or All')
}
if (types !== true) {
// replace type placeholder e.g. 'One' with it's constituent types e.g. 'HasOne', 'BelongsTo'
all.splice(i, 1)
i--
for (var j = 0; j < types.length; j++) {
if (all.indexOf(types[j]) == -1) {
all.unshift(types[j])
i++
}
}
}
}
}
// add all associations of types specified to includes
var nested = include.nested
if (nested) {
delete include.nested
if (!include.include) {
include.include = []
} else if (!Array.isArray(include.include)) {
include.include = [include.include]
}
}
var used = []
;(function addAllIncludes(parent, includes) {
used.push(parent)
Utils._.forEach(parent.associations, function(association) {
if (all !== true && all.indexOf(association.associationType) == -1) {
return
}
// check if model already included, and skip if so
var model = association.target
var as = association.options.as
if (Utils._.find(includes, {daoFactory: model, as: as})) {
return
}
// skip if recursing over a model already nested
if (nested && used.indexOf(model) != -1) {
return
}
// include this model
var thisInclude = optClone(include)
thisInclude.daoFactory = model
if (as) {
thisInclude.as = as
}
includes.push(thisInclude)
// run recursively if nested
if (nested) {
addAllIncludes(model, thisInclude.include)
}
})
used.pop()
})(this, includes)
}
var replaceReferencesWithTableNames = function(attributes) {
Object.keys(attributes).forEach(function(attrName) {
if (attributes[attrName].references instanceof DAOFactory) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!