Base Model

You can create a base-mode.ts file, that is the global abstraction of all the entities you will create in your project.

If you create a new entity in the database, you must also add the .entity.ts file within the project.

import {
  CreateDateColumn,
  Generated,
  PrimaryColumn,
  UpdateDateColumn,
} from 'typeorm';

export class BaseModel {
  @PrimaryColumn({ type: 'uuid' })
  @Generated('uuid')
  public id?: string;

  @CreateDateColumn()
  public createdAt?: Date;

  @UpdateDateColumn()
  public updatedAt?: Date;
}

Now to create a new entity, you just need to inherit from the BaseModel class and declare the specific fields for that entity.

import { Entity, Column} from 'typeorm';
import { BaseModel } from './common/database/base.model';

@Entity()
export class User extends BaseModel {
  @Column()
  public name?: string;

  @Column()
  public email: string;
}

Last updated