uauth/pkg/store/client.go

47 lines
585 B
Go
Raw Normal View History

package store
2024-10-23 17:46:15 +08:00
import (
"context"
"uauth/tool"
2024-10-23 17:46:15 +08:00
"gorm.io/gorm"
)
type Store interface {
Session(ctx context.Context) *gorm.DB
}
2024-10-23 17:46:15 +08:00
var (
Default *Client
)
type Client struct {
ctx context.Context
cli *gorm.DB
ttype string
debug bool
2024-10-23 17:46:15 +08:00
}
func (c *Client) Type() string {
return c.ttype
}
func (c *Client) Session(ctx context.Context) *gorm.DB {
if ctx == nil {
2024-10-23 17:46:15 +08:00
ctx = tool.Timeout(30)
}
session := c.cli.Session(&gorm.Session{Context: ctx})
if c.debug {
2024-10-23 17:46:15 +08:00
session = session.Debug()
}
return session
}
func (c *Client) Close() {
d, _ := c.cli.DB()
d.Close()
}