Documentation
¶
Overview ¶
Package gomodel provides a simple and eloquent way to interact with the GORM ORM.
Index ¶
- func Create(db *gorm.DB, m ModelInterface) error
- func CreateOrUpdate(db *gorm.DB, m ModelInterface) error
- func CreateOrUpdateAll(db *gorm.DB, m ModelInterface) error
- func Delete(db *gorm.DB, m ModelInterface, id int64) error
- func Exist(db *gorm.DB, m ModelInterface, id int64, withTrashed bool) (bool, error)
- func Find[t ModelInterface](db *gorm.DB, m t, id int64, relations ...string) (t, error)
- func FindWithTrashed[t ModelInterface](db *gorm.DB, m t, id int64, withTrashed bool, relations ...string) (t, error)
- func MustExist(db *gorm.DB, m ModelInterface, id int64, withTrashed bool) bool
- func Restore(db *gorm.DB, m ModelInterface, id int64) error
- func Update(db *gorm.DB, m ModelInterface) error
- func UpdateAll(db *gorm.DB, m ModelInterface) error
- type Model
- func (m *Model[t]) All() ([]t, error)
- func (m *Model[t]) Count() (int64, error)
- func (m *Model[t]) Create(model t) *Model[t]
- func (m *Model[t]) CreateOrUpdate(model t) *Model[t]
- func (m *Model[t]) CreateOrUpdateAll(model t) *Model[t]
- func (m *Model[t]) Delete() error
- func (m *Model[t]) Exist() (bool, error)
- func (m *Model[t]) Find(id int64) *Model[t]
- func (m *Model[t]) First() *Model[t]
- func (m *Model[t]) Fresh() *Model[t]
- func (m *Model[t]) Get() (t, error)
- func (m *Model[t]) Load(relations ...string) *Model[t]
- func (m *Model[t]) MustCreate(model t) *Model[t]
- func (m *Model[t]) MustCreateOrUpdate(model t) *Model[t]
- func (m *Model[t]) MustExist() bool
- func (m *Model[t]) MustUpdate(model t) *Model[t]
- func (m *Model[t]) OrderBy(field string, sortDirection SortDirection) *Model[t]
- func (m *Model[t]) Query() *gorm.DB
- func (m *Model[t]) Restore() error
- func (m *Model[t]) Save() error
- func (m *Model[t]) Select(query interface{}, params ...interface{}) *Model[t]
- func (m *Model[t]) Set(model t) *Model[t]
- func (m *Model[t]) Skip(offset int) *Model[t]
- func (m *Model[t]) Take(limit int) *Model[t]
- func (m *Model[t]) Update(model t) *Model[t]
- func (m *Model[t]) UpdateAll(model t) *Model[t]
- func (m *Model[t]) Where(query string, params ...interface{}) *Model[t]
- func (m *Model[t]) WhereGroup(query func(q *gorm.DB) *gorm.DB) *Model[t]
- func (m *Model[t]) With(relations ...string) *Model[t]
- func (m *Model[t]) WithTrashed() *Model[t]
- type ModelInterface
- type SortDirection
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CreateOrUpdate ¶
func CreateOrUpdate(db *gorm.DB, m ModelInterface) error
CreateOrUpdate a new model in the database or update it if it already exists.
func CreateOrUpdateAll ¶ added in v1.2.4
func CreateOrUpdateAll(db *gorm.DB, m ModelInterface) error
CreateOrUpdateAll a new model in the database or update it if it already exists.
func Delete ¶
func Delete(db *gorm.DB, m ModelInterface, id int64) error
Delete the model from the database. If ModelInterface has a deleted_at field, it will be set to the current time.
func FindWithTrashed ¶ added in v1.2.5
func FindWithTrashed[t ModelInterface](db *gorm.DB, m t, id int64, withTrashed bool, relations ...string) (t, error)
Find the model for a given id.
func MustExist ¶ added in v1.2.0
MustExist return true if the model with the primary key (id) exists in the database. Panic if an error occurs.
Types ¶
type Model ¶
type Model[t ModelInterface] struct { Base t DB *gorm.DB FilterQuery *gorm.DB Err error // contains filtered or unexported fields }
func New ¶
func New[t ModelInterface](db *gorm.DB, m t) *Model[t]
New instantiate a new Model for the given t type.
func (*Model[t]) CreateOrUpdate ¶
CreateOrUpdate a new model in the database or update it if it already exists.
func (*Model[t]) CreateOrUpdateAll ¶ added in v1.2.4
CreateOrUpdateAll a new model in the database or update it if it already exists.
func (*Model[t]) Exist ¶
Exist return true if the model with the primary key (id) exists in the database.
func (*Model[t]) MustCreate ¶
MustCreate a new model in the database. Panic if an error occurs.
func (*Model[t]) MustCreateOrUpdate ¶
MustCreateOrUpdate a new model in the database or update it if it already exists. Panic if an error occurs.
func (*Model[t]) MustExist ¶ added in v1.2.0
MustExist return true if the model with the primary key (id) exists in the database. Panic if an error occurs.
func (*Model[t]) MustUpdate ¶
MustUpdate the model in the database. Panic if an error occurs.
func (*Model[t]) OrderBy ¶ added in v1.2.7
func (m *Model[t]) OrderBy(field string, sortDirection SortDirection) *Model[t]
OrderBy Allows to set the order
func (*Model[t]) 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[t]) Select ¶ added in v1.2.11
Select allows you to specify the fields that you want to retrieve from database. Otherwise, GORM will select all fields by default.
func (*Model[t]) Set ¶
Set sets the internal model to the given model, but doesn't save it to the database.
func (*Model[t]) Skip ¶ added in v1.2.8
Skip specify the number of records to skip before starting to return the records
model.Skip(5).All() // return 5 rows
func (*Model[t]) Take ¶ added in v1.2.8
Take limits the numbers of row returned.
model.Take(5).All() // return 5 rows
func (*Model[t]) Where ¶ added in v1.2.1
Where filters the query with a given condition.
Find the first user with name dom model.Where("name = ?", "dom").First()
func (*Model[t]) WhereGroup ¶ added in v1.2.10
func (*Model[t]) WithTrashed ¶
WithTrashed returns a new Model with the withTrashed flag set to true.
type ModelInterface ¶
type SortDirection ¶ added in v1.2.7
type SortDirection int
SortDirection Enum used to sort the results of a query
const ( ASC SortDirection = iota DESC )