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

Commit 1bc8b0e8 by Sushant Committed by GitHub

chore: deprecate public apis removed from v5.beta (#10049)

1 parent 43ab707d
...@@ -147,7 +147,7 @@ class BelongsTo extends Association { ...@@ -147,7 +147,7 @@ class BelongsTo extends Association {
}; };
} else { } else {
if (association.targetKeyIsPrimary && !options.where) { if (association.targetKeyIsPrimary && !options.where) {
return Target.findById(instance.get(association.foreignKey), options); return Target.findByPk(instance.get(association.foreignKey), options);
} else { } else {
where[association.targetKey] = instance.get(association.foreignKey); where[association.targetKey] = instance.get(association.foreignKey);
options.limit = null; options.limit = null;
......
...@@ -132,6 +132,7 @@ const Hooks = { ...@@ -132,6 +132,7 @@ const Hooks = {
}, },
hook() { hook() {
Utils.deprecate('hook() method has been deprecated, please use addHook() method instead');
return Hooks.addHook.apply(this, arguments); return Hooks.addHook.apply(this, arguments);
}, },
...@@ -152,8 +153,13 @@ const Hooks = { ...@@ -152,8 +153,13 @@ const Hooks = {
} }
debug(`adding hook ${hookType}`); debug(`adding hook ${hookType}`);
const originalHookType = hookType;
hookType = hookAliases[hookType] || hookType; hookType = hookAliases[hookType] || hookType;
if (hookAliases[originalHookType]) {
Utils.deprecate(`${originalHookType} hook has been deprecated, please use ${hookType} hook instead`);
}
// check for proxies, add them too // check for proxies, add them too
hookType = getProxiedHooks(hookType); hookType = getProxiedHooks(hookType);
......
...@@ -1534,7 +1534,7 @@ class Model { ...@@ -1534,7 +1534,7 @@ class Model {
*/ */
static findAll(options) { static findAll(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new sequelizeErrors.QueryError('The argument passed to findAll must be an options object, use findById if you wish to pass a single primary key value'); throw new sequelizeErrors.QueryError('The argument passed to findAll must be an options object, use findByPk if you wish to pass a single primary key value');
} }
if (options !== undefined && options.attributes) { if (options !== undefined && options.attributes) {
...@@ -1697,8 +1697,6 @@ class Model { ...@@ -1697,8 +1697,6 @@ class Model {
/** /**
* Search for a single instance by its primary key. * Search for a single instance by its primary key.
* *
* __Alias__: _findByPrimary_
*
* @param {Number|String|Buffer} id The value of the desired instance's primary key. * @param {Number|String|Buffer} id The value of the desired instance's primary key.
* @param {Object} [options] * @param {Object} [options]
* @param {Transaction} [options.transaction] Transaction to run query under * @param {Transaction} [options.transaction] Transaction to run query under
...@@ -1707,7 +1705,7 @@ class Model { ...@@ -1707,7 +1705,7 @@ class Model {
* @see {@link Model.findAll} for a full explanation of options * @see {@link Model.findAll} for a full explanation of options
* @return {Promise<Model>} * @return {Promise<Model>}
*/ */
static findById(param, options) { static findByPk(param, options) {
// return Promise resolved with null if no arguments are passed // return Promise resolved with null if no arguments are passed
if ([null, undefined].indexOf(param) !== -1) { if ([null, undefined].indexOf(param) !== -1) {
return Promise.resolve(null); return Promise.resolve(null);
...@@ -1719,7 +1717,7 @@ class Model { ...@@ -1719,7 +1717,7 @@ class Model {
options.where = {}; options.where = {};
options.where[this.primaryKeyAttribute] = param; options.where[this.primaryKeyAttribute] = param;
} else { } else {
throw new Error('Argument passed to findById is invalid: '+param); throw new Error('Argument passed to findByPk is invalid: '+param);
} }
// Bypass a possible overloaded findOne // Bypass a possible overloaded findOne
...@@ -1740,7 +1738,7 @@ class Model { ...@@ -1740,7 +1738,7 @@ class Model {
*/ */
static findOne(options) { static findOne(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findOne must be an options object, use findById if you wish to pass a single primary key value'); throw new Error('The argument passed to findOne must be an options object, use findByPk if you wish to pass a single primary key value');
} }
options = Utils.cloneDeep(options); options = Utils.cloneDeep(options);
...@@ -1891,16 +1889,14 @@ class Model { ...@@ -1891,16 +1889,14 @@ class Model {
* ``` * ```
* Because the include for `Profile` has `required` set it will result in an inner join, and only the users who have a profile will be counted. If we remove `required` from the include, both users with and without profiles will be counted * Because the include for `Profile` has `required` set it will result in an inner join, and only the users who have a profile will be counted. If we remove `required` from the include, both users with and without profiles will be counted
* *
* __Alias__: _findAndCountAll_
*
* @param {Object} [findOptions] See findAll * @param {Object} [findOptions] See findAll
* *
* @see {@link Model.findAll} for a specification of find and query options * @see {@link Model.findAll} for a specification of find and query options
* @return {Promise<{count: Integer, rows: Model[]}>} * @return {Promise<{count: Integer, rows: Model[]}>}
*/ */
static findAndCount(options) { static findAndCountAll(options) {
if (options !== undefined && !_.isPlainObject(options)) { if (options !== undefined && !_.isPlainObject(options)) {
throw new Error('The argument passed to findAndCount must be an options object, use findById if you wish to pass a single primary key value'); throw new Error('The argument passed to findAndCountAll must be an options object, use findByPk if you wish to pass a single primary key value');
} }
const countOptions = Utils.cloneDeep(options); const countOptions = Utils.cloneDeep(options);
...@@ -2058,7 +2054,7 @@ class Model { ...@@ -2058,7 +2054,7 @@ class Model {
let values; let values;
return this.find(options).then(instance => { return this.findOne(options).then(instance => {
if (instance === null) { if (instance === null) {
values = _.clone(options.defaults) || {}; values = _.clone(options.defaults) || {};
if (_.isPlainObject(options.where)) { if (_.isPlainObject(options.where)) {
...@@ -2286,7 +2282,7 @@ class Model { ...@@ -2286,7 +2282,7 @@ class Model {
return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options); return this.QueryInterface.upsert(this.getTableName(options), insertValues, updateValues, instance.where(), this, options);
}).spread((created, primaryKey) => { }).spread((created, primaryKey) => {
if (options.returning === true && primaryKey) { if (options.returning === true && primaryKey) {
return this.findById(primaryKey, options).then(record => [record, created]); return this.findByPk(primaryKey, options).then(record => [record, created]);
} }
return created; return created;
...@@ -4206,16 +4202,39 @@ class Model { ...@@ -4206,16 +4202,39 @@ class Model {
} }
// Aliases // Aliases
Model.prototype.updateAttributes = Model.prototype.update;
Model._conformOptions = Model._conformOptions; Model._conformOptions = Model._conformOptions;
Model._validateIncludedElements = Model._validateIncludedElements; Model._validateIncludedElements = Model._validateIncludedElements;
Model._expandIncludeAll = Model._expandIncludeAll; Model._expandIncludeAll = Model._expandIncludeAll;
Model.findByPrimary = Model.findById;
Model.find = Model.findOne; Model.findByPrimary = function () {
Model.findAndCountAll = Model.findAndCount; Utils.deprecate('Model.findByPrimary has been deprecated, please use Model.findByPk instead');
Model.findOrInitialize = Model.findOrBuild; return Model.findByPk.apply(this, arguments);
Model.insertOrUpdate = Model.upsert; };
Model.findById = function () {
Utils.deprecate('Model.findById has been deprecated, please use Model.findByPk instead');
return Model.findByPk.apply(this, arguments);
};
Model.find = function () {
Utils.deprecate('Model.find has been deprecated, please use Model.findOne instead');
return Model.findOne.apply(this, arguments);
};
Model.findAndCount = function () {
Utils.deprecate('Model.findAndCount has been deprecated, please use Model.findAndCountAll instead');
return Model.findAndCountAll.apply(this, arguments);
};
Model.findOrInitialize = function () {
Utils.deprecate('Model.findOrInitialize has been deprecated, please use Model.findOrBuild instead');
return Model.findOrBuild.apply(this, arguments);
};
Model.insertOrUpdate = function () {
Utils.deprecate('Model.insertOrUpdate has been deprecated, please use Model.upsert instead');
return Model.upsert.apply(this, arguments);
};
Model.prototype.updateAttributes = function () {
Utils.deprecate('Instance.updateAttributes has been deprecated, please use Instance.update instead');
return Model.prototype.update.apply(this, arguments);
};
_.extend(Model, associationsMixin); _.extend(Model, associationsMixin);
Hooks.applyTo(Model); Hooks.applyTo(Model);
......
...@@ -941,7 +941,7 @@ class Sequelize { ...@@ -941,7 +941,7 @@ class Sequelize {
* *
* ```js * ```js
* sequelize.transaction().then(transaction => { * sequelize.transaction().then(transaction => {
* return User.find(..., {transaction}) * return User.findOne(..., {transaction})
* .then(user => user.updateAttributes(..., {transaction})) * .then(user => user.updateAttributes(..., {transaction}))
* .then(() => transaction.commit()) * .then(() => transaction.commit())
* .catch(() => transaction.rollback()); * .catch(() => transaction.rollback());
...@@ -952,7 +952,7 @@ class Sequelize { ...@@ -952,7 +952,7 @@ class Sequelize {
* *
* ```js * ```js
* sequelize.transaction(transaction => { // Note that we use a callback rather than a promise.then() * sequelize.transaction(transaction => { // Note that we use a callback rather than a promise.then()
* return User.find(..., {transaction}) * return User.findOne(..., {transaction})
* .then(user => user.updateAttributes(..., {transaction})) * .then(user => user.updateAttributes(..., {transaction}))
* }).then(() => { * }).then(() => {
* // Committed * // Committed
...@@ -1161,7 +1161,11 @@ Sequelize.prototype.literal = Sequelize.asIs = Sequelize.prototype.asIs = Sequel ...@@ -1161,7 +1161,11 @@ Sequelize.prototype.literal = Sequelize.asIs = Sequelize.prototype.asIs = Sequel
Sequelize.prototype.and = Sequelize.and; Sequelize.prototype.and = Sequelize.and;
Sequelize.prototype.or = Sequelize.or; Sequelize.prototype.or = Sequelize.or;
Sequelize.prototype.json = Sequelize.json; Sequelize.prototype.json = Sequelize.json;
Sequelize.prototype.where = Sequelize.condition = Sequelize.prototype.condition = Sequelize.where; Sequelize.prototype.where = Sequelize.where;
Sequelize.condition = Sequelize.prototype.condition = function () {
Utils.deprecate('Sequelize.condition has been deprecated, please use Sequelize.where instead');
return Sequelize.where.apply(this, arguments);
};
Sequelize.prototype.validate = Sequelize.prototype.authenticate; Sequelize.prototype.validate = Sequelize.prototype.authenticate;
/** /**
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!