typegoose 装饰器 @prop()

2,084 阅读4分钟

本文是学习typegoose装饰器的英文文档,顺便翻译记录下来,方便以后查阅

文档地址是:typegoose.github.io/typegoose/d…

typegoose 装饰器

@prop

@prop(options:object,kind:WhatIsIt) 用来在类里面设置属性(没有这个设置,类不会出现最终的模型里面)

单个选项

required

接受的类型是:boolean

如果属性是必须的,设置问true(最佳实践是 public property!:any,注意 !)

示例: class boy { @prop({ required: true }) // 现在在schema里面是必须的 public money!: number;

@prop() // 默认是false public girlfriend?: string; // 使用问号来表示可选的 }

index

接收的类型:Boolean

为这个属性创建一个index,跟@index 装饰器一样,单没有options选项。

示例: class IndexedClass { @prop({ index: true }) public indexedField?: string; }

unique

接收的类型:Boolean

创建一个索引设置这个属性为唯一的 示例: class IndexedClass { @prop({ index: true }) public indexedField?: string; }

default

接收的类型:any

当创建的时候没有值,给一个默认值 示例: class Defaulted { @prop({ default: 'hello world' }) public upperCase?: string; }

你同样可以设置default 选项为一个函数,mongoose会执行这个函数并且把返回值作为默认值。

class Defaulted { @prop({ required: true }) firstName!: string @prop({ required: true }) lastName!: string

@prop({ default: function (this: DocumentType) { return ${this.firstName} ${this.lastName} } }) public fullName?: string; // mark as optional, because it will be defaulted }

_id

接收的类型:Boolean

如果你想要关闭创建时IDS为子文档的,设置为false

示例: class Nested {}

class Parent { @prop({ _id: false }) public nest: Nested; }

ref

接收的类型:Class | string

设置class为引用(不能来推导类型

示例: class Kitten { @prop() public name?: string; }

class Cat { // single examples @prop({ ref: () => Kitten }) public kitten?: Ref; // or @prop({ ref: 'Kitten' }) public kitten?: Ref;

// array examples @prop({ ref: () => Kitten }) public kittens?: Ref[]; // or @prop({ ref: 'Kitten' }) public kittens?: Ref[]; }

ref

接收的类型:Class | string

设置class为引用(不能来推导类型

示例: class Kitten { @prop() public name?: string; }

class Cat { // single examples @prop({ ref: () => Kitten }) public kitten?: Ref; // or @prop({ ref: 'Kitten' }) public kitten?: Ref;

// array examples @prop({ ref: () => Kitten }) public kittens?: Ref[]; // or @prop({ ref: 'Kitten' }) public kittens?: Ref[]; }

refPath

接收的类型: string

设置要查找要使用的类的路径。

示例: class Car {} class Shop {}

// in another class class Another { @prop({ required: true, enum: 'Car' | 'Shop' }) public which!: string;

@prop({ refPath: 'which' }) public kind?: Ref<Car | Shop>; }

validate

接收的类型: object OR RegExp OR (value) => boolean OR object[]

在object 中有两个选项

validator: (value)=> boolean message: String,当验证失败的时候,这个消息就会出现。

为验证器设置自定义函数(必须返回一个布尔值)

示例:

class Validated { @prop({ validate: { validator: (v) => { return v.length <= 10; }, message: 'value is over 10 characters long!' } }) public validated?: string; }

alias

接收的类型: string

给属性设置一个别名(最佳实践是给属性添加类型信息)

示例: class Dummy { @prop({ alias: 'helloWorld' }) public hello: string; public helloWorld: string; }

select

接收的类型: boolean 默认情况下,如果要在没有此属性的情况下检索数据,请将其设置为False

class Dummy { @prop({ select: false }) public hello: string; }

如果要检索一个属性是select:false ,你必须显式的请求他。

const dummies = await DummyModel.find().select('+hello').exec();

get & set

接收的类型: (input)=> output 为字段设置 getter 和 setter,它不是虚拟的。

预处理字符串到字符串 示例: class Dummy { @prop({ set: (val: string) => val.toLowerCase(), get: (val: string) => val }) public hello: string; }

class Dummy { // this value is a "string-array" during runtime and is stored in the database as a "primite-string" @prop({ set: (val: string[]) => val.join(' '), get: (val: string) => val.split(' '), type: String }, WhatIsIt.NONE) // requires explicit setting of "WhatIsIt" public fullName?: string[]; }

type

接收的类型:any | () => any

会覆盖使用 design:type 生成的

enum SomeEnum { One, Two } class Dummy { @prop({ enum: SomeEnum, type: Number }) public enumprop: SomeEnum; }

enum

接收的类型: enum | any[] 只允许接收来枚举的值(最佳实践是使用typeScript的枚举)

示例: enum Gender { MALE = 'male', FEMALE = 'female' }

class Enumed { @prop({ enum: Gender }) public gender?: Gender; }

addNullToEnum

接收的类型: boolean 添加 null 到枚举数组

示例: enum SomeNumberEnum { one = 1, two = 2 } class AddNullToEnum { @prop({ enum: SomeNumberEnum, addNullToEnum: true }) public value?: SomeNumberEnum; }

const AddNullToEnumModel = getModelForClass(AddNullToEnum);

AddNullToEnumModel.schema.path('value').options.enum === [1, 2, null]; // true

// this is necessary to avoid a validation error new AddNullToEnumModel({ value: null } as AddNullToEnum);

discriminators

接收的类型:

innerOptions

innerOptions 用于覆盖“类型”级别的选项

示例:

class Something { @prop({ required: true }) public propy: string[]; }

// This would be mapped to { type: [{ type: String }], required: true }

// when using the override class Something { @prop({ innerOptions: { required: true } }) public propy: string[]; }

// This would be mapped to { type: [{ type: String, required: true }] }

outerOptions

externalOptions 用于覆盖“数组”级别的选项

示例:

class Something { @prop({ maxlength: 1 }) public propy: string[]; }

// This would be mapped to { type: [{ type: String, maxlength: 1 }] }

// when using the override class Something { @prop({ outerOptions: { maxlength: 1 } }) public propy: string[]; }

// This would be mapped to { type: [{ type: String }], maxlength: 1 }

数组选项

dim

dim是用来设置数组的维度(类似矩阵的可以用上)

必须高于0

这个选项会被type () =>[type] 覆盖

示例: class Something { @prop({ dim: 3, type: String }) public propy: string[][][]; //or @prop({ type: () => [[[String]]] }) public propy: string[][][]; }

{ type: [[[{ type: String }]]] }

字符串转换选项

lowercase

接收的类型: boolean 设置为true, 值总会转换成小写

示例: class LowerCased { @prop({ lowercase: true }) public lowerCase: string; // "HELLO" -> "hello" }

uppercase

接收的类型: boolean 设置为true, 值总会转换成大写 示例: class UpperCased { @prop({ uppercase: true }) public upperCase: string; // "hello" -> "HELLO" }

trim

接收的类型: boolean 设置为true, 传进来的值就会去除前后空格

示例: class Trimmed { @prop({ trim: true }) public trim: string; // " Trim me " -> "Trim me" }

字符串验证选项

maxlength

接收的类型: number 设置字符串可以拥有的最大长度

示例: class MaxLengthed { @prop({ maxlength: 10 }) public maxlengthed?: string;}

minlength

接收的类型: number 设置字符串的最小长度(必须大于0)

示例: class MinLengthed { @prop({ minlength: 10 }) public minlengthed?: string;

}

RegExg

接收的类型: number 设置一个正则表达式,字符串必须匹配到

示例: class RegExpString { @prop({ match: /^H/i }) public matched?: string; }

数字验证选项

max

接收的类型: number 设置属性允许的最大值

示例: class Maxed { @prop({ max: 10 }) public maxed?: number; }

min

接收的类型: number 设置属性允许的最小值

示例: class Mined { @prop({ min: 0 }) public mined?: number; }

whatIsIt

这是一个枚举来表示prop应该是什么,这在大多数情况下是自动设置的。可以在@prop的第二个参数中覆盖

enum WhatIsIt { ARRAY, MAP, NONE // default for properties if no Map / Array is detected }