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) => { 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;