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

Commit c5696e49 by Mick Hansen

refactor: better query passing for model findAll (plugin support)

1 parent bbae7602
Showing with 21 additions and 24 deletions
...@@ -672,13 +672,15 @@ module.exports = (function() { ...@@ -672,13 +672,15 @@ module.exports = (function() {
* @param {Object} [options.include[].where] Where clauses to apply to the child models. Note that this converts the eager load to an inner join, unless you explicitly set `required: false` * @param {Object} [options.include[].where] Where clauses to apply to the child models. Note that this converts the eager load to an inner join, unless you explicitly set `required: false`
* @param {Array<String>} [options.include[].attributes] A list of attributes to select from the child model * @param {Array<String>} [options.include[].attributes] A list of attributes to select from the child model
* @param {Boolean} [options.include[].required] If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if `include.where` is set, false otherwise. * @param {Boolean} [options.include[].required] If true, converts to an inner join, which means that the parent model will only be loaded if it has any matching children. True if `include.where` is set, false otherwise.
* @param {Object} [options.include[].through.where] Filter on the join model for belongsToMany relations
* @param {Array} [options.include[].through.attributes] A list of attributes to select from the join model for belongsToMany relations
* @param {Array<Object|Model>} [options.include[].include] Load further nested related models * @param {Array<Object|Model>} [options.include[].include] Load further nested related models
* @param {String|Array|Sequelize.fn} [options.order] Specifies an ordering. If a string is provided, it will be esacped. Using an array, you can provide several columns / functions to order by. Each element can be further wrapped in a two-element array. The first element is the column / function to order by, the second is the direction. For example: `order: [['name', 'DESC']]`. In this way the column will be escaped, but the direction will not. * @param {String|Array|Sequelize.fn} [options.order] Specifies an ordering. If a string is provided, it will be esacped. Using an array, you can provide several columns / functions to order by. Each element can be further wrapped in a two-element array. The first element is the column / function to order by, the second is the direction. For example: `order: [['name', 'DESC']]`. In this way the column will be escaped, but the direction will not.
* @param {Number} [options.limit] * @param {Number} [options.limit]
* @param {Number} [options.offset] * @param {Number} [options.offset]
* @param {Transaction} [options.transaction] * @param {Transaction} [options.transaction]
* @param {Object} [queryOptions] Set the query options, e.g. raw, specifying that you want raw data instead of built Instances. See sequelize.query for options * @param {String} [options.lock] Lock the selected rows in either share or update mode. Possible options are transaction.LOCK.UPDATE and transaction.LOCK.SHARE. See [transaction.LOCK for an example](https://github.com/sequelize/sequelize/wiki/API-Reference-Transaction#LOCK)
* @param {String} [queryOptions.lock] Lock the selected rows in either share or update mode. Possible options are transaction.LOCK.UPDATE and transaction.LOCK.SHARE. See [transaction.LOCK for an example](https://github.com/sequelize/sequelize/wiki/API-Reference-Transaction#LOCK) * @param {Boolean} [options.raw] Return raw result. See sequelize.query for more information.
* *
* @see {Sequelize#query} * @see {Sequelize#query}
* @return {Promise<Array<Instance>>} * @return {Promise<Array<Instance>>}
...@@ -686,7 +688,7 @@ module.exports = (function() { ...@@ -686,7 +688,7 @@ module.exports = (function() {
*/ */
Model.prototype.findAll = function(options, queryOptions) { Model.prototype.findAll = function(options, queryOptions) {
var hasJoin = false var hasJoin = false
, tableNames = { }; , tableNames = {};
tableNames[this.getTableName(options)] = true; tableNames[this.getTableName(options)] = true;
options = optClone(options || {}); options = optClone(options || {});
...@@ -694,6 +696,9 @@ module.exports = (function() { ...@@ -694,6 +696,9 @@ module.exports = (function() {
hooks: true hooks: true
}); });
_.assign(options, queryOptions);
options.tableNames = Object.keys(tableNames);
return Promise.bind(this).then(function() { return Promise.bind(this).then(function() {
conformOptions(options); conformOptions(options);
...@@ -709,7 +714,7 @@ module.exports = (function() { ...@@ -709,7 +714,7 @@ module.exports = (function() {
}).then(function() { }).then(function() {
if (typeof options === 'object') { if (typeof options === 'object') {
if (options.include) { if (options.include) {
hasJoin = true; options.hasJoin = true;
validateIncludedElements.call(this, options, tableNames); validateIncludedElements.call(this, options, tableNames);
...@@ -736,10 +741,7 @@ module.exports = (function() { ...@@ -736,10 +741,7 @@ module.exports = (function() {
return this.runHooks('beforeFindAfterOptions', options); return this.runHooks('beforeFindAfterOptions', options);
} }
}).then(function() { }).then(function() {
return this.QueryInterface.select(this, this.getTableName(options), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(options), options);
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
}, queryOptions, { transaction: options.transaction, logging: options.logging }));
}).tap(function(results) { }).tap(function(results) {
if (options.hooks) { if (options.hooks) {
return this.runHooks('afterFind', results, options); return this.runHooks('afterFind', results, options);
......
...@@ -655,6 +655,12 @@ module.exports = (function() { ...@@ -655,6 +655,12 @@ module.exports = (function() {
QueryInterface.prototype.select = function(model, tableName, options, queryOptions) { QueryInterface.prototype.select = function(model, tableName, options, queryOptions) {
options = options || {}; options = options || {};
if (queryOptions) {
_.assign(options, queryOptions);
}
options.type = QueryTypes.SELECT;
// See if we need to merge options and model.scopeObj // See if we need to merge options and model.scopeObj
// we're doing this on the QueryInterface level because it's a bridge between // we're doing this on the QueryInterface level because it's a bridge between
// sequelize and the databases // sequelize and the databases
...@@ -671,22 +677,11 @@ module.exports = (function() { ...@@ -671,22 +677,11 @@ module.exports = (function() {
}); });
} }
options.lock = queryOptions.lock; return this.sequelize.query(
options.subQuery = queryOptions.subQuery; this.QueryGenerator.selectQuery(tableName, options, model),
model,
var sql = this.QueryGenerator.selectQuery(tableName, options, model); options
queryOptions = Utils._.extend({}, queryOptions, { );
type: QueryTypes.SELECT,
include: options.include,
includeNames: options.includeNames,
includeMap: options.includeMap,
hasSingleAssociation: options.hasSingleAssociation,
hasMultiAssociation: options.hasMultiAssociation,
attributes: options.attributes,
originalAttributes: options.originalAttributes,
});
return this.sequelize.query(sql, model, queryOptions);
}; };
QueryInterface.prototype.increment = function(dao, tableName, values, identifier, options) { QueryInterface.prototype.increment = function(dao, tableName, values, identifier, options) {
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!