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

generator-beyond-component

v1.0.6

Published

生成haian-admin-nodets中的基础CRUD业务代码

Downloads

9

Readme

generator-beyond-component

一键生成CRUD业务组件,支持按Schema生成模型、验证代码!

安装

如果您已经安装过yeoman,则不需要重复安装

npm install --global yo
npm i generator-beyond-component -D

使用

生成骨架

使用yo启动生成器,按照提示进行即可

yo beyond-component

生成代码

填写Schema部分

const MetaComponentSchema: Schema = new Schema({
  name: { type: String, required: true, minLength: 3, maxLength: 20 },
  email: { type: String, required: true, unique: true },
  age: { type: Number, required: true, min: 1, max: 120 },
  hobby: { type: [String] },
  sex: { type: String, required: true, enum: ["female", "male"] },
  birthday: { type: Date },
  status: { type: Boolean, required: true },
  profile: { type: Schema.Types.Mixed },
})

生成

再运行一次指令,组件名称填写刚刚生成的组件名称(默认保存上次输入,直接回车即可),选择根据组件Schema生成Model和Validation,并视情况改造service.ts

yo beyond-component
// 选如下选项
? 😕该组件已存在,请选择操作: 
  无视该组件,继续执行 
❯ 根据组件Schema生成Model和Validation,并视情况改造service.ts 
  终止执行并退出 

// 输入a,回车(全部覆盖)
❗️注册操作需要修改文件,请检查:(回车以展开选项)
 conflict src/components/MetaComponent/model.ts
? Overwrite src/components/MetaComponent/model.ts? (ynarxdeiH) a
>> overwrite this and all others

结果

你会在这几个文件下,看到生成的代码


// model.ts
export interface MetaComponentModel extends Document {
  readonly _id: Schema.Types.ObjectId
  name: string
  email: string
  age: number
  hobby?: string[]
  sex: 'female' | 'male'
  birthday?: Date
  status: boolean
  profile?: any

}

// validation.ts
const validateCreateSchema = Joi.object({
  name: Joi.string().required().min(3).max(20),
  email: Joi.string().required(),
  age: Joi.number().required().min(1).max(120),
  sex: Joi.string().required().valid('female','male'),
  birthday: Joi.date(),
  status: Joi.boolean().required(),
  profile: Joi.any(),

}).unknown(true)

const validateUpdateSchema = Joi.object({
  name: Joi.string().min(3).max(20),
  email: Joi.string(),
  age: Joi.number().min(1).max(120),
  sex: Joi.string().valid('female','male'),
  birthday: Joi.date(),
  status: Joi.boolean(),
  profile: Joi.any(),

}).unknown(true)

如果Schema中存在unique属性字段,则会修改service.ts

// 判断到email为唯一键,自动添加
export const create = (data: any) => Service.create<MetaComponentModel>(MetaComponent, data, Validation.create, 'email')

export const find = (data: any) => Service.find<MetaComponentModel>(MetaComponent, data, Validation.find)

export const findById = (_id: string) => Service.findById<MetaComponentModel>(MetaComponent, _id)

export const update = (data: any) => Service.update<MetaComponentModel>(MetaComponent, data, Validation.update, 'email')

export const remove = (data: any) => Service.remove<MetaComponentModel>(MetaComponent, data)

注意事项

  1. Schema目前仅支持基础属性,不支持联表。
  2. Schema必须使用展开写法,例如name:{type:String},而不是name:String,因为这会使检测混淆,会造成类型推断不明确的问题。
  3. 此cli仅用于生成骨架和初始化代码,如修改Schema请手动修改,不要使用cli。
  4. 如果组件被注册过,那么删除的时候应该也删掉components/index.ts中的引入导出,route文件夹下的路由文件,和route/index.ts中的引入。
  5. Schema当中的唯一键只能设置0或1个,如果需要多个唯一键,请写自定义service和validation。
  6. 此项目仅供haian-admin-nodets使用

Future version

TODO

  1. 支持在Schema联表情况下生成代码
  2. 支持在已有结构情况下,根据Schema结构同步修改代码
  3. 支持一键删除组件,自动删除注册时的相关引入

FIX

  1. 删除import路径多余的斜杠(不影响运行)
  2. 修改组件在注册时import的位置,目前默认在行首