MCPcopy
hub / github.com/sequelize/sequelize / _initValues

Method _initValues

src/model.js:145–196  ·  view source on GitHub ↗
(values, options)

Source from the content-addressed store, hash-verified

143 }
144
145 _initValues(values, options) {
146 let defaults;
147 let key;
148
149 values = { ...values };
150
151 if (options.isNewRecord) {
152 defaults = {};
153
154 if (this.constructor._hasDefaultValues) {
155 defaults = _.mapValues(this.constructor._defaultValues, valueFn => {
156 const value = valueFn();
157 return value && value instanceof Utils.SequelizeMethod ? value : _.cloneDeep(value);
158 });
159 }
160
161 // set id to null if not passed as value, a newly created dao has no id
162 // removing this breaks bulkCreate
163 // do after default values since it might have UUID as a default value
164 if (this.constructor.primaryKeyAttributes.length) {
165 this.constructor.primaryKeyAttributes.forEach(primaryKeyAttribute => {
166 if (!Object.prototype.hasOwnProperty.call(defaults, primaryKeyAttribute)) {
167 defaults[primaryKeyAttribute] = null;
168 }
169 });
170 }
171
172 if (this.constructor._timestampAttributes.createdAt && defaults[this.constructor._timestampAttributes.createdAt]) {
173 this.dataValues[this.constructor._timestampAttributes.createdAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.createdAt], this.sequelize.options.dialect);
174 delete defaults[this.constructor._timestampAttributes.createdAt];
175 }
176
177 if (this.constructor._timestampAttributes.updatedAt && defaults[this.constructor._timestampAttributes.updatedAt]) {
178 this.dataValues[this.constructor._timestampAttributes.updatedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.updatedAt], this.sequelize.options.dialect);
179 delete defaults[this.constructor._timestampAttributes.updatedAt];
180 }
181
182 if (this.constructor._timestampAttributes.deletedAt && defaults[this.constructor._timestampAttributes.deletedAt]) {
183 this.dataValues[this.constructor._timestampAttributes.deletedAt] = Utils.toDefaultValue(defaults[this.constructor._timestampAttributes.deletedAt], this.sequelize.options.dialect);
184 delete defaults[this.constructor._timestampAttributes.deletedAt];
185 }
186
187 for (key in defaults) {
188 if (values[key] === undefined) {
189 this.set(key, Utils.toDefaultValue(defaults[key], this.sequelize.options.dialect), { raw: true });
190 delete values[key];
191 }
192 }
193 }
194
195 this.set(values, options);
196 }
197
198 // validateIncludedElements should have been called before this method
199 static _paranoidClause(model, options = {}) {

Callers 1

constructorMethod · 0.95

Calls 1

setMethod · 0.95

Tested by

no test coverage detected