Fix #90: Update global model naming convention

This commit is contained in:
Aurélien Georget 2016-08-12 12:04:00 +02:00
parent 7ac86fd68e
commit 55d65ccde8
15 changed files with 94 additions and 172 deletions

View File

@ -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.

View File

@ -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;
};

View File

@ -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`'
});

View File

@ -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
}
}

View File

@ -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;
}

View File

@ -1,6 +1,6 @@
{
"connection": "<%= connection %>",
"collectionName": "<%= id %>",
"collectionName": "<%= humanizeId %>",
"attributes": {
}

View File

@ -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 %>);
});
});
}

View File

@ -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`'
});

View File

@ -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())))
}
}
});

View File

@ -1,6 +1,6 @@
{
"connection": "<%= connection %>",
"tableName": "<%= id %>",
"tableName": "<%= humanizeId %>",
"options": {
"increments": true,
"timestamps": true,

View File

@ -1,5 +1,5 @@
{
"connection": "<%= connection %>",
"collectionName": "<%= id %>",
"collectionName": "<%= humanizeId %>",
"attributes": <%- JSON.stringify(api, null, '\t') %>
}

View File

@ -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`'
});

View File

@ -1,9 +1,11 @@
'use strict';
/**
* `<%= id %>` service.
* `<%= globalID %>` service.
*/
exports.<%= id %> = function () {
module.exports = {
// exampleService: (arg1, arg2) => {
// return isUserOnline(arg1, arg2);
// }
};

View File

@ -1,10 +1,10 @@
'use strict';
/**
* `<%= id %>` service.
* `<%= globalID %>` service.
*/
modules.exports = {
module.exports = {
// exampleService: (arg1, arg2) => {
// return isUserOnline(arg1, arg2);
// }

View File

@ -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.