Fix strapi-bookshelf to work w/ associations w/ plugins & JWT condition

This commit is contained in:
Aurelsicoko 2017-12-12 16:14:38 +01:00
parent 8db92c22fd
commit 628ad4add4
9 changed files with 22 additions and 28 deletions

View File

@ -40,8 +40,6 @@ module.exports = function(strapi) {
*/
initialize: cb => {
let globalName;
// Initialize collections
_.set(strapi, 'bookshelf.collections', {});
@ -97,7 +95,7 @@ module.exports = function(strapi) {
definition.globalId = _.upperFirst(_.camelCase(`${plugin}-${model}`));
}
globalName = _.upperFirst(_.camelCase(definition.globalId));
definition.globalName = _.upperFirst(_.camelCase(definition.globalId));
_.defaults(definition, {
primaryKey: 'id'
@ -105,10 +103,7 @@ module.exports = function(strapi) {
// Make sure the model has a table name.
// If not, use the model name.
if (_.isEmpty(definition.collectionName)) {
definition.collectionName = model;
}
definition.collectionName = _.isEmpty(definition.collectionName) ? definition.globalName.toLowerCase() : definition.collectionName;
// Add some informations about ORM & client connection
definition.orm = 'bookshelf';
definition.client = _.get(connection.settings, 'client');
@ -154,7 +149,7 @@ module.exports = function(strapi) {
// Initialize the global variable with the
// capitalized model name.
if (!plugin) {
global[globalName] = {};
global[definition.globalName] = {};
}
// Call this callback function after we are done parsing
@ -223,13 +218,13 @@ module.exports = function(strapi) {
);
if (!plugin) {
global[globalName] = ORM.Model.extend(loadedModel);
global[pluralize(globalName)] = ORM.Collection.extend({
model: global[globalName]
global[definition.globalName] = ORM.Model.extend(loadedModel);
global[pluralize(definition.globalName)] = ORM.Collection.extend({
model: global[definition.globalName]
});
// Expose ORM functions through the `target` object.
target[model] = _.assign(global[globalName], target[model]);
target[model] = _.assign(global[definition.globalName], target[model]);
} else {
target[model] = _.assign(ORM.Model.extend(loadedModel), target[model]);
}
@ -261,7 +256,7 @@ module.exports = function(strapi) {
// Build associations key
utilsModels.defineAssociations(
globalName,
definition.globalName,
definition,
details,
name
@ -418,15 +413,13 @@ module.exports = function(strapi) {
});
};
mountModels(models, strapi.models);
// Mount `./api` models.
mountModels(_.pickBy(strapi.models, { connection: connectionName }), strapi.models);
// Mount `./plugins` models.
_.forEach(strapi.plugins, (plugin, name) => {
models = _.pickBy(strapi.plugins[name].models, { connection: connectionName })
if (connectionName === strapi.config.currentEnvironment.database.defaultConnection) {
_.assign(models, _.pickBy(strapi.plugins[name].models, (model) => model.connection === undefined));
}
mountModels(models, plugin.models, name);
mountModels(_.pickBy(strapi.plugins[name].models, { connection: connectionName }), plugin.models, name);
});
});
},

View File

@ -55,4 +55,4 @@
"npm": ">= 5.3.0"
},
"license": "MIT"
}
}

File diff suppressed because one or more lines are too long

View File

@ -95,6 +95,7 @@ module.exports = strapi => {
charset: _.get(connection.settings, 'charset'),
schema: _.get(connection.settings, 'schema') || 'public',
port: _.get(connection.settings, 'port'),
socket: _.get(connection.settings, 'socketPath')
},
debug: _.get(connection.options, 'debug') || false,
acquireConnectionTimeout: _.get(connection.options, 'acquireConnectionTimeout'),

View File

@ -46,4 +46,4 @@
"npm": ">= 5.0.0"
},
"license": "MIT"
}
}

View File

@ -133,9 +133,9 @@ module.exports = function (strapi) {
});
if (!plugin) {
global[definition.globalName] = instance.model(definition.globalName, collection.schema);
global[definition.globalName] = instance.model(definition.globalName, collection.schema, definition.globalName.toLowerCase());
} else {
instance.model(definition.globalName, collection.schema);
instance.model(definition.globalName, collection.schema, definition.globalName.toLowerCase());
}
// Expose ORM functions through the `target` object.

View File

@ -30,7 +30,7 @@
},
"post": {
"model": "post",
"via": "authors"
"via": "author"
}
}
}

View File

@ -50,7 +50,7 @@ module.exports = {
process.env.JWT_SECRET || _.get(strapi.plugins['users-permissions'], 'config.jwtSecret') || 'oursecret',
{},
function (err, user) {
if (err || !user || !user.id) {
if (err || !user || !_.get(user, 'id', '').toString()) {
return reject('Invalid token.');
}
resolve(user);

View File

@ -9,7 +9,7 @@ module.exports = async function() {
// Set if is admin destination for middleware application.
this.app.use(async (ctx, next) => {
if (ctx.request.header['origin'] === 'http://localhost:4000') {
ctx.request.header['x-forwarded-host'] = 'strapi';
ctx.request.header['x-forwarded-host'] = 'strapi';
}
ctx.request.admin = ctx.request.header['x-forwarded-host'] === 'strapi';