I'm trying to find the best way to work with objects represented by multiple database tables.
For database access I wrapped sqlx with my own type and then defined some methods:
type DB struct {
*sqlx.DB
}
func (db *DB) GetSomething(id uint32) *Something {}
func (db *DB) AddSomething(*Something) {}
It works nice for objects that map one-to-one with single database table. I see a couple of ways to work with multi-table objects:
- Define methods on *DB that do selects from multiple tables and then construct an object. Save is similiar.
- Create a layer on top of *DB which would construct an object.
- Do not construct an object, use its parts.
I rather prefer number 2, but need help on how to design it in Go.