forked from nodejh/sequelize-automate
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
366 additions
and
150 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.