* 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)
| 3110 | * @returns {Promise} |
| 3111 | */ |
| 3112 | static async restore(options) { |
| 3113 | if (!this._timestampAttributes.deletedAt) throw new Error('Model is not paranoid'); |
| 3114 | |
| 3115 | options = { |
| 3116 | hooks: true, |
| 3117 | individualHooks: false, |
| 3118 | ...options |
| 3119 | }; |
| 3120 | |
| 3121 | // Add CLS transaction |
| 3122 | if (options.transaction === undefined && this.sequelize.constructor._cls) { |
| 3123 | const t = this.sequelize.constructor._cls.get('transaction'); |
| 3124 | if (t) { |
| 3125 | options.transaction = t; |
| 3126 | } |
| 3127 | } |
| 3128 | |
| 3129 | options.type = QueryTypes.RAW; |
| 3130 | options.model = this; |
| 3131 | |
| 3132 | Utils.mapOptionFieldNames(options, this); |
| 3133 | |
| 3134 | // Run before hook |
| 3135 | if (options.hooks) { |
| 3136 | await this.runHooks('beforeBulkRestore', options); |
| 3137 | } |
| 3138 | |
| 3139 | let instances; |
| 3140 | // Get daos and run beforeRestore hook on each record individually |
| 3141 | if (options.individualHooks) { |
| 3142 | instances = await this.findAll({ where: options.where, transaction: options.transaction, logging: options.logging, benchmark: options.benchmark, paranoid: false }); |
| 3143 | |
| 3144 | await Promise.all(instances.map(instance => this.runHooks('beforeRestore', instance, options))); |
| 3145 | } |
| 3146 | // Run undelete query |
| 3147 | const attrValueHash = {}; |
| 3148 | const deletedAtCol = this._timestampAttributes.deletedAt; |
| 3149 | const deletedAtAttribute = this.rawAttributes[deletedAtCol]; |
| 3150 | const deletedAtDefaultValue = Object.prototype.hasOwnProperty.call(deletedAtAttribute, 'defaultValue') ? deletedAtAttribute.defaultValue : null; |
| 3151 | |
| 3152 | attrValueHash[deletedAtAttribute.field || deletedAtCol] = deletedAtDefaultValue; |
| 3153 | options.omitNull = false; |
| 3154 | const result = await this.queryInterface.bulkUpdate(this.getTableName(options), attrValueHash, options.where, options, this.rawAttributes); |
| 3155 | // Run afterDestroy hook on each record individually |
| 3156 | if (options.individualHooks) { |
| 3157 | await Promise.all( |
| 3158 | instances.map(instance => this.runHooks('afterRestore', instance, options)) |
| 3159 | ); |
| 3160 | } |
| 3161 | // Run after hook |
| 3162 | if (options.hooks) { |
| 3163 | await this.runHooks('afterBulkRestore', options); |
| 3164 | } |
| 3165 | return result; |
| 3166 | } |
| 3167 | |
| 3168 | /** |
| 3169 | * Update multiple instances that match the where options. |