daddy/internal/orm/provider.go
William Petit 05dd505d6b Bascule sur l'ORM GORM
- On n'utilise plus la pattern CQRS trop lourde pour le système
- Un système de models/repository "à la Symfony" est utilisé pour les
  requêtes
2020-07-16 14:30:16 +02:00

50 lines
898 B
Go

package orm
import (
"context"
"github.com/jinzhu/gorm"
"github.com/pkg/errors"
"gitlab.com/wpetit/goweb/service"
// Import postgres dialect
_ "github.com/jinzhu/gorm/dialects/postgres"
)
func ServiceProvider(dialect, dsn string, debug bool) service.Provider {
db, err := gorm.Open(dialect, dsn)
if err != nil {
err = errors.Wrap(err, "could not connect to database")
}
var srv *Service
if err == nil {
db = db.LogMode(debug)
versionResolver := NewDBVersionResolver(db)
ctx := context.Background()
err := versionResolver.Init(ctx)
if err != nil {
err = errors.Wrap(err, "could not initialize version resolver")
}
if err == nil {
srv = &Service{
db: db,
migration: NewMigrationManager(versionResolver),
}
}
}
return func(ctn *service.Container) (interface{}, error) {
if err != nil {
return nil, err
}
return srv, nil
}
}