Files
workspace/code/go-project/base-farmework/pkg/utils/db/db.go
T
2025-08-14 21:45:18 +08:00

39 lines
743 B
Go

package db
import (
"base-framework/pkg/config"
"base-framework/pkg/reponse"
"base-framework/pkg/router"
"database/sql"
"fmt"
)
// Client 封装数据库连接对象
type Client struct {
Conn *sql.DB
}
// NewClient 根据 context 获取 orgId 并返回 Client 对象
func NewClient(c *router.Context) *Client {
val, ok := c.Get("orgId")
fmt.Println(val)
if !ok {
reponse.Error(c).Code(reponse.CodeInvalidOrgCode).Send()
return nil
}
orgId, ok := val.(string)
if !ok || orgId == "" {
reponse.Error(c).Code(reponse.CodeInvalidOrgCode).Send()
return nil
}
conn, ok := config.GetDB(orgId)
if !ok || conn == nil {
reponse.Error(c).Code(reponse.CodeInvalidOrgCode).Send()
return nil
}
return &Client{Conn: conn}
}