mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 01:47:13 +00:00 
			
		
		
		
	Use a wrapper to cleanup
Signed-off-by: Alexandre Bodin <bodin.alex@gmail.com>
This commit is contained in:
		
							parent
							
								
									de5d1d6d54
								
							
						
					
					
						commit
						9ddeaffed1
					
				| @ -5,62 +5,52 @@ | |||||||
|  */ |  */ | ||||||
| 
 | 
 | ||||||
| module.exports = { | module.exports = { | ||||||
|   // Before saving a value.
 |   beforeCreate(...args) { | ||||||
|   // Fired before an `insert` or `update` query.
 |  | ||||||
|   // beforeSave: async (model, attrs, options) => {},
 |  | ||||||
|   // After saving a value.
 |  | ||||||
|   // Fired after an `insert` or `update` query.
 |  | ||||||
|   // afterSave: async (model, response, options) => {},
 |  | ||||||
|   // Before fetching a value.
 |  | ||||||
|   // Fired before a `fetch` operation.
 |  | ||||||
|   beforeFetch: async (...args) => { |  | ||||||
|     console.log('beforeFetch', ...args); |  | ||||||
|   }, |  | ||||||
|   // After fetching a value.
 |  | ||||||
|   // Fired after a `fetch` operation.
 |  | ||||||
|   afterFetch: async (...args) => { |  | ||||||
|     console.log('afterFetch', ...args); |  | ||||||
|   }, |  | ||||||
|   // Before fetching all values.
 |  | ||||||
|   // Fired before a `fetchAll` operation.
 |  | ||||||
|   beforeFetchAll: async (...args) => { |  | ||||||
|     console.log('beforeFetchAll', ...args); |  | ||||||
|   }, |  | ||||||
|   // After fetching all values.
 |  | ||||||
|   // Fired after a `fetchAll` operation.
 |  | ||||||
|   afterFetchAll: async (...args) => { |  | ||||||
|     console.log('afterFetchAll', ...args); |  | ||||||
|   }, |  | ||||||
|   // Before creating a value.
 |  | ||||||
|   // Fired before an `insert` query.
 |  | ||||||
|   beforeCreate: async (...args) => { |  | ||||||
|     console.log('beforeCreate', ...args); |     console.log('beforeCreate', ...args); | ||||||
|     args[0].name += ' - Coucou'; |  | ||||||
|   }, |   }, | ||||||
|   // After creating a value.
 |   afterCreate(...args) { | ||||||
|   // Fired after an `insert` query.
 |  | ||||||
|   afterCreate: async (...args) => { |  | ||||||
|     console.log('afterCreate', ...args); |     console.log('afterCreate', ...args); | ||||||
|   }, |   }, | ||||||
|   // Before updating a value.
 |   beforeUpdate(...args) { | ||||||
|   // Fired before an `update` query.
 |  | ||||||
|   beforeUpdate: async (...args) => { |  | ||||||
|     console.log('beforeUpdate', ...args); |     console.log('beforeUpdate', ...args); | ||||||
|     args[1].name += ' - Coucou'; |  | ||||||
|   }, |   }, | ||||||
|   // After updating a value.
 |   afterUpdate(...args) { | ||||||
|   // Fired after an `update` query.
 |  | ||||||
|   afterUpdate: async (...args) => { |  | ||||||
|     console.log('afterUpdate', ...args); |     console.log('afterUpdate', ...args); | ||||||
|   }, |   }, | ||||||
|   // Before destroying a value.
 |   beforeDelete(...args) { | ||||||
|   // Fired before a `delete` query.
 |     console.log('beforeDelete', ...args); | ||||||
|   beforeDestroy: async (...args) => { |  | ||||||
|     console.log('beforeDestroy', ...args); |  | ||||||
|   }, |   }, | ||||||
|   // After destroying a value.
 |   afterDelete(...args) { | ||||||
|   // Fired after a `delete` query.
 |     console.log('afterDelete', ...args); | ||||||
|   afterDestroy: async (...args) => { |   }, | ||||||
|     console.log('afterDestroy', ...args); |   beforeFind(...args) { | ||||||
|  |     console.log('beforeFind', ...args); | ||||||
|  |   }, | ||||||
|  |   afterFind(...args) { | ||||||
|  |     console.log('afterFind', ...args); | ||||||
|  |   }, | ||||||
|  |   beforeFindOne(...args) { | ||||||
|  |     console.log('beforeFindOne', ...args); | ||||||
|  |   }, | ||||||
|  |   afterFindOne(...args) { | ||||||
|  |     console.log('afterFindOne', ...args); | ||||||
|  |   }, | ||||||
|  |   beforeCount(...args) { | ||||||
|  |     console.log('beforeCount', ...args); | ||||||
|  |   }, | ||||||
|  |   afterCount(...args) { | ||||||
|  |     console.log('afterCount', ...args); | ||||||
|  |   }, | ||||||
|  |   beforeSearch(...args) { | ||||||
|  |     console.log('beforeSearch', ...args); | ||||||
|  |   }, | ||||||
|  |   afterSearch(...args) { | ||||||
|  |     console.log('afterSearch', ...args); | ||||||
|  |   }, | ||||||
|  |   beforeCountSearch(...args) { | ||||||
|  |     console.log('beforeCountSearch', ...args); | ||||||
|  |   }, | ||||||
|  |   afterCountSearch(...args) { | ||||||
|  |     console.log('afterCountSearch', ...args); | ||||||
|   }, |   }, | ||||||
| }; | }; | ||||||
|  | |||||||
| @ -1,36 +1,27 @@ | |||||||
| 'use strict'; | 'use strict'; | ||||||
| 
 | 
 | ||||||
| const _ = require('lodash'); |  | ||||||
| 
 |  | ||||||
| const { replaceIdByPrimaryKey } = require('../utils/primary-key'); | const { replaceIdByPrimaryKey } = require('../utils/primary-key'); | ||||||
|  | const { executeBeforeHook, executeAfterHook } = require('../utils/hooks'); | ||||||
| 
 | 
 | ||||||
| module.exports = function createQuery(opts) { | module.exports = function createQuery(opts) { | ||||||
|   return new Query(opts); |   const { model, connectorQuery } = opts; | ||||||
| }; |  | ||||||
| 
 | 
 | ||||||
| class Query { |   return { | ||||||
|   constructor({ model, connectorQuery }) { |     get model() { | ||||||
|     this.connectorQuery = connectorQuery; |       return model; | ||||||
|     this.model = model; |     }, | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|     get orm() { |     get orm() { | ||||||
|     return this.model.orm; |       return model.orm; | ||||||
|   } |     }, | ||||||
| 
 | 
 | ||||||
|     get primaryKey() { |     get primaryKey() { | ||||||
|     return this.model.primaryKey; |       return model.primaryKey; | ||||||
|   } |     }, | ||||||
| 
 | 
 | ||||||
|     get associations() { |     get associations() { | ||||||
|     return this.model.associations; |       return model.associations; | ||||||
|   } |     }, | ||||||
| 
 |  | ||||||
|   async executeHook(hook, ...args) { |  | ||||||
|     if (_.has(this.model, hook)) { |  | ||||||
|       await this.model[hook](...args); |  | ||||||
|     } |  | ||||||
|   } |  | ||||||
| 
 | 
 | ||||||
|     /** |     /** | ||||||
|      * Run custom database logic |      * Run custom database logic | ||||||
| @ -49,85 +40,35 @@ class Query { | |||||||
|       } |       } | ||||||
| 
 | 
 | ||||||
|       return mapping[this.model.orm].call(this, { model: this.model }); |       return mapping[this.model.orm].call(this, { model: this.model }); | ||||||
|   } |     }, | ||||||
| 
 | 
 | ||||||
|   async find(params = {}, ...args) { |     create: wrapQuery({ hook: 'create', model, connectorQuery }), | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |     update: wrapQuery({ hook: 'update', model, connectorQuery }), | ||||||
|  |     delete: wrapQuery({ hook: 'delete', model, connectorQuery }), | ||||||
|  |     find: wrapQuery({ hook: 'find', model, connectorQuery }), | ||||||
|  |     findOne: wrapQuery({ hook: 'findOne', model, connectorQuery }), | ||||||
|  |     count: wrapQuery({ hook: 'count', model, connectorQuery }), | ||||||
|  |     search: wrapQuery({ hook: 'search', model, connectorQuery }), | ||||||
|  |     countSearch: wrapQuery({ hook: 'countSearch', model, connectorQuery }), | ||||||
|  |   }; | ||||||
|  | }; | ||||||
| 
 | 
 | ||||||
|     await this.executeHook('beforeFetchAll', newParams, ...args); | // wraps a connectorQuery call with:
 | ||||||
|     const results = await this.connectorQuery.find(newParams, ...args); | // - param substitution
 | ||||||
|     await this.executeHook('afterFetchAll', results); | // - lifecycle hooks
 | ||||||
|  | const wrapQuery = ({ hook, model, connectorQuery }) => async (params, ...rest) => { | ||||||
|  |   // substite id for primaryKey value in params
 | ||||||
|  |   const newParams = replaceIdByPrimaryKey(params, model); | ||||||
| 
 | 
 | ||||||
|     return results; |   // execute before hook
 | ||||||
|   } |   await executeBeforeHook(hook, model, newParams, ...rest); | ||||||
| 
 | 
 | ||||||
|   async findOne(params = {}, ...args) { |   // execute query
 | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |   const result = await connectorQuery[hook](newParams, ...rest); | ||||||
| 
 | 
 | ||||||
|     await this.executeHook('beforeFetch', newParams, ...args); |   // execute after hook
 | ||||||
|     const result = await this.connectorQuery.findOne(newParams, ...args); |   await executeAfterHook(hook, model, result); | ||||||
|     await this.executeHook('afterFetch', result); |  | ||||||
| 
 | 
 | ||||||
|  |   // return result
 | ||||||
|   return result; |   return result; | ||||||
|   } | }; | ||||||
| 
 |  | ||||||
|   async create(params = {}, ...args) { |  | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |  | ||||||
| 
 |  | ||||||
|     await this.executeHook('beforeCreate', newParams, ...args); |  | ||||||
|     const entry = await this.connectorQuery.create(newParams, ...args); |  | ||||||
|     await this.executeHook('afterCreate', entry); |  | ||||||
| 
 |  | ||||||
|     return entry; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   async update(params = {}, ...args) { |  | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |  | ||||||
| 
 |  | ||||||
|     await this.executeHook('beforeUpdate', newParams, ...args); |  | ||||||
|     const entry = await this.connectorQuery.update(newParams, ...args); |  | ||||||
|     await this.executeHook('afterUpdate', entry); |  | ||||||
| 
 |  | ||||||
|     return entry; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   async delete(params = {}, ...args) { |  | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |  | ||||||
| 
 |  | ||||||
|     await this.executeHook('beforeDestroy', newParams, ...args); |  | ||||||
|     const entry = await this.connectorQuery.delete(newParams, ...args); |  | ||||||
|     await this.executeHook('afterDestroy', entry); |  | ||||||
| 
 |  | ||||||
|     return entry; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   async count(params = {}, ...args) { |  | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |  | ||||||
| 
 |  | ||||||
|     await this.executeHook('beforeCount', newParams, ...args); |  | ||||||
|     const count = await this.connectorQuery.count(newParams, ...args); |  | ||||||
|     await this.executeHook('afterCount', count); |  | ||||||
| 
 |  | ||||||
|     return count; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   async search(params = {}, ...args) { |  | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |  | ||||||
| 
 |  | ||||||
|     await this.executeHook('beforeSearch', newParams, ...args); |  | ||||||
|     const results = await this.connectorQuery.search(newParams, ...args); |  | ||||||
|     await this.executeHook('afterSearch', results); |  | ||||||
| 
 |  | ||||||
|     return results; |  | ||||||
|   } |  | ||||||
| 
 |  | ||||||
|   async countSearch(params = {}, ...args) { |  | ||||||
|     const newParams = replaceIdByPrimaryKey(params, this.model); |  | ||||||
| 
 |  | ||||||
|     await this.executeHook('beforeCountSearch', newParams, ...args); |  | ||||||
|     const count = await this.connectorQuery.countSearch(newParams, ...args); |  | ||||||
|     await this.executeHook('afterCountSearch', count); |  | ||||||
| 
 |  | ||||||
|     return count; |  | ||||||
|   } |  | ||||||
| } |  | ||||||
|  | |||||||
							
								
								
									
										20
									
								
								packages/strapi-database/lib/utils/hooks.js
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										20
									
								
								packages/strapi-database/lib/utils/hooks.js
									
									
									
									
									
										Normal file
									
								
							| @ -0,0 +1,20 @@ | |||||||
|  | 'use strict'; | ||||||
|  | 
 | ||||||
|  | const _ = require('lodash'); | ||||||
|  | 
 | ||||||
|  | const executeHook = async (hook, model, ...args) => { | ||||||
|  |   if (_.has(model, hook)) { | ||||||
|  |     await model[hook](...args); | ||||||
|  |   } | ||||||
|  | }; | ||||||
|  | 
 | ||||||
|  | const executeBeforeHook = (hook, model, ...args) => | ||||||
|  |   executeHook(`before${_.upperFirst(hook)}`, model, ...args); | ||||||
|  | 
 | ||||||
|  | const executeAfterHook = (hook, model, ...args) => | ||||||
|  |   executeHook(`after${_.upperFirst(hook)}`, model, ...args); | ||||||
|  | 
 | ||||||
|  | module.exports = { | ||||||
|  |   executeBeforeHook, | ||||||
|  |   executeAfterHook, | ||||||
|  | }; | ||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Alexandre Bodin
						Alexandre Bodin