* Count everything currently associated with this, using an optional where clause. * * @param {Model} instance the source instance * @param {object} [options] find & count options * @param {object} [options.where] An optional where clause to limit the associated mo
(instance, options)
| 251 | * @returns {Promise<number>} |
| 252 | */ |
| 253 | async count(instance, options) { |
| 254 | options = Utils.cloneDeep(options); |
| 255 | |
| 256 | options.attributes = [ |
| 257 | [ |
| 258 | this.sequelize.fn( |
| 259 | 'COUNT', |
| 260 | this.sequelize.col(`${this.target.name}.${this.target.primaryKeyField}`) |
| 261 | ), |
| 262 | 'count' |
| 263 | ] |
| 264 | ]; |
| 265 | options.raw = true; |
| 266 | options.plain = true; |
| 267 | |
| 268 | const result = await this.get(instance, options); |
| 269 | |
| 270 | return parseInt(result.count, 10); |
| 271 | } |
| 272 | |
| 273 | /** |
| 274 | * Check if one or more rows are associated with `this`. |