0.0.25-alpha.0 • Published 2 years ago

wt-data-model v0.0.25-alpha.0

Weekly downloads
-
License
MIT
Repository
-
Last release
2 years ago

业务数据模型 ( 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'
}
0.0.21-alpha.0

2 years ago

0.0.22-alpha.0

2 years ago

0.0.25-alpha.0

2 years ago

0.0.24-alpha.0

2 years ago

0.0.23-alpha.0

2 years ago

0.0.20-alpha.0

2 years ago

0.0.19-alpha.0

2 years ago

0.0.18-alpha.0

2 years ago

0.0.17-alpha.0

2 years ago

0.0.16-alpha.0

2 years ago

0.0.15-alpha.0

2 years ago

0.0.14-alpha.0

2 years ago

0.0.13-alpha.0

2 years ago

0.0.12-alpha.0

2 years ago

0.0.11-alpha.0

2 years ago

0.0.10-alpha.0

2 years ago

0.0.9-alpha.0

2 years ago

0.0.8-alpha.0

2 years ago

0.0.7-alpha.0

2 years ago

0.0.6-alpha.0

2 years ago