Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加jsdoc注释,满足egg-ts-helper智能提示要求 #3

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

# 新增更新

添加egg-sequelize 多数据库源的delegate名称参数,option中添加:egg_delegate:'some_model', 即可

# Egg-Sequelize-Auto

Automatically generate models for [egg-sequelize](https://github.com/eggjs/egg-sequelize) via the command line.
Expand Down Expand Up @@ -114,7 +119,6 @@ Produces a file/files such as ./app/model/users.js which looks like:
return Model;
};


## Configuration options

For the `-c, --config` option the following JSON/configuration parameters are defined by Sequelize's `options` flag within the constructor. For more info:
Expand All @@ -127,7 +131,7 @@ For the `-c, --config` option the following JSON/configuration parameters are de
const EggSequelizeAuto = require('egg-sequelize-auto')
const auto = new EggSequelizeAuto'database', 'user', 'pass');

// start
// start
auto.run(function (err) {
if (err) throw err;

Expand All @@ -141,6 +145,7 @@ const auto = new EggSequelizeAuto('database', 'user', 'pass', {
dialect: 'mysql'|'mariadb'|'sqlite'|'postgres'|'mssql',
directory: false, // prevents the program from writing to disk
port: 'port',
egg_delegate:'model'
additional: {
timestamps: false
//...
Expand Down
17 changes: 12 additions & 5 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ class EggSequelizeAuto extends SequelizeAuto {
}

text[table] = "/* indent size: " + self.options.indentation + " */\n\n";
text[table] += "/**\n";
text[table] += " * @param {Egg.Application} app app\n";
text[table] += " */\n";
text[table] += "module.exports = app => {\n";
text[table] += spaces + "const DataTypes = app.Sequelize;\n\n";
var tableName = self.options.camelCase ? _.camelCase(table) : table;
text[table] += spaces + "const Model = app.model.define('" + tableName + "', {\n";
let egg_delegate = self.options.egg_delegate || 'model';
text[table] += spaces + "const Model = app." + egg_delegate + ".define('" + tableName + "', {\n";

_.each(fields, function(field, i) {
var additional = self.options.additional
Expand Down Expand Up @@ -97,6 +101,9 @@ class EggSequelizeAuto extends SequelizeAuto {
if (self.sequelize.options.dialect === "mssql" && defaultVal && defaultVal.toLowerCase() === '(newid())') {
defaultVal = null; // disable adding "default value" attribute for UUID fields if generating for MS SQL
}
if (self.sequelize.options.dialect === "postgres" && defaultVal && defaultVal.toLowerCase().indexOf('nextval') > -1) {
defaultVal = null; // disable adding "default value" attribute for UUID fields if generating for MS SQL
}

var val_text = defaultVal;

Expand All @@ -115,9 +122,9 @@ class EggSequelizeAuto extends SequelizeAuto {
var field_type = self.tables[table][field].type.toLowerCase();
if (field_type.indexOf('date') === 0 || field_type.indexOf('timestamp') === 0) {
if (_.endsWith(defaultVal, '()')) {
val_text = "sequelize.fn('" + defaultVal.replace(/\(\)$/, '') + "')"
val_text = "DataTypes.fn('" + defaultVal.replace(/\(\)$/, '') + "')"
} else if (_.includes(['current_timestamp', 'current_date', 'current_time', 'localtime', 'localtimestamp'], defaultVal.toLowerCase())) {
val_text = "sequelize.literal('" + defaultVal + "')"
val_text = "DataTypes.literal('" + defaultVal + "')"
} else {
val_text = quoteWrapper + val_text + quoteWrapper
}
Expand All @@ -129,7 +136,7 @@ class EggSequelizeAuto extends SequelizeAuto {
if (defaultVal === null || defaultVal === undefined) {
return true;
} else {
val_text = _.isString(val_text) && !val_text.match(/^sequelize\.[^(]+\(.*\)$/) ? SqlString.escape(_.trim(val_text, '"'), null, self.options.dialect) : val_text;
val_text = _.isString(val_text) && !val_text.match(/^DataTypes\.[^(]+\(.*\)$/) ? SqlString.escape(_.trim(val_text, '"'), null, self.options.dialect) : val_text;

// don't prepend N for MSSQL when building models...
val_text = _.trimStart(val_text, 'N')
Expand Down Expand Up @@ -260,4 +267,4 @@ class EggSequelizeAuto extends SequelizeAuto {
}
}

module.exports = EggSequelizeAuto;
module.exports = EggSequelizeAuto;
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
{
"name": "egg-sequelize-auto",
"version": "0.1.1",
"version": "0.1.3",
"description": "Automatically generate bare sequelize models from your database, adjustment for egg",
"main": "index.js",
"bin": {
"egg-sequelize-auto": "bin/egg-sequelize-auto",
"sequelize-auto":"bin/sequelize-auto"
"sequelize-auto": "bin/sequelize-auto"
},
"scripts": {
"test": "node ./bin/egg-sequelize-auto -c ./config.json"
Expand All @@ -26,4 +26,4 @@
"devDependencies": {
"mysql2": "^1.4.2"
}
}
}