Dynamic collection selection

This commit is contained in:
Pierre Burgy 2017-05-06 17:27:24 +02:00
parent 11cd4b2c50
commit 31ad1dfbb7

View File

@ -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;