mirror of
				https://github.com/strapi/strapi.git
				synced 2025-10-31 01:47:13 +00:00 
			
		
		
		
	Dynamic collection selection
This commit is contained in:
		
							parent
							
								
									11cd4b2c50
								
							
						
					
					
						commit
						31ad1dfbb7
					
				| @ -11,8 +11,9 @@ module.exports = { | ||||
|   }, | ||||
| 
 | ||||
|   find: async(ctx) => { | ||||
|     const model = ctx.params.model; | ||||
|     const primaryKey = strapi.models[model].primaryKey; | ||||
|     const model = strapi.models[ctx.params.model]; | ||||
|     const collection = global[model.globalName]; | ||||
|     const primaryKey = model.primaryKey; | ||||
| 
 | ||||
|     const { | ||||
|       limit = 10, | ||||
| @ -20,7 +21,7 @@ module.exports = { | ||||
|       sort = primaryKey | ||||
|     } = ctx.request.query; | ||||
| 
 | ||||
|     const entries = await User | ||||
|     const entries = await collection | ||||
|       .find() | ||||
|       .limit(Number(limit)) | ||||
|       .sort(sort) | ||||
| @ -30,9 +31,10 @@ module.exports = { | ||||
|   }, | ||||
| 
 | ||||
|   count: async(ctx) => { | ||||
|     const model = ctx.params.model; | ||||
|     const model = strapi.models[ctx.params.model]; | ||||
|     const collection = global[model.globalName]; | ||||
| 
 | ||||
|     const count = await User | ||||
|     const count = await collection | ||||
|       .count(); | ||||
| 
 | ||||
|     ctx.body = { | ||||
| @ -41,45 +43,50 @@ module.exports = { | ||||
|   }, | ||||
| 
 | ||||
|   findOne: async(ctx) => { | ||||
|     const model = ctx.params.model; | ||||
|     const primaryKey = strapi.models[model].primaryKey; | ||||
|     const model = strapi.models[ctx.params.model]; | ||||
|     const collection = global[model.globalName]; | ||||
|     const primaryKey = model.primaryKey; | ||||
|     const params = {}; | ||||
|     params[primaryKey] = ctx.params.id; | ||||
| 
 | ||||
|     const entry = await User | ||||
|     const entry = await collection | ||||
|       .findOne(params); | ||||
| 
 | ||||
|     ctx.body = entry; | ||||
|   }, | ||||
| 
 | ||||
|   create: async(ctx) => { | ||||
|     const model = ctx.params.model; | ||||
|     const model = strapi.models[ctx.params.model]; | ||||
|     const collection = global[model.globalName]; | ||||
| 
 | ||||
|     const entryCreated = await User | ||||
|     const entryCreated = await collection | ||||
|       .create(ctx.request.body); | ||||
| 
 | ||||
|     ctx.body = entryCreated; | ||||
|   }, | ||||
| 
 | ||||
|   update: async(ctx) => { | ||||
|     const model = ctx.params.model; | ||||
|     const primaryKey = strapi.models[model].primaryKey; | ||||
|     const model = strapi.models[ctx.params.model]; | ||||
|     const collection = global[model.globalName]; | ||||
|     const primaryKey = model.primaryKey; | ||||
|     const params = {}; | ||||
| 
 | ||||
|     params[primaryKey] = ctx.params.id; | ||||
| 
 | ||||
|     const entryUpdated = await User | ||||
|     const entryUpdated = await collection | ||||
|       .update(params, ctx.request.body); | ||||
| 
 | ||||
|     ctx.body = entryUpdated; | ||||
|   }, | ||||
| 
 | ||||
|   delete: async(ctx) => { | ||||
|     const model = ctx.params.model; | ||||
|     const primaryKey = strapi.models[model].primaryKey; | ||||
|     const model = strapi.models[ctx.params.model]; | ||||
|     const collection = global[model.globalName]; | ||||
|     const primaryKey = model.primaryKey; | ||||
|     const params = {}; | ||||
|     params[primaryKey] = ctx.params.id; | ||||
| 
 | ||||
|     const entryDeleted = await User | ||||
|     const entryDeleted = await collection | ||||
|       .remove(params); | ||||
| 
 | ||||
|     ctx.body = entryDeleted; | ||||
|  | ||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user
	 Pierre Burgy
						Pierre Burgy