Skip to content

Commit

Permalink
refactor(ent): split created/updated/deleted schema and improve indexing
Browse files Browse the repository at this point in the history
- Split CreatedSchema into CreateSchema and UpdateSchema
- Rename UpdatedSchema to UpdateSchema
- Rename DeletedSchema to DeleteSchema
- Update CreateUpdateSchema to include fields and indexes from both CreateSchema and UpdateSchema
- Add Immutable() constraint to UniqueString method
  • Loading branch information
godcong committed Nov 29, 2024
1 parent ae58683 commit 3ca827d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 14 deletions.
47 changes: 34 additions & 13 deletions dbo/ent/mixin/mixin.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,34 @@ func (Audit) Indexes() []ent.Index {
}
}

// CreatedSchema schema to include control and time fields.
type CreatedSchema struct {
// CreateUpdateSchema schema to include control and time fields.
type CreateUpdateSchema struct {
mixin.Schema
}

// Fields of the mixin.
func (CreatedSchema) Fields() []ent.Field {
func (CreateUpdateSchema) Fields() []ent.Field {
return append(
CreateSchema{}.Fields(),
UpdateSchema{}.Fields()...,
)
}

// Indexes of the mixin.
func (CreateUpdateSchema) Indexes() []ent.Index {
return append(
CreateSchema{}.Indexes(),
UpdateSchema{}.Indexes()...,
)
}

// CreateSchema schema to include control and time fields.
type CreateSchema struct {
mixin.Schema
}

// Fields of the mixin.
func (CreateSchema) Fields() []ent.Field {
return []ent.Field{
field.Time("created_time").
Default(time.Now).
Expand All @@ -67,19 +88,19 @@ func (CreatedSchema) Fields() []ent.Field {
}

// Indexes of the mixin.
func (CreatedSchema) Indexes() []ent.Index {
func (CreateSchema) Indexes() []ent.Index {
return []ent.Index{
index.Fields("created_time"),
}
}

// UpdatedSchema schema to include control and time fields.
type UpdatedSchema struct {
// UpdateSchema schema to include control and time fields.
type UpdateSchema struct {
mixin.Schema
}

// Fields of the mixin.
func (UpdatedSchema) Fields() []ent.Field {
func (UpdateSchema) Fields() []ent.Field {
return []ent.Field{
field.Time("update_time").
Default(time.Now).
Expand All @@ -88,19 +109,19 @@ func (UpdatedSchema) Fields() []ent.Field {
}

// Indexes of the mixin.
func (UpdatedSchema) Indexes() []ent.Index {
func (UpdateSchema) Indexes() []ent.Index {
return []ent.Index{
index.Fields("update_time"),
}
}

// DeletedSchema schema to include control and time fields.
type DeletedSchema struct {
// DeleteSchema schema to include control and time fields.
type DeleteSchema struct {
mixin.Schema
}

// Fields of the Model.
func (DeletedSchema) Fields() []ent.Field {
func (DeleteSchema) Fields() []ent.Field {
return []ent.Field{
field.Time("delete_time").
Optional().
Expand All @@ -109,11 +130,11 @@ func (DeletedSchema) Fields() []ent.Field {
}

// Indexes of the mixin.
func (DeletedSchema) Indexes() []ent.Index {
func (DeleteSchema) Indexes() []ent.Index {
return []ent.Index{
index.Fields("delete_time"),
}
}

// SoftDeleteSchema schema to include control and time fields.
type SoftDeleteSchema = DeletedSchema
type SoftDeleteSchema = DeleteSchema
3 changes: 2 additions & 1 deletion dbo/ent/mixin/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,8 @@ func PrimaryID(name string) ent.Field {
// Create a unique string field with the given name and maximum length.
return field.String(name).
MaxLen(36).
Unique()
Unique().
Immutable()
}

// Time returns a time field with a default value of ZeroTime and a custom schema type for MySQL.
Expand Down

0 comments on commit 3ca827d

Please sign in to comment.