Documentation
¶
Overview ¶
Package gomodel provides a simple and eloquent way to interact with the GORM ORM.
Index ¶
- func Create[K comparable](db *gorm.DB, m ModelInterface[K]) error
- func CreateOrUpdate[K comparable](db *gorm.DB, m ModelInterface[K]) error
- func CreateOrUpdateAll[K comparable](db *gorm.DB, m ModelInterface[K]) error
- func Delete[K comparable](db *gorm.DB, m ModelInterface[K], id K) error
- func Exist[K comparable](db *gorm.DB, m ModelInterface[K], id K, withTrashed bool) (bool, error)
- func Find[M ModelInterface[K], K comparable](db *gorm.DB, m M, id K, relations ...string) (M, error)
- func FindWithTrashed[M ModelInterface[K], K comparable](db *gorm.DB, m M, id K, withTrashed bool, relations ...string) (M, error)
- func MustExist[K comparable](db *gorm.DB, m ModelInterface[K], id K, withTrashed bool) bool
- func Restore[K comparable](db *gorm.DB, m ModelInterface[K], id K) error
- func Update[K comparable](db *gorm.DB, m ModelInterface[K]) error
- func UpdateAll[K comparable](db *gorm.DB, m ModelInterface[K]) error
- type Model
- func (m *Model[M, K]) All() ([]M, error)
- func (m *Model[M, K]) Count() (int64, error)
- func (m *Model[M, K]) Create(model M) *Model[M, K]
- func (m *Model[M, K]) CreateOrUpdate(model M) *Model[M, K]
- func (m *Model[M, K]) CreateOrUpdateAll(model M) *Model[M, K]
- func (m *Model[M, K]) Delete() error
- func (m *Model[M, K]) Exist() (bool, error)
- func (m *Model[M, K]) Find(id K) *Model[M, K]
- func (m *Model[M, K]) First() *Model[M, K]
- func (m *Model[M, K]) Fresh() *Model[M, K]
- func (m *Model[M, K]) Get() (M, error)
- func (m *Model[M, K]) Load(relations ...string) *Model[M, K]
- func (m *Model[M, K]) MustCreate(model M) *Model[M, K]
- func (m *Model[M, K]) MustCreateOrUpdate(model M) *Model[M, K]
- func (m *Model[M, K]) MustExist() bool
- func (m *Model[M, K]) MustUpdate(model M) *Model[M, K]
- func (m *Model[M, K]) OrderBy(field string, sortDirection SortDirection) *Model[M, K]
- func (m *Model[M, K]) Query() *gorm.DB
- func (m *Model[M, K]) Restore() error
- func (m *Model[M, K]) Save() error
- func (m *Model[M, K]) Select(query interface{}, params ...interface{}) *Model[M, K]
- func (m *Model[M, K]) Set(model M) *Model[M, K]
- func (m *Model[M, K]) Skip(offset int) *Model[M, K]
- func (m *Model[M, K]) Take(limit int) *Model[M, K]
- func (m *Model[M, K]) Update(model M) *Model[M, K]
- func (m *Model[M, K]) UpdateAll(model M) *Model[M, K]
- func (m *Model[M, K]) Where(query string, params ...interface{}) *Model[M, K]
- func (m *Model[M, K]) WhereGroup(query func(q *gorm.DB) *gorm.DB) *Model[M, K]
- func (m *Model[M, K]) With(relations ...string) *Model[M, K]
- func (m *Model[M, K]) WithTrashed() *Model[M, K]
- type ModelInterface
- type SortDirection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Create ¶
func Create[K comparable](db *gorm.DB, m ModelInterface[K]) error
Create a new model in the database.
func CreateOrUpdate ¶
func CreateOrUpdate[K comparable](db *gorm.DB, m ModelInterface[K]) error
CreateOrUpdate a new model in the database or update it if it already exists.
func CreateOrUpdateAll ¶
func CreateOrUpdateAll[K comparable](db *gorm.DB, m ModelInterface[K]) error
CreateOrUpdateAll a new model in the database or update it if it already exists.
func Delete ¶
func Delete[K comparable](db *gorm.DB, m ModelInterface[K], id K) error
Delete the model from the database. If ModelInterface has a deleted_at field, it will be set to the current time.
The id is always bound as a query parameter. Passing it to GORM as an inline condition would let a string key be interpreted as raw SQL.
func Exist ¶
func Exist[K comparable](db *gorm.DB, m ModelInterface[K], id K, withTrashed bool) (bool, error)
Exist return true if the model with the primary key (id) exists in the database.
func Find ¶
func Find[M ModelInterface[K], K comparable](db *gorm.DB, m M, id K, relations ...string) (M, error)
Find the model for a given id.
func FindWithTrashed ¶
func FindWithTrashed[M ModelInterface[K], K comparable](db *gorm.DB, m M, id K, withTrashed bool, relations ...string) (M, error)
FindWithTrashed the model for a given id. When withTrashed is true, soft-deleted rows are included.
func MustExist ¶
func MustExist[K comparable](db *gorm.DB, m ModelInterface[K], id K, withTrashed bool) bool
MustExist return true if the model with the primary key (id) exists in the database. Panic if an error occurs.
func Restore ¶
func Restore[K comparable](db *gorm.DB, m ModelInterface[K], id K) error
Restore the model in the database. The deleted_at field will be set to nil.
func Update ¶
func Update[K comparable](db *gorm.DB, m ModelInterface[K]) error
Update the model in the database.
func UpdateAll ¶
func UpdateAll[K comparable](db *gorm.DB, m ModelInterface[K]) error
UpdateAll update the model in the database, even nil value
Types ¶
type Model ¶
type Model[M ModelInterface[K], K comparable] struct { Base M DB *gorm.DB FilterQuery *gorm.DB Err error // contains filtered or unexported fields }
Model is a chainable query builder around a ModelInterface implementation. M is the model type and K the type of its primary key.
Errors are carried by the chain in Err and surfaced by the terminal methods (Get, Save, All, ...), so intermediate calls can be chained without checking an error after every step.
func New ¶
func New[M ModelInterface[K], K comparable](db *gorm.DB, m M) *Model[M, K]
New instantiate a new Model for the given M type.
func (*Model[M, K]) CreateOrUpdate ¶
CreateOrUpdate a new model in the database or update it if it already exists.
func (*Model[M, K]) CreateOrUpdateAll ¶
CreateOrUpdateAll a new model in the database or update it if it already exists.
func (*Model[M, K]) Exist ¶
Exist return true if the model with the primary key (id) exists in the database.
func (*Model[M, K]) MustCreate ¶
MustCreate a new model in the database. Panic if an error occurs.
func (*Model[M, K]) MustCreateOrUpdate ¶
MustCreateOrUpdate a new model in the database or update it if it already exists. Panic if an error occurs.
func (*Model[M, K]) MustExist ¶
MustExist return true if the model with the primary key (id) exists in the database. Panic if an error occurs.
func (*Model[M, K]) MustUpdate ¶
MustUpdate the model in the database. Panic if an error occurs.
func (*Model[M, K]) OrderBy ¶
func (m *Model[M, K]) OrderBy(field string, sortDirection SortDirection) *Model[M, K]
OrderBy Allows to set the order
func (*Model[M, K]) Restore ¶
Restore restore a soft-deleted model instance. Set deleted_at to null.
func (*Model[M, K]) Save ¶
Save the model to the database. If the model doesn't exist yet, it will be created. Otherwise, it will be updated.
func (*Model[M, K]) Select ¶
Select allows you to specify the fields that you want to retrieve from database. Otherwise, GORM will select all fields by default.
func (*Model[M, K]) Set ¶
Set sets the internal model to the given model, but doesn't save it to the database.
func (*Model[M, K]) Skip ¶
Skip specify the number of records to skip before starting to return the records
model.Skip(5).All() // return 5 rows
func (*Model[M, K]) Take ¶
Take limits the numbers of row returned.
model.Take(5).All() // return 5 rows
func (*Model[M, K]) Where ¶
Where filters the query with a given condition.
Find the first user with name dom
model.Where("name = ?", "dom").First()
func (*Model[M, K]) WhereGroup ¶
func (*Model[M, K]) WithTrashed ¶
WithTrashed returns a new Model with the withTrashed flag set to true.
type ModelInterface ¶
type ModelInterface[K comparable] interface { // GetId returns the model's primary key. GetId() K // SetId sets the model's primary key. SetId(K) // Query returns the base query for the model's table. When withTrashed is // true, soft-deleted rows are included. Query(*gorm.DB, bool) *gorm.DB }
ModelInterface is implemented by every model managed by Model. K is the type of the model's primary key, which must be comparable so that a model can be told apart from its zero value.
type SortDirection ¶
type SortDirection int
SortDirection Enum used to sort the results of a query
const ( ASC SortDirection = iota DESC )