* Execute a query on the DB, optionally bypassing all the Sequelize goodness. * * By default, the function will return two arguments: an array of results, and a metadata object, containing number of affected rows etc. * * If you are running a type of query where you don't need the metada
(sql, options)
| 538 | */ |
| 539 | |
| 540 | async query(sql, options) { |
| 541 | options = { ...this.options.query, ...options }; |
| 542 | |
| 543 | if (options.instance && !options.model) { |
| 544 | options.model = options.instance.constructor; |
| 545 | } |
| 546 | |
| 547 | if (!options.instance && !options.model) { |
| 548 | options.raw = true; |
| 549 | } |
| 550 | |
| 551 | // map raw fields to model attributes |
| 552 | if (options.mapToModel) { |
| 553 | options.fieldMap = _.get(options, 'model.fieldAttributeMap', {}); |
| 554 | } |
| 555 | |
| 556 | options = _.defaults(options, { |
| 557 | // eslint-disable-next-line no-console |
| 558 | logging: Object.prototype.hasOwnProperty.call(this.options, 'logging') ? this.options.logging : console.log, |
| 559 | searchPath: Object.prototype.hasOwnProperty.call(this.options, 'searchPath') ? this.options.searchPath : 'DEFAULT' |
| 560 | }); |
| 561 | |
| 562 | if (!options.type) { |
| 563 | if (options.model || options.nest || options.plain) { |
| 564 | options.type = QueryTypes.SELECT; |
| 565 | } else { |
| 566 | options.type = QueryTypes.RAW; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | //if dialect doesn't support search_path or dialect option |
| 571 | //to prepend searchPath is not true delete the searchPath option |
| 572 | if ( |
| 573 | !this.dialect.supports.searchPath || |
| 574 | !this.options.dialectOptions || |
| 575 | !this.options.dialectOptions.prependSearchPath || |
| 576 | options.supportsSearchPath === false |
| 577 | ) { |
| 578 | delete options.searchPath; |
| 579 | } else if (!options.searchPath) { |
| 580 | //if user wants to always prepend searchPath (dialectOptions.preprendSearchPath = true) |
| 581 | //then set to DEFAULT if none is provided |
| 582 | options.searchPath = 'DEFAULT'; |
| 583 | } |
| 584 | |
| 585 | if (typeof sql === 'object') { |
| 586 | if (sql.values !== undefined) { |
| 587 | if (options.replacements !== undefined) { |
| 588 | throw new Error('Both `sql.values` and `options.replacements` cannot be set at the same time'); |
| 589 | } |
| 590 | options.replacements = sql.values; |
| 591 | } |
| 592 | |
| 593 | if (sql.bind !== undefined) { |
| 594 | if (options.bind !== undefined) { |
| 595 | throw new Error('Both `sql.bind` and `options.bind` cannot be set at the same time'); |
| 596 | } |
| 597 | options.bind = sql.bind; |
no test coverage detected