* Check if one or more rows are associated with `this`. * * @param {Model} sourceInstance the source instance * @param {Model|Model[]|string[]|string|number[]|number} [targetInstances] Can be an array of instances or their primary keys * @param {object} [options] Options passed to getAss
(sourceInstance, targetInstances, options)
| 280 | * @returns {Promise} |
| 281 | */ |
| 282 | async has(sourceInstance, targetInstances, options) { |
| 283 | const where = {}; |
| 284 | |
| 285 | if (!Array.isArray(targetInstances)) { |
| 286 | targetInstances = [targetInstances]; |
| 287 | } |
| 288 | |
| 289 | options = { |
| 290 | ...options, |
| 291 | scope: false, |
| 292 | attributes: [this.target.primaryKeyAttribute], |
| 293 | raw: true |
| 294 | }; |
| 295 | |
| 296 | where[Op.or] = targetInstances.map(instance => { |
| 297 | if (instance instanceof this.target) { |
| 298 | return instance.where(); |
| 299 | } |
| 300 | return { |
| 301 | [this.target.primaryKeyAttribute]: instance |
| 302 | }; |
| 303 | }); |
| 304 | |
| 305 | options.where = { |
| 306 | [Op.and]: [ |
| 307 | where, |
| 308 | options.where |
| 309 | ] |
| 310 | }; |
| 311 | |
| 312 | const associatedObjects = await this.get(sourceInstance, options); |
| 313 | |
| 314 | return associatedObjects.length === targetInstances.length; |
| 315 | } |
| 316 | |
| 317 | /** |
| 318 | * Set the associated models by passing an array of persisted instances or their primary keys. Everything that is not in the passed array will be un-associated |
no test coverage detected