Skip to content

Commit

Permalink
feat: rewrite the part of QueryGenerator responsible for WHERE (#15598)
Browse files Browse the repository at this point in the history
- The `Op.not` operator *always* means `NOT (x)`. If you need `IS NOT`, use `Op.isNot`, if you need `!=`, use `Op.ne`.
  This should not impact you much however, because writing `{ [Op.not]: 1 }` will be interpreted as `{ [Op.not]: { [Op.eq]: 1 } }` and will result in `NOT (x = 1)` instead of `x != 1`
- Removed the ability to use string operators in `where` (use the `sql` tag for that)
- Removed operator aliases (they were deprecated since Sequelize 5)
- Removed the ability to use an attribute object (one of the values of `Model.rawAttributes`) as one of the operands of `where`, it made little sense, was undocumented, and complicated the code.
- `or([])` & `or({})` produce `''` instead of `'0=1'`
- `not({})` produces `''` instead of `'0=1'`
- JSON operations now produce JSON strings (`->` operator) instead of unquoting them (`->>` operator) to fix #15238
- The `Json` class, that is produced by `json()` has been removed. `json()` now returns either `Where` or `Attribute` depending on its parameters.
- `json()` only accepts the sequelize JSON notation, it will not accept the dialect-specific syntaxes, or `json()` functions anymore. Use the `sql` tag, `literal` or `fn` for that
- Array values in replacements are not escaped as SQL Lists anymore, but as SQL arrays. If the user wishes to use a list, they need to wrap their array with the new `list()` method
- The signature of the `escape` method has changed. `escape` is now able to handle any SQL value (dynamic like `fn` or literal like `'a string'`) and accepts a `bindParam` option which can be used to make it add bind parameters instead of escaping
- Removed the ability to pass the value of the primary key directly to `where`. `User.findOne({ where: 1 })` will throw. Use `User.findByPk(1)` instead.
- `SequelizeMethod` has been renamed to `BaseSqlExpression`
  • Loading branch information
ephys committed Mar 12, 2023
1 parent 5c42821 commit 50898ca
Show file tree
Hide file tree
Showing 103 changed files with 5,616 additions and 6,849 deletions.
1 change: 1 addition & 0 deletions packages/core/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@
"dependencies": {
"@types/debug": "^4.1.7",
"@types/validator": "^13.7.5",
"bnf-parser": "^3.1.1",
"dayjs": "^1.11.5",
"debug": "^4.3.4",
"dottie": "^2.0.2",
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/associations/belongs-to-many.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import each from 'lodash/each';
import isEqual from 'lodash/isEqual';
import omit from 'lodash/omit';
import upperFirst from 'lodash/upperFirst';
import type { WhereOptions } from '../dialects/abstract/where-sql-builder-types.js';
import { AssociationError } from '../errors';
import { col } from '../expression-builders/col.js';
import { fn } from '../expression-builders/fn.js';
Expand All @@ -23,7 +24,6 @@ import type {
ModelStatic,
Transactionable,
UpdateOptions,
WhereOptions,
} from '../model';
import { Op } from '../operators';
import type { Sequelize } from '../sequelize';
Expand Down
2 changes: 1 addition & 1 deletion packages/core/src/associations/has-many.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import upperFirst from 'lodash/upperFirst';
import type { WhereOptions } from '../dialects/abstract/where-sql-builder-types.js';
import { AssociationError } from '../errors/index.js';
import { col } from '../expression-builders/col.js';
import { fn } from '../expression-builders/fn.js';
Expand All @@ -12,7 +13,6 @@ import type {
Transactionable,
ModelStatic,
AttributeNames, UpdateValues, Attributes,
WhereOptions,
} from '../model';
import { Op } from '../operators';
import { isPlainObject } from '../utils/check.js';
Expand Down
15 changes: 15 additions & 0 deletions packages/core/src/dialects/abstract/data-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,14 @@ export abstract class AbstractDataType<
return isEqual(value, originalValue);
}

/**
* Whether this DataType wishes to handle NULL values itself.
* This is almost exclusively used by {@link JSON} and {@link JSONB} which serialize `null` as the JSON string `'null'`.
*/
acceptsNull(): boolean {
return false;
}

/**
* Called when a value is retrieved from the Database, and its DataType is specified.
* Used to normalize values from the database.
Expand Down Expand Up @@ -1605,6 +1613,13 @@ export class JSON extends AbstractDataType<any> {
}
}

/**
* We stringify null too.
*/
acceptsNull(): boolean {
return true;
}

toBindableValue(value: any): string {
return globalThis.JSON.stringify(value);
}
Expand Down
Loading

0 comments on commit 50898ca

Please sign in to comment.