@smallprod/models
Version:
67 lines (59 loc) • 1.71 kB
text/typescript
/* Exemple of an entity class */
/* There are some warnings becouse you must enable decorators in your tsconfig file */
import {
AllowNull,
AutoCreateNUpdate,
AutoIncrement,
BigInt,
Date,
Entity,
EntityManager,
Id,
ManyToMany,
NonPersistent,
PrimaryKey,
Table,
Unique,
Varchar,
} from '@smallprod/models';
import ArticleEntity from './article.entity';
// This is the table name in which are stored our users
export default class UserEntity extends Entity {
private id = -1; // This attribute is a primary key and is treated has the id for findById(), delete() and update()
private firstname: string;
private lastname: string;
private email: string;
private birthDate: Date;
private age: number; // This attribute is not persisted in the table
// The second parameter specifies if the ORM should always fetch articles or not
private articles: ArticleEntity[] = [];
constructor(
firstname: string,
lastname: string,
email: string,
birthDate: Date,
) {
super();
this.firstname = firstname;
this.lastname = lastname;
this.email = email;
this.birthDate = birthDate;
this.age = 0; // Imagine we compute the age from the birthDate
}
public addArticle = (article: ArticleEntity) => {
this.articles.push(article);
};
}
EntityManager.registerEntity(UserEntity); // This is really important but it can also be done with EntityManager.registerEntities([all your entities]) wich can be easier