26 lines
922 B
Go
26 lines
922 B
Go
package dbutil
|
|
|
|
import "reflect"
|
|
|
|
// tableRegistry maps a model struct type to its database table name. The query
|
|
// builder (T[M]) resolves table names through it, so the framework never has to
|
|
// import the application's models package. Apps populate it once at startup via
|
|
// Register / RegisterAll — typically from an init() in their models package:
|
|
//
|
|
// func init() { dbutil.RegisterAll(Tables) }
|
|
var tableRegistry = map[reflect.Type]string{}
|
|
|
|
// Register maps a single model type to a table name.
|
|
func Register(t reflect.Type, name string) { tableRegistry[t] = name }
|
|
|
|
// RegisterAll merges a whole type->table map (e.g. an app's models.Tables) into
|
|
// the registry.
|
|
func RegisterAll(m map[reflect.Type]string) {
|
|
for k, v := range m {
|
|
tableRegistry[k] = v
|
|
}
|
|
}
|
|
|
|
// tableNameFor returns the registered table name for t, or "" if none is set.
|
|
func tableNameFor(t reflect.Type) string { return tableRegistry[t] }
|