From 55d65ccde839fc93377a35ea2bd31cf3cf38c683 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Georget?= Date: Fri, 12 Aug 2016 12:04:00 +0200 Subject: [PATCH] Fix #90: Update global model naming convention --- packages/strapi-bookshelf/lib/index.js | 2 +- .../strapi-generate-api/json/routes.json.js | 146 +++++------------- packages/strapi-generate-api/lib/before.js | 9 +- packages/strapi-generate-api/lib/index.js | 10 +- .../templates/mongoose/controller.template | 20 +-- .../mongoose/model.settings.template | 2 +- .../templates/mongoose/service.template | 24 +-- .../strapi-generate-controller/lib/before.js | 8 +- packages/strapi-generate-model/lib/before.js | 12 +- .../bookshelf/model.settings.template | 2 +- .../mongoose/model.settings.template | 2 +- .../strapi-generate-service/lib/before.js | 9 +- .../templates/bookshelf/service.template | 8 +- .../templates/mongoose/service.template | 4 +- packages/strapi-mongoose/lib/index.js | 8 +- 15 files changed, 94 insertions(+), 172 deletions(-) diff --git a/packages/strapi-bookshelf/lib/index.js b/packages/strapi-bookshelf/lib/index.js index 1195ffdd7e..6fbee6938b 100755 --- a/packages/strapi-bookshelf/lib/index.js +++ b/packages/strapi-bookshelf/lib/index.js @@ -61,7 +61,7 @@ module.exports = function (strapi) { // Parse every registered model. _.forEach(strapi.models, function (definition, model) { - globalName = _.capitalize(definition.globalId); + globalName = _.upperFirst(_.camelCase(definition.globalId)); // Make sure the model has a table name. // If not, use the model name. diff --git a/packages/strapi-generate-api/json/routes.json.js b/packages/strapi-generate-api/json/routes.json.js index 177e40e608..51d61990c8 100755 --- a/packages/strapi-generate-api/json/routes.json.js +++ b/packages/strapi-generate-api/json/routes.json.js @@ -20,123 +20,47 @@ module.exports = scope => { routes: {} }; - // JSON API support is enabled or not. - let hasJSONAPI = false; + newRoutes.routes['GET /' + scope.humanizeId] = { + controller: scope.globalID, + action: 'find', + policies: [] + }; - try { - const JSONAPI = JSON.parse(fs.readFileSync(path.resolve(process.cwd(), 'config', 'general.json'))).jsonapi; + newRoutes.routes['GET /' + scope.humanizeId + '/:id'] = { + controller: scope.globalID, + action: 'findOne', + policies: [] + }; - if (_.isPlainObject(JSONAPI) && _.get(JSONAPI, 'enabled') === true) { - hasJSONAPI = true; - } - } catch (err) { - throw err; - } + newRoutes.routes['POST /' + scope.humanizeId] = { + controller: scope.globalID, + action: 'create', + policies: [] + }; - // JSON API enabled - if (hasJSONAPI) { - newRoutes.routes['GET /' + scope.idPluralized] = { - controller: scope.globalID, - action: 'find', - policies: [] - }; + newRoutes.routes['PUT /' + scope.humanizeId + '/:id'] = { + controller: scope.globalID, + action: 'update', + policies: [] + }; - newRoutes.routes['GET /' + scope.id + '/:id'] = { - controller: scope.globalID, - action: 'findOne', - policies: [] - }; + newRoutes.routes['DELETE /' + scope.humanizeId + '/:id'] = { + controller: scope.globalID, + action: 'destroy', + policies: [] + }; - newRoutes.routes['GET /' + scope.id + '/:id/relationships/:relation'] = { - controller: scope.globalID, - action: 'findOne', - policies: [] - }; + newRoutes.routes['POST /' + scope.humanizeId + '/:id/relationships/:relation'] = { + controller: scope.globalID, + action: 'createRelation', + policies: [] + }; - newRoutes.routes['GET /' + scope.id + '/:id/:relation'] = { - controller: scope.globalID, - action: 'findOne', - policies: [] - }; - - newRoutes.routes['POST /' + scope.id] = { - controller: scope.globalID, - action: 'create', - policies: [] - }; - - newRoutes.routes['PATCH /' + scope.id + '/:id'] = { - controller: scope.globalID, - action: 'update', - policies: [] - }; - - newRoutes.routes['DELETE /' + scope.id + '/:id'] = { - controller: scope.globalID, - action: 'destroy', - policies: [] - }; - - newRoutes.routes['POST /' + scope.id + '/:id/relationships/:relation'] = { - controller: scope.globalID, - action: 'createRelation', - policies: [] - }; - - newRoutes.routes['PATCH /' + scope.id + '/:id/relationships/:relation'] = { - controller: scope.globalID, - action: 'updateRelation', - policies: [] - }; - - newRoutes.routes['DELETE /' + scope.id + '/:id/relationships/:relation'] = { - controller: scope.globalID, - action: 'destroyRelation', - policies: [] - }; - } else { - newRoutes.routes['GET /' + scope.id] = { - controller: scope.globalID, - action: 'find', - policies: [] - }; - - newRoutes.routes['GET /' + scope.id + '/:id'] = { - controller: scope.globalID, - action: 'findOne', - policies: [] - }; - - newRoutes.routes['POST /' + scope.id] = { - controller: scope.globalID, - action: 'create', - policies: [] - }; - - newRoutes.routes['PUT /' + scope.id + '/:id'] = { - controller: scope.globalID, - action: 'update', - policies: [] - }; - - newRoutes.routes['DELETE /' + scope.id + '/:id'] = { - controller: scope.globalID, - action: 'destroy', - policies: [] - }; - - newRoutes.routes['POST /' + scope.id + '/:parentId/:relation'] = { - controller: scope.globalID, - action: 'createRelation', - policies: [] - }; - - newRoutes.routes['DELETE /' + scope.id + '/:parentId/:relation/:id'] = { - controller: scope.globalID, - action: 'destroyRelation', - policies: [] - }; - } + newRoutes.routes['DELETE /' + scope.humanizeId + '/:id/relationships/:relation'] = { + controller: scope.globalID, + action: 'destroyRelation', + policies: [] + }; return newRoutes; }; diff --git a/packages/strapi-generate-api/lib/before.js b/packages/strapi-generate-api/lib/before.js index c81b534bb4..bebe268cc6 100755 --- a/packages/strapi-generate-api/lib/before.js +++ b/packages/strapi-generate-api/lib/before.js @@ -27,14 +27,14 @@ module.exports = (scope, cb) => { // `scope.args` are the raw command line arguments. _.defaults(scope, { - id: scope.args[0], - idPluralized: pluralize.plural(scope.args[0]), + id: _.trim(_.deburr(scope.args[0])), + idPluralized: pluralize.plural(_.trim(_.deburr(scope.args[0]))), environment: process.NODE_ENV || 'development' }); // Determine default values based on the available scope. _.defaults(scope, { - globalID: _.capitalize(scope.id), + globalID: _.upperFirst(_.camelCase(scope.id)), ext: '.js' }); @@ -47,7 +47,8 @@ module.exports = (scope, cb) => { // Humanize output. _.defaults(scope, { - humanizeId: scope.args[0], + humanizeId: _.camelCase(scope.id).toLowerCase(), + humanizeIdPluralized: pluralize.plural(_.camelCase(scope.id).toLowerCase()), humanizedPath: '`./api`' }); diff --git a/packages/strapi-generate-api/lib/index.js b/packages/strapi-generate-api/lib/index.js index d34c921911..572da22d28 100755 --- a/packages/strapi-generate-api/lib/index.js +++ b/packages/strapi-generate-api/lib/index.js @@ -32,28 +32,28 @@ module.exports = { // Use the default `controller` file as a template for // every generated controller. - 'api/:id/controllers/:filename': { + 'api/:humanizeId/controllers/:filename': { template: 'controller.template' }, // every generated controller. - 'api/:id/services/:filename': { + 'api/:humanizeId/services/:filename': { template: 'service.template' }, // Copy an empty JavaScript model where every functions will be. - 'api/:id/models/:filename': { + 'api/:humanizeId/models/:filename': { template: 'model.template' }, // Copy the generated JSON model for the connection, // schema and attributes. - 'api/:id/models/:filenameSettings': { + 'api/:humanizeId/models/:filenameSettings': { template: 'model.settings.template' }, // Generate routes. - 'api/:id/config/routes.json': { + 'api/:humanizeId/config/routes.json': { jsonfile: routesJSON } } diff --git a/packages/strapi-generate-api/templates/mongoose/controller.template b/packages/strapi-generate-api/templates/mongoose/controller.template index 8439ff805f..3dc23e72df 100755 --- a/packages/strapi-generate-api/templates/mongoose/controller.template +++ b/packages/strapi-generate-api/templates/mongoose/controller.template @@ -6,70 +6,70 @@ module.exports = { /** - * Get <%= id %> entries. + * Get <%= humanizeId %> entries. * * @return {Object|Array} */ find: function * () { try { - this.body = yield strapi.services.<%= id %>.fetchAll(this.query); + this.body = yield strapi.services.<%= humanizeId %>.fetchAll(this.query); } catch (err) { this.body = err; } }, /** - * Get a specific <%= id %>. + * Get a specific <%= humanizeId %>. * * @return {Object|Array} */ findOne: function * () { try { - this.body = yield strapi.services.<%= id %>.fetch(this.params) + this.body = yield strapi.services.<%= humanizeId %>.fetch(this.params) } catch (err) { this.body = err; } }, /** - * Create a/an <%= id %> entry. + * Create a/an <%= humanizeId %> entry. * * @return {Object} */ create: function * () { try { - this.body = yield strapi.services.<%= id %>.add(this.request.body); + this.body = yield strapi.services.<%= humanizeId %>.add(this.request.body); } catch (err) { this.body = err; } }, /** - * Update a/an <%= id %> entry. + * Update a/an <%= humanizeId %> entry. * * @return {Object} */ update: function * () { try { - this.body = yield strapi.services.<%= id %>.edit(this.params, this.request.body) ; + this.body = yield strapi.services.<%= humanizeId %>.edit(this.params, this.request.body) ; } catch (err) { this.body = err; } }, /** - * Destroy a/an <%= id %> entry. + * Destroy a/an <%= humanizeId %> entry. * * @return {Object} */ destroy: function * () { try { - this.body = yield strapi.services.<%= id %>.remove(this.params); + this.body = yield strapi.services.<%= humanizeId %>.remove(this.params); } catch (err) { this.body = err; } diff --git a/packages/strapi-generate-api/templates/mongoose/model.settings.template b/packages/strapi-generate-api/templates/mongoose/model.settings.template index 00a0f6d020..67dae50239 100755 --- a/packages/strapi-generate-api/templates/mongoose/model.settings.template +++ b/packages/strapi-generate-api/templates/mongoose/model.settings.template @@ -1,6 +1,6 @@ { "connection": "<%= connection %>", - "collectionName": "<%= id %>", + "collectionName": "<%= humanizeId %>", "attributes": { } diff --git a/packages/strapi-generate-api/templates/mongoose/service.template b/packages/strapi-generate-api/templates/mongoose/service.template index fe1bb1c303..5d9ce72c75 100755 --- a/packages/strapi-generate-api/templates/mongoose/service.template +++ b/packages/strapi-generate-api/templates/mongoose/service.template @@ -14,20 +14,20 @@ const _ = require('lodash'); module.exports = { /** - * Promise to fetch all <%= idPluralized %>. + * Promise to fetch all <%= humanizeIdPluralized %>. * * @return {Promise} */ fetchAll: params => { return new Promise((resolve, reject) => { - <%= globalID %>.find(params).populate(_.keys(_.pickBy(strapi.models.<%= id %>.attributes, { autoPopulate: true })).join(' ')) - .exec((err, <%= idPluralized %> =>) => { + <%= globalID %>.find(params).populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).join(' ')) + .exec((err, <%= idPluralized %>) => { if (err) { return reject(err); } - resolve(<%= idPluralized %> =>); + resolve(<%= idPluralized %>); }); }); }, @@ -40,13 +40,13 @@ module.exports = { fetch: params => { return new Promise((resolve, reject) => { - <%= globalID %>.findOne(params).populate(_.keys(_.pickBy(strapi.models.<%= id %>.attributes, { autoPopulate: true })).join(' ')) - .exec((err, <%= id %>) => { + <%= globalID %>.findOne(params).populate(_.keys(_.pickBy(strapi.models.<%= humanizeId %>.attributes, { autoPopulate: true })).join(' ')) + .exec((err, <%= humanizeId %>) => { if (err) { return reject(err); } - resolve(<%= id %>); + resolve(<%= humanizeId %>); }); }); }, @@ -59,9 +59,9 @@ module.exports = { add: values => { return new Promise((resolve, reject) => { - const <%= id %> = new <%= globalID %>(values); + const <%= humanizeId %> = new <%= globalID %>(values); - <%= id %>.save((err, <%= id %>) => { + <%= humanizeId %>.save((err, <%= humanizeId %>) => { if (err) { return reject(err); } @@ -83,7 +83,7 @@ module.exports = { if (err) { return reject(err); } - + // NB: Raw contains the full response of Mongo. // To get the updated object, you have to execute the `findOne()` method // or use the `findOneOrUpdate()` method with `{ new:true }` option. @@ -101,7 +101,7 @@ module.exports = { remove: params => { return new Promise((resolve, reject) => { - <%= globalID %>.findOneAndRemove(params, {}, (err, <%= id %>) => { + <%= globalID %>.findOneAndRemove(params, {}, (err, <%= humanizeId %>) => { if (err) { return reject(err); } @@ -109,7 +109,7 @@ module.exports = { // NB: To get the full response of Mongo, use the `remove()` method // or add spent the parameter `{ passRawResult: true }` as second argument. - resolve(<%= id %>); + resolve(<%= humanizeId %>); }); }); } diff --git a/packages/strapi-generate-controller/lib/before.js b/packages/strapi-generate-controller/lib/before.js index e72dee9c05..92db99994b 100755 --- a/packages/strapi-generate-controller/lib/before.js +++ b/packages/strapi-generate-controller/lib/before.js @@ -22,25 +22,25 @@ module.exports = (scope, cb) => { // `scope.args` are the raw command line arguments. _.defaults(scope, { - id: scope.args[0], + id: _.trim(_.deburr(scope.args[0])), api: scope.args[1] }); // Determine default values based on the available scope. _.defaults(scope, { - globalID: _.capitalize(scope.id), + globalID: _.upperFirst(_.camelCase(scope.id)), ext: '.js' }); // Take another pass to take advantage of the defaults absorbed in previous passes. _.defaults(scope, { rootPath: scope.rootPath, - filename: _.capitalize(scope.id + scope.ext) + filename: scope.globalID + scope.ext }); // Humanize output. _.defaults(scope, { - humanizeId: scope.args[0], + humanizeId: _.camelCase(scope.id).toLowerCase(), humanizedPath: '`./api/' + scope.api + '/controllers`' }); diff --git a/packages/strapi-generate-model/lib/before.js b/packages/strapi-generate-model/lib/before.js index 300658c0c6..e78ca34c8a 100755 --- a/packages/strapi-generate-model/lib/before.js +++ b/packages/strapi-generate-model/lib/before.js @@ -26,7 +26,7 @@ module.exports = (scope, cb) => { // `scope.args` are the raw command line arguments. _.defaults(scope, { - id: scope.args[0], + id: _.trim(_.deburr(scope.args[0])), attributes: _.takeRight(scope.args, _.size(scope.args) - 1), api: {}, environment: process.NODE_ENV || 'development' @@ -34,7 +34,7 @@ module.exports = (scope, cb) => { // Determine default values based on the available scope. _.defaults(scope, { - globalID: _.capitalize(scope.id), + globalID: _.upperFirst(_.camelCase(scope.id)), ext: '.js' }); @@ -47,16 +47,16 @@ module.exports = (scope, cb) => { // Humanize output. _.defaults(scope, { - humanizeId: scope.args[0], - humanizedPath: '`./api/' + scope.globalID + '/models`' + humanizeId: _.camelCase(scope.id).toLowerCase(), + humanizedPath: '`./api/' + scope.id + '/models`' }); _.forEach(scope.attributes, attribute => { const object = attribute.split(':'); if (_.size(object) === 2) { - scope.api[_.first(object)] = { - type: _.last(object) + scope.api[_.trim(_.deburr(_.camelCase(_.first(object)).toLowerCase()))] = { + type: _.trim(_.deburr(_.camelCase(_.last(object).toLowerCase()))) } } }); diff --git a/packages/strapi-generate-model/templates/bookshelf/model.settings.template b/packages/strapi-generate-model/templates/bookshelf/model.settings.template index aa999f9208..e214e60430 100755 --- a/packages/strapi-generate-model/templates/bookshelf/model.settings.template +++ b/packages/strapi-generate-model/templates/bookshelf/model.settings.template @@ -1,6 +1,6 @@ { "connection": "<%= connection %>", - "tableName": "<%= id %>", + "tableName": "<%= humanizeId %>", "options": { "increments": true, "timestamps": true, diff --git a/packages/strapi-generate-model/templates/mongoose/model.settings.template b/packages/strapi-generate-model/templates/mongoose/model.settings.template index da175a8dd9..0c1d620f8b 100755 --- a/packages/strapi-generate-model/templates/mongoose/model.settings.template +++ b/packages/strapi-generate-model/templates/mongoose/model.settings.template @@ -1,5 +1,5 @@ { "connection": "<%= connection %>", - "collectionName": "<%= id %>", + "collectionName": "<%= humanizeId %>", "attributes": <%- JSON.stringify(api, null, '\t') %> } diff --git a/packages/strapi-generate-service/lib/before.js b/packages/strapi-generate-service/lib/before.js index b0249c0850..2f296bc460 100755 --- a/packages/strapi-generate-service/lib/before.js +++ b/packages/strapi-generate-service/lib/before.js @@ -22,24 +22,25 @@ module.exports = (scope, cb) => { // `scope.args` are the raw command line arguments. _.defaults(scope, { - id: scope.args[0], + id: _.trim(_.deburr(scope.args[0])), api: scope.args[1] }); // Determine default values based on the available scope. _.defaults(scope, { - ext: '.js' + globalID: _.upperFirst(_.camelCase(scope.id)), + ext: '.js', }); // Take another pass to take advantage of the defaults absorbed in previous passes. _.defaults(scope, { rootPath: scope.rootPath, - filename: scope.id + scope.ext + filename: scope.globalID + scope.ext }); // Humanize output. _.defaults(scope, { - humanizeId: scope.args[0], + humanizeId: _.camelCase(scope.id).toLowerCase(), humanizedPath: '`./api/' + scope.api + '/services`' }); diff --git a/packages/strapi-generate-service/templates/bookshelf/service.template b/packages/strapi-generate-service/templates/bookshelf/service.template index 758ada280a..511a0c9ac7 100755 --- a/packages/strapi-generate-service/templates/bookshelf/service.template +++ b/packages/strapi-generate-service/templates/bookshelf/service.template @@ -1,9 +1,11 @@ 'use strict'; /** - * `<%= id %>` service. + * `<%= globalID %>` service. */ -exports.<%= id %> = function () { - +module.exports = { + // exampleService: (arg1, arg2) => { + // return isUserOnline(arg1, arg2); + // } }; diff --git a/packages/strapi-generate-service/templates/mongoose/service.template b/packages/strapi-generate-service/templates/mongoose/service.template index c73cddd3d0..511a0c9ac7 100755 --- a/packages/strapi-generate-service/templates/mongoose/service.template +++ b/packages/strapi-generate-service/templates/mongoose/service.template @@ -1,10 +1,10 @@ 'use strict'; /** - * `<%= id %>` service. + * `<%= globalID %>` service. */ -modules.exports = { +module.exports = { // exampleService: (arg1, arg2) => { // return isUserOnline(arg1, arg2); // } diff --git a/packages/strapi-mongoose/lib/index.js b/packages/strapi-mongoose/lib/index.js index 2ae0d8f24e..feb540558c 100644 --- a/packages/strapi-mongoose/lib/index.js +++ b/packages/strapi-mongoose/lib/index.js @@ -133,13 +133,7 @@ module.exports = function (strapi) { // Parse every registered model. _.forEach(strapi.models, function (definition, model) { - definition.globalName = _.capitalize(definition.globalId); - - // Make sure the model has a table name. - // If not, use the model name. - if (_.isEmpty(definition.collectionName)) { - definition.collectionName = model; - } + definition.globalName = _.upperFirst(_.camelCase(definition.globalId)); // Make sure the model has a connection. // If not, use the default connection.