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

Commit 11347233 by Mick Hansen

Merge pull request #1952 from quiddle/pass-options-to-getTableName

Pass options to getTableName function
2 parents 938e167f 40c474b5
Showing with 13 additions and 12 deletions
...@@ -402,7 +402,7 @@ module.exports = (function() { ...@@ -402,7 +402,7 @@ module.exports = (function() {
return self.drop(options); return self.drop(options);
} }
}).then(function () { }).then(function () {
return self.QueryInterface.createTable(self.getTableName(), attributes, options); return self.QueryInterface.createTable(self.getTableName(options), attributes, options);
}).return(this); }).return(this);
}; };
...@@ -413,7 +413,7 @@ module.exports = (function() { ...@@ -413,7 +413,7 @@ module.exports = (function() {
* @return {Promise} * @return {Promise}
*/ */
Model.prototype.drop = function(options) { Model.prototype.drop = function(options) {
return this.QueryInterface.dropTable(this.getTableName(), options); return this.QueryInterface.dropTable(this.getTableName(options), options);
}; };
Model.prototype.dropSchema = function(schema) { Model.prototype.dropSchema = function(schema) {
...@@ -449,9 +449,10 @@ module.exports = (function() { ...@@ -449,9 +449,10 @@ module.exports = (function() {
* Get the tablename of the model, taking schema into account. The method will return The name as a string if the model has no schema, * Get the tablename of the model, taking schema into account. The method will return The name as a string if the model has no schema,
* or an object with `tableName`, `schema` and `delimiter` properties. * or an object with `tableName`, `schema` and `delimiter` properties.
* *
* @param {Object} options The hash of options from any query. You can use one model to access tables with matching schemas by overriding `getTableName` and using custom key/values to alter the name of the table. (eg. subscribers_1, subscribers_2)
* @return {String|Object} * @return {String|Object}
*/ */
Model.prototype.getTableName = function() { Model.prototype.getTableName = function(options) {
return this.QueryGenerator.addSchema(this); return this.QueryGenerator.addSchema(this);
}; };
...@@ -643,7 +644,7 @@ module.exports = (function() { ...@@ -643,7 +644,7 @@ module.exports = (function() {
* @param {Object} [options.where] A hash of attributes to describe your search. See above for examples. * @param {Object} [options.where] A hash of attributes to describe your search. See above for examples.
* @param {Array<String>} [options.attributes] A list of the attributes that you want to select. To rename an attribute, you can pass an array, with two elements - the first is the name of the attribute in the DB (or some kind of expression such as `Sequelize.literal`, `Sequelize.fn` and so on), and the second is the name you want the attribute to have in the returned instance * @param {Array<String>} [options.attributes] A list of the attributes that you want to select. To rename an attribute, you can pass an array, with two elements - the first is the name of the attribute in the DB (or some kind of expression such as `Sequelize.literal`, `Sequelize.fn` and so on), and the second is the name you want the attribute to have in the returned instance
* @param {Array<Object|Model>} [options.include] A list of associations to eagerly load using a left join. Supported is either `{ include: [ Model1, Model2, ...]}` or `{ include: [{ model: Model1, as: 'Alias' }]}`. If your association are set up with an `as` (eg. `X.hasMany(Y, { as: 'Z }`, you need to specify Z in the as attribute when eager loading Y). * @param {Array<Object|Model>} [options.include] A list of associations to eagerly load using a left join. Supported is either `{ include: [ Model1, Model2, ...]}` or `{ include: [{ model: Model1, as: 'Alias' }]}`. If your association are set up with an `as` (eg. `X.hasMany(Y, { as: 'Z }`, you need to specify Z in the as attribute when eager loading Y).
* @param {Model} [optinos.include[].model] The model you want to eagerly load * @param {Model} [options.include[].model] The model you want to eagerly load
* @param {String} [options.include[].as] The alias of the relation, in case the model you want to eagerly load is aliassed. * @param {String} [options.include[].as] The alias of the relation, in case the model you want to eagerly load is aliassed.
* @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: true` * @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: true`
* @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
...@@ -692,7 +693,7 @@ module.exports = (function() { ...@@ -692,7 +693,7 @@ module.exports = (function() {
options = paranoidClause.call(this, options); options = paranoidClause.call(this, options);
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(options), options, Utils._.defaults({
type: QueryTypes.SELECT, type: QueryTypes.SELECT,
hasJoin: hasJoin, hasJoin: hasJoin,
tableNames: Object.keys(tableNames) tableNames: Object.keys(tableNames)
...@@ -706,7 +707,7 @@ module.exports = (function() { ...@@ -706,7 +707,7 @@ module.exports = (function() {
// whereCollection is used for non-primary key updates // whereCollection is used for non-primary key updates
this.options.whereCollection = optcpy.where || null; this.options.whereCollection = optcpy.where || null;
return this.QueryInterface.select(this, [[this.getTableName(), this.name], joinTableName], optcpy, Utils._.defaults({ return this.QueryInterface.select(this, [[this.getTableName(options), this.name], joinTableName], optcpy, Utils._.defaults({
type: QueryTypes.SELECT type: QueryTypes.SELECT
}, queryOptions, { transaction: (options || {}).transaction })); }, queryOptions, { transaction: (options || {}).transaction }));
}; };
...@@ -799,7 +800,7 @@ module.exports = (function() { ...@@ -799,7 +800,7 @@ module.exports = (function() {
mapFieldNames.call(this, options, this); mapFieldNames.call(this, options, this);
options = paranoidClause.call(this, options); options = paranoidClause.call(this, options);
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({ return this.QueryInterface.select(this, this.getTableName(options), options, Utils._.defaults({
plain: true, plain: true,
type: QueryTypes.SELECT, type: QueryTypes.SELECT,
hasJoin: hasJoin, hasJoin: hasJoin,
...@@ -833,7 +834,7 @@ module.exports = (function() { ...@@ -833,7 +834,7 @@ module.exports = (function() {
options = paranoidClause.call(this, options); options = paranoidClause.call(this, options);
return this.QueryInterface.rawSelect(this.getTableName(), options, aggregateFunction, this); return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this);
}; };
/** /**
...@@ -1251,7 +1252,7 @@ module.exports = (function() { ...@@ -1251,7 +1252,7 @@ module.exports = (function() {
} }
// Insert all records at once // Insert all records at once
return self.QueryInterface.bulkInsert(self.getTableName(), records, options, attributes); return self.QueryInterface.bulkInsert(self.getTableName(options), records, options, attributes);
} }
}).then(function() { }).then(function() {
// Run after hook // Run after hook
...@@ -1312,9 +1313,9 @@ module.exports = (function() { ...@@ -1312,9 +1313,9 @@ module.exports = (function() {
if (self._timestampAttributes.deletedAt && !options.force) { if (self._timestampAttributes.deletedAt && !options.force) {
var attrValueHash = {}; var attrValueHash = {};
attrValueHash[self._timestampAttributes.deletedAt] = Utils.now(self.modelManager.sequelize.options.dialect); attrValueHash[self._timestampAttributes.deletedAt] = Utils.now(self.modelManager.sequelize.options.dialect);
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, where, options, self.rawAttributes); return self.QueryInterface.bulkUpdate(self.getTableName(options), attrValueHash, where, options, self.rawAttributes);
} else { } else {
return self.QueryInterface.bulkDelete(self.getTableName(), where, options, self); return self.QueryInterface.bulkDelete(self.getTableName(options), where, options, self);
} }
}).tap(function() { }).tap(function() {
// Run afterDestroy hook on each record individually // Run afterDestroy hook on each record individually
...@@ -1460,7 +1461,7 @@ module.exports = (function() { ...@@ -1460,7 +1461,7 @@ module.exports = (function() {
} }
// Run query to update all rows // Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHashUse, where, options, self.tableAttributes).then(function(affectedRows) { return self.QueryInterface.bulkUpdate(self.getTableName(options), attrValueHashUse, where, options, self.tableAttributes).then(function(affectedRows) {
if (options.returning) { if (options.returning) {
return [affectedRows.length, affectedRows]; return [affectedRows.length, affectedRows];
} }
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!