-
Notifications
You must be signed in to change notification settings - Fork 0
Base Entity
samithiwat edited this page Feb 11, 2023
·
1 revision
The entity is the object that we interested in database
The fundamental entity
The base entity is contains the essential attributes that entity should have
type Base struct {
ID *uuid.UUID `json:"id" gorm:"primary_key"`
CreatedAt time.Time `json:"created_at" gorm:"type:timestamp;autoCreateTime:nano"`
UpdatedAt time.Time `json:"updated_at" gorm:"type:timestamp;autoUpdateTime:nano"`
DeletedAt gorm.DeletedAt `json:"deleted_at" gorm:"index;type:timestamp"`
}
The Base
entity will create new uuid everytime when save to database if the id is blank
func (b *Base) BeforeCreate(_ *gorm.DB) error {
if b.ID == nil {
b.ID = UUIDAdr(uuid.New())
}
return nil
}
When you want to define new entity you need to embed this entity
type NewEntity struct{
gosdk.Base
// other fields
}
The entity for collect the metadata of pagination
The metadata of pagination
type PaginationMetadata struct {
ItemsPerPage int
ItemCount int
TotalItem int
CurrentPage int
TotalPage int
}
The method for get the offset value
offset := meta.GetOffset()
name | description | example |
---|---|---|
offset | the offset value (calculate from ItemPerPage value and CurrentPage value) | 10 |
The method for get the item per page value
itemPerPage := meta.GetItemPerPage()
name | description | example |
---|---|---|
itemPerPage | the item per page value (min: 10, max: 100) | 10 |
The method for get the current page value
currentPage := meta.GetItemPerPage()
name | description | example |
---|---|---|
currentPage | the current page value (min: 1) | 1 |
Convert to proto type
metaProto := meta.ToProto()
name | description | example |
---|---|---|
metaProto | metadata in *pb.PaginationMetadata
|