connect, createschema, close
This commit is contained in:
@ -1 +1,8 @@
|
||||
package service
|
||||
|
||||
type CreateAccountInput struct{}
|
||||
|
||||
// CreateAccount creates an account, returns nil if successful or error if not
|
||||
func (s *Service) CreateAccount(in *CreateAccountInput) error {
|
||||
panic("implement me")
|
||||
}
|
||||
|
@ -1,9 +1,44 @@
|
||||
package service
|
||||
|
||||
import (
|
||||
"code.icod.de/auth/accountserver/configuration"
|
||||
"code.icod.de/auth/accountserver/ent"
|
||||
"context"
|
||||
"database/sql"
|
||||
"entgo.io/ent/dialect"
|
||||
entsql "entgo.io/ent/dialect/sql"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Service struct {
|
||||
client *ent.Client
|
||||
}
|
||||
|
||||
func NewService() *Service {
|
||||
s := new(Service)
|
||||
return s
|
||||
}
|
||||
|
||||
func (s *Service) Connect() {
|
||||
s.client = open(configuration.GetDatabaseConnectionString())
|
||||
}
|
||||
|
||||
func (s *Service) Close() error {
|
||||
return s.client.Close()
|
||||
}
|
||||
|
||||
func (s *Service) CreateSchema() error {
|
||||
return s.client.Schema.Create(context.Background())
|
||||
}
|
||||
|
||||
// open new connection
|
||||
func open(databaseUrl string) *ent.Client {
|
||||
db, err := sql.Open("pgx", databaseUrl)
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
|
||||
// Create an ent.Driver from `db`.
|
||||
drv := entsql.OpenDB(dialect.Postgres, db)
|
||||
return ent.NewClient(ent.Driver(drv))
|
||||
}
|
||||
|
Reference in New Issue
Block a user