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 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 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
- type Model
- func (m *Model[t]) All() ([]t, error)
- func (m *Model[t]) Create(model t) *Model[t]
- func (m *Model[t]) CreateOrUpdate(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]) 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]) Query() *gorm.DB
- func (m *Model[t]) Restore() error
- func (m *Model[t]) Save() error
- func (m *Model[t]) Set(model t) *Model[t]
- func (m *Model[t]) Update(model t) *Model[t]
- func (m *Model[t]) With(relations ...string) *Model[t]
- func (m *Model[t]) WithTrashed() *Model[t]
- type ModelInterface
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 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 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 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]) 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]) 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]) Set ¶
Set sets the internal model to the given model, but doesn't save it to the database.
func (*Model[t]) WithTrashed ¶
WithTrashed returns a new Model with the withTrashed flag set to true.