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

wt-data-model

v0.0.25-alpha.0

Published

一个为业务数据准备的通用的数据模型。可以通过 meta 提供业务对象的描述信息。并以此为基础,自动生成对应的表格配置和表单。

Readme

业务数据模型 ( wt-data-model )

一个为业务数据准备的通用的数据模型。可以通过 meta 提供业务对象的描述信息。并以此为基础,自动生成对应的表格配置和表单。

Model 基类

所有业务模型的基类

  • key: () => string

    获取业务数据的 key 字段值

class Question extends Model{
    @key
    questionId = '0000-0000-0000-0000'
}

console.log(new Question().$key()) // '0000-0000-0000-0000'
  • [static] meta: () => ModelMeta

    获取业务类型的元字段信息

class Question extends Model{
    @key
    questionId = '0000-0000-0000-0000'
}

console.log(Question.meta()) // ModelMeta
  • [static] $server: (json) => Model

    根据定义的字段和字段类型将后台 json 转换为 Model 对象

class Question extends Model{
    @server('createBy')
    @kind(FieldKind.string)
    author = '(佚名)'
}

console.log(Question.$server({{createBy:'someone'}})) // Question {author:'someone'}
  • [static] $server: () => json

    根据定义的字段和字段类型将 Model 对象转换为后台 json

class Question extends Model{
    @server('createBy')
    @kind(FieldKind.string)
    author = '(佚名)'
}

const q = Question.$server({{createBy:'someone'}}) // Question {author:'someone'}

console.log(q.$server()) // {createBy:'someone'}
class Question extends Model{
    @key
    questionId = '0000-0000-0000-0000'

    @title('创建者')
    @kind(FieldKind.string)
    author = '(佚名)'
}

console.log(Question.meta()) // ModelMeta<Question>
console.log(Question.meta().field('title')) // FieldInfo<string,Question>

字段继承

业务的 key 信息和字段信息可以从父类中继承

class Question extends Model{
    @key
    questionId = '0000-0000-0000-0000'

    @title('创建者')
    @kind(FieldKind.string)
    author = '(佚名)'
}

console.log(Question.meta()) // ModelMeta<Question>
console.log(Question.meta().field('title')) // FieldInfo<string,Question>

class MarkDownQuestion extends Quest{
    @title('html 文本')
    @kind(FieldKind.string)
    html = ''
}

console.log(new MarkDownQuestion().key()) // '0000-0000-0000-0000'

console.log(MarkDownQuestion.meta().field('title')) // FieldInfo<string,Question>

装饰器

@key

设置 model 中的 key 字段

class Question extends Model{
    @key
    questionId = '0000-0000-0000-0000'
}

@title

( title:string )=>void

设置字段的标题(用于表单和表格字段生成)

class Question extends Model{
    @title('html 文本')
    @kind(FieldKind.string)
    html = ''
}

@validate

(...argus:(
  (value:any,data:Model) => (string | true ) | Promise<(string | true )>
)[]) => void

设置字段的验证方法

class Question extends Model{
    @title('html 文本')
    @validate(
      (value)=> value === ''? '请输入 html 文本': true ),
      (value)=> value.lenght> 1000? 'html 文本 长度不能大于 1000 个字符': true )
    )
    html = ''
}

@server

(from: string|(m:Model,j:Json)=>void,to?: string|(m:Model,j:Json)=>void) => void

设置字段与后台的转换方法

class Question extends Model{
    @title('html 文本')
    @server('fromHtml','toHtml')
    html = ''

    
    @title('标题')
    @server('topTitle')
    title = ''

    
    @title('产品')
    @server(
      (m,j)=> {m.product = j.product.type + '|' + j.product.name}
      (m,j)=> {j.product = { type: m.product.split('|')[0],name: m.product.split('|')[0]}}
    )
    product = ''
}

Meta 对象结构

0. meta

用于获取 meta 信息的接口

  • modelMap : Map<constructor, ModelMeta>

所有构造函数和 ModelMeta 实例的对应关系

  • getMeta: (constractor: { new(): Model }) => ModelMeta

获取构造函数对应的 ModelMeta 实例

  • setMeta: (constractor: { new(): Model }, m: ModelMeta) => void

设置构造函数对应的 ModelMeta 实例

  • setFiled: (constractor: { new(): Model }, name: string, setter: (m: FieldInfo) => void) => void

设置构造函数的字段描述信息

1. ModelMeta

业务模型的元数据对象。一个 class 对应一个 ModelData 实例。

  • cls: { new(): Model }

ModelMeta 实例对应的构造函数

  • parent: ModelMeta

对应构造函数父类对应的 ModelMeta 实例

  • fields: Map<string, FieldInfo>

当前类中的各字段的描述信息集合

  • field: (name:string) => FieldInfo

递归获取 name 字段的描述信息

  • keyField?: stirng

key 字段名字

  • key: () => string

递归获取 key 字段名字

2. FieldInfo

字段的描述信息

  • kind: FieldKind

    字段类型

  • title: string

    字段的标题

  • validate: ( (value: FieldValueType, data: Model) => Promise<boolean | string> | (boolean | string))[]

    字段的验证条件

3. FieldKind

字段类型的枚举


export enum FieldKind {
    string = 'string',
    number = 'number',
    datetime = 'datetime',
    boolen = 'boolen',
    kvalue = 'kvalue', // key value
    other = 'other'
}