Skip to content

Commit

Permalink
Merge pull request #9 from origadmin/dev
Browse files Browse the repository at this point in the history
refactor(ent): split created/updated/deleted schema and improve indexing
  • Loading branch information
godcong authored Nov 29, 2024
2 parents d23fa7c + 3ca827d commit 81cf73a
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 81cf73a

Please sign in to comment.