MCPcopy
hub / github.com/sequelize/sequelize / _setInclude

Method _setInclude

lib/model.js:3804–3837  ·  view source on GitHub ↗
(key, value, options)

Source from the content-addressed store, hash-verified

3802 }
3803
3804 _setInclude(key, value, options) {
3805 if (!Array.isArray(value)) value = [value];
3806 if (value[0] instanceof Model) {
3807 value = value.map(instance => instance.dataValues);
3808 }
3809
3810 const include = this._options.includeMap[key];
3811 const association = include.association;
3812 const accessor = key;
3813 const primaryKeyAttribute = include.model.primaryKeyAttribute;
3814 const childOptions = {
3815 isNewRecord: this.isNewRecord,
3816 include: include.include,
3817 includeNames: include.includeNames,
3818 includeMap: include.includeMap,
3819 includeValidated: true,
3820 raw: options.raw,
3821 attributes: include.originalAttributes
3822 };
3823 let isEmpty;
3824
3825 if (include.originalAttributes === undefined || include.originalAttributes.length) {
3826 if (association.isSingleAssociation) {
3827 if (Array.isArray(value)) {
3828 value = value[0];
3829 }
3830 isEmpty = value && value[primaryKeyAttribute] === null || value === null;
3831 this[accessor] = this.dataValues[accessor] = isEmpty ? null : include.model.build(value, childOptions);
3832 } else {
3833 isEmpty = value[0] && value[0][primaryKeyAttribute] === null;
3834 this[accessor] = this.dataValues[accessor] = isEmpty ? [] : include.model.bulkBuild(value, childOptions);
3835 }
3836 }
3837 }
3838
3839 /**
3840 * Validates this instance, and if the validation passes, persists it to the database.

Callers 1

setMethod · 0.95

Calls 2

buildMethod · 0.80
bulkBuildMethod · 0.80

Tested by

no test coverage detected