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

Commit 5cbd2452 by Overlook Motel

refactor validateIncludedElement to utilise altered getAssociation

1 parent 2ed685be
Showing with 59 additions and 74 deletions
...@@ -1349,100 +1349,85 @@ module.exports = (function() { ...@@ -1349,100 +1349,85 @@ module.exports = (function() {
var validateIncludedElement = function(include, parent) { var validateIncludedElement = function(include, parent) {
if (include instanceof DAOFactory) { if (include instanceof DAOFactory) {
include = { daoFactory: include, as: include.tableName } include = { daoFactory: include }
} }
if (typeof parent === "undefined") { if (typeof parent === "undefined") {
parent = this parent = this
} }
if (typeof include === 'object') { if (typeof include !== 'object') {
if (include.hasOwnProperty('model')) { throw new Error('Include unexpected. Element has to be either an instance of DAOFactory or an object.')
include.daoFactory = include.model }
delete include.model
}
if (!include.hasOwnProperty('as')) { if (include.hasOwnProperty('model')) {
include.as = include.daoFactory.tableName include.daoFactory = include.model
} delete include.model
} else if (!include.hasOwnProperty('daoFactory')) {
throw new Error('Include malformed. Expected attributes: daoFactory, as!')
}
if (include.hasOwnProperty('attributes')) { if (include.hasOwnProperty('attributes')) {
var primaryKeys; var primaryKeys;
if (include.daoFactory.hasPrimaryKeys) { if (include.daoFactory.hasPrimaryKeys) {
primaryKeys = [] primaryKeys = []
for (var field_name in include.daoFactory.primaryKeys) { for (var field_name in include.daoFactory.primaryKeys) {
primaryKeys.push(field_name) primaryKeys.push(field_name)
}
} else {
primaryKeys = ['id']
} }
include.attributes = include.attributes.concat(primaryKeys)
} else { } else {
include.attributes = Object.keys(include.daoFactory.attributes) primaryKeys = ['id']
} }
include.attributes = include.attributes.concat(primaryKeys)
} else {
include.attributes = Object.keys(include.daoFactory.attributes)
}
// pseudo include just needed the attribute logic, return // pseudo include just needed the attribute logic, return
if (include._pseudo) return include if (include._pseudo) return include
if (include.hasOwnProperty('daoFactory') && (include.hasOwnProperty('as'))) { // check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include
var usesAlias = (include.as !== include.daoFactory.tableName) var association = parent.getAssociation(include.daoFactory, include.as)
, association = (usesAlias ? parent.getAssociationByAlias(include.as) : parent.getAssociation(include.daoFactory)) if (association) {
include.association = association
// If single (1:1) association, we singularize the alias, so it will match the automatically generated alias of belongsTo/HasOne include.as = association.as
if (association && !usesAlias && association.isSingleAssociation) {
include.as = Utils.singularize(include.daoFactory.tableName, include.daoFactory.options.language) // If through, we create a pseudo child include, to ease our parsing later on
if (Object(include.association.through) === include.association.through) {
if (!include.include) include.include = []
var through = include.association.through
include.through = {
daoFactory: through,
as: Utils.singularize(through.tableName, through.options.language),
association: {
isSingleAssociation: true
},
_pseudo: true
} }
// check if the current daoFactory is actually associated with the passed daoFactory - or it's a pseudo include include.include.push(include.through)
if (association && (!association.options.as || (association.options.as === include.as))) { }
include.association = association
// If through, we create a pseudo child include, to ease our parsing later on
if (Object(include.association.through) === include.association.through) {
if (!include.include) include.include = []
var through = include.association.through
include.through = {
daoFactory: through,
as: Utils.singularize(through.tableName, through.options.language),
association: {
isSingleAssociation: true
},
_pseudo: true
}
include.include.push(include.through)
}
if (include.required === undefined) { if (include.required === undefined) {
include.required = false include.required = !!include.where
if (include.where) { }
include.required = true
}
}
// Validate child includes // Validate child includes
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
validateIncludedElements(include) validateIncludedElements(include)
} }
return include return include
} else { } else {
var msg = include.daoFactory.name var msg = include.daoFactory.name
if (usesAlias) { if (include.as) {
msg += " (" + include.as + ")" msg += " (" + include.as + ")"
} }
msg += " is not associated to " + this.name + "!" msg += " is not associated to " + this.name + "!"
throw new Error(msg) throw new Error(msg)
}
} else {
throw new Error('Include malformed. Expected attributes: daoFactory, as!')
}
} else {
throw new Error('Include unexpected. Element has to be either an instance of DAOFactory or an object.')
} }
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!