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

typeorm-code-generator

v1.0.37

Published

Code generator for typeorm and koa, auto-generate Entities and Koala respositories.

Downloads

4

Readme

typeorm-code-generator ,简称 TCG ,是基于 hygen 二次开发的代码生成工具。

TCG 用来生成两种类型的源代码:

  • typeorm 定义的实体类
  • 实体类对应的 Koa CRUD 接口

@see http://www.hygen.io @see https://typeorm.io @see https://koajs.com/

1. 安装 hygen

唯一需要手动安装的是 hygen ,需要手动把它安装到全局空间中:

npm i -g hygen

不同环境中的安装方法请参考官方文档,http://www.hygen.io/docs/quick-start

2. 验证环境是否 OK

进入此项目根目录下的 /test 目录,执行如下命令:


node test.js

如果成功生成了模板文件和对应的代码,则说明环境已经 ready:

test
└─_templates
    └─tcg
        └─new
                Category.ejs.t
                CategoryController.ejs.t
src
└─test
    └─test2
        └─test3
                Category.ts
                CategoryController.ts

3. TCG 用法

TCG 支持两种用法:

  • 在命令行中调用,就像调用 cli 脚手架工具一样
  • 在 nodejs(koa) 代码中调用

3.1 在命令行中调用

  • 在你的项目中安装 tcg: npm i typeorm-code-generator --save (yarn add typeorm-code-generator)
  • 编写配置文件:your_json_file.json ,语法参考 typeorm 官方文档 https://typeorm.io/#/separating-entity-definition ,唯一的注意点:所有 JS 内置的类型都需要采用字符串写法,例如 type:"String" ,而不能写成 type:String ,因为 JSON 格式不能接受函数类型,其它写法与 typeorm 官方定义的写法完全一致。
  • 批量生成实体类和 CRUD 接口: tcg g -i your_json_file.json
tcg g -i your_json_file.json

json 配置文件中的内容:

[
  {
    columns: {
      age: {
        nullable: false,
        type: 'Number',
      },
      firstName: {
        length: 30,
        type: 'String',
      },
      id: {
        generated: 'increment',
        primary: true,
        type: 'int',
      },
      lastName: {
        length: 50,
        nullable: false,
        type: 'String',
      },
    },
    name: 'category',
  },
];

生成的代码示例:

/**
 * This file is genarated by typeorm-code-generator, it may be overwrited at any time.
 * Please do NOT modify this file mannually.
 */

import { EntitySchema } from 'typeorm';

/**
 * @class Category
 * @author 大漠穷秋<[email protected]>
 */
export const Category = new EntitySchema(
  JSON.parse(
    '{"columns":{"age":{"nullable":false,"type":Number},"firstName":{"length":30,"type":String},"id":{"generated":"increment","primary":true,"type":"int"},"lastName":{"length":50,"nullable":false,"type":String}},"name":"Category"}',
  ),
);

/**
 * This file is genarated by typeorm-code-generator, it may be overwrited at any time.
 * Please do NOT modify this file mannually.
 */

import { Context } from 'koa';
import { isNil } from 'lodash';
import { getManager } from 'typeorm';
import { Category } from './Category';
import { router } from '../router';

/**
 * @class CategoryController
 * @author 大漠穷秋<[email protected]>
 */
export default class CategoryController {
  /**
   * 列出所有记录,不带分页。
   */
  public static async categoryList(ctx: Context) {
    const categoryRepository = getManager().getRepository(Category);
    const list = await categoryRepository.find();

    ctx.status = 200;
    ctx.body = list;
  }

  public static async getCategory(ctx: Context) {
    const categoryRepository = getManager().getRepository(Category);
    const category = await categoryRepository.createQueryBuilder().where({ id: ctx.params.id }).getOne();
    ctx.status = 200;
    ctx.body = category;
  }

  public static async saveCategory(ctx: Context) {
    const categoryRepository = getManager().getRepository(Category);
    const newCategory=JSON.parse(ctx.request.body.Category);
    const category = await categoryRepository.save(newCategory);
    ctx.status = 200;
    ctx.body = category;
  }

  public static async deleteById(ctx: Context) {
    const categoryRepository = getManager().getRepository(Category);
    const id = ctx.request.body.id;
    if (isNil(id)) {
      return;
    }
    const qb = categoryRepository.createQueryBuilder();
    const result = await qb.delete().from(Category).where(' id = :id', { id: id }).execute();
    ctx.status = 200;
    ctx.body = result;
  }
}

router.get('/api/category-list', CategoryController.categoryList);
router.get('/api/category/:id', CategoryController.getCategory);
router.post('/api/category/save', CategoryController.saveCategory);
router.delete('/api/category/:id', CategoryController.deleteById);

3.2 在 nodejs 代码中调用

const { generate } = require('typeorm-code-generator');

// 默认把代码生成在 src/tcg-generated 目录中
// generate({ inputJSON: 'test.json' });
generate({ inputJSON: 'test.json', distPath: './src/test/test2/test3', entity: true, repository: true });

可以在 nodejs 代码中进行调用之后,我们就可以动态生成 json 配置文件,然后用代码来生成代码了,在 https://github.com/craft-codeless-designer/craft-codeless-designer-demo 中提供了完整的例子,用图形化的方式生成 json 文件,然后调用 tcg 大批量生成实体类和 CRUD 接口。

4. License

MIT licensed.