I was wondering if it's a good idea to add some custom methods to entities, or would this be discouraged?
using external templates seems quite cumbersome to add things like this, would it be a bad idea to just add a ent/user_custom.go and define them there?
ie. simple helpers like
func (u *User) IsActiveUser() bool {
return !u.IsBot && user.LastActiveAt != nil
}
and for updating?
func (u *User) Deactivate(ctx context.Context) error {
u.IsActive = false
return u.Update().Save(ctx)
}
and same for querying?
func (uq *UserQuery) AllActiveForCompany(company *Company) *UserQuery {
...
}
or?
func (u *User) AllChibiPets(ctx context.Context) ([]*Pet, error) {
return u.PetsQuery().Where(pets.SizeEQ("chibi")).All(ctx)
}
Hey @rubensayshi!
Both options are fine. I usually suggest using custom templates when the custom logic has to be generated for more than one entity type, or it's based on code-generation information.
func (u *User) Deactivate(ctx context.Context) error {
u.IsActive = false
return u.Update().Save(ctx)
}
Just note that this will _not_ work, see docs on how to perform updates.
awesome, thx for both replies!
Most helpful comment
Just note that this will _not_ work, see docs on how to perform updates.