Skip to content

BaseEntity Base Entity Class

BaseEntityis a basic entity class that is used to standardize the structure of database entities throughout the application. It contains common basic fields that are often used to track the status of an entity, create and update information, and soft delete functions.

Field description

Column NameLibrary FieldTypeDescriptionConstraintsPreset Values ​​
remarkremarkstringNotesLength 255, available-
statusstatusnumberstatus valuetinyint1
isDeletedis_deletednumberWhether to delete the tagtinyint0
creatorcreatorstringCreate a person IDbigint, can be empty-
createTimecreate_timeDateSettlement timedatetimeCURRENT_TIMESTAMP
updaterupdaterstringupdate person IDbigint, can be empty-
updateTimeupdate_timeDateUpdate timedatetime, available-

State value description

status column

  • 0: Disabled status
  • 1: Enabled status (preset value)

isDeleted column

  • 0: Not deleted (preset value)
  • 1: Deleted

Automatic timestamp

createTime

  • Automatically set to the current time stamp when entity is created
  • useCURRENT_TIMESTAMPAs a preset value
  • Not empty

updateTime

  • Automatically update to the current time stamp when the entity is updated
  • useonUpdate: 'CURRENT_TIMESTAMP'Configuration
  • Can be null (maybe empty when initially created)

How to use

Inherited use

Other entity classes can be inheritedBaseEntityTo get these basic fields:

typescript
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'

import { BaseEntity } from '@/common/entities/base.entity'

@Entity('users')
export class User extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  @Column({ length: 50, comment: '使用者名稱' })
  username: string

  // 其他特定欄位...
}

Soft Delete Function

passisDeletedThe column implements soft deletion function:

typescript
// 查詢未刪除的紀錄
const users = await userRepository.find({
  where: { isDeleted: 0 }
})

// 軟刪除記錄
await userRepository.update(id, { isDeleted: 1 })

Status Management

passstatusEnable/disabled status of field management entities:

typescript
// 查詢啟用的紀錄
const activeUsers = await userRepository.find({
  where: { status: 1, isDeleted: 0 }
})

// 禁用記錄
await userRepository.update(id, { status: 0 })

Database constraints

  • remark: Maximum length 255 characters, can be empty
  • status: tinyint type, with a preset value of 1
  • isDeleted: tinyint type, the default value is 0
  • creator: bigint type, can be empty, used to store user ID
  • createTime: datetime type, automatically set to the current time
  • updater: bigint type, can be empty, used to store updater ID
  • updateTime: datetime type, can be empty, automatically updated

Things to note

  1. Primary key:BaseEntityThe primary key column does not include, inherited entities need to define the primary key by themselves
  2. Index: It is recommended toisDeletedandstatusCreate indexes on columns to improve query performance
  3. Timestamp:createTimeandupdateTimeAutomatically managed by the database without manual settings
  4. Soft Deletion: Remember to filter deleted records when querying (isDeleted = 0
  5. Status Management: State change rules that need to be considered in business logic

Relationship with BaseReqDto

BaseEntityandBaseReqDtoCorrespondingly, but there are some important differences:

  • BaseEntityUsed for library mapping, including library constraints and preset values
  • BaseReqDtoUsed to request verification, including verification rules and transformation logic
  • Both contain the same basic field, but have different uses and configurations.