Specifies the type of change tracking to use for an entity.
See ChangeTrackingType for more information on the types of change tracking that are available.
@Entity()
@ChangeTracking(ChangeTrackingType.DeferredImplicit)
export class Person {
...
}
Specifies the name of the collection used to hold the entity.
If a name for the collection is not specified using the Collection decorator, an entity is mapped to a collection in MongoDB based on the name of the class and the collectionNamingStrategy in the Configuration. The default naming strategy is CamelCase.
@Collection("people")
@Entity()
export class Person {
...
}
Describes the collection used to hold the entity.
If a name for the collection is not specified using the Collection decorator, an entity is mapped to a collection in MongoDB based on the name of the class and the collectionNamingStrategy in the Configuration. The default naming strategy is CamelCase.
@Collection({ name: "logs", db: "dbname", options: { capped: true })
@Entity()
export class Log {
...
}
Specifies the property converter to use.
See PropertyConverter for more information on creating property converts. The Converter decorator can be used at the class or property level. The decorator accepts an instance of a PropertyConverter, the parameterless constructor for a PropertyConverter, or a string for a named PropertyConverter.
The example below defines the property converter on the Point class. In this case the convert is used for all instances of the Point class.
@Converter(PointConverter)
export class Point {
x: number;
y: number;
}
The example below defines the property converter as an instance of the converter. Passing an instance of the converter to the decorator allows for options to be passed to the converter.
@Converter(new EnumerationConverter(IdentifierType))
export class IdentifierType extends Enumeration {
...
}
The example below specifies the property converter on the properties of the target class.
@Embeddable(Rectangle)
export class Rectangle {
@Converter(PointConverter)
topLeft: Point;
@Converter(PointConverter)
bottomRight: Point;
}
Specifies the name of the field used for the class inheritance discriminator.
If the discriminator field is not specified, a value of __t
is used.
@Entity()
@DiscriminatorField("type")
export class Person {
...
}
Specifies the value to use for the discriminator for a class.
If the discriminator value is not specified, the discriminator value for a class is determined using the discriminatorNamingStrategy on the Configuration. By default, the name of the class is used.
@Entity()
class Party {
...
}
@Entity()
@DiscriminatorValue("P")
class Person extends Party {
...
}
@Entity()
@DiscriminatorValue("O")
class Organization extends Party {
...
}
Specified the type of an array element.
TypeScript does not provide the type of an array element, so the type of the array element must be indicate with the ElementType decorator.
@Entity()
export class Organization {
@ElementType(Address)
addresses: Address[];
}
Specifies that a class can be embedded as a subdocument within an entity, array, or another embeddable.
@Embeddable()
export class Address {
...
}
Specifies that a class is a persistent entity and will be serialized to a document within a MongoDB collection.
@Entity()
export class Task {
...
}
Specifies that an enum value should be serialized as a string.
By default enums are serialized as numbers. To serialize an enum as a string, add the Enumerated decorator to a
property, passing in the enum. Note that this cannot be used with const
enums.
export enum TaskStatus {
Pending,
Completed,
Archived
}
@Entity()
export class Task {
@Enumerated(TaskStatus)
status: TaskStatus;
...
}
By default entity references are not loaded and must be fetched using Session#fetch or similar. If a FetchType of Eager is specified on an entity reference then that reference is automatically fetched when the entity is loaded.
@Entity()
export class Task {
@Fetch(FetchType.Eager)
owner: Person;
}
When an entity is loaded, all fields for that entity are retrieved from the database. Specifying a FetchType of Lazy for a field causes that field to not be retrieved from the database when the entity is loaded. The field is only loaded by calling Session#fetch and indicating which field to load.
@Entity()
export class Person {
@Fetch(FetchType.Lazy)
image: Buffer;
}
Specifies the name of the field that should be used to serialize a property on an entity or embeddable class.
@Entity()
export class Person {
@Field("n")
name: string;
...
}
Describes the field that should be used to serialize a property on an entity or embeddable class.
Setting a value for nullable
in the field description indicates whether or not null
values should be saved to the database. If not
specified, the value in the Configuration is used. If null
values are not allowed, null
values are treated the same as
undefined
values and removed from the serialized document.
@Entity()
export class Person {
@Field({ nullable: true })
name: string;
...
}
Specifies that a class is immutable. Can be used on Entity or Embeddable types. When specified on an Entity, the Entity is excluded from dirty checking. When specified on an Embeddable, the original document for the Embeddable is cached and used for serialization.
@Embeddable()
@Immutable()
export class Name {
get first(): string {
return this._first;
}
get last(): string {
return this._last;
}
@Field()
private _last: string;
@Field()
private _first: string;
constructor(last: string, first: string) {
this._last = last;
this._first = first;
}
}
Specifies a database index should be generated on the collection that holds this entity.
This is the required method of defining indexes when compound keys are needed.
@Entity()
@Index({ keys: [['name', 1], ['age', 1]] })
export class Person {
@Field()
name: string;
@Field()
age: number;
...
}
Specifies a database index should be generated for the specified field on the collection holds this entity.
@Entity()
export class Person {
@Index()
name: string;
...
}
Specifies that a property is the inverse side of a entity reference.
In the example below, the reference is stored on the patient
property of the Admission
entity.
@Entity()
export class Patient {
@InverseOf("patient")
@ElementType(Admission)
admissions: Admission[];
...
}
@Entity()
export class Admission {
@Field()
patient: Patient;
...
}
...
session.fetch(patient.admissions, (err, admissions) => {
...
});
In the example below, the references are stored on the admissions
property of the Patient
entity.
@Entity()
export class Patient {
@ElementType(Admission)
admissions: Admission[];
...
}
@Entity()
export class Admission {
@InverseOf("admissions")
patient: Patient;
...
}
...
session.fetch(admission.patient, (err, patient) => {
...
});
Indicates that a property is a reference to the parent of an embeddable class.
The reference to the parent is automatically populated when the document is read from the database and the object graph is built. The value of the property is not saved to the database.
@Entity()
export class Person {
@ElementType(Address)
addresses: Address[];
...
}
@Embeddable()
export class Address {
@Parent()
resident: Person;
...
}
Specifies that a property should not be persisted.
Properties are mapped to persistent document fields on an opt-in basis. All properties that are decorated are mapped. However, if you wish to prevent a decorated property from being mapped, decorate that property with the Transient decorator as demonstrated below.
@Entity()
export class User {
@Transient()
@SomeOtherDecorator()
private _something: string;
}
Specifies the type of a property.
It is generally not necessary to explicitly indicate the type of a property.
However, when a property is an embeddable or a reference to an entity, sometimes the type of the property cannot be
determined because of circular references of import
statements. In this case the Type decorator should be used
with the name of the type.
@Entity()
export class Task {
@Type("Person")
owner: Person;
}
Specifies the name of the field used to hold the entity version for optimistic locking.
If the version field is not specified, __v
is used.
@Entity()
@VersionField("version")
export class Person {
...
}
Specifies if optimistic locking should be used or not.
Optimistic locking is enabled by default. A value of false
disables optimistic locking.
@Entity()
@Versioned(false)
export class Person {
...
}
Returns true if values are equivalent. Values can be entities or references to entities.
The first reference or entity to compare.
The second reference or entity to compare.
Generated using TypeDoc
Specifies which operations should cascade to a property on an entity or embeddable. The type of the property must be an entity. See CascadeFlags for more information on the available cascade options.
Example
@Entity() export class Task { @Cascade(CascadeFlags.Save | CascadeFlags.Remove) owner: Person; }