-
Notifications
You must be signed in to change notification settings - Fork 0
Gorm Repository
samithiwat edited this page Feb 27, 2023
·
4 revisions
Gorm repository is the instance that has the code set for simple use case of gorm and can be extends by insert the scope
repo := gosdk.NewGormRepository(*GormDB)
name | description |
---|---|
Gorm DB | the client of the gorm |
name | description | example |
---|---|---|
repo | the gorm repository instance |
type Entity interface {
TableName() string
}
name | description |
---|---|
TableName() | name of the table of the entity |
the gorm scope for pagination query
if err := r.db.GetDB().
Scopes(gosdk.Pagination[Entity](entityList, metadata, gormDB, ...Scope)).
Find(&entityList).
Error; err != nil {
return err
}
metadata.ItemCount = len(*entityList)
metadata.CalItemPerPage()
return nil
name | description | example |
---|---|---|
entityList | list of entities | |
metadata | pagination metadata | |
gormDB | gorm client | |
Scope | extends scope (optional) |
Example Basic
if err := r.db.GetDB().
Scopes(gosdk.Pagination[Entity](entity, metadata, r.db.GetDB())).
Find(&entity).
Error; err != nil {
return err
}
metadata.ItemCount = len(*entity)
metadata.CalItemPerPage()
return nil
Example with Scope
if err := r.db.GetDB().
Preload("Relationship")
Scopes(gosdk.Pagination[Entity](entity, metadata, r.db.GetDB(), func(db *gorm.DB) *gorm.DB{
return db.Where("something = ?", something)
})).
Find(&entity, "something = ?", something).
Error; err != nil {
return err
}
metadata.ItemCount = len(*entity)
metadata.CalItemPerPage()
return nil
return the gorm db instance
db = gorm.GetDB()
name | description | example |
---|---|---|
gormDB | gorm client |
findAll with pagination
var entityList []Entity
if err := repo.FindOne(metadata, &entityList, ...scope); err != nil{
// handle error
}
name | description | example |
---|---|---|
entityList | list of entities | |
metadata | pagination metadata | |
Scope | extends scope (optional) |
findOne entity
entity := Entity{}
if err := repo.FindOne(id, &entity, ...scope); err != nil{
// handle error
}
name | description | example |
---|---|---|
id | id of entity | |
entity | empty entity for receive data | |
Scope | extends scope (optional) |
Example with Scope
if err := repo.FindOne(id, &entity, func(db *gorm.DB)*gorm.DB{
return db.Preload("Relationship").Where("something = ?")
}).
Error; err != nil {
return err
}
create entity
entity := Entity{}
if err := repo.Create(&entity, ...scope); err != nil{
// handle error
}
name | description | example |
---|---|---|
entity | entity with data | |
Scope | extends scope (optional) |
update entity
entity := Entity{}
if err := repo.Update(id, &entity, ...scope); err != nil{
// handle error
}
name | description | example |
---|---|---|
id | id of entity | |
entity | entity with data | |
Scope | extends scope (optional) |
delete entity
entity := Entity{}
if err := repo.Delete(id, &entity, ...scope); err != nil{
// handle error
}
name | description | example |
---|---|---|
id | id of entity | |
entity | entity with data | |
Scope | extends scope (optional) |