From b16b86bb758c75de8fbce0a946c8e1b852d7f3dc Mon Sep 17 00:00:00 2001 From: Aurelsicoko Date: Tue, 29 Aug 2017 19:34:34 +0200 Subject: [PATCH] Add query method to Strapi to switch on right ORM in the plugins --- .../config/queries/bookshelf.js | 11 ++-- .../config/queries/mongoose.js | 62 +++++++------------ .../controllers/ContentManager.js | 61 +++--------------- packages/strapi/lib/Strapi.js | 53 +++++++++++++++- packages/strapi/package.json | 1 + 5 files changed, 92 insertions(+), 96 deletions(-) diff --git a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js index fb351f7975..f3494d777d 100644 --- a/packages/strapi-plugin-content-manager/config/queries/bookshelf.js +++ b/packages/strapi-plugin-content-manager/config/queries/bookshelf.js @@ -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; } -}; \ No newline at end of file +}; diff --git a/packages/strapi-plugin-content-manager/config/queries/mongoose.js b/packages/strapi-plugin-content-manager/config/queries/mongoose.js index e60a28d790..33b792d76a 100644 --- a/packages/strapi-plugin-content-manager/config/queries/mongoose.js +++ b/packages/strapi-plugin-content-manager/config/queries/mongoose.js @@ -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 + }); } -}; \ No newline at end of file +}; diff --git a/packages/strapi-plugin-content-manager/controllers/ContentManager.js b/packages/strapi-plugin-content-manager/controllers/ContentManager.js index 5790c4c019..db9926f5d2 100644 --- a/packages/strapi-plugin-content-manager/controllers/ContentManager.js +++ b/packages/strapi-plugin-content-manager/controllers/ContentManager.js @@ -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; diff --git a/packages/strapi/lib/Strapi.js b/packages/strapi/lib/Strapi.js index 36320ea578..ad7c8a2cf7 100644 --- a/packages/strapi/lib/Strapi.js +++ b/packages/strapi/lib/Strapi.js @@ -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(); diff --git a/packages/strapi/package.json b/packages/strapi/package.json index 1a7d38d525..46e81fd31d 100644 --- a/packages/strapi/package.json +++ b/packages/strapi/package.json @@ -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",