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