Prop
The prop
decorator util is used to define the properties of the model. If needed, multiple prop decorators can be used on the same property.
For usage examples check out the defining models guide.
It supports the following methods:
prop
Just a regular property without any default value
@prop
public name: string;
prop.identifier
Defines the ID attribute of the model. If the decorator is not used on a model, the default ID attribute will be id
.
@prop.identifier
public key: number;
prop.type
Defines the type attribute of the model. If not used, the type will default to the static type property. This decorator is mostly for internal use and dynamic model types.
@prop.type
public type: string;
prop.defaultValue
Used to set a default value of the property.
Note: This needs to be used instead of property assignment. Because of the execution order, property assignment will also override the initial values when creating a model.
@prop.defaultValue(true)
public favorite: boolean;
prop.toOne
Used to define a reference to one model. Note: The value can also be undefined.
@prop.toOne(Person)
public spouse?: Person;
prop.toMany
Used to define a reference to multiple models. This decorator is useful because it supports indirect reference (the second argument).
@prop.toMany(Person, 'owner')
public children: Array<Person>;
prop.toOneOrMany
Defines a relationship to a single or multiple models. The value can also be undefined.
@prop.toOneOrMany(Pet)
public pets?: Pet|Array<Pet>;
prop
without decorators
Using In some cases, you might not be able to use the @prop
decorator, but you can still use it as a function. The function has two arguments: the class you're decorating and name of the property you're decorating. You can find the example in the defining models section.