Skip to content

Commit

Permalink
✨ 增加配置项(type=freesun)适配飞尚架构
Browse files Browse the repository at this point in the history
  • Loading branch information
xiapazhi committed Nov 20, 2020
1 parent 6341f58 commit 473541b
Show file tree
Hide file tree
Showing 8 changed files with 366 additions and 150 deletions.
267 changes: 145 additions & 122 deletions README.md

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"name": "sequelize-automate",
"name": "sequelize-automate-freesun",
"version": "1.2.2",
"description": "Automatically generate bare sequelize models from your database.",
"main": "index",
Expand All @@ -15,10 +15,10 @@
},
"repository": {
"type": "git",
"url": "https://github.com/nodejh/sequelize-automate.git"
"url": "https://github.com/xiapazhi/sequelize-automate.git"
},
"bugs": {
"url": "https://github.com/nodejh/sequelize-automate/issues"
"url": "https://github.com/xiapazhi/sequelize-automate/issues"
},
"nyc": {
"exclude": [
Expand All @@ -33,7 +33,7 @@
"engines": {
"node": ">=0.10"
},
"author": "nodejh <jianghangscu@gmail.com>",
"author": "xiapazhi <grazing-stars@qq.com>",
"license": "MIT",
"dependencies": {
"@babel/generator": "^7.7.4",
Expand Down
92 changes: 92 additions & 0 deletions src/generate/freesun.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
const { parse } = require('@babel/parser');
const { default: generate } = require('@babel/generator');
const t = require('@babel/types');
const { default: traverse } = require('@babel/traverse');
const fs = require('fs');
const { join } = require('path');
const { bigCamelCase } = require('../util/wordCase');
const {
generateOptions,
processAttributesProperties,
processOptionsProperties,
} = require('./common/index');


/**
* Generate codes
* @param {object} definition
* @param {object} options
*/
function generateCode(definition, options) {
const file = './template/freesun/user.text'
const source = fs
.readFileSync(join(__dirname, file))
.toString();

const ast = parse(source, {
strictMode: true,
});

traverse(ast, {
VariableDeclarator: (path) => {
const { node } = path;

if (t.isIdentifier(node.id, { name: 'UserModel' })) {
node.id = t.identifier(bigCamelCase(definition.modelName));
}
},

CallExpression: (path) => {
const { node } = path;
if (
t.isMemberExpression(node.callee)
&& t.isIdentifier(node.callee.property, { name: 'define' })
) {
node.arguments[0] = t.stringLiteral(definition.modelName);
node.arguments[1] = t.objectExpression(processAttributesProperties(definition.attributes));
node.arguments[2] = t.objectExpression(processOptionsProperties(
node.arguments[2].properties,
definition,
));
}
},

ExpressionStatement: (path) => {
const { node } = path;
if (t.isIdentifier(node.expression.left.property, { name: 'UserModel' })) {
node.expression.left.property.name = bigCamelCase(definition.modelName);
}
if (t.isIdentifier(node.expression.right, { name: 'UserModel' })) {
node.expression.right.name = bigCamelCase(definition.modelName);
}
},

ReturnStatement: (path) => {
const { node } = path;
if (t.isIdentifier(node.argument, { name: 'UserModel' })) {
node.argument = t.identifier(bigCamelCase(definition.modelName));
}
},
});


const { code } = generate(ast, generateOptions);
return code;
}

function process(definitions, options) {
const modelCodes = definitions.map((definition) => {
const { modelFileName } = definition;
const fileType = 'model';
const file = `${modelFileName}.js`;
const code = generateCode(definition, options);
return {
file,
code,
fileType,
};
});
return modelCodes;
}

module.exports = process;
3 changes: 3 additions & 0 deletions src/generate/index.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const processFS = require('./freesun');
const processJS = require('./javascript');
const processTS = require('./typescript');
const processMidway = require('./midway');
Expand All @@ -11,6 +12,8 @@ const processMidway = require('./midway');
function generate(definition, options) {
const { type, tsNoCheck } = options;
switch (type) {
case 'freesun':
return processFS(definition, { isEgg: false });
case 'js':
return processJS(definition, { isEgg: false });
case 'ts':
Expand Down
72 changes: 72 additions & 0 deletions src/generate/template/freesun/user.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/* eslint-disable*/

'use strict';

module.exports = dc => {
const DataTypes = dc.ORM;
const sequelize = dc.orm;

const UserModel = sequelize.define('userModel',{
id: {
type: DataTypes.BIGINT,
allowNull: false,
defaultValue: null,
primaryKey: false,
autoIncrement: false,
comment: null,
field: 'id',
unique: 'uk_id',
},
name: {
type: DataTypes.STRING(32),
allowNull: false,
defaultValue: null,
primaryKey: false,
autoIncrement: false,
comment: 'user name',
field: 'name',
},
email: {
type: DataTypes.STRING(32),
allowNull: false,
defaultValue: null,
primaryKey: false,
autoIncrement: false,
comment: 'user email',
field: 'name',
},
createdAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: null,
primaryKey: false,
autoIncrement: false,
comment: 'created time',
field: 'created_at',
},
updatedAt: {
type: DataTypes.DATE,
allowNull: false,
defaultValue: null,
primaryKey: false,
autoIncrement: false,
comment: 'update time',
field: 'updated_at',
},
},{
tableName: 'user',
comment: 'user table',
indexes: [{
name: 'uk_name_email',
unique: true,
fields: [
'name',
'email',
],
}]
});

dc.models.UserModel = UserModel;

return UserModel;
};
19 changes: 16 additions & 3 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ class Automate {
tables: null, // Use these tables, Example: ['user'], default is null.
skipTables: null, // Skip these tables. Example: ['user'], default is null.
tsNoCheck: false, // Whether add `@ts-nocheck` to model files, default is false.
match: null // Regex to match table name
match: null, // Regex to match table name,
ignorePrefix: null, // Ignore the prefix of table name(for freesun), Example: ['t_']. Default is null
attrLength: true,// Whether to generate attribute length(design for freesun,but use for any type)
};

// https://sequelize.org/master/class/lib/sequelize.js~Sequelize.html#instance-constructor-constructor
Expand All @@ -31,7 +33,7 @@ class Automate {
// default `options.typesDir` is the same with `options.dir`
this.options.typesDir = this.options.typesDir || this.options.dir;

const supportTypes = ['js', 'ts', 'egg', 'midway', '@ali/midway'];
const supportTypes = ['freesun', 'js', 'ts', 'egg', 'midway', '@ali/midway'];
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');
Expand All @@ -40,8 +42,10 @@ class Automate {
assert(_.isString(this.options.typesDir), 'Invalid params typesDir');
assert(_.isBoolean(this.options.emptyDir), 'Invalid params cleanDir');
assert(_.isNull(this.options.tables) || _.isArray(this.options.tables), 'Invalid params table');
assert(_.isNull(this.options.skipTables) || _.isArray(this.options.skipTables), 'invalid params table');
assert(_.isNull(this.options.skipTables) || _.isArray(this.options.skipTables), 'invalid params skipTables');
assert(_.isBoolean(this.options.tsNoCheck), 'Invalid params tsNoCheck');
assert(_.isNull(this.options.ignorePrefix) || _.isArray(this.options.ignorePrefix), 'invalid params ignorePrefix');
assert(_.isBoolean(this.options.attrLength), 'Invalid params attrLength');

this.sequelize = new Sequelize(this.dbOptions);
this.queryInterface = this.sequelize.getQueryInterface();
Expand Down Expand Up @@ -89,6 +93,7 @@ class Automate {
});

debug('tableNames: ', tableNames);

const tableStructures = await Promise.all(tableNames.map(
(tableName) => this.queryInterface.describeTable(tableName),
));
Expand Down Expand Up @@ -122,6 +127,8 @@ class Automate {
camelCase,
fileNameCamelCase,
modalNameSuffix,
ignorePrefix,
attrLength,
} = this.options;
const allTables = await this.getTables({
tables,
Expand All @@ -132,6 +139,8 @@ class Automate {
fileNameCamelCase,
modalNameSuffix,
dialect: this.dbOptions.dialect,
ignorePrefix,
attrLength,
});
debug('get model definitions');
return definitions;
Expand All @@ -149,12 +158,16 @@ class Automate {
dir,
typesDir,
emptyDir,
ignorePrefix,
attrLength,
} = this.options;
const definitions = await this.getDefinitions({
tables,
skipTables,
camelCase,
fileNameCamelCase,
ignorePrefix,
attrLength,
});

const codes = generate(definitions, {
Expand Down
Loading

0 comments on commit 473541b

Please sign in to comment.