2024-11-01 17:47:33 +08:00
|
|
|
package store
|
2024-10-23 17:46:15 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-11-01 17:47:33 +08:00
|
|
|
"uauth/tool"
|
2024-10-23 17:46:15 +08:00
|
|
|
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
2024-11-01 17:47:33 +08:00
|
|
|
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
|
2024-11-01 17:47:33 +08:00
|
|
|
debug bool
|
2024-10-23 17:46:15 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Client) Type() string {
|
|
|
|
return c.ttype
|
|
|
|
}
|
|
|
|
|
2024-11-01 17:47:33 +08:00
|
|
|
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})
|
|
|
|
|
2024-11-01 17:47:33 +08:00
|
|
|
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()
|
|
|
|
}
|