mirror of
https://github.com/strapi/strapi.git
synced 2025-12-14 08:44:16 +00:00
Add query method to Strapi to switch on right ORM in the plugins
This commit is contained in:
parent
7ee1d90cb7
commit
b16b86bb75
@ -1,7 +1,8 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
|
|
||||||
find: async (params) => {
|
find: async function (params) {
|
||||||
const entries = await params.model
|
console.log(params);
|
||||||
|
const entries = await this
|
||||||
.forge()
|
.forge()
|
||||||
.query((qb) => {
|
.query((qb) => {
|
||||||
qb.limit(Number(params.limit));
|
qb.limit(Number(params.limit));
|
||||||
@ -19,8 +20,8 @@ module.exports = {
|
|||||||
return entries;
|
return entries;
|
||||||
},
|
},
|
||||||
|
|
||||||
count: async (params) => {
|
count: async function (params) {
|
||||||
const count = await params.model
|
const count = await this
|
||||||
.forge()
|
.forge()
|
||||||
.count();
|
.count();
|
||||||
|
|
||||||
@ -67,4 +68,4 @@ module.exports = {
|
|||||||
|
|
||||||
return entry;
|
return entry;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -1,56 +1,40 @@
|
|||||||
module.exports = {
|
module.exports = {
|
||||||
|
find: async function (params) {
|
||||||
find: async (params) => {
|
return this
|
||||||
const entries = params.model
|
|
||||||
.find()
|
.find()
|
||||||
.limit(Number(params.limit))
|
.limit(Number(params.limit))
|
||||||
.sort(params.sort)
|
.sort(params.sort)
|
||||||
.skip(Number(params.skip));
|
.skip(Number(params.skip));
|
||||||
|
|
||||||
return entries;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
count: async (params) => {
|
count: async function (params) {
|
||||||
const count = await params.model
|
return Number(await this
|
||||||
.count();
|
.count());
|
||||||
|
|
||||||
return Number(count);
|
|
||||||
},
|
},
|
||||||
|
|
||||||
findOne: async (params) => {
|
findOne: async function (params) {
|
||||||
const where = {};
|
return await this
|
||||||
where[params.primaryKey] = params.id;
|
.findOne({
|
||||||
|
[this.primaryKey]: params.id
|
||||||
const entry = await params.model
|
});
|
||||||
.findOne(where);
|
|
||||||
|
|
||||||
return entry;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
create: async (params) => {
|
create: async function (params) {
|
||||||
const entry = await params.model
|
return await this
|
||||||
.create(params.values);
|
.create(params.values);
|
||||||
|
|
||||||
return entry;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
update: async (params) => {
|
update: async function (params) {
|
||||||
const where = {};
|
return await this
|
||||||
where[params.primaryKey] = params.id;
|
.update({
|
||||||
|
[this.primaryKey]: params.id
|
||||||
const entry = await params.model
|
}, params.values);
|
||||||
.update(where, params.values);
|
|
||||||
|
|
||||||
return entry;
|
|
||||||
},
|
},
|
||||||
|
|
||||||
delete: async (params) => {
|
delete: async function (params) {
|
||||||
const where = {};
|
return await this
|
||||||
where[params.primaryKey] = params.id;
|
.destroy({
|
||||||
|
[this.primaryKey]: params.id
|
||||||
const entry = await params.model
|
});
|
||||||
.destroy(where);
|
|
||||||
|
|
||||||
return entry;
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@ -24,16 +24,10 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
find: async ctx => {
|
find: async ctx => {
|
||||||
const model = strapi.models[ctx.params.model];
|
const { limit, skip = 0, sort, query, queryAttribute } = ctx.request.query;
|
||||||
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;
|
|
||||||
|
|
||||||
// Find entries using `queries` system
|
// Find entries using `queries` system
|
||||||
const entries = await queries
|
const entries = await strapi.query(ctx.params.model).find({
|
||||||
.find({
|
|
||||||
model,
|
|
||||||
limit,
|
limit,
|
||||||
skip,
|
skip,
|
||||||
sort,
|
sort,
|
||||||
@ -45,12 +39,8 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
count: async ctx => {
|
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
|
// Count using `queries` system
|
||||||
const count = await queries.count({model});
|
const count = await strapi.query(ctx.params.model).count();
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
count,
|
count,
|
||||||
@ -58,17 +48,9 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
findOne: async ctx => {
|
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
|
// Find an entry using `queries` system
|
||||||
const entry = await queries.findOne({
|
const entry = await strapi.query(ctx.params.model).findOne({
|
||||||
model,
|
id: ctx.params.id
|
||||||
primaryKey,
|
|
||||||
id
|
|
||||||
});
|
});
|
||||||
|
|
||||||
// Entry not found
|
// Entry not found
|
||||||
@ -80,51 +62,28 @@ module.exports = {
|
|||||||
},
|
},
|
||||||
|
|
||||||
create: async ctx => {
|
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
|
// Create an entry using `queries` system
|
||||||
const entryCreated = await queries.create({
|
const entryCreated = await strapi.query(ctx.params.model).create({
|
||||||
model,
|
values: ctx.request.body
|
||||||
values
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = entryCreated;
|
ctx.body = entryCreated;
|
||||||
},
|
},
|
||||||
|
|
||||||
update: async ctx => {
|
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
|
// Update an entry using `queries` system
|
||||||
const entryUpdated = await queries.update({
|
const entryUpdated = await queries.update({
|
||||||
model,
|
id: ctx.params.id
|
||||||
primaryKey,
|
values: ctx.request.body
|
||||||
id,
|
|
||||||
values
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = entryUpdated;
|
ctx.body = entryUpdated;
|
||||||
},
|
},
|
||||||
|
|
||||||
delete: async ctx => {
|
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
|
// Delete an entry using `queries` system
|
||||||
const entryDeleted = await queries.delete({
|
const entryDeleted = await queries.delete({
|
||||||
model,
|
id: ctx.params.id
|
||||||
primaryKey,
|
|
||||||
id
|
|
||||||
});
|
});
|
||||||
|
|
||||||
ctx.body = entryDeleted;
|
ctx.body = entryDeleted;
|
||||||
|
|||||||
@ -6,12 +6,13 @@ const utils = require('./utils');
|
|||||||
const http = require('http');
|
const http = require('http');
|
||||||
const path = require('path');
|
const path = require('path');
|
||||||
const cluster = require('cluster');
|
const cluster = require('cluster');
|
||||||
const { includes } = require('lodash');
|
const { includes, get } = require('lodash');
|
||||||
const { logger } = require('strapi-utils');
|
const { logger } = require('strapi-utils');
|
||||||
const { nestedConfigurations, appConfigurations, apis, middlewares, hooks } = require('./core');
|
const { nestedConfigurations, appConfigurations, apis, middlewares, hooks } = require('./core');
|
||||||
const initializeMiddlewares = require('./middlewares');
|
const initializeMiddlewares = require('./middlewares');
|
||||||
const initializeHooks = require('./hooks');
|
const initializeHooks = require('./hooks');
|
||||||
const { EventEmitter } = require('events');
|
const { EventEmitter } = require('events');
|
||||||
|
const stackTrace = require('stack-trace');
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Construct an Strapi instance.
|
* Construct an Strapi instance.
|
||||||
@ -223,6 +224,56 @@ class Strapi extends EventEmitter {
|
|||||||
Object.freeze(this[key]);
|
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();
|
module.exports = new Strapi();
|
||||||
|
|||||||
@ -50,6 +50,7 @@
|
|||||||
"koa-static": "^4.0.1",
|
"koa-static": "^4.0.1",
|
||||||
"lodash": "^4.16.5",
|
"lodash": "^4.16.5",
|
||||||
"node-schedule": "^1.2.0",
|
"node-schedule": "^1.2.0",
|
||||||
|
"stack-trace": "0.0.10",
|
||||||
"strapi-generate": "^3.0.0-alpha.4.8",
|
"strapi-generate": "^3.0.0-alpha.4.8",
|
||||||
"strapi-generate-admin": "^3.0.0-alpha.4.8",
|
"strapi-generate-admin": "^3.0.0-alpha.4.8",
|
||||||
"strapi-generate-api": "^3.0.0-alpha.4.8",
|
"strapi-generate-api": "^3.0.0-alpha.4.8",
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user