2.0.2 • Published 2 years ago
decorator-dto v2.0.2
decorator-dto插件使用说明
- @Dto:用在class的上面,见下面demo
- @Format(fn):用在class的属性上面,参数为function,fn函数接收当前属性值并进行处理,执行format后将fn返回的值赋值到当前属性上.见下面demo
- @Link(k,fn):用在class的属性上面,参数分别为string和function,fn函数接收属性k(class未申明的属性)的值并进行处理,执行format后将fn返回的值赋值到当前属性上.见下面demo
- @OneToOne(k, fn) 用在class的属性上面,参数k(class申明的属性)为string,fn为可选的转换函数,执行format后将属性k值赋值到当前属性上.见下面demo
- @ManyToOne(K,...,fn) 用在class的属性上面,参数为k,..,k必须为class申明的属性,fn为可选的转换函数,执行format后判断是否存在数组里的属性,若存在则将任一个值赋值到当前属性上.见下面demo
- @Property,用在class的属性上面,收集属性字段,当于其他装饰器同时存在时无意义,见下面demo
- @Inject(k, fn),用在class的属性上面, 第一个参数为当前类上的属性,可以为数组,在Format、Link、OneToOne、ManyToOne、Inject执行完后Condition执行前执行。
- @Condition(fn),用在class的属性上面,参数为function,fn的参数为当前最新对象,fn的返回值必须为boolean,若为true则当前属性显示,反之当前属性不显示,见下面demo
- warp: 当前class的静态方法用于批量格式化,见下面demo
注:
- Link不能与OneToOne、ManyToOne、Format一起使用在同一属性上。
- OneToOne与ManyToOne不能相互作用
@Dto
class Demo extends BaseDto {
@Format(val => val + 1)
test = 1
@Link('name', val => val + 1)
test1 = 2;
@OneToOne('test1')
test2 = 2;
@ManyToOne(['name2'])
test3 = 2;
@ManyToOne(['name3'], val => val + 1 )
test4 = 2;
@OneToOne('name4', val => val + 1)
@Inject('test4', (v, m) => {
return v + m
})
test5 = 2;
@Condition(obj => true)
test6 = 2;
}
// 调用
const demo = new Demo();
const tmp = Demo.warp([{ test: 3, name: 4, name1: 5, name2: 6, name3: 7, name4: 8 }])
console.log(tmp)
//打印 [
{
test: 4,
test1: 5,
test2: 5,
test3: 6,
test4: 8,
test5: 17,
test6: 2
}
]
demo.name = 2
demo.name1 = 33
demo.name2 = 44
demo.name3 = 55
demo.name4 = 66
const t = demo.format();
console.log(t.test) // 2
console.log(t.test1) // 3
console.log(t.test2) // 33
console.log(t.test3) // 44
console.log(t.test4) // 56 = 55 + 1
console.log(t.test5) // 67 = 66 + 1
console.log(t.test6) // 2