Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Latest commit

 

History

History
42 lines (29 loc) · 934 Bytes

entity-constructors.md

File metadata and controls

42 lines (29 loc) · 934 Bytes


Using Entity Constructors

Internally, MikroORM never calls entity constructor, so you are free to use it as you wish. The constructor will be called only when you instantiate the class yourself via new operator, so it is a handy place to require your data when creating new entity.

For example following Book entity definition will always require to set title and author, but publisher will be optional:

@Entity()
export class Book {

  @PrimaryKey()
  _id: ObjectId;

  @Property()
  title: string;

  @ManyToOne()
  author: Author;

  @ManyToOne()
  publisher: Publisher;

  @ManyToMany({ entity: () => BookTag, inversedBy: 'books' })
  tags = new Collection<BookTag>(this);

  constructor(title: string, author: Author) {
    this.title = title;
    this.author = author;
  }

}

export interface Book extends IEntity { }

← Back to table of contents