This article is to learn typeGOOSE decorator English document, incidentally translated and recorded, convenient to refer to later
Document the address is: typegoose. Making. IO/typegoose/d…
Typegoose decorator
@prop
@ prop (options: object, kind: WhatIsIt) is used to set properties in class (without this setting, there will be no final model inside)
A single option
required
The accepted type is: Boolean
If the property is required, the setting asks true(best practice is public property! : Any, pay attention!
Example: Class boy {@prop({required: true}) // Now in schema is required public money! : number;
@prop() // Default is false public girlfriend? : string; // Use a question mark to indicate optional}
index
Type received: Boolean
Create an index for this property, just like the @index decorator, without the options option.
Example: Class IndexedClass {@prop({index: true}) public indexedField? : string; }
unique
Type received: Boolean
Example of creating an index that sets this property as unique: Class IndexedClass {@prop({index: true}) public indexedField? : string; }
default
Received type: any
Class Defaulted {@prop({default: ‘hello world’}) public upperCase? : string; }
You can also set the default option to a function that Mongoose will execute with the return value as the default.
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
Type received: Boolean
If you want to disable IDS as a subdocument when created, set it to false
Example: class Nested {}
class Parent { @prop({ _id: false }) public nest: Nested; }
ref
Receive types: Class | string
Set class to reference (cannot derive type
Example: 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
Receive types: Class | string
Set class to reference (cannot derive type
Example: 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
Received type: string
Set the path to find the class to use.
Example: 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[]
There are two options in Object
Validator: (value)=> Boolean message: String. This message is displayed when validation fails.
Set custom functions for validators (must return a Boolean value)
Example:
class Validated { @prop({ validate: { validator: (v) => { return v.length <= 10; }, message: ‘value is over 10 characters long! ‘ } }) public validated? : string; }
alias
Received type: string
Give the property an alias (best practice is to add type information to the property)
Dummy {@prop({alias: ‘helloWorld’}) public hello: string; Dummy {@prop({alias: ‘helloWorld’}) public Hello: string; public helloWorld: string; }
select
Type received: Boolean By default, if you want to retrieve data without this property, set it to False
class Dummy { @prop({ select: false }) public hello: string; }
To retrieve an attribute with select: false, you must explicitly request it.
const dummies = await DummyModel.find().select(‘+hello’).exec();
get & set
Type received: (INPUT) => Output Sets getters and setters for fields, it is not virtual.
Example: 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
Receive types: any | () = > any)
Overrides those generated using design:type
enum SomeEnum { One, Two } class Dummy { @prop({ enum: SomeEnum, type: Number }) public enumprop: SomeEnum; }
enum
Receive types: enum | any [] is only allowed to receive to enumeration values (best practice is to use a typeScript enumeration)
Example: enum Gender {MALE = ‘MALE ‘, FEMALE =’ FEMALE ‘}
class Enumed { @prop({ enum: Gender }) public gender? : Gender; }
addNullToEnum
Type received: Boolean Adds NULL to an enumerated array
Example: 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
Received type:
innerOptions
InnerOptions is used to override options at the “type” level
Example:
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 is used to override “array” level options
Example:
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 }
An array of options
dim
Dim is used to set the dimension of an array (like a matrix can be used)
It has to be higher than zero
This option is overridden by type () =>[type]
Example: class Something {@prop({dim: 3, type: String}) public propy: String [][][]; //or @prop({ type: () => [[[String]]] }) public propy: string[][][]; }
{ type: [[[{ type: String }]]] }
String conversion options
lowercase
Type received: Boolean set to true, value always converted to lowercase
Class LowerCased {@prop({lowercase: true}) public lowercase: string; // “HELLO” -> “hello” }
uppercase
Type received: Boolean Set to true, values are always UpperCased Example: class uppercase {@prop({uppercase: true}) public uppercase: string; // “hello” -> “HELLO” }
trim
Type received: Boolean set to true, and the incoming value will strip out Spaces before and after
Trimmed {@prop({trim: true}) public trim: string; // ” Trim me ” -> “Trim me” }
String validation options
maxlength
Type received: number Sets the maximum length that a string can have
Example: Class MaxLengthed {@prop({maxLength: 10}) public MaxLengthed? : string; }
minlength
Sets the minimum length of the string (must be greater than 0).
Example: Class MinLengthed {@prop({minLength: 10}) public MinLengthed? : string;
}
RegExg
Type received: number Sets a regular expression that the string must match to
Example: class RegExpString {@prop({match: /^H/ I}) public matched? : string; }
Digital validation options
max
Type received: number Sets the maximum value allowed by the property
Example: Class Maxed {@prop({Max: 10}) public Maxed? : number; }
min
Type received: number Sets the minimum value allowed by the property
Example: Class Mined {@prop({min: 0}) public Mined? : number; }
whatIsIt
This is an enumeration to indicate what prop should be, which is set automatically in most cases. This can be overridden in the second argument to @prop
enum WhatIsIt { ARRAY, MAP, NONE // default for properties if no Map / Array is detected }