* Validates this instance, and if the validation passes, persists it to the database. * * Returns a Promise that resolves to the saved instance (or rejects with a `Sequelize.ValidationError`, which will have a property for each of the fields for which the validation failed, with the error mess
(options)
| 3858 | * @returns {Promise<Model>} |
| 3859 | */ |
| 3860 | async save(options) { |
| 3861 | if (arguments.length > 1) { |
| 3862 | throw new Error(class="st">'The second argument was removed in favor of the options object.'); |
| 3863 | } |
| 3864 | |
| 3865 | options = Utils.cloneDeep(options); |
| 3866 | options = _.defaults(options, { |
| 3867 | hooks: true, |
| 3868 | validate: true |
| 3869 | }); |
| 3870 | |
| 3871 | if (!options.fields) { |
| 3872 | if (this.isNewRecord) { |
| 3873 | options.fields = Object.keys(this.constructor.rawAttributes); |
| 3874 | } else { |
| 3875 | options.fields = _.intersection(this.changed(), Object.keys(this.constructor.rawAttributes)); |
| 3876 | } |
| 3877 | |
| 3878 | options.defaultFields = options.fields; |
| 3879 | } |
| 3880 | |
| 3881 | if (options.returning === undefined) { |
| 3882 | if (options.association) { |
| 3883 | options.returning = false; |
| 3884 | } else if (this.isNewRecord) { |
| 3885 | options.returning = true; |
| 3886 | } |
| 3887 | } |
| 3888 | |
| 3889 | const primaryKeyName = this.constructor.primaryKeyAttribute; |
| 3890 | const primaryKeyAttribute = primaryKeyName && this.constructor.rawAttributes[primaryKeyName]; |
| 3891 | const createdAtAttr = this.constructor._timestampAttributes.createdAt; |
| 3892 | const versionAttr = this.constructor._versionAttribute; |
| 3893 | const hook = this.isNewRecord ? class="st">'Create' : class="st">'Update'; |
| 3894 | const wasNewRecord = this.isNewRecord; |
| 3895 | const now = Utils.now(this.sequelize.options.dialect); |
| 3896 | let updatedAtAttr = this.constructor._timestampAttributes.updatedAt; |
| 3897 | |
| 3898 | if (updatedAtAttr && options.fields.length > 0 && !options.fields.includes(updatedAtAttr)) { |
| 3899 | options.fields.push(updatedAtAttr); |
| 3900 | } |
| 3901 | if (versionAttr && options.fields.length > 0 && !options.fields.includes(versionAttr)) { |
| 3902 | options.fields.push(versionAttr); |
| 3903 | } |
| 3904 | |
| 3905 | if (options.silent === true && !(this.isNewRecord && this.get(updatedAtAttr, { raw: true }))) { |
| 3906 | class="cm">// UpdateAtAttr might have been added as a result of Object.keys(Model.rawAttributes). In that case we have to remove it again |
| 3907 | _.remove(options.fields, val => val === updatedAtAttr); |
| 3908 | updatedAtAttr = false; |
| 3909 | } |
| 3910 | |
| 3911 | if (this.isNewRecord === true) { |
| 3912 | if (createdAtAttr && !options.fields.includes(createdAtAttr)) { |
| 3913 | options.fields.push(createdAtAttr); |
| 3914 | } |
| 3915 | |
| 3916 | if (primaryKeyAttribute && primaryKeyAttribute.defaultValue && !options.fields.includes(primaryKeyName)) { |
| 3917 | options.fields.unshift(primaryKeyName); |