* Count everything currently associated with this, using an optional where clause. * * @param {Model} instance instance * @param {object} [options] find options * @param {object} [options.where] An optional where clause to limit the associated models * @param {string|boolean} [options
(instance, options)
| 492 | * @returns {Promise<number>} |
| 493 | */ |
| 494 | async count(instance, options) { |
| 495 | const sequelize = this.target.sequelize; |
| 496 | |
| 497 | options = Utils.cloneDeep(options); |
| 498 | options.attributes = [ |
| 499 | [sequelize.fn('COUNT', sequelize.col([this.target.name, this.targetKeyField].join('.'))), 'count'] |
| 500 | ]; |
| 501 | options.joinTableAttributes = []; |
| 502 | options.raw = true; |
| 503 | options.plain = true; |
| 504 | |
| 505 | const result = await this.get(instance, options); |
| 506 | |
| 507 | return parseInt(result.count, 10); |
| 508 | } |
| 509 | |
| 510 | /** |
| 511 | * Check if one or more instance(s) are associated with this. If a list of instances is passed, the function returns true if _all_ instances are associated |