Add models package with embedded DB interface pattern
- internal/models/model.go: DB interface + Base struct for all models - internal/models/channel.go: Channel model with DB access for relation queries - Database.NewChannel() factory injects db reference into model instances - Uses interface to avoid circular imports (models -> db)
This commit is contained in:
18
internal/models/model.go
Normal file
18
internal/models/model.go
Normal file
@@ -0,0 +1,18 @@
|
||||
package models
|
||||
|
||||
import "database/sql"
|
||||
|
||||
// DB is the interface that models use to query relations.
|
||||
// This avoids a circular import with the db package.
|
||||
type DB interface {
|
||||
GetDB() *sql.DB
|
||||
}
|
||||
|
||||
// Base is embedded in all model structs to provide database access.
|
||||
type Base struct {
|
||||
db DB
|
||||
}
|
||||
|
||||
func (b *Base) SetDB(d DB) {
|
||||
b.db = d
|
||||
}
|
||||
Reference in New Issue
Block a user