Load bookshelf plugins models

This commit is contained in:
Jim Laurie 2017-11-15 17:27:07 +01:00
parent 4bd0427ba6
commit 8cf8c24ee7
3 changed files with 31 additions and 19 deletions

View File

@ -100,9 +100,13 @@ module.exports = function(strapi) {
done();
});
const mountModels: (models, target) => {
const mountModels = (models, target, plugin = false) => {
// Parse every registered model.
_.forEach(models, (definition, model) => {
if (plugin) {
definition.globalId = _.upperFirst(_.camelCase(`${plugin}-${model}`));
}
globalName = _.upperFirst(_.camelCase(definition.globalId));
_.defaults(definition, {
@ -159,7 +163,9 @@ module.exports = function(strapi) {
// Initialize the global variable with the
// capitalized model name.
global[globalName] = {};
if (!plugin) {
global[globalName] = {};
}
// Call this callback function after we are done parsing
// all attributes for relationships-- see below.
@ -194,8 +200,8 @@ module.exports = function(strapi) {
};
_.forEach(lifecycle, (fn, key) => {
if (_.isFunction(strapi.models[model.toLowerCase()][fn])) {
this.on(key, strapi.models[model.toLowerCase()][fn]);
if (_.isFunction(target[model.toLowerCase()][fn])) {
this.on(key, target[model.toLowerCase()][fn]);
}
});
@ -204,9 +210,9 @@ module.exports = function(strapi) {
attrs = mapper(attrs);
return _.isFunction(
strapi.models[model.toLowerCase()]['beforeSave']
target[model.toLowerCase()]['beforeSave']
)
? strapi.models[model.toLowerCase()]['beforeSave']
? target[model.toLowerCase()]['beforeSave']
: Promise.resolve();
});
};
@ -226,16 +232,18 @@ module.exports = function(strapi) {
)
);
global[globalName] = ORM.Model.extend(loadedModel);
global[pluralize(globalName)] = ORM.Collection.extend({
model: global[globalName]
});
if (!plugin) {
global[globalName] = ORM.Model.extend(loadedModel);
global[pluralize(globalName)] = ORM.Collection.extend({
model: global[globalName]
});
// Expose ORM functions through the `strapi.models` object.
strapi.models[model] = _.assign(global[globalName], strapi.models[model]);
// Expose ORM functions through the `target` object.
target[model] = _.assign(global[globalName], target[model]);
}
// Push attributes to be aware of model schema.
strapi.models[model]._attributes = definition.attributes;
target[model]._attributes = definition.attributes;
loadedHook();
} catch (err) {
@ -418,7 +426,11 @@ module.exports = function(strapi) {
});
};
mountModels(models);
mountModels(models, strapi.models);
_.forEach(strapi.plugins, (plugin, name) => {
mountModels(_.pickBy(strapi.plugins[name].models, { connection: connectionName }), plugin.models, name);
});
});
},

View File

@ -3,7 +3,7 @@ const _ = require('lodash');
module.exports = {
find: async function (params) {
return this
.find()
.find(params.where)
.limit(Number(params.limit))
.sort(params.sort)
.skip(Number(params.skip));

View File

@ -26,12 +26,12 @@ module.exports = {
},
init: async (ctx) => {
const hasAdmin = await strapi.plugins['users-permissions'].models.user.find({
admin: true
const hasAdmin = await strapi.query('user', 'users-permissions').find({
where: {
admin: true
}
});
console.log(await strapi.query('user', 'users-permissions'));
ctx.send({ hasAdmin: !_.isEmpty(hasAdmin) });
}
};