2020-07-13 09:20:14 +02:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/jackc/pgx/v4/pgxpool"
|
|
|
|
"github.com/pkg/errors"
|
|
|
|
"gitlab.com/wpetit/goweb/service"
|
|
|
|
)
|
|
|
|
|
|
|
|
const ServiceName service.Name = "database"
|
|
|
|
|
2020-07-13 14:44:05 +02:00
|
|
|
// From retrieves the database pool service in the given container.
|
2020-07-13 09:20:14 +02:00
|
|
|
func From(container *service.Container) (*pgxpool.Pool, error) {
|
|
|
|
service, err := container.Service(ServiceName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.Wrapf(err, "error while retrieving '%s' service", ServiceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
srv, ok := service.(*pgxpool.Pool)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.Errorf("retrieved service is not a valid '%s' service", ServiceName)
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
2020-07-13 14:44:05 +02:00
|
|
|
// Must retrieves the database pool service in the given container or panic otherwise.
|
2020-07-13 09:20:14 +02:00
|
|
|
func Must(container *service.Container) *pgxpool.Pool {
|
|
|
|
srv, err := From(container)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return srv
|
|
|
|
}
|