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

Commit 72cb5464 by Mick Hansen

Merge pull request #2795 from quiddle/pass-options-to-get-tablename

Add ability to pass options back to the getTableName function
2 parents 3150ef40 a72daaed
Showing with 19 additions and 19 deletions
......@@ -551,7 +551,7 @@ module.exports = (function() {
if (self.isNewRecord) {
query = 'insert';
args = [self, self.QueryInterface.QueryGenerator.addSchema(self.Model), values, options];
args = [self, self.Model.getTableName(options), values, options];
hook = 'Create';
} else {
var identifier = self.primaryKeyValues;
......@@ -572,7 +572,7 @@ module.exports = (function() {
}
query = 'update';
args = [self, self.QueryInterface.QueryGenerator.addSchema(self.Model), values, identifier, options];
args = [self, self.Model.getTableName(options), values, identifier, options];
hook = 'Update';
}
......@@ -732,7 +732,7 @@ module.exports = (function() {
} else {
where = {};
where[self.Model.rawAttributes[self.Model.primaryKeyAttribute].field] = self.get(self.Model.primaryKeyAttribute, {raw: true});
return self.QueryInterface.delete(self, self.QueryInterface.QueryGenerator.addSchema(self.Model), where, _.defaults(options, {limit: null}));
return self.QueryInterface.delete(self, self.Model.getTableName(options), where, _.defaults(options, {limit: null}));
}
}).tap(function(result) {
// Run after hook
......@@ -840,7 +840,7 @@ module.exports = (function() {
countOrOptions.attributes[updatedAtAttr] = this.Model.__getTimestamp(updatedAtAttr);
}
return this.QueryInterface.increment(this, this.QueryInterface.QueryGenerator.addSchema(this.Model), values, where, countOrOptions);
return this.QueryInterface.increment(this, this.Model.getTableName(countOrOptions), values, where, countOrOptions);
};
/**
......
......@@ -389,9 +389,9 @@ module.exports = (function() {
return self.drop(options);
}
}).then(function () {
return self.QueryInterface.createTable(self.getTableName(), attributes, options);
return self.QueryInterface.createTable(self.getTableName(options), attributes, options);
}).then(function () {
return self.QueryInterface.showIndex(self.getTableName(), options);
return self.QueryInterface.showIndex(self.getTableName(options), options);
}).then(function (indexes) {
// Assign an auto-generated name to indexes which are not named by the user
self.options.indexes = self.QueryInterface.nameIndexes(self.options.indexes, self.tableName);
......@@ -403,7 +403,7 @@ module.exports = (function() {
});
return Promise.map(indexes, function (index) {
return self.QueryInterface.addIndex(self.getTableName(), index.fields, index, self.tableName);
return self.QueryInterface.addIndex(self.getTableName(options), index.fields, index, self.tableName);
});
}).return(this);
};
......@@ -415,7 +415,7 @@ module.exports = (function() {
* @return {Promise}
*/
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) {
......@@ -454,7 +454,7 @@ module.exports = (function() {
* @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}
*/
Model.prototype.getTableName = function() {
Model.prototype.getTableName = function(options) {
return this.QueryGenerator.addSchema(this);
};
......@@ -676,7 +676,7 @@ module.exports = (function() {
var hasJoin = false
, tableNames = { };
tableNames[this.getTableName()] = true;
tableNames[this.getTableName(options)] = true;
options = optClone(options || {});
options = Utils._.defaults(options, {
......@@ -725,7 +725,7 @@ module.exports = (function() {
return this.runHooks('beforeFindAfterOptions', options);
}
}).then(function() {
return this.QueryInterface.select(this, this.getTableName(), options, Utils._.defaults({
return this.QueryInterface.select(this, this.getTableName(options), options, Utils._.defaults({
type: QueryTypes.SELECT,
hasJoin: hasJoin,
tableNames: Object.keys(tableNames)
......@@ -744,7 +744,7 @@ module.exports = (function() {
// whereCollection is used for non-primary key updates
this.options.whereCollection = options.where || null;
return this.QueryInterface.select(this, [[this.getTableName(), this.name], joinTableName], options, Utils._.defaults({
return this.QueryInterface.select(this, [[this.getTableName(options), this.name], joinTableName], options, Utils._.defaults({
type: QueryTypes.SELECT
}, queryOptions, { transaction: (options || {}).transaction }));
};
......@@ -817,7 +817,7 @@ module.exports = (function() {
options = paranoidClause.call(this, options);
return this.QueryInterface.rawSelect(this.getTableName(), options, aggregateFunction, this);
return this.QueryInterface.rawSelect(this.getTableName(options), options, aggregateFunction, this);
};
/**
......@@ -1216,7 +1216,7 @@ module.exports = (function() {
delete values[this.primaryKeyField];
}
return this.QueryInterface.upsert(this.getTableName(), values, this, options);
return this.QueryInterface.upsert(this.getTableName(options), values, this, options);
};
Model.prototype.insertOrUpdate = Model.prototype.upsert;
......@@ -1378,7 +1378,7 @@ module.exports = (function() {
}
// 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() {
// Run after hook
......@@ -1440,9 +1440,9 @@ module.exports = (function() {
if (self._timestampAttributes.deletedAt && !options.force) {
var attrValueHash = {};
attrValueHash[self._timestampAttributes.deletedAt] = Utils.now(self.modelManager.sequelize.options.dialect);
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, options.where, options, self.rawAttributes);
return self.QueryInterface.bulkUpdate(self.getTableName(options), attrValueHash, options.where, options, self.rawAttributes);
} else {
return self.QueryInterface.bulkDelete(self.getTableName(), options.where, options, self);
return self.QueryInterface.bulkDelete(self.getTableName(options), options.where, options, self);
}
}).tap(function() {
// Run afterDestroy hook on each record individually
......@@ -1509,7 +1509,7 @@ module.exports = (function() {
var attrValueHash = {};
attrValueHash[self._timestampAttributes.deletedAt] = null;
options.omitNull = false;
return self.QueryInterface.bulkUpdate(self.getTableName(), attrValueHash, options.where, options, self._timestampAttributes.deletedAt);
return self.QueryInterface.bulkUpdate(self.getTableName(options), attrValueHash, options.where, options, self._timestampAttributes.deletedAt);
}).tap(function() {
// Run afterDestroy hook on each record individually
if (options.individualHooks) {
......@@ -1673,7 +1673,7 @@ module.exports = (function() {
mapFieldNames.call(self, options, self);
// Run query to update all rows
return self.QueryInterface.bulkUpdate(self.getTableName(), valuesUse, options.where, options, self.tableAttributes).then(function(affectedRows) {
return self.QueryInterface.bulkUpdate(self.getTableName(options), valuesUse, options.where, options, self.tableAttributes).then(function(affectedRows) {
if (options.returning) {
daos = 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!