mirror of
https://github.com/strapi/strapi.git
synced 2025-09-01 12:53:03 +00:00
Upload setup in bookshelf and create morph tables
This commit is contained in:
parent
79ddc27cb1
commit
1ae9e65b42
@ -310,10 +310,105 @@ 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 tableExist = await ORM.knex.schema.hasTable(loadedModel.tableName);
|
const handler = async (table, attributes, tableExist) => {
|
||||||
|
// Generate fields type
|
||||||
|
const generateColumns = (attrs, start) => {
|
||||||
|
return Object.keys(attrs).reduce((acc, attr) => {
|
||||||
|
const attribute = attributes[attr];
|
||||||
|
|
||||||
|
let type;
|
||||||
|
|
||||||
|
if (!attribute.type) {
|
||||||
|
const relation = definition.associations.find((association) => {
|
||||||
|
return association.alias === attr;
|
||||||
|
});
|
||||||
|
|
||||||
|
switch (relation.nature) {
|
||||||
|
case 'manyToOne':
|
||||||
|
type = definition.client === 'pg' ? 'integer' : 'int';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return acc;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
switch (attribute.type) {
|
||||||
|
case 'string':
|
||||||
|
case 'text':
|
||||||
|
case 'password':
|
||||||
|
case 'email':
|
||||||
|
case 'json':
|
||||||
|
type = 'text';
|
||||||
|
break;
|
||||||
|
case 'integer':
|
||||||
|
case 'biginteger':
|
||||||
|
case 'float':
|
||||||
|
case 'decimal':
|
||||||
|
type = definition.client === 'pg' ? 'integer' : 'int';
|
||||||
|
break;
|
||||||
|
case 'date':
|
||||||
|
case 'time':
|
||||||
|
case 'datetime':
|
||||||
|
case 'timestamp':
|
||||||
|
type = definition.client === 'pg' ? 'timestamp with time zone' : 'timestamp';
|
||||||
|
break;
|
||||||
|
case 'boolean':
|
||||||
|
type = 'boolean';
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
acc.push(`${quote}${attr}${quote} ${type}`);
|
||||||
|
|
||||||
|
return acc;
|
||||||
|
}, start);
|
||||||
|
};
|
||||||
|
|
||||||
|
if (!tableExist) {
|
||||||
|
const columns = generateColumns(attributes, [`id ${definition.client === 'pg' ? 'SERIAL' : 'INT AUTO_INCREMENT'} NOT NULL PRIMARY KEY`]).join(',\n\r');
|
||||||
|
|
||||||
|
// Create table
|
||||||
|
await ORM.knex.raw(`
|
||||||
|
CREATE TABLE ${quote}${table}${quote} (
|
||||||
|
${columns}
|
||||||
|
)
|
||||||
|
`);
|
||||||
|
} else {
|
||||||
|
const columns = Object.keys(attributes);
|
||||||
|
|
||||||
|
// Fetch existing column
|
||||||
|
const columnsExist = await Promise.all(columns.map(attribute =>
|
||||||
|
ORM.knex.schema.hasColumn(table, attribute)
|
||||||
|
));
|
||||||
|
|
||||||
|
const columnsToAdd = {};
|
||||||
|
|
||||||
|
// Get columns to add
|
||||||
|
columnsExist.forEach((columnExist, index) => {
|
||||||
|
const attribute = attributes[columns[index]];
|
||||||
|
|
||||||
|
if (!columnExist) {
|
||||||
|
columnsToAdd[columns[index]] = attribute;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
// Generate and execute query to add missing column
|
||||||
|
if (Object.keys(columnsToAdd).length > 0) {
|
||||||
|
const columns = generateColumns(columnsToAdd, []);
|
||||||
|
const queries = columns.reduce((acc, attribute) => {
|
||||||
|
acc.push(`ALTER TABLE ${quote}${table}${quote} ADD ${attribute};`)
|
||||||
|
return acc;
|
||||||
|
}, []).join('\n\r');
|
||||||
|
|
||||||
|
await ORM.knex.raw(queries);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
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'] = {
|
||||||
@ -321,97 +416,31 @@ module.exports = function(strapi) {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
// Generate fields type
|
await handler(loadedModel.tableName, definition.attributes, tableExist);
|
||||||
const generateColumns = (attributes, start) => {
|
|
||||||
return Object.keys(attributes).reduce((acc, attr) => {
|
|
||||||
const attribute = definition.attributes[attr];
|
|
||||||
|
|
||||||
let type;
|
const morphRelations = definition.associations.find((association) => {
|
||||||
|
return association.nature.toLowerCase().includes('morph');
|
||||||
|
});
|
||||||
|
|
||||||
if (!attribute.type) {
|
if (morphRelations) {
|
||||||
const relation = definition.associations.find((association) => {
|
const tableExist = await ORM.knex.schema.hasTable(`${loadedModel.tableName}_morph`);
|
||||||
return association.alias === attr;
|
|
||||||
});
|
|
||||||
|
|
||||||
switch (relation.nature) {
|
const attributes = {
|
||||||
case 'manyToOne':
|
[`${loadedModel.tableName}_id`]: {
|
||||||
type = definition.client === 'pg' ? 'integer' : 'int';
|
type: 'integer'
|
||||||
break;
|
},
|
||||||
default:
|
[`${morphRelations.alias}_id`]: {
|
||||||
return acc;
|
type: 'integer'
|
||||||
}
|
},
|
||||||
} else {
|
[`${morphRelations.alias}_type`]: {
|
||||||
switch (attribute.type) {
|
type: 'text'
|
||||||
case 'string':
|
},
|
||||||
case 'text':
|
[definition.attributes[morphRelations.alias].filter]: {
|
||||||
case 'password':
|
type: 'text'
|
||||||
case 'email':
|
|
||||||
case 'json':
|
|
||||||
type = 'text';
|
|
||||||
break;
|
|
||||||
case 'integer':
|
|
||||||
case 'biginteger':
|
|
||||||
case 'float':
|
|
||||||
case 'decimal':
|
|
||||||
type = definition.client === 'pg' ? 'integer' : 'int';
|
|
||||||
break;
|
|
||||||
case 'date':
|
|
||||||
case 'time':
|
|
||||||
case 'datetime':
|
|
||||||
case 'timestamp':
|
|
||||||
type = definition.client === 'pg' ? 'timestamp with time zone' : 'timestamp';
|
|
||||||
break;
|
|
||||||
case 'boolean':
|
|
||||||
type = 'boolean';
|
|
||||||
break;
|
|
||||||
default:
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
};
|
||||||
|
|
||||||
acc.push(`${quote}${attr}${quote} ${type}`);
|
await handler(`${loadedModel.tableName}_morph`, attributes, tableExist);
|
||||||
|
|
||||||
return acc;
|
|
||||||
}, start);
|
|
||||||
};
|
|
||||||
|
|
||||||
if (!tableExist) {
|
|
||||||
const columns = generateColumns(definition.attributes, [`id ${definition.client === 'pg' ? 'SERIAL' : 'INT AUTO_INCREMENT'} NOT NULL PRIMARY KEY`]).join(',\n\r');
|
|
||||||
|
|
||||||
// Create table
|
|
||||||
await ORM.knex.raw(`
|
|
||||||
CREATE TABLE ${quote}${loadedModel.tableName}${quote} (
|
|
||||||
${columns}
|
|
||||||
)
|
|
||||||
`);
|
|
||||||
} else {
|
|
||||||
const columns = Object.keys(definition.attributes);
|
|
||||||
|
|
||||||
// Fetch existing column
|
|
||||||
const columnsExist = await Promise.all(columns.map(attribute =>
|
|
||||||
ORM.knex.schema.hasColumn(loadedModel.tableName, attribute)
|
|
||||||
));
|
|
||||||
|
|
||||||
const columnsToAdd = {};
|
|
||||||
|
|
||||||
// Get columns to add
|
|
||||||
columnsExist.forEach((columnExist, index) => {
|
|
||||||
const attribute = definition.attributes[columns[index]];
|
|
||||||
|
|
||||||
if (!columnExist) {
|
|
||||||
columnsToAdd[columns[index]] = attribute;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// Generate and execute query to add missing column
|
|
||||||
if (Object.keys(columnsToAdd).length > 0) {
|
|
||||||
const columns = generateColumns(columnsToAdd, []);
|
|
||||||
const queries = columns.reduce((acc, attribute) => {
|
|
||||||
acc.push(`ALTER TABLE ${quote}${loadedModel.tableName}${quote} ADD ${attribute};`)
|
|
||||||
return acc;
|
|
||||||
}, []).join('\n\r');
|
|
||||||
|
|
||||||
await ORM.knex.raw(queries);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
resolve();
|
resolve();
|
||||||
|
@ -13,39 +13,6 @@ const _ = require('lodash');
|
|||||||
const fs = require('fs');
|
const fs = require('fs');
|
||||||
|
|
||||||
module.exports = async cb => {
|
module.exports = async cb => {
|
||||||
const Model = strapi.plugins.upload.models.file;
|
|
||||||
|
|
||||||
if (Model.orm === 'bookshelf') {
|
|
||||||
const hasTable = await strapi.connections[Model.connection].schema.hasTable(Model.tableName || Model.collectionName);
|
|
||||||
|
|
||||||
if (!hasTable) {
|
|
||||||
const quote = Model.client === 'pg' ? '"' : '`';
|
|
||||||
|
|
||||||
await strapi.connections[Model.connection].raw(`
|
|
||||||
CREATE TABLE ${quote}${Model.tableName || Model.collectionName}${quote} (
|
|
||||||
id ${Model.client === 'pg' ? 'SERIAL' : 'INT AUTO_INCREMENT'} NOT NULL PRIMARY KEY,
|
|
||||||
name text,
|
|
||||||
hash text,
|
|
||||||
ext text,
|
|
||||||
mime text,
|
|
||||||
size text,
|
|
||||||
url text,
|
|
||||||
provider text,
|
|
||||||
updated_at ${Model.client === 'pg' ? 'timestamp with time zone' : 'timestamp'},
|
|
||||||
created_at ${Model.client === 'pg' ? 'timestamp with time zone' : 'timestamp'}
|
|
||||||
);
|
|
||||||
|
|
||||||
CREATE TABLE ${quote}upload_file_morph${quote} (
|
|
||||||
id ${Model.client === 'pg' ? 'SERIAL' : 'INT AUTO_INCREMENT'} NOT NULL PRIMARY KEY,
|
|
||||||
upload_file_id ${Model.client === 'pg' ? 'integer' : 'int'},
|
|
||||||
related_id ${Model.client === 'pg' ? 'integer' : 'int'},
|
|
||||||
related_type text,
|
|
||||||
field text
|
|
||||||
);
|
|
||||||
`);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// set plugin store
|
// set plugin store
|
||||||
const pluginStore = strapi.store({
|
const pluginStore = strapi.store({
|
||||||
environment: strapi.config.environment,
|
environment: strapi.config.environment,
|
||||||
|
Loading…
x
Reference in New Issue
Block a user