I'm implementing a database API. I have models. I need to implement CRUD actions to each model. For now I create an individual GetAllModels
function and Get
method for each model. How could I do that once for all models and just pass around some variables if needed?
The pattern below I use for each model:
type City struct {
Attr1 string
Attr2 string
}
type Country struct {
Attr1 string
Attr2 string
}
func GetAllCities(db *sqlx.DB) ([]*City, error) {
items := []*City{}
err := db.Select(&items, "SELECT * FROM cities")
// check err
return items, nil
}
func (m *City) Get(db *sqlx.DB, id string) error {
if err := db.Get(m, "SELECT FROM cities WHERE id = ?", id); err != nil {
return err
}
return nil
}
func GetAllCountries(db *sqlx.DB) ([]*Country, error) {
items := []*Country{}
err := db.Select(&items, "SELECT * FROM countries")
// check err
return items, nil
}
func (m *Country) Get(db *sqlx.DB, id string) error {
if err := db.Get(m, "SELECT FROM countries WHERE id = ?", id); err != nil {
return err
}
return nil
}
But what actually changes from model to model is a query string and a type of slice objects.
How to make one universal GetAll
function and Get
method for all future models?