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

Commit d81e4937 by Mick Hansen

chore: rename include.daoFactory to include.model

1 parent 57073848
...@@ -362,7 +362,7 @@ module.exports = (function() { ...@@ -362,7 +362,7 @@ module.exports = (function() {
, self = this , self = this
, accessor = Utils._.camelize(key) , accessor = Utils._.camelize(key)
, childOptions , childOptions
, primaryKeyAttribute = include.daoFactory.primaryKeyAttribute , primaryKeyAttribute = include.model.primaryKeyAttribute
, isEmpty = value[0] && value[0][primaryKeyAttribute] === null , isEmpty = value[0] && value[0][primaryKeyAttribute] === null
if (!isEmpty) { if (!isEmpty) {
...@@ -383,9 +383,9 @@ module.exports = (function() { ...@@ -383,9 +383,9 @@ module.exports = (function() {
if (association.isSingleAssociation) { if (association.isSingleAssociation) {
accessor = Utils.singularize(accessor, self.Model.options.language) accessor = Utils.singularize(accessor, self.Model.options.language)
self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.daoFactory.build(value[0], childOptions) self[accessor] = self.dataValues[accessor] = isEmpty ? null : include.model.build(value[0], childOptions)
} else { } else {
self[accessor] = self.dataValues[accessor] = isEmpty ? [] : include.daoFactory.bulkBuild(value, childOptions) self[accessor] = self.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions)
} }
}; };
......
var Utils = require("../../utils") var Utils = require("../../utils")
, SqlString = require("../../sql-string") , SqlString = require("../../sql-string")
, daoFactory = require("../../model") , Model = require("../../model")
, _ = require('lodash') , _ = require('lodash')
module.exports = (function() { module.exports = (function() {
...@@ -396,7 +396,7 @@ module.exports = (function() { ...@@ -396,7 +396,7 @@ module.exports = (function() {
Strings: should proxy to quoteIdentifiers Strings: should proxy to quoteIdentifiers
Arrays: Arrays:
* Expects array in the form: [<model> (optional), <model> (optional),... String, String (optional)] * Expects array in the form: [<model> (optional), <model> (optional),... String, String (optional)]
Each <model> can be a daoFactory or an object {model: DaoFactory, as: String}, matching include Each <model> can be a model or an object {model: Model, as: String}, matching include
* Zero or more models can be included in the array and are used to trace a path through the tree of * Zero or more models can be included in the array and are used to trace a path through the tree of
included nested associations. This produces the correct table name for the ORDER BY/GROUP BY SQL included nested associations. This produces the correct table name for the ORDER BY/GROUP BY SQL
and quotes it. and quotes it.
...@@ -428,7 +428,7 @@ module.exports = (function() { ...@@ -428,7 +428,7 @@ module.exports = (function() {
} }
var model, as var model, as
if (item instanceof daoFactory) { if (item instanceof Model) {
model = item model = item
} else { } else {
model = item.model model = item.model
...@@ -581,7 +581,7 @@ module.exports = (function() { ...@@ -581,7 +581,7 @@ module.exports = (function() {
- offset -> An offset value to start from. Only useable with limit! - offset -> An offset value to start from. Only useable with limit!
*/ */
selectQuery: function(tableName, options, Model) { selectQuery: function(tableName, options, model) {
// Enter and change at your own peril -- Mick Hansen // Enter and change at your own peril -- Mick Hansen
options = options || {} options = options || {}
...@@ -600,8 +600,8 @@ module.exports = (function() { ...@@ -600,8 +600,8 @@ module.exports = (function() {
, subJoinQueries = [] , subJoinQueries = []
, mainTableAs = null , mainTableAs = null
if (!Array.isArray(tableName) && Model) { if (!Array.isArray(tableName) && model) {
options.tableAs = mainTableAs = this.quoteTable(Model.name) options.tableAs = mainTableAs = this.quoteTable(model.name)
} }
options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) { options.table = table = !Array.isArray(tableName) ? this.quoteTable(tableName) : tableName.map(function(t) {
if (Array.isArray(t)) { if (Array.isArray(t)) {
...@@ -611,8 +611,8 @@ module.exports = (function() { ...@@ -611,8 +611,8 @@ module.exports = (function() {
}.bind(this)).join(", ") }.bind(this)).join(", ")
if (subQuery && mainAttributes) { if (subQuery && mainAttributes) {
if (Model.hasPrimaryKeys) { if (model.hasPrimaryKeys) {
Model.primaryKeyAttributes.forEach(function(keyAtt){ model.primaryKeyAttributes.forEach(function(keyAtt){
if(mainAttributes.indexOf(keyAtt) == -1){ if(mainAttributes.indexOf(keyAtt) == -1){
mainAttributes.push(keyAtt) mainAttributes.push(keyAtt)
} }
...@@ -663,7 +663,7 @@ module.exports = (function() { ...@@ -663,7 +663,7 @@ module.exports = (function() {
if (options.include) { if (options.include) {
var generateJoinQueries = function(include, parentTable) { var generateJoinQueries = function(include, parentTable) {
var table = include.daoFactory.getTableName() var table = include.model.getTableName()
, as = include.as , as = include.as
, joinQueryItem = "" , joinQueryItem = ""
, joinQueries = { , joinQueries = {
...@@ -697,7 +697,7 @@ module.exports = (function() { ...@@ -697,7 +697,7 @@ module.exports = (function() {
} }
if (through) { if (through) {
var throughTable = through.daoFactory.getTableName() var throughTable = through.model.getTableName()
, throughAs = as + "." + through.as , throughAs = as + "." + through.as
, throughAttributes = through.attributes.map(function(attr) { , throughAttributes = through.attributes.map(function(attr) {
return self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(throughAs + "." + attr) return self.quoteIdentifier(throughAs) + "." + self.quoteIdentifier(attr) + " AS " + self.quoteIdentifier(throughAs + "." + attr)
...@@ -742,7 +742,7 @@ module.exports = (function() { ...@@ -742,7 +742,7 @@ module.exports = (function() {
if (include.where) { if (include.where) {
targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.daoFactory, whereOptions) targetWhere = self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions)
joinQueryItem += " AND "+ targetWhere joinQueryItem += " AND "+ targetWhere
if (subQuery) { if (subQuery) {
if (!options.where) options.where = {} if (!options.where) options.where = {}
...@@ -789,7 +789,7 @@ module.exports = (function() { ...@@ -789,7 +789,7 @@ module.exports = (function() {
joinQueryItem += where joinQueryItem += where
if (include.where) { if (include.where) {
joinQueryItem += " AND "+self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.daoFactory, whereOptions) joinQueryItem += " AND "+self.getWhereConditions(include.where, self.sequelize.literal(self.quoteIdentifier(as)), include.model, whereOptions)
// If its a multi association we need to add a where query to the main where (executed in the subquery) // If its a multi association we need to add a where query to the main where (executed in the subquery)
if (subQuery && association.isMultiAssociation && include.required) { if (subQuery && association.isMultiAssociation && include.required) {
...@@ -851,7 +851,7 @@ module.exports = (function() { ...@@ -851,7 +851,7 @@ module.exports = (function() {
// Add WHERE to sub or main query // Add WHERE to sub or main query
if (options.hasOwnProperty('where')) { if (options.hasOwnProperty('where')) {
options.where = this.getWhereConditions(options.where, mainTableAs || tableName, Model, options) options.where = this.getWhereConditions(options.where, mainTableAs || tableName, model, options)
if (subQuery) { if (subQuery) {
subQueryItems.push(" WHERE " + options.where) subQueryItems.push(" WHERE " + options.where)
} else { } else {
...@@ -861,7 +861,7 @@ module.exports = (function() { ...@@ -861,7 +861,7 @@ module.exports = (function() {
// Add GROUP BY to sub or main query // Add GROUP BY to sub or main query
if (options.group) { if (options.group) {
options.group = Array.isArray(options.group) ? options.group.map(function (t) { return this.quote(t, Model) }.bind(this)).join(', ') : options.group options.group = Array.isArray(options.group) ? options.group.map(function (t) { return this.quote(t, model) }.bind(this)).join(', ') : options.group
if (subQuery) { if (subQuery) {
subQueryItems.push(" GROUP BY " + options.group) subQueryItems.push(" GROUP BY " + options.group)
} else { } else {
...@@ -871,7 +871,7 @@ module.exports = (function() { ...@@ -871,7 +871,7 @@ module.exports = (function() {
// Add HAVING to sub or main query // Add HAVING to sub or main query
if (options.hasOwnProperty('having')) { if (options.hasOwnProperty('having')) {
options.having = this.getWhereConditions(options.having, tableName, Model, options, false) options.having = this.getWhereConditions(options.having, tableName, model, options, false)
if (subQuery) { if (subQuery) {
subQueryItems.push(" HAVING " + options.having) subQueryItems.push(" HAVING " + options.having)
} else { } else {
...@@ -886,10 +886,10 @@ module.exports = (function() { ...@@ -886,10 +886,10 @@ module.exports = (function() {
if (Array.isArray(options.order)) { if (Array.isArray(options.order)) {
options.order.forEach(function (t) { options.order.forEach(function (t) {
if (subQuery && !(t[0] instanceof daoFactory) && !(t[0].model instanceof daoFactory)) { if (subQuery && !(t[0] instanceof Model) && !(t[0].model instanceof Model)) {
subQueryOrder.push(this.quote(t, Model)) subQueryOrder.push(this.quote(t, model))
} }
mainQueryOrder.push(this.quote(t, Model)) mainQueryOrder.push(this.quote(t, model))
}.bind(this)) }.bind(this))
} else { } else {
mainQueryOrder.push(options.order) mainQueryOrder.push(options.order)
......
...@@ -232,7 +232,7 @@ module.exports = (function() { ...@@ -232,7 +232,7 @@ module.exports = (function() {
// Queries with include // Queries with include
} else if (this.options.hasJoin === true) { } else if (this.options.hasJoin === true) {
results = groupJoinData(results, { results = groupJoinData(results, {
daoFactory: this.callee, model: this.callee,
includeMap: this.options.includeMap, includeMap: this.options.includeMap,
includeNames: this.options.includeNames includeNames: this.options.includeNames
}, { }, {
...@@ -359,8 +359,8 @@ module.exports = (function() { ...@@ -359,8 +359,8 @@ module.exports = (function() {
if (includeOptions.includeNames) { if (includeOptions.includeNames) {
calleeDataIgnore = calleeDataIgnore.concat(includeOptions.includeNames) calleeDataIgnore = calleeDataIgnore.concat(includeOptions.includeNames)
} }
if (includeOptions.daoFactory.primaryKeyAttributes.length === 1) { if (includeOptions.model.primaryKeyAttributes.length === 1) {
primaryKeyAttribute = includeOptions.daoFactory.primaryKeyAttribute primaryKeyAttribute = includeOptions.model.primaryKeyAttribute
} }
data.forEach(function parseRow(row) { data.forEach(function parseRow(row) {
......
...@@ -1563,8 +1563,8 @@ module.exports = (function() { ...@@ -1563,8 +1563,8 @@ module.exports = (function() {
// Apply on each include // Apply on each include
if ( options.include && options.include.length ) { if ( options.include && options.include.length ) {
options.include.forEach(function(include) { options.include.forEach(function(include) {
if( typeof include == 'object' && include.daoFactory || include.daoFactory ){ if( typeof include == 'object' && include.model || include.model ){
paranoidClause.call( include.daoFactory || include.daoFactory, include ) paranoidClause.call( include.model || include.model, include )
} }
}) })
} }
...@@ -1667,13 +1667,13 @@ module.exports = (function() { ...@@ -1667,13 +1667,13 @@ module.exports = (function() {
// convert all included elements to { Model: Model } form // convert all included elements to { Model: Model } form
var includes = options.include = options.include.map(function(include) { var includes = options.include = options.include.map(function(include) {
if (include instanceof Model) { if (include instanceof Model) {
return { daoFactory: include } include = { model: include }
} else if (typeof include !== 'object') { } else if (typeof include !== 'object') {
throw new Error('Include unexpected. Element has to be either an instance of Model or an object.') throw new Error('Include unexpected. Element has to be either an instance of Model or an object.')
} else if (include.hasOwnProperty('model')) { } else if (include.hasOwnProperty('daoFactory')) {
include.daoFactory = include.model include.model = include.daoFactory
delete include.model
} }
return include return include
}) })
...@@ -1715,24 +1715,24 @@ module.exports = (function() { ...@@ -1715,24 +1715,24 @@ module.exports = (function() {
} }
var validateIncludedElement = function(include, tableNames) { var validateIncludedElement = function(include, tableNames) {
if (!include.hasOwnProperty('daoFactory')) { if (!include.hasOwnProperty('model')) {
throw new Error('Include malformed. Expected attributes: model') throw new Error('Include malformed. Expected attributes: model')
} }
tableNames[include.daoFactory.tableName] = true tableNames[include.model.tableName] = true
if (include.hasOwnProperty('attributes')) { if (include.hasOwnProperty('attributes')) {
include.originalAttributes = include.attributes; include.originalAttributes = include.attributes;
include.attributes = include.attributes.concat(include.daoFactory.primaryKeyAttributes) include.attributes = include.attributes.concat(include.model.primaryKeyAttributes)
} else { } else {
include.attributes = Object.keys(include.daoFactory.attributes) include.attributes = Object.keys(include.model.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
// check if the current Model is actually associated with the passed Model - or it's a pseudo include // check if the current Model is actually associated with the passed Model - or it's a pseudo include
var association = this.getAssociation(include.daoFactory, include.as) var association = this.getAssociation(include.model, include.as)
if (association) { if (association) {
include.association = association include.association = association
include.as = association.as include.as = association.as
...@@ -1743,7 +1743,7 @@ module.exports = (function() { ...@@ -1743,7 +1743,7 @@ module.exports = (function() {
var through = include.association.through var through = include.association.through
include.through = { include.through = {
daoFactory: through, model: through,
as: Utils.singularize(through.tableName, through.options.language), as: Utils.singularize(through.tableName, through.options.language),
association: { association: {
isSingleAssociation: true isSingleAssociation: true
...@@ -1761,12 +1761,12 @@ module.exports = (function() { ...@@ -1761,12 +1761,12 @@ module.exports = (function() {
// Validate child includes // Validate child includes
if (include.hasOwnProperty('include')) { if (include.hasOwnProperty('include')) {
validateIncludedElements.call(include.daoFactory, include, tableNames) validateIncludedElements.call(include.model, include, tableNames)
} }
return include return include
} else { } else {
var msg = include.daoFactory.name var msg = include.model.name
if (include.as) { if (include.as) {
msg += " (" + include.as + ")" msg += " (" + include.as + ")"
...@@ -1846,7 +1846,7 @@ module.exports = (function() { ...@@ -1846,7 +1846,7 @@ module.exports = (function() {
// check if model already included, and skip if so // check if model already included, and skip if so
var model = association.target var model = association.target
var as = association.options.as var as = association.options.as
if (Utils._.find(includes, {daoFactory: model, as: as})) { if (Utils._.find(includes, {model: model, as: as})) {
return return
} }
...@@ -1857,7 +1857,7 @@ module.exports = (function() { ...@@ -1857,7 +1857,7 @@ module.exports = (function() {
// include this model // include this model
var thisInclude = optClone(include) var thisInclude = optClone(include)
thisInclude.daoFactory = model thisInclude.model = model
if (as) { if (as) {
thisInclude.as = as thisInclude.as = as
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!