| 3928 | } |
| 3929 | |
| 3930 | _setInclude(key, value, options) { |
| 3931 | if (!Array.isArray(value)) value = [value]; |
| 3932 | if (value[0] instanceof Model) { |
| 3933 | value = value.map(instance => instance.dataValues); |
| 3934 | } |
| 3935 | |
| 3936 | const include = this._options.includeMap[key]; |
| 3937 | const association = include.association; |
| 3938 | const accessor = key; |
| 3939 | const primaryKeyAttribute = include.model.primaryKeyAttribute; |
| 3940 | const childOptions = { |
| 3941 | isNewRecord: this.isNewRecord, |
| 3942 | include: include.include, |
| 3943 | includeNames: include.includeNames, |
| 3944 | includeMap: include.includeMap, |
| 3945 | includeValidated: true, |
| 3946 | raw: options.raw, |
| 3947 | attributes: include.originalAttributes |
| 3948 | }; |
| 3949 | let isEmpty; |
| 3950 | |
| 3951 | if (include.originalAttributes === undefined || include.originalAttributes.length) { |
| 3952 | if (association.isSingleAssociation) { |
| 3953 | if (Array.isArray(value)) { |
| 3954 | value = value[0]; |
| 3955 | } |
| 3956 | isEmpty = value && value[primaryKeyAttribute] === null || value === null; |
| 3957 | this[accessor] = this.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions); |
| 3958 | } else { |
| 3959 | isEmpty = value[0] && value[0][primaryKeyAttribute] === null; |
| 3960 | this[accessor] = this.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions); |
| 3961 | } |
| 3962 | } |
| 3963 | } |
| 3964 | |
| 3965 | /** |
| 3966 | * Validates this instance, and if the validation passes, persists it to the database. |