MCPcopy
hub / github.com/sequelize/sequelize / whereItemQuery

Method whereItemQuery

src/dialects/abstract/query-generator.js:2478–2580  ·  view source on GitHub ↗
(key, value, options = {})

Source from the content-addressed store, hash-verified

2476 }
2477
2478 whereItemQuery(key, value, options = {}) {
2479 if (value === undefined) {
2480 throw new Error(`WHERE parameter "${key}" has invalid "undefined" value`);
2481 }
2482
2483 if (typeof key === 'string' && key.includes('.') && options.model) {
2484 const keyParts = key.split('.');
2485 if (options.model.rawAttributes[keyParts[0]] && options.model.rawAttributes[keyParts[0]].type instanceof DataTypes.JSON) {
2486 const tmp = {};
2487 const field = options.model.rawAttributes[keyParts[0]];
2488 _.set(tmp, keyParts.slice(1), value);
2489 return this.whereItemQuery(field.field || keyParts[0], tmp, { field, ...options });
2490 }
2491 }
2492
2493 const field = this._findField(key, options);
2494 const fieldType = field && field.type || options.type;
2495
2496 const isPlainObject = _.isPlainObject(value);
2497 const isArray = !isPlainObject && Array.isArray(value);
2498 key = this.OperatorsAliasMap && this.OperatorsAliasMap[key] || key;
2499 if (isPlainObject) {
2500 value = this._replaceAliases(value);
2501 }
2502 const valueKeys = isPlainObject && Utils.getComplexKeys(value);
2503
2504 if (key === undefined) {
2505 if (typeof value === 'string') {
2506 return value;
2507 }
2508
2509 if (isPlainObject && valueKeys.length === 1) {
2510 return this.whereItemQuery(valueKeys[0], value[valueKeys[0]], options);
2511 }
2512 }
2513
2514 if (value === null) {
2515 const opValue = options.bindParam ? 'NULL' : this.escape(value, field);
2516 return this._joinKeyValue(key, opValue, this.OperatorMap[Op.is], options.prefix);
2517 }
2518
2519 if (!value) {
2520 const opValue = options.bindParam ? this.format(value, field, options, options.bindParam) : this.escape(value, field);
2521 return this._joinKeyValue(key, opValue, this.OperatorMap[Op.eq], options.prefix);
2522 }
2523
2524 if (value instanceof Utils.SequelizeMethod && !(key !== undefined && value instanceof Utils.Fn)) {
2525 return this.handleSequelizeMethod(value);
2526 }
2527
2528 // Convert where: [] to Op.and if possible, else treat as literal/replacements
2529 if (key === undefined && isArray) {
2530 if (Utils.canTreatArrayAsAnd(value)) {
2531 key = Op.and;
2532 } else {
2533 throw new Error('Support for literal replacements in the `where` object has been removed.');
2534 }
2535 }

Callers 11

selectQueryMethod · 0.95
handleSequelizeMethodMethod · 0.95
whereItemsQueryMethod · 0.95
_whereBindMethod · 0.95
_whereJSONMethod · 0.95
_traverseJSONMethod · 0.95
testsqlFunction · 0.80
where.test.jsFile · 0.80
json.test.jsFile · 0.80

Calls 10

_findFieldMethod · 0.95
escapeMethod · 0.95
_joinKeyValueMethod · 0.95
formatMethod · 0.95
handleSequelizeMethodMethod · 0.95
_whereGroupBindMethod · 0.95
_whereBindMethod · 0.95
_whereJSONMethod · 0.95
setMethod · 0.65

Tested by 1

testsqlFunction · 0.64