* Restore multiple instances if `paranoid` is enabled. * * @param {object} options restore options * @param {object} [options.where] Filter the restore * @param {boolean} [options.hooks=true] Run before / after bulk r
(options)
| 3000 | * @returns {Promise} |
| 3001 | */ |
| 3002 | static async restore(options) { |
| 3003 | if (!this._timestampAttributes.deletedAt) throw new Error('Model is not paranoid'); |
| 3004 | |
| 3005 | options = { |
| 3006 | hooks: true, |
| 3007 | individualHooks: false, |
| 3008 | ...options |
| 3009 | }; |
| 3010 | |
| 3011 | options.type = QueryTypes.RAW; |
| 3012 | options.model = this; |
| 3013 | |
| 3014 | Utils.mapOptionFieldNames(options, this); |
| 3015 | |
| 3016 | // Run before hook |
| 3017 | if (options.hooks) { |
| 3018 | await this.runHooks('beforeBulkRestore', options); |
| 3019 | } |
| 3020 | |
| 3021 | let instances; |
| 3022 | // Get daos and run beforeRestore hook on each record individually |
| 3023 | if (options.individualHooks) { |
| 3024 | instances = await this.findAll({ where: options.where, transaction: options.transaction, logging: options.logging, benchmark: options.benchmark, paranoid: false }); |
| 3025 | |
| 3026 | await Promise.all(instances.map(instance => this.runHooks('beforeRestore', instance, options))); |
| 3027 | } |
| 3028 | // Run undelete query |
| 3029 | const attrValueHash = {}; |
| 3030 | const deletedAtCol = this._timestampAttributes.deletedAt; |
| 3031 | const deletedAtAttribute = this.rawAttributes[deletedAtCol]; |
| 3032 | const deletedAtDefaultValue = Object.prototype.hasOwnProperty.call(deletedAtAttribute, 'defaultValue') ? deletedAtAttribute.defaultValue : null; |
| 3033 | |
| 3034 | attrValueHash[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue; |
| 3035 | options.omitNull = false; |
| 3036 | const result = await this.queryInterface.bulkUpdate(this.getTableName(options), attrValueHash, options.where, options, this.rawAttributes); |
| 3037 | // Run afterDestroy hook on each record individually |
| 3038 | if (options.individualHooks) { |
| 3039 | await Promise.all( |
| 3040 | instances.map(instance => this.runHooks('afterRestore', instance, options)) |
| 3041 | ); |
| 3042 | } |
| 3043 | // Run after hook |
| 3044 | if (options.hooks) { |
| 3045 | await this.runHooks('afterBulkRestore', options); |
| 3046 | } |
| 3047 | return result; |
| 3048 | } |
| 3049 | |
| 3050 | /** |
| 3051 | * Update multiple instances that match the where options. |
no test coverage detected