| 
									
										
										
										
											2020-04-27 19:54:57 +02:00
										 |  |  | 'use strict'; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | const _ = require('lodash'); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-04 11:16:19 +02:00
										 |  |  | const executeLifecycle = async (lifecycle, model, ...args) => { | 
					
						
							| 
									
										
										
										
											2021-02-16 12:08:35 +01:00
										 |  |  |   // Run registered lifecycles
 | 
					
						
							|  |  |  |   await strapi.db.lifecycles.run(lifecycle, model, ...args); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   // Run user lifecycles
 | 
					
						
							| 
									
										
										
										
											2020-04-27 19:54:57 +02:00
										 |  |  |   if (_.has(model, `lifecycles.${lifecycle}`)) { | 
					
						
							|  |  |  |     await model.lifecycles[lifecycle](...args); | 
					
						
							|  |  |  |   } | 
					
						
							|  |  |  | }; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-04 11:16:19 +02:00
										 |  |  | const executeBeforeLifecycle = (lifecycle, model, ...args) => | 
					
						
							|  |  |  |   executeLifecycle(`before${_.upperFirst(lifecycle)}`, model, ...args); | 
					
						
							| 
									
										
										
										
											2020-04-27 19:54:57 +02:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2020-05-04 11:16:19 +02:00
										 |  |  | const executeAfterLifecycle = (lifecycle, model, ...args) => | 
					
						
							|  |  |  |   executeLifecycle(`after${_.upperFirst(lifecycle)}`, model, ...args); | 
					
						
							| 
									
										
										
										
											2020-04-27 19:54:57 +02:00
										 |  |  | 
 | 
					
						
							|  |  |  | module.exports = { | 
					
						
							| 
									
										
										
										
											2020-05-04 11:16:19 +02:00
										 |  |  |   executeBeforeLifecycle, | 
					
						
							|  |  |  |   executeAfterLifecycle, | 
					
						
							| 
									
										
										
										
											2020-04-27 19:54:57 +02:00
										 |  |  | }; |