gomodel

package module
v1.0.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 22, 2024 License: Apache-2.0 Imports: 1 Imported by: 0

README

GoModel

Installation

go get bitbucket.org/amotus/gomodel@v1.0.0

Usages

Implement the ModelInterface in your model


type MyModel struct {
    Id           int64           `gorm:"primaryKey"`
    DeletedAt    *gorm.DeletedAt `json:"deleted_at"`
}

func (d *MyModel) GetId() int64 {
	return d.Id
}
func (d *MyModel) SetId(id int64) {
	d.Id = id
}

func (d *MyModel) Query(db *gorm.DB, withTrash bool) *gorm.DB {
	if withTrash {
		return db.Unscoped().Model(&Device{})
	}
	return db.Model(&Device{})
}

Use Model[T]

once your struct implement the interface ModelInterface, you can use the New function to get a Model[T] object

modelPointer := gomodel.New[*MyModel](db, &MyModel{})

It is recommended to define an helper function in your model namespace

func NewMyModel(db *gorm.DB) *gomodel.Model[*MyModel] {
    return gomodel.New[*MyModel](db, &MyModel{})
}

Use the Model[T] object

// Create a new model
modelPointer := NewMyModel(db)

// Create a new model with data
modelPointer := NewMyModel(db).Set(&MyModel{
    Id: 1,
})

// Create a new model with data and save it to the database
modelPointer := NewMyModel(db).Set(&MyModel{
    Id: 1,
}).Save()

//Or use directly the Create function
modelPointer, errDb := NewMyModel(db).Create(&MyModel{
    Id: 1,
})

// Get a model from the database
modelStruct, errDb := NewMyModel(db).Find(1)

// Update a model
modelPointer, errDb := NewMyModel(db).Update(&MyModel{
    Name: "updated name",
})

// Create or Update
modelPointer, errDb := NewMyModel(db).CreateOrUpdate(&MyModel{
    Id: 1,
})

// Delete a model
modelPointer := NewMyModel(db).Set(&MyModel{
Id: 1,
})

errDb := modelPointer.Delete()

// Restore a model
errDb := modelPointer.Restore()

// Query model with trash items included
modelPointer := NewMyModel(db).WithTrash()

exist := modelPointer.Set(&MyModel{ Id: 1}).Exist()

// Find methods doesn't use WithTrash, it will return the model only if it isn't in the trash

Look at the model_test.go file for more examples

Documentation

Overview

Package gomodel provides a simple and eloquent way to interact with the GORM ORM.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Create

func Create(db *gorm.DB, m ModelInterface) error

func CreateOrUpdate

func CreateOrUpdate(db *gorm.DB, m ModelInterface) error

func Delete

func Delete(db *gorm.DB, m ModelInterface, id int64) error

func Exist

func Exist(db *gorm.DB, m ModelInterface, id int64, withTrashed bool) bool

func Find

func Find[t ModelInterface](db *gorm.DB, m t, id int64, relations ...string) (t, error)

func Restore

func Restore(db *gorm.DB, m ModelInterface, id int64) error

func Update

func Update(db *gorm.DB, m ModelInterface) error

Types

type Model

type Model[t ModelInterface] struct {
	Base t
	DB   *gorm.DB
	// 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]) All

func (m *Model[t]) All() ([]t, error)

All get all models from the database. returns []t

func (*Model[t]) Create

func (m *Model[t]) Create(model t) (*Model[t], error)

func (*Model[t]) CreateOrUpdate

func (m *Model[t]) CreateOrUpdate(model t) (*Model[t], error)

func (*Model[t]) Delete

func (m *Model[t]) Delete() error

Delete soft delete the model from the database.

func (*Model[t]) Exist

func (m *Model[t]) Exist() bool

Exist return true if the model with the primary key (id) exists in the database.

func (*Model[t]) Find

func (m *Model[t]) Find(id int64) (t, error)

Find the model for a given id.

func (*Model[t]) FindModel

func (m *Model[t]) FindModel(id int64) (*Model[t], error)

FindModel the model for a given id.

func (*Model[t]) Fresh

func (m *Model[t]) Fresh() error

Fresh reload the model from the database.

func (*Model[t]) Get

func (m *Model[t]) Get() (t, error)

Get the model for a given id set on the model.

func (*Model[t]) MustCreate

func (m *Model[t]) MustCreate(model t) *Model[t]

func (*Model[t]) MustCreateOrUpdate

func (m *Model[t]) MustCreateOrUpdate(model t) *Model[t]

func (*Model[t]) MustUpdate

func (m *Model[t]) MustUpdate(model t) *Model[t]

func (*Model[t]) Query

func (m *Model[t]) Query() *gorm.DB

Query return a new query builder for the model's table.

func (*Model[t]) Restore

func (m *Model[t]) Restore() error

Restore restore a soft-deleted model instance. Set deleted_at to null.

func (*Model[t]) Save

func (m *Model[t]) Save() (*Model[t], error)

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

func (m *Model[t]) Set(model t) *Model[t]

Set sets the internal model to the given model, but doesn't save it to the database.

func (*Model[t]) Update

func (m *Model[t]) Update(model t) (*Model[t], error)

func (*Model[t]) With

func (m *Model[t]) With(relations ...string) *Model[t]

With sets the relations to be loaded when querying the database.

func (*Model[t]) WithTrashed

func (m *Model[t]) WithTrashed() *Model[t]

WithTrashed returns a new Model with the withTrashed flag set to true.

type ModelInterface

type ModelInterface interface {
	GetId() int64
	SetId(int64)
	Query(*gorm.DB, bool) *gorm.DB
}

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL