diff --git a/packages/strapi-generate-api/lib/before.js b/packages/strapi-generate-api/lib/before.js index 756db00a1b..4714b92167 100755 --- a/packages/strapi-generate-api/lib/before.js +++ b/packages/strapi-generate-api/lib/before.js @@ -118,6 +118,9 @@ module.exports = (scope, cb) => { // Set collectionName scope.collectionName = _.get(scope.args, 'collectionName', undefined); + // Set description + scope.description = _.get(scope.args, 'description', undefined); + // Get default connection try { scope.connection = _.get(scope.args, 'connection') || JSON.parse(fs.readFileSync(path.resolve(scope.rootPath, 'config', 'environments', scope.environment, 'database.json'))).defaultConnection || ''; diff --git a/packages/strapi-generate-api/templates/bookshelf/model.settings.template b/packages/strapi-generate-api/templates/bookshelf/model.settings.template index 12112c36cc..4f4c18b68f 100755 --- a/packages/strapi-generate-api/templates/bookshelf/model.settings.template +++ b/packages/strapi-generate-api/templates/bookshelf/model.settings.template @@ -1,6 +1,7 @@ { "connection": "<%= connection %>", - "collectionName": "<%= idPluralized %>", + "description": "<%= description %>", + "collectionName": "<%= collectionName || idPluralized %>", "options": { "increments": true, "timestamps": true, diff --git a/packages/strapi-generate-api/templates/mongoose/model.settings.template b/packages/strapi-generate-api/templates/mongoose/model.settings.template index 84a7578d28..80ed29bad1 100755 --- a/packages/strapi-generate-api/templates/mongoose/model.settings.template +++ b/packages/strapi-generate-api/templates/mongoose/model.settings.template @@ -1,5 +1,6 @@ { "connection": "<%= connection %>", + "description": "<%= description %>", "collectionName": "<%= collectionName || idPluralized %>", "attributes": { <%= attributes %> diff --git a/packages/strapi-mongoose/lib/index.js b/packages/strapi-mongoose/lib/index.js index 8d907402a1..38640400ce 100644 --- a/packages/strapi-mongoose/lib/index.js +++ b/packages/strapi-mongoose/lib/index.js @@ -205,6 +205,8 @@ module.exports = function (strapi) { _.forEach(definition.attributes, (details, name) => { const verbose = _.get(utilsModels.getNature(details, name), 'verbose') || ''; + console.log("coucou", details, name); + // Build associations key if (!_.isEmpty(verbose)) { utilsModels.defineAssociations(globalName, definition, details, name); diff --git a/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js b/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js index 06618c43b8..6da9b36999 100644 --- a/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js +++ b/packages/strapi-plugin-content-type-builder/controllers/ContentTypeBuilder.js @@ -25,14 +25,22 @@ module.exports = { }, createModel: async ctx => { - const { name, connection, collectionName, attributes = [] } = JSON.parse(ctx.request.body); + const { name, description, connection, collectionName, attributes = [] } = JSON.parse(ctx.request.body); if (!name) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.name.missing' }] }]); + if (!_.includes(Service.getConnections(), connection)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.connection.unknow' }] }]); if (strapi.models[name]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.exist' }] }]); + if (!_.isNaN(parseFloat(name[0]))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.name' }] }]); + + const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes); + + if (!_.isEmpty(attributesErrors)) { + return ctx.badRequest(null, [{ messages: attributesErrors }]); + } strapi.reload.isWatching = false; - await Service.generateAPI(name, connection, collectionName, attributes); + await Service.generateAPI(name, description, connection, collectionName, []); const [modelFilePath, modelFilePathErrors] = Service.getModelPath(name); @@ -43,7 +51,7 @@ module.exports = { try { const modelJSON = require(modelFilePath); - modelJSON.attributes = Service.formatAttributes(attributes); + modelJSON.attributes = formatedAttributes; const clearRelationsErrors = Service.clearRelations(name); @@ -73,9 +81,18 @@ module.exports = { updateModel: async ctx => { const { model } = ctx.params; - const { name, attributes = [] } = JSON.parse(ctx.request.body); + const { name, description, connection, collectionName, attributes = [] } = JSON.parse(ctx.request.body); - if (!_.get(strapi.models, model)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.unknow' }] }]); + if (!name) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.name.missing' }] }]); + if (!_.includes(Service.getConnections(), connection)) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.connection.unknow' }] }]); + if (!strapi.models[name]) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.unknow' }] }]); + if (!_.isNaN(parseFloat(name[0]))) return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.name' }] }]); + + const [formatedAttributes, attributesErrors] = Service.formatAttributes(attributes); + + if (!_.isEmpty(attributesErrors)) { + return ctx.badRequest(null, [{ messages: attributesErrors }]); + } const [modelFilePath, modelFilePathErrors] = Service.getModelPath(model); @@ -86,7 +103,10 @@ module.exports = { try { const modelJSON = require(modelFilePath); - modelJSON.attributes = Service.formatAttributes(attributes); + modelJSON.attributes = formatedAttributes; + modelJSON.description = description; + modelJSON.description = connection; + modelJSON.collectionName = collectionName; strapi.reload.isWatching = false; @@ -112,7 +132,6 @@ module.exports = { return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.write' }] }]); } } catch (e) { - console.log(e); return ctx.badRequest(null, [{ messages: [{ id: 'request.error.model.read' }] }]); } }, diff --git a/packages/strapi-plugin-content-type-builder/services/ContentTypeBuilder.js b/packages/strapi-plugin-content-type-builder/services/ContentTypeBuilder.js index c8be70693c..ef518993d4 100644 --- a/packages/strapi-plugin-content-type-builder/services/ContentTypeBuilder.js +++ b/packages/strapi-plugin-content-type-builder/services/ContentTypeBuilder.js @@ -50,16 +50,10 @@ module.exports = { }, getConnections: () => { - let connections = []; - - _.forEach(_.keys(strapi.config.environments), env => { - connections = _.assign(connections, _.keys(strapi.config.environments[env].database.connections)); - }); - - return _.compact(connections); + return _.keys(strapi.config.currentEnvironment.database.connections); }, - generateAPI: (name, connection, collectionName, attributes) => { + generateAPI: (name, description, connection, collectionName, attributes) => { return new Promise((resolve, reject) => { const scope = { generatorType: 'api', @@ -67,6 +61,7 @@ module.exports = { rootPath: strapi.config.appPath, args: { api: name, + description, attributes, connection, collectionName: !_.isEmpty(collectionName) ? collectionName : undefined @@ -124,13 +119,14 @@ module.exports = { }, formatAttributes: attributes => { + const errors = []; const attrs = {}; _.forEach(attributes, attribute => { if (_.has(attribute, 'params.type')) { - attrs[attribute.name] = _.get(attribute, 'params'); + attrs[attribute.name] = attribute.params; } else if (_.has(attribute, 'params.target')) { - const relation = _.get(attribute, 'params'); + const relation = attribute.params; const attr = { required: relation.required, columnName: relation.columnName, @@ -143,8 +139,8 @@ module.exports = { case 'manyToOne': attr.model = relation.target; break; - case 'oneToMany': case 'manyToMany': + case 'oneToMany': attr.collection = relation.target; break; default: @@ -152,9 +148,18 @@ module.exports = { attrs[attribute.name] = attr; } + + if (!_.isNaN(parseFloat(attribute.name[0])) || !_.isNaN(parseFloat(_.get(attribute, 'params.key'), NaN))) { + errors.push({ + id: 'request.error.attribute.values', + params: { + attribute + } + }); + } }); - return attrs; + return [attrs, errors]; }, clearRelations: model => { diff --git a/packages/strapi-plugin-settings-manager/package.json b/packages/strapi-plugin-settings-manager/package.json index c4f169ec22..e0969b3e04 100644 --- a/packages/strapi-plugin-settings-manager/package.json +++ b/packages/strapi-plugin-settings-manager/package.json @@ -65,4 +65,4 @@ "npm": ">= 3.0.0" }, "license": "MIT" -} \ No newline at end of file +} diff --git a/packages/strapi-utils/lib/models.js b/packages/strapi-utils/lib/models.js index 20d2ebb370..bc2e364cf5 100644 --- a/packages/strapi-utils/lib/models.js +++ b/packages/strapi-utils/lib/models.js @@ -167,7 +167,7 @@ module.exports = { nature: 'oneToOne', verbose: 'hasOne' }; - } else if (types.current === 'model' && types.other === 'collection') { + } else if ((types.current === 'model' || types.current === 'modelD') && types.other === 'collection') { return { nature: 'oneToMany', verbose: 'belongsTo'