-
-
Notifications
You must be signed in to change notification settings - Fork 26
/
ent.go
46 lines (36 loc) · 831 Bytes
/
ent.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package datastore
import (
"context"
"entgo.io/ent/dialect"
_ "github.com/lib/pq"
"github.com/nekomeowww/insights-bot/ent"
"github.com/nekomeowww/insights-bot/internal/configs"
"go.uber.org/fx"
)
type NewEntParams struct {
fx.In
Lifecycle fx.Lifecycle
Configs *configs.Config
}
type Ent struct {
*ent.Client
}
func NewEnt() func(NewEntParams) (*Ent, error) {
return func(param NewEntParams) (*Ent, error) {
opts := make([]ent.Option, 0)
client, err := ent.Open(dialect.Postgres, param.Configs.DB.ConnectionString, opts...)
if err != nil {
return nil, err
}
err = client.Schema.Create(context.Background())
if err != nil {
return nil, err
}
param.Lifecycle.Append(fx.Hook{
OnStop: func(ctx context.Context) error {
return client.Close()
},
})
return &Ent{client}, nil
}
}