connect, createschema, close

This commit is contained in:
2024-08-19 14:15:44 +02:00
parent 6943eb83f4
commit fd8fdc899c
6 changed files with 93 additions and 0 deletions

View File

@ -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")
}

View File

@ -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))
}