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 🙏

© 2026 – Pkg Stats / Ryan Hefner

egg-crud-generator

v1.2.0

Published

A CLI tool to automatically generate CRUD files for egg.js projects with MongoDB.

Readme

Egg CRUD 生成器 (egg-gen)

一个强大且可定制的命令行工具,专为您的 egg.js + egg-mongoose 项目自动化生成CRUD(增删改查)文件。从此告别重复的样板代码!

✨ 产品特性

  • JSON驱动: 在单个JSON文件中定义您的整个业务模型。
  • 全栈代码生成: 一键生成 ControllerServiceModelRouter 文件。
  • 高度可定制: 支持生成特殊的业务逻辑方法和路由。
  • 智能校验: 根据您的模型字段,自动生成 egg-validate 校验规则。
  • 项目校验: 确保只在合法的 egg.js + egg-mongoose 项目中运行。
  • TypeScript构建: 提供了一个健壮且易于维护的代码库。

🚀 安装

全局安装本工具,即可在您系统的任何位置使用:

npm install -g egg-crud-generator

💡 使用方法

本工具的核心命令是 generate (别名 g)。

egg-gen generate <configFile> [options]

参数

  • <configFile>: (必需) 指向您的模型定义JSON文件的路径。

选项

  • -p, --project <projectPath>: (可选) 指向您的目标egg.js项目的路径。如果未提供,则默认为当前工作目录。
  • --help: 显示命令的帮助信息。

使用示例

  1. 首先,进入您的egg.js项目的根目录。
  2. 创建一个 article.json 文件来定义您的“文章”模型。
  3. 运行以下命令:
egg-gen g ./article.json

此命令将在您的项目中自动生成以下文件:

  • app/controller/article.js
  • app/service/article.js
  • app/model/article.js
  • 同时,新的路由规则将被追加app/router.js 文件中。

⚙️ model.json 配置文件详解

这是本生成器的核心。以下是一个功能完备的配置示例:

{
  "modelName": "Article",
  "modelDescription": "文章",
  "routeName": "article",
  "fields": [
    { "name": "title", "type": "String", "required": true, "description": "文章标题" },
    { "name": "content", "type": "String", "required": true, "description": "文章内容" },
    { "name": "authorId", "type": "ObjectId", "ref": "User", "description": "作者的用户ID" },
    { "name": "viewCount", "type": "Number", "default": 0, "description": "浏览量" },
    { "name": "isPublished", "type": "Boolean", "default": false, "description": "发布状态" },
    { "name": "tags", "type": "Array", "subType": "String", "description": "文章标签" }
  ],
  "specialControllers": [
    {
      "name": "publishArticle",
      "method": "POST",
      "route": "/publish/:id",
      "description": "发布一篇文章",
      "param": "id",
      "serviceMethod": "publish"
    }
  ],
  "specialServices": [
    {
      "name": "publish",
      "params": ["id"],
      "description": "用于发布文章的业务逻辑"
    }
  ]
}

字段定义说明

| 属性 | 类型 | 是否必需 | 描述 | | -------------------- | -------- | -------- | ------------------------------------------------------------------- | | modelName | string | 是 | 您的模型名称,采用帕斯卡命名法(PascalCase),例如 "Article"。 | | modelDescription | string | 否 | 人类可读的描述,用于生成代码注释。 | | routeName | string | 是 | 用于URL的路由名称,全小写,例如 "article"。 | | fields | Array | 是 | 一个字段对象数组,用于定义Mongoose的Schema。 | | specialControllers | Array | 否 | 一个对象数组,用于定义非RESTful标准的Controller方法。 | | specialServices | Array | 否 | 一个对象数组,用于在Service层生成对应的特殊业务方法骨架。 |

fields 对象详解:

| 属性 | 类型 | 描述 | | ------------- | --------- | ----------------------------------------------------------------------- | | name | string | 字段名称。 | | type | string | Mongoose的Schema类型 (String, Number, Boolean, Date, ObjectId, Array)。 | | required | boolean | 字段是否必需。此项会影响自动生成的校验规则。 | | default | any | 字段的默认值。 | | ref | string | 当类型为 ObjectId 时,指定其引用的模型名称。 | | subType | string | 当类型为 Array 时,指定数组内元素的类型。 | | description | string | 字段的描述,用于生成注释。 |

specialControllers & specialServices 对象详解:

这两个配置项允许您为超越简单CRUD的自定义业务逻辑生成代码骨架。生成器会在controller和service文件中创建相应的方法,您只需填充具体的业务实现即可。

📜 开源许可

本项目基于 MIT 许可开源。