Giter Site home page Giter Site logo

lichenglong-blog / nest-code-generate Goto Github PK

View Code? Open in Web Editor NEW

This project forked from kuangshp/nest-code-generate

0.0 0.0 0.0 1.06 MB

@nestjs/cli脚手架的增强版,提供输入数据库表名自动生成控制器、服务层、实体类等基本的curd

JavaScript 56.04% TypeScript 36.65% EJS 7.31%

nest-code-generate's Introduction

关于本包的介绍

在使用 NestJs + Mysql 项目开发中,也许你会开启由实体类同步生成数据表的方式,这种方式不是不行,但是也面临找数据会被误删的风险。本包使用的是先创建数据库和数据表,运行命令根据数据表有选择的来反向生成对应的实体类、控制器、服务层,减少我们手动一个一个目录结构来创建的工作。

使用步骤

  • 1、全局安装方式
  npm install nest-code-generate -g
  • 2、关于配置数据库连接信息

    根目录下创建数据库连接信息 code-gen.yml 文件

# 配置数据库连接信息
data_source:
  host: "localhost"
  port: 3306
  user: "root"
  password: "password"
  database: "nest-code-generate"
# 配置字段收取及基础类(可选,如果没有配置的话,那么就是默认每个实体类都带了相同的字段)
data_config:
  collect: "id, created_at, updated_at, deleted_at"
  base_name: "BaseEntity"
  • 4、使用方式

指令参数介绍

  code-gen <表名> [存放的目录(目录需要提前存在)]

  # 1.其中code-gen是指令前缀
  # 2.表名为必传,可以传多个表名, 用","隔开即可, <表名1,表名2>
  # 3.存放的目录为可选, 如果不传递该参数则默认存在到src目录下,如果指定存放目录,那么你要先创建对应目录

示例

table_name

# 1.例如当前有一数据表, 名为type
# 2.项目结构:
├── src
  ├── demo
# 3.我们想将它挂载到src中的demo目录下

执行命令

code-gen type 或者 code-gen type src 进入配置选项如下图: command_line

目录生成结构

# 选择Entity:
├── src
  ├── entities
  │   └── type.entity.ts

# 选择Tier
├── src
  ├── controllers
  │   ├── type.controller.spec.ts
  │   └── type.controller.ts
  ├── entities
  │   └── type.entity.ts
  ├── services
  │   ├── type.service.spec.ts
  │   └── type.service.ts

# 选择CURD: 目录结构与Tier一致, 内容增加简单的增删改查
# 选择All: 目前与CURD一致, 留作后期扩展使用

⚠️ 注意事项

1.路径问题

# 如果存放目录 不是唯一的, 例如下面的结构, lib和src文件夹下都存在一个demo文件夹, 此时如果想将生成内容放到src中的demo就有一点麻烦了, 不过好在, 存放路径支持"不完整路径", 请看下面的指令.
├── root
  ├── lib
  │   ├── demo
  ├── src
  │   └── demo

code-gen type src/demo
# 同样的如果是想存放到lib下面的demo文件夹可以执行下面的指令
code-gen type lib/demo

nest-code-generate's People

Contributors

kuangshp avatar veloma-timer avatar shuaiqidekaige avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.