npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

code-gen2

v1.0.5

Published

代码生成器

Downloads

3

Readme

code-gen

根据Mysql/MSSQL数据库,生成实体等功能,具体差异请查看 code-gen.mysql.js 和 code-gen.mssql.js

1. 使用

1.1 安装code-gen

> npm i -g code-gen2

1.2 编写 code-gen.config.js

以下是一个简单的mysql实例,详见注释

module.exports = {
	"database":{ //配置要解析的数据库信息,暂时只支持MySQL
		"host": "127.0.0.1", 
		"port": 3306,      
		"database": "sampleDb",
		"user": "root",          
		"password": "123333@"
	},
	"template":{ //模板相关配置
		"imports":"./templates/imports.js", //导入函数的位置,注意是命令行执行路径的相对位置
		"target":"./.generate", //目标生成目录,每次生成都会被删除后重新生成
		"templates":[ //模板信息,可以配置多个
			{
				"splitType":1, // 1= table split每个table 一个文件 0 =所有的table 一个文件
				"path":"./templates/vulcanEntity.art",//相对的模板路径,使用art-template模板
			    "targetFile": function( t ){  //生成的目标文件名,可以是字符串或者是一个函数返回文件名
					var arrParts = t.tableName.split('_');
					var resArr = [];
					resArr.push("./");
					arrParts.forEach( a=>{
						if(!a){
							return "";
						}
						var s = a.replace(/\b(\w)(\w*)/g, function($0, $1, $2) {
							return $1.toUpperCase() + $2.toLowerCase();
						});
						resArr.push(s);
					})
					resArr.push(".cs");
					return resArr.join("");
				},
				"extend":{ //额外的变量,可以在模板中获取
					"nameSpace":"Sample.Impl.Model",
					"baseModel" :"Sample.Impl.Model.BaseModel"
				}
			}
		]
	}

}

1.3 编写模板中使用的格式化函数

可以在import.js中定义自己的格式化函数,如下所示,另外该文件中可以使用loadhash的所有方法,当然要引用哦

module.exports = {
    toCamelCase : function(colName){
        var arrParts = colName.split('_');
        var resArr = [];
        arrParts.forEach( a=>{
            resArr.push(a.firstUpperCase());
        })
        return resArr.join("");
    },
    formatMulilineSummary : function(summary){
      
        if(!summary){
            return "没写注释";
        }
      
        return summary.replace(/([\r\n]+)/ig,"\n\t\t/// ");
    }
};

1.4 编写模板文件

code-gen默认使用的是art-template模板,可通过这里获取详细语法说明

以下为模板文件

//---------------------------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated by code-gen.
//    Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Vulcan.DataAccess.ORMapping;

namespace {{nameSpace}}
{

	[TableName("{{tableName}}")]
	public partial class {{ tableName | toCamelCase }} : {{baseModel}}
	{ 
{{each columns col colIndex}}
		private {{col | toCSharpType}} _{{ col.name | toCamelCase }};
		/// <summary>
		/// {{col.description | formatMulilineSummary}}
		/// {{col.columnType}}
		/// </summary>	
        [MapField("{{col.name}}"){{if col.isNullable ==1 }},Nullable{{/if}}{{if col.isIdentity ==1 }},Identity{{/if}}{{if col.isPK ==1 }},PrimaryKey({{col.colid}}){{/if}}] 
		public {{col | toCSharpType}} {{ col.name | toCamelCase }}
		{ get{ return _{{ col.name | toCamelCase }}; } 	set{ _{{ col.name | toCamelCase }} = value ; OnPropertyChanged("{{col.name}}"); } }
{{/each}}
    }
}

实际生成的效果

//---------------------------------------------------------------------------------------------------
// <auto-generated>
//    This code was generated by code-gen.
//    Changes to this file may cause incorrect behavior and will be lost if the code is regenerated.
// </auto-generated>
//---------------------------------------------------------------------------------------------------
using System;
using System.Collections.Generic;
using Vulcan.DataAccess.ORMapping;

namespace Sample.Impl.Model
{

	[TableName("bim_app_info")]
	public partial class BimAppInfo : Sample.Impl.Model.BaseModel
	{ 

		private string _AppCode;
		/// <summary>
		/// App代码
		/// varchar(50)
		/// </summary>	
        [MapField("app_code"),PrimaryKey(1)] 
		public string AppCode
		{ get{ return _AppCode; } 	set{ _AppCode = value ; OnPropertyChanged("app_code"); } }

		private string _AppName;
		/// <summary>
		/// 系统名称
		/// varchar(200)
		/// </summary>	
        [MapField("app_name")] 
		public string AppName
		{ get{ return _AppName; } 	set{ _AppName = value ; OnPropertyChanged("app_name"); } }

		private string _Description;
		/// <summary>
		/// 系统说明
		/// varchar(1000)
		/// </summary>	
        [MapField("description"),Nullable] 
		public string Description
		{ get{ return _Description; } 	set{ _Description = value ; OnPropertyChanged("description"); } }

		private string _AppAdmin;
		/// <summary>
		/// 系统负责人 负责人描述信息
		/// varchar(100)
		/// </summary>	
        [MapField("app_admin"),Nullable] 
		public string AppAdmin
		{ get{ return _AppAdmin; } 	set{ _AppAdmin = value ; OnPropertyChanged("app_admin"); } }

		private string _CreatorId;
		/// <summary>
		/// 创建人账号 创建人账号
		/// varchar(50)
		/// </summary>	
        [MapField("creator_id")] 
		public string CreatorId
		{ get{ return _CreatorId; } 	set{ _CreatorId = value ; OnPropertyChanged("creator_id"); } }

		private DateTime _CreateTime;
		/// <summary>
		/// 最后一次更新时间 创建时间
		/// datetime
		/// </summary>	
        [MapField("create_time")] 
		public DateTime CreateTime
		{ get{ return _CreateTime; } 	set{ _CreateTime = value ; OnPropertyChanged("create_time"); } }

		private string _ModifierId;
		/// <summary>
		/// 更新人账号 更新人账号
		/// varchar(50)
		/// </summary>	
        [MapField("modifier_id")] 
		public string ModifierId
		{ get{ return _ModifierId; } 	set{ _ModifierId = value ; OnPropertyChanged("modifier_id"); } }

		private DateTime _ModifyTime;
		/// <summary>
		/// 最后一次更新时间 最后一次更新时间
		/// datetime
		/// </summary>	
        [MapField("modify_time")] 
		public DateTime ModifyTime
		{ get{ return _ModifyTime; } 	set{ _ModifyTime = value ; OnPropertyChanged("modify_time"); } }

    }
}

1.5 在code-gen.config.js目录执行命令

> code-gen

或者指定配置文件

> code-gen2 -c code-gen.config.js