Skip to content

Commit

Permalink
✨ 增加 modalNameSuffix: true 配置,可生成不带Model后缀的模型名称,以符合个人使用习惯~
Browse files Browse the repository at this point in the history
  • Loading branch information
xiapazhi committed Nov 19, 2020
1 parent 872f7bc commit 6341f58
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 2 additions & 0 deletions src/generate/template/egg/user.text
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint-disable*/

'use strict';

module.exports = app => {
Expand Down
4 changes: 4 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class Automate {
type: 'js', // Which code style want to generate, supported: js/ts/egg/midway. Default is `js`.
camelCase: false, // Model name camel case. Default is false.
fileNameCamelCase: false, // Model file name camel case. Default is false.
modalNameSuffix: true, // Model name 'Modal' suffix. Default is true.
dir: 'models', // What directory to place the models. Default is `models`.
typesDir: null, // What directory to place the models' definitions (for typescript), default is the same with dir.
emptyDir: false, // Remove all files in `dir` and `typesDir` directories before generate models.
Expand All @@ -34,6 +35,7 @@ class Automate {
assert(supportTypes.includes(this.options.type), 'type not support');
assert(_.isBoolean(this.options.camelCase), 'Invalid params camelCase');
assert(_.isBoolean(this.options.fileNameCamelCase), 'Invalid params fileNameCamelCase');
assert(_.isBoolean(this.options.modalNameSuffix), 'Invalid params modalNameSuffix');
assert(_.isString(this.options.dir), 'Invalid params dir');
assert(_.isString(this.options.typesDir), 'Invalid params typesDir');
assert(_.isBoolean(this.options.emptyDir), 'Invalid params cleanDir');
Expand Down Expand Up @@ -119,6 +121,7 @@ class Automate {
skipTables,
camelCase,
fileNameCamelCase,
modalNameSuffix,
} = this.options;
const allTables = await this.getTables({
tables,
Expand All @@ -127,6 +130,7 @@ class Automate {
const definitions = getModelDefinitions(allTables, {
camelCase,
fileNameCamelCase,
modalNameSuffix,
dialect: this.dbOptions.dialect,
});
debug('get model definitions');
Expand Down
20 changes: 11 additions & 9 deletions src/util/definition.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ function getFieldName(fieldName, camelCase) {
return camelCase ? _.camelCase(fieldName) : fieldName;
}

function getModelName(tableName, camelCase) {
const modelString = camelCase ? 'Model' : '_model';
function getModelName(tableName, camelCase, modalNameSuffix) {
const modelString = modalNameSuffix ?
camelCase ? 'Model' : '_model'
: '';
return `${getFieldName(tableName, camelCase)}${modelString}`;
}

Expand Down Expand Up @@ -195,7 +197,7 @@ function getDataType(field) {

/**
* Process a table
* @param {object} params { structures, allIndexes, foreignKeys, options: { camelCase, dialect } }
* @param {object} params { structures, allIndexes, foreignKeys, options: { camelCase, dialect, modalNameSuffix } }
* @return {object} { attributes: { filed: { attribute } }, indexes: [{ name, type, fields }] }
*/
function processTable({
Expand All @@ -204,7 +206,7 @@ function processTable({
foreignKeys,
options,
}) {
const { camelCase, dialect } = options;
const { camelCase, dialect, modalNameSuffix } = options;
const attributes = {};
_.forEach(structures, (structure, fieldName) => {
const key = getFieldName(fieldName, camelCase);
Expand Down Expand Up @@ -245,7 +247,7 @@ function processTable({
const filed = getFieldName(columnName, camelCase);
attributes[filed].references = {
key: referencedColumnName,
model: getModelName(referencedTableName, camelCase),
model: getModelName(referencedTableName, camelCase, modalNameSuffix),
};
});

Expand All @@ -255,20 +257,20 @@ function processTable({
/**
* Get model definitions
* @param {object} tables { structures, indexes, foreignKeys }
* @param {object} options { camelCase, fileNameCamelCase }
* @param {object} options { camelCase, fileNameCamelCase, dialect, modalNameSuffix }
* @return {object} [{ modelName, modelFileName, tableName, attributes, indexes }]
*/
function getModelDefinitions(tables, options) {
const { camelCase, fileNameCamelCase, dialect } = options || {};
const { camelCase, fileNameCamelCase, dialect, modalNameSuffix } = options || {};
const definitions = _.map(tables, (table, tableName) => {
const { attributes, indexes } = processTable({
structures: table.structures,
allIndexes: table.indexes,
foreignKeys: table.foreignKeys,
options: { camelCase, dialect },
options: { camelCase, dialect, modalNameSuffix },
});

const modelName = getModelName(tableName, camelCase);
const modelName = getModelName(tableName, camelCase, modalNameSuffix);
const modelFileName = getFieldName(tableName, fileNameCamelCase);
return {
modelName,
Expand Down

0 comments on commit 6341f58

Please sign in to comment.