Add comments and clean code

This commit is contained in:
Jim Laurie 2018-04-24 15:34:26 +02:00
parent 1ae9e65b42
commit d6c16f2373

View File

@ -310,8 +310,11 @@ module.exports = function(strapi) {
target[model]._attributes = definition.attributes; target[model]._attributes = definition.attributes;
databaseUpdate.push(new Promise(async (resolve) => { databaseUpdate.push(new Promise(async (resolve) => {
const handler = async (table, attributes, tableExist) => { // Equilize database tables
// Generate fields type const handler = async (table, attributes) => {
const tableExist = await ORM.knex.schema.hasTable(table);
// Apply field type of attributes definition
const generateColumns = (attrs, start) => { const generateColumns = (attrs, start) => {
return Object.keys(attrs).reduce((acc, attr) => { return Object.keys(attrs).reduce((acc, attr) => {
const attribute = attributes[attr]; const attribute = attributes[attr];
@ -319,6 +322,7 @@ module.exports = function(strapi) {
let type; let type;
if (!attribute.type) { if (!attribute.type) {
// Add interger value is if relation
const relation = definition.associations.find((association) => { const relation = definition.associations.find((association) => {
return association.alias === attr; return association.alias === attr;
}); });
@ -407,8 +411,6 @@ module.exports = function(strapi) {
const quote = definition.client === 'pg' ? '"' : '`'; 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 // Add created_at and updated_at field if timestamp option is true
if (loadedModel.hasTimestamps) { if (loadedModel.hasTimestamps) {
definition.attributes['created_at'] = definition.attributes['updated_at'] = { 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) => { const morphRelations = definition.associations.find((association) => {
return association.nature.toLowerCase().includes('morph'); return association.nature.toLowerCase().includes('morph');
}); });
if (morphRelations) { if (morphRelations) {
const tableExist = await ORM.knex.schema.hasTable(`${loadedModel.tableName}_morph`);
const attributes = { const attributes = {
[`${loadedModel.tableName}_id`]: { [`${loadedModel.tableName}_id`]: {
type: 'integer' type: 'integer'
@ -440,7 +442,7 @@ module.exports = function(strapi) {
} }
}; };
await handler(`${loadedModel.tableName}_morph`, attributes, tableExist); await handler(`${loadedModel.tableName}_morph`, attributes);
} }
resolve(); resolve();