Skip to content

Commit

Permalink
refactor: remove aliases (#9933)
Browse files Browse the repository at this point in the history
  • Loading branch information
frlinw authored and sushantdhiman committed Sep 19, 2018
1 parent aa92764 commit b37985d
Show file tree
Hide file tree
Showing 207 changed files with 1,102 additions and 1,223 deletions.
8 changes: 4 additions & 4 deletions docs/hooks.md
Original file line number Diff line number Diff line change
Expand Up @@ -65,13 +65,13 @@ const User = sequelize.define('user', {
}
});

// Method 2 via the .hook() method (or its alias .addHook() method)
User.hook('beforeValidate', (user, options) => {
// Method 2 via the .addHook() method
User.addHook('beforeValidate', (user, options) => {
user.mood = 'happy';
});

User.addHook('afterValidate', 'someCustomName', (user, options) => {
return sequelize.Promise.reject(new Error("I'm afraid I can't let you do that!"));
return Promise.reject(new Error("I'm afraid I can't let you do that!"));
});

// Method 3 via the direct method
Expand Down Expand Up @@ -316,7 +316,7 @@ Note that many model operations in Sequelize allow you to specify a transaction
```js
// Here we use the promise-style of async hooks rather than
// the callback.
User.hook('afterCreate', (user, options) => {
User.addHook('afterCreate', (user, options) => {
// 'transaction' will be available in options.transaction

// This operation will be part of the same transaction as the
Expand Down
12 changes: 6 additions & 6 deletions docs/instances.md
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ In order to increment values of an instance without running into concurrency iss
First of all you can define a field and the value you want to add to it.

```js
User.findById(1).then(user => {
User.findByPk(1).then(user => {
return user.increment('my-integer-field', {by: 2})
}).then(user => {
// Postgres will return the updated user by default (unless disabled by setting { returning: false })
Expand All @@ -330,15 +330,15 @@ User.findById(1).then(user => {
Second, you can define multiple fields and the value you want to add to them.

```js
User.findById(1).then(user => {
User.findByPk(1).then(user => {
return user.increment([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}).then(/* ... */)
```

Third, you can define an object containing fields and its increment values.

```js
User.findById(1).then(user => {
User.findByPk(1).then(user => {
return user.increment({
'my-integer-field': 2,
'my-very-other-field': 3
Expand All @@ -353,7 +353,7 @@ In order to decrement values of an instance without running into concurrency iss
First of all you can define a field and the value you want to add to it.

```js
User.findById(1).then(user => {
User.findByPk(1).then(user => {
return user.decrement('my-integer-field', {by: 2})
}).then(user => {
// Postgres will return the updated user by default (unless disabled by setting { returning: false })
Expand All @@ -364,15 +364,15 @@ User.findById(1).then(user => {
Second, you can define multiple fields and the value you want to add to them.

```js
User.findById(1).then(user => {
User.findByPk(1).then(user => {
return user.decrement([ 'my-integer-field', 'my-very-other-field' ], {by: 2})
}).then(/* ... */)
```

Third, you can define an object containing fields and its decrement values.

```js
User.findById(1).then(user => {
User.findByPk(1).then(user => {
return user.decrement({
'my-integer-field': 2,
'my-very-other-field': 3
Expand Down
2 changes: 1 addition & 1 deletion docs/models-usage.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ In this document we'll explore what finder methods can do:
### `find` - Search for one specific element in the database
```js
// search for known ids
Project.findById(123).then(project => {
Project.findByPk(123).then(project => {
// project will be an instance of Project and stores the content of the table entry
// with id 123. if such an entry is not defined you will get null
})
Expand Down
2 changes: 1 addition & 1 deletion docs/transactions.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ sequelize.transaction({
## Usage with other sequelize methods

The `transaction` option goes with most other options, which are usually the first argument of a method.
For methods that take values, like `.create`, `.update()`, `.updateAttributes()` etc. `transaction` should be passed to the option in the second argument.
For methods that take values, like `.create`, `.update()`, etc. `transaction` should be passed to the option in the second argument.
If unsure, refer to the API documentation for the method you are using to be sure of the signature.

## After commit hook
Expand Down
2 changes: 1 addition & 1 deletion lib/associations/belongs-to.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ class BelongsTo extends Association {
};
} else {
if (this.targetKeyIsPrimary && !options.where) {
return Target.findById(instance.get(this.foreignKey), options);
return Target.findByPk(instance.get(this.foreignKey), options);
} else {
where[this.targetKey] = instance.get(this.foreignKey);
options.limit = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/associations/mixin.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const BelongsTo = require('./belongs-to');
function isModel(model, sequelize) {
return model
&& model.prototype
&& model.prototype instanceof sequelize.Model;
&& model.prototype instanceof sequelize.Sequelize.Model;
}

const Mixin = {
Expand Down
1 change: 0 additions & 1 deletion lib/data-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -1230,7 +1230,6 @@ const DataTypes = module.exports = {
JSONB,
VIRTUAL,
ARRAY,
NONE: VIRTUAL,
ENUM,
RANGE,
REAL,
Expand Down
10 changes: 5 additions & 5 deletions lib/dialects/abstract/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -1827,7 +1827,7 @@ class QueryGenerator {
model: topInclude.through.model,
where: {
[Op.and]: [
this.sequelize.asIs([
this.sequelize.literal([
this.quoteTable(topParent.model.name) + '.' + this.quoteIdentifier(topParent.model.primaryKeyField),
this.quoteIdentifier(topInclude.through.model.name) + '.' + this.quoteIdentifier(topAssociation.identifierField)
].join(' = ')),
Expand All @@ -1839,8 +1839,8 @@ class QueryGenerator {
}, topInclude.through.model);
} else {
const isBelongsTo = topAssociation.associationType === 'BelongsTo';
const sourceField = isBelongsTo ? topAssociation.identifierField : (topAssociation.sourceKeyField || topParent.model.primaryKeyField);
const targetField = isBelongsTo ? (topAssociation.sourceKeyField || topInclude.model.primaryKeyField) : topAssociation.identifierField;
const sourceField = isBelongsTo ? topAssociation.identifierField : topAssociation.sourceKeyField || topParent.model.primaryKeyField;
const targetField = isBelongsTo ? topAssociation.sourceKeyField || topInclude.model.primaryKeyField : topAssociation.identifierField;

const join = [
this.quoteIdentifier(topInclude.as) + '.' + this.quoteIdentifier(targetField),
Expand All @@ -1854,7 +1854,7 @@ class QueryGenerator {
where: {
[Op.and]: [
topInclude.where,
{ [Op.join]: this.sequelize.asIs(join) }
{ [Op.join]: this.sequelize.literal(join) }
]
},
limit: 1,
Expand All @@ -1867,7 +1867,7 @@ class QueryGenerator {
topLevelInfo.options.where[Op.and] = [];
}

topLevelInfo.options.where[`__${includeAs.internalAs}`] = this.sequelize.asIs([
topLevelInfo.options.where[`__${includeAs.internalAs}`] = this.sequelize.literal([
'(',
query.replace(/\;$/, ''),
')',
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/mysql/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,7 @@ class MySQLQueryGenerator extends AbstractQueryGenerator {
'REFERENCED_TABLE_SCHEMA as referencedTableSchema',
'REFERENCED_TABLE_SCHEMA as referencedTableCatalog',
'REFERENCED_TABLE_NAME as referencedTableName',
'REFERENCED_COLUMN_NAME as referencedColumnName',
'REFERENCED_COLUMN_NAME as referencedColumnName'
].join(',');
}

Expand Down
3 changes: 2 additions & 1 deletion lib/dialects/mysql/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
*/

const _ = require('lodash');
const Promise = require('../../promise');
const sequelizeErrors = require('../../errors');

/**
Expand Down Expand Up @@ -36,7 +37,7 @@ function removeColumn(tableName, columnName, options) {
// No foreign key constraints found, so we can remove the column
return;
}
return this.sequelize.Promise.map(results, constraint => this.sequelize.query(
return Promise.map(results, constraint => this.sequelize.query(
this.QueryGenerator.dropForeignKeyQuery(tableName, constraint.constraint_name),
_.assign({ raw: true }, options)
));
Expand Down
2 changes: 1 addition & 1 deletion lib/dialects/postgres/query-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -473,7 +473,7 @@ class PostgresQueryGenerator extends AbstractQueryGenerator {
let type;
if (
attribute.type instanceof DataTypes.ENUM ||
(attribute.type instanceof DataTypes.ARRAY && attribute.type.type instanceof DataTypes.ENUM)
attribute.type instanceof DataTypes.ARRAY && attribute.type.type instanceof DataTypes.ENUM
) {
const enumType = attribute.type.type || attribute.type;
let values = attribute.values;
Expand Down
4 changes: 2 additions & 2 deletions lib/dialects/postgres/query-interface.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ function ensureEnums(tableName, attributes, options, model) {

if (
type instanceof DataTypes.ENUM ||
(type instanceof DataTypes.ARRAY && type.type instanceof DataTypes.ENUM) //ARRAY sub type is ENUM
type instanceof DataTypes.ARRAY && type.type instanceof DataTypes.ENUM //ARRAY sub type is ENUM
) {
sql = this.QueryGenerator.pgListEnums(tableName, attribute.field || keys[i], options);
promises.push(this.sequelize.query(
Expand All @@ -60,7 +60,7 @@ function ensureEnums(tableName, attributes, options, model) {

if (
type instanceof DataTypes.ENUM ||
(type instanceof DataTypes.ARRAY && enumType instanceof DataTypes.ENUM) //ARRAY sub type is ENUM
type instanceof DataTypes.ARRAY && enumType instanceof DataTypes.ENUM //ARRAY sub type is ENUM
) {
// If the enum type doesn't exist then create it
if (!results[enumIdx]) {
Expand Down
3 changes: 1 addition & 2 deletions lib/dialects/sqlite/query.js
Original file line number Diff line number Diff line change
Expand Up @@ -424,9 +424,8 @@ class Query extends AbstractQuery {
}

handleShowIndexesQuery(data) {

// Sqlite returns indexes so the one that was defined last is returned first. Lets reverse that!
return this.sequelize.Promise.map(data.reverse(), item => {
return Promise.map(data.reverse(), item => {
item.fields = [];
item.primary = false;
item.unique = !!item.unique;
Expand Down
2 changes: 1 addition & 1 deletion lib/errors/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* Sequelize provides a host of custom error classes, to allow you to do easier debugging. All of these errors are exposed on the sequelize object and the sequelize constructor.
* All sequelize errors inherit from the base JS error object.
*
* This means that errors can be accessed using `Sequelize.ValidationError` or `sequelize.ValidationError`
* This means that errors can be accessed using `Sequelize.ValidationError`
* The Base Error all Sequelize Errors inherit from.
*
* @extends Error
Expand Down
22 changes: 1 addition & 21 deletions lib/hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,6 @@ const hookTypes = {
};
exports.hooks = hookTypes;

const hookAliases = {
beforeDelete: 'beforeDestroy',
afterDelete: 'afterDestroy',
beforeBulkDelete: 'beforeBulkDestroy',
afterBulkDelete: 'afterBulkDestroy',
beforeConnection: 'beforeConnect'
};
exports.hookAliases = hookAliases;

/**
* get array of current hook and its proxies combined
Expand Down Expand Up @@ -137,10 +129,6 @@ const Hooks = {
}).return();
},

hook() {
return Hooks.addHook.apply(this, arguments);
},

/**
* Add a hook to the model
*
Expand All @@ -158,8 +146,6 @@ const Hooks = {
}

debug(`adding hook ${hookType}`);
hookType = hookAliases[hookType] || hookType;

// check for proxies, add them too
hookType = getProxiedHooks(hookType);

Expand All @@ -181,7 +167,6 @@ const Hooks = {
* @memberof Sequelize.Model
*/
removeHook(hookType, name) {
hookType = hookAliases[hookType] || hookType;
const isReference = typeof name === 'function' ? true : false;

if (!this.hasHook(hookType)) {
Expand Down Expand Up @@ -227,8 +212,7 @@ Hooks.hasHooks = Hooks.hasHook;
function applyTo(target) {
_.mixin(target, Hooks);

const allHooks = Object.keys(hookTypes).concat(Object.keys(hookAliases));
for (const hook of allHooks) {
for (const hook of Object.keys(hookTypes)) {
target[hook] = function(name, callback) {
return this.addHook(hook, name, callback);
};
Expand Down Expand Up @@ -315,7 +299,6 @@ exports.applyTo = applyTo;
* @param {Function} fn A callback function that is called with instance, options
*
* @name beforeDestroy
* @alias beforeDelete
* @memberof Sequelize.Model
*/

Expand All @@ -325,7 +308,6 @@ exports.applyTo = applyTo;
* @param {Function} fn A callback function that is called with instance, options
*
* @name afterDestroy
* @alias afterDelete
* @memberof Sequelize.Model
*/

Expand Down Expand Up @@ -385,7 +367,6 @@ exports.applyTo = applyTo;
* @param {Function} fn A callback function that is called with options
*
* @name beforeBulkDestroy
* @alias beforeBulkDelete
* @memberof Sequelize.Model
*/

Expand All @@ -395,7 +376,6 @@ exports.applyTo = applyTo;
* @param {Function} fn A callback function that is called with options
*
* @name afterBulkDestroy
* @alias afterBulkDelete
* @memberof Sequelize.Model
*/

Expand Down
Loading

0 comments on commit b37985d

Please sign in to comment.