* Get everything currently associated with this, using an optional where clause. * * @see * {@link Model} for a full explanation of options * * @param {Model} instance instance * @param {object} [options] find options * @param {object} [options.where] An optional where clause to
(instance, options)
| 423 | * @returns {Promise<Array<Model>>} |
| 424 | */ |
| 425 | async get(instance, options) { |
| 426 | options = Utils.cloneDeep(options) || {}; |
| 427 | |
| 428 | const through = this.through; |
| 429 | let scopeWhere; |
| 430 | let throughWhere; |
| 431 | |
| 432 | if (this.scope) { |
| 433 | scopeWhere = { ...this.scope }; |
| 434 | } |
| 435 | |
| 436 | options.where = { |
| 437 | [Op.and]: [ |
| 438 | scopeWhere, |
| 439 | options.where |
| 440 | ] |
| 441 | }; |
| 442 | |
| 443 | if (Object(through.model) === through.model) { |
| 444 | throughWhere = {}; |
| 445 | throughWhere[this.foreignKey] = instance.get(this.sourceKey); |
| 446 | |
| 447 | if (through.scope) { |
| 448 | Object.assign(throughWhere, through.scope); |
| 449 | } |
| 450 | |
| 451 | //If a user pass a where on the options through options, make an "and" with the current throughWhere |
| 452 | if (options.through && options.through.where) { |
| 453 | throughWhere = { |
| 454 | [Op.and]: [throughWhere, options.through.where] |
| 455 | }; |
| 456 | } |
| 457 | |
| 458 | options.include = options.include || []; |
| 459 | options.include.push({ |
| 460 | association: this.oneFromTarget, |
| 461 | attributes: options.joinTableAttributes, |
| 462 | required: true, |
| 463 | paranoid: _.get(options.through, 'paranoid', true), |
| 464 | where: throughWhere |
| 465 | }); |
| 466 | } |
| 467 | |
| 468 | let model = this.target; |
| 469 | if (Object.prototype.hasOwnProperty.call(options, 'scope')) { |
| 470 | if (!options.scope) { |
| 471 | model = model.unscoped(); |
| 472 | } else { |
| 473 | model = model.scope(options.scope); |
| 474 | } |
| 475 | } |
| 476 | |
| 477 | if (Object.prototype.hasOwnProperty.call(options, 'schema')) { |
| 478 | model = model.schema(options.schema, options.schemaDelimiter); |
| 479 | } |
| 480 | |
| 481 | return model.findAll(options); |
| 482 | } |