From d6c16f23735051fb99486d4b3e96e092ae2017be Mon Sep 17 00:00:00 2001 From: Jim Laurie Date: Tue, 24 Apr 2018 15:34:26 +0200 Subject: [PATCH] Add comments and clean code --- packages/strapi-bookshelf/lib/index.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/packages/strapi-bookshelf/lib/index.js b/packages/strapi-bookshelf/lib/index.js index 0c5b2c56a7..576564220e 100755 --- a/packages/strapi-bookshelf/lib/index.js +++ b/packages/strapi-bookshelf/lib/index.js @@ -310,8 +310,11 @@ module.exports = function(strapi) { target[model]._attributes = definition.attributes; databaseUpdate.push(new Promise(async (resolve) => { - const handler = async (table, attributes, tableExist) => { - // Generate fields type + // Equilize database tables + const handler = async (table, attributes) => { + const tableExist = await ORM.knex.schema.hasTable(table); + + // Apply field type of attributes definition const generateColumns = (attrs, start) => { return Object.keys(attrs).reduce((acc, attr) => { const attribute = attributes[attr]; @@ -319,6 +322,7 @@ module.exports = function(strapi) { let type; if (!attribute.type) { + // Add interger value is if relation const relation = definition.associations.find((association) => { return association.alias === attr; }); @@ -407,8 +411,6 @@ module.exports = function(strapi) { const quote = definition.client === 'pg' ? '"' : '`'; - const tableExist = await ORM.knex.schema.hasTable(loadedModel.tableName); - // Add created_at and updated_at field if timestamp option is true if (loadedModel.hasTimestamps) { definition.attributes['created_at'] = definition.attributes['updated_at'] = { @@ -416,15 +418,15 @@ module.exports = function(strapi) { }; } - await handler(loadedModel.tableName, definition.attributes, tableExist); + // Equilize tables + await handler(loadedModel.tableName, definition.attributes); + // Equilize polymorphic releations const morphRelations = definition.associations.find((association) => { return association.nature.toLowerCase().includes('morph'); }); if (morphRelations) { - const tableExist = await ORM.knex.schema.hasTable(`${loadedModel.tableName}_morph`); - const attributes = { [`${loadedModel.tableName}_id`]: { type: 'integer' @@ -440,7 +442,7 @@ module.exports = function(strapi) { } }; - await handler(`${loadedModel.tableName}_morph`, attributes, tableExist); + await handler(`${loadedModel.tableName}_morph`, attributes); } resolve();