Add query method to Strapi to switch on right ORM in the plugins

This commit is contained in:
Aurelsicoko 2017-08-29 19:34:34 +02:00
parent 7ee1d90cb7
commit b16b86bb75
5 changed files with 92 additions and 96 deletions

View File

@ -1,7 +1,8 @@
module.exports = {
find: async (params) => {
const entries = await params.model
find: async function (params) {
console.log(params);
const entries = await this
.forge()
.query((qb) => {
qb.limit(Number(params.limit));
@ -19,8 +20,8 @@ module.exports = {
return entries;
},
count: async (params) => {
const count = await params.model
count: async function (params) {
const count = await this
.forge()
.count();
@ -67,4 +68,4 @@ module.exports = {
return entry;
}
};
};

View File

@ -1,56 +1,40 @@
module.exports = {
find: async (params) => {
const entries = params.model
find: async function (params) {
return this
.find()
.limit(Number(params.limit))
.sort(params.sort)
.skip(Number(params.skip));
return entries;
},
count: async (params) => {
const count = await params.model
.count();
return Number(count);
count: async function (params) {
return Number(await this
.count());
},
findOne: async (params) => {
const where = {};
where[params.primaryKey] = params.id;
const entry = await params.model
.findOne(where);
return entry;
findOne: async function (params) {
return await this
.findOne({
[this.primaryKey]: params.id
});
},
create: async (params) => {
const entry = await params.model
create: async function (params) {
return await this
.create(params.values);
return entry;
},
update: async (params) => {
const where = {};
where[params.primaryKey] = params.id;
const entry = await params.model
.update(where, params.values);
return entry;
update: async function (params) {
return await this
.update({
[this.primaryKey]: params.id
}, params.values);
},
delete: async (params) => {
const where = {};
where[params.primaryKey] = params.id;
const entry = await params.model
.destroy(where);
return entry;
delete: async function (params) {
return await this
.destroy({
[this.primaryKey]: params.id
});
}
};
};

View File

@ -24,16 +24,10 @@ module.exports = {
},
find: async ctx => {
const model = strapi.models[ctx.params.model];
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
const primaryKey = model.primaryKey;
const {limit, skip = 0, sort = primaryKey, query, queryAttribute} = ctx.request.query;
const { limit, skip = 0, sort, query, queryAttribute } = ctx.request.query;
// Find entries using `queries` system
const entries = await queries
.find({
model,
const entries = await strapi.query(ctx.params.model).find({
limit,
skip,
sort,
@ -45,12 +39,8 @@ module.exports = {
},
count: async ctx => {
const model = strapi.models[ctx.params.model];
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
// Count using `queries` system
const count = await queries.count({model});
const count = await strapi.query(ctx.params.model).count();
ctx.body = {
count,
@ -58,17 +48,9 @@ module.exports = {
},
findOne: async ctx => {
const model = strapi.models[ctx.params.model];
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
const primaryKey = model.primaryKey;
const id = ctx.params.id;
// Find an entry using `queries` system
const entry = await queries.findOne({
model,
primaryKey,
id
const entry = await strapi.query(ctx.params.model).findOne({
id: ctx.params.id
});
// Entry not found
@ -80,51 +62,28 @@ module.exports = {
},
create: async ctx => {
const model = strapi.models[ctx.params.model];
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
const values = ctx.request.body;
// Create an entry using `queries` system
const entryCreated = await queries.create({
model,
values
const entryCreated = await strapi.query(ctx.params.model).create({
values: ctx.request.body
});
ctx.body = entryCreated;
},
update: async ctx => {
const model = strapi.models[ctx.params.model];
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
const primaryKey = model.primaryKey;
const id = ctx.params.id;
const values = ctx.request.body;
// Update an entry using `queries` system
const entryUpdated = await queries.update({
model,
primaryKey,
id,
values
id: ctx.params.id
values: ctx.request.body
});
ctx.body = entryUpdated;
},
delete: async ctx => {
const model = strapi.models[ctx.params.model];
const orm = _.get(strapi.plugins, ['content-manager', 'config', 'admin', 'schema', ctx.params.model, 'orm']) || model.orm;
const queries = _.get(strapi.plugins, ['content-manager', 'config', 'queries', orm]);
const primaryKey = model.primaryKey;
const id = ctx.params.id;
// Delete an entry using `queries` system
const entryDeleted = await queries.delete({
model,
primaryKey,
id
id: ctx.params.id
});
ctx.body = entryDeleted;

View File

@ -6,12 +6,13 @@ const utils = require('./utils');
const http = require('http');
const path = require('path');
const cluster = require('cluster');
const { includes } = require('lodash');
const { includes, get } = require('lodash');
const { logger } = require('strapi-utils');
const { nestedConfigurations, appConfigurations, apis, middlewares, hooks } = require('./core');
const initializeMiddlewares = require('./middlewares');
const initializeHooks = require('./hooks');
const { EventEmitter } = require('events');
const stackTrace = require('stack-trace');
/**
* Construct an Strapi instance.
@ -223,6 +224,56 @@ class Strapi extends EventEmitter {
Object.freeze(this[key]);
});
}
query(entity) {
if (!entity) {
return this.log.error(`You can't call the query method without passing the model's name as a first argument.`);
}
const model = entity.toLowerCase();
if (!this.models.hasOwnProperty(model)) {
return this.log.error(`The model ${model} can't be found.`);
}
const connector = this.models[model].orm;
if (!connector) {
return this.log.error(`Impossible to determine the use ORM for the model ${model}.`);
}
// Get stack trace.
const stack = stackTrace.get()[1];
const file = stack.getFileName();
const method = stack.getFunctionName();
// Extract plugin path.
const pluginPath = file.indexOf('strapi-plugin-') !== -1 ?
file.split(path.sep).filter(x => x.indexOf('strapi-plugin-') !== -1)[0] :
undefined;
if (!pluginPath) {
return this.log.error('Impossible to find the plugin where `strapi.query` has been called.');
}
// Get plugin name.
const pluginName = pluginPath.replace('strapi-plugin-', '').toLowerCase();
const queries = get(this.plugins, `${pluginName}.config.queries.${connector}`);
if (!queries) {
return this.log.error(`There is no query available for the model ${model}.`);
}
// Bind queries with the current model to allow the use of `this`.
const bindQueries = Object.keys(queries).reduce((acc, current) => {
return acc[current] = queries[current].bind(this.models[model]), acc;
}, {});
// Send ORM to the called function.
bindQueries.orm = connector;
return bindQueries;
}
}
module.exports = new Strapi();

View File

@ -50,6 +50,7 @@
"koa-static": "^4.0.1",
"lodash": "^4.16.5",
"node-schedule": "^1.2.0",
"stack-trace": "0.0.10",
"strapi-generate": "^3.0.0-alpha.4.8",
"strapi-generate-admin": "^3.0.0-alpha.4.8",
"strapi-generate-api": "^3.0.0-alpha.4.8",