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

fast-db-codegen

v2.0.1

Published

a code generator base on ejs template and mysql database

Downloads

7

Readme

Fast Db Codegen

以Mysql数据库作为数据源, 根据读取到的表结构和提前写好的模板, 生成对应的代码, 使用ejs作为模板引擎

使用

安装

# npm
npm install -D fast-db-codegen
# yarn
yarn add -D fast-db-codegen

数据源配置

  • 在项目根目录新建 .fast-codegen 文件夹
  • 创建 config.json 数据源配置
{
    "datasource": {
        "host": "yourMysqlHost",
        "user": "yourMysqlUser",
        "password": "yourMysqlPassword",
        "port": "yourMysqlPort",
        "database": "yourMysqlDatabase"
    }
}

模板内参数

  • 所有 .fast-codegen/default 文件夹下, 以.ejs为扩展名, 并且内容的第一行以#!开头的文件, 都会当做模板处理进行编译
  • 可以根据需求创建多套模板,在 .fast-codege 下创建新的文件夹 dirName,在运行的时候通过 npm run codegen template=dirName xxxx 进行选择模板
  • 第一行为描述行 #! xxx/xxx.xx 为渲染目标路径(第一行如果不是以 #! 开头, 不进行渲染), #!后的内容同样支持 ejs 语法
  • 可以给描述行传参进行不同的渲染模式, 格式为 #! xxx/xxx/xx.xx?mode=youMode, 其中youMode替换为需要的模式
    • 啥都不传: 默认模式, 目标文件不存在, 则会创建目录并且创建目标文件并写入
    • append: 追加模式, 目标文件不存在, 则创建并写入内容, 如目标文件已存在, 则在文件末尾处追加模板渲染结果
    • overwrite: 重写模式, 目标文件不存在, 则创建别写入内容, 如目标文件已存在, 则删除模板文件后, 再进行创建和写入的流程
  • 模板内model格式如下
/**
 * 全局参数类型
 */
class ModelType {
     /** 根据表名转换的类型名称 `fs_user` -> `FsUser` */
    typeName: string
    /**  根据表名转换类型名称连字符模式 `fs_demo_table_string` -> `fs-demo-table-string` */
    typeNameWithHyphen: string
    /** 所有表字段 */
    params: Field[]
    /** 目前和typeName一致 */
    realType: string
    /** 表名 */
    tableName?: string
    /** 主键名称 */
    primaryKey?: string
    /** 表备注 */
    typeRemark?: string
}

 class Field {
    /** 字段名驼峰 test_field -> testField */
    name: string
    /** 字段类型(经过转换的javascript类型) */
    type: string
    /** 原始数据库类型 */
    rawType: string
    /** 字段备注 */
    remark?: string
}

举个例子

  • fs_user 表结构如下:
CREATE TABLE `fs_user` (
  `id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'ID',
  `username` varchar(36) CHARACTER  NOT NULL COMMENT '用户名',
  `password` char(64) CHARACTER  NOT NULL COMMENT '密码',
  `password_salt` char(32) CHARACTER  NOT NULL COMMENT '密码Salt',
  `real_name` varchar(36) CHARACTER  DEFAULT NULL COMMENT '真实姓名',
  `email` varchar(128) DEFAULT NULL COMMENT '邮箱',
  `mobile` varchar(20) DEFAULT NULL COMMENT '电话',
  `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
  `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
  `last_login_time` timestamp NULL DEFAULT NULL COMMENT '最后一次登录时间',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_general_ci COMMENT='用户基础表'
  • 有个模板文件如下 .fast-codege/default/demo.ejs :
#! .temp/<%=realType%>.json
{
    "typeName":  <%-JSON.stringify(typeName)%>,
    "fields": [<%-
        fields.map(JSON.stringify).join(',\n')%>
    ],
    "realType": <%-JSON.stringify(realType)%>,
    "tableName": <%-JSON.stringify(tableName)%>,
    "primaryKey": <%-JSON.stringify(primaryKey)%>,
    "typeRemark": <%-JSON.stringify(typeRemark)%>
}
  • 生成文件 .temp/FsUser.json
{
    "typeName":  "FsUser",
    "typeNameWithHyphen":  "fs-user",
    "fields": [
        {"name":"id","rawType":"int","type":"number","remark":"ID"},
{"name":"username","rawType":"varchar","type":"string","remark":"用户名"},
{"name":"password","rawType":"char","type":"string","remark":"密码"},
{"name":"passwordSalt","rawType":"char","type":"string","remark":"密码Salt"},
{"name":"realName","rawType":"varchar","type":"string","remark":"真实姓名"},
{"name":"email","rawType":"varchar","type":"string","remark":"邮箱"},
{"name":"mobile","rawType":"varchar","type":"string","remark":"电话"},
{"name":"createTime","rawType":"timestamp","type":"number","remark":"创建时间"},
{"name":"updateTime","rawType":"timestamp","type":"number","remark":"更新时间"},
{"name":"lastLoginTime","rawType":"timestamp","type":"number","remark":"最后一次登录时间"}
    ],
    "realType": "FsUser",
    "tableName": "fs_user",
    "primaryKey": "id",
    "typeRemark": "用户基础表"
}

生成代码

package.jsonscripts中添加配置

{
    codegen: "codegen"
}

运行

npm run codegen yourTable yourTable2 yourTable3

开发

  • 运行 npm i
  • 运行 npm run dev 编译 bin/index.tsdist\index.js
  • 运行 npm run start || node dist\index.js tablename1 tablename2 tablename3 进行测试