145 lines
3.3 KiB
Go
145 lines
3.3 KiB
Go
package mongo
|
|
|
|
import (
|
|
"errors"
|
|
|
|
"go.mongodb.org/mongo-driver/mongo"
|
|
"mongo.games.com/goserver/core/logger"
|
|
|
|
"mongo.games.com/game/mongo/internal"
|
|
)
|
|
|
|
type DatabaseType string
|
|
|
|
const (
|
|
KeyGlobal = "global"
|
|
DatabaseUser DatabaseType = "user"
|
|
DatabaseLog DatabaseType = "log"
|
|
)
|
|
|
|
var NotInitError = errors.New("mongo manager is nil, please call Init() first")
|
|
|
|
type Config = internal.Config
|
|
type DatabaseConfig = internal.DatabaseConfig
|
|
type Collection = internal.Collection
|
|
type Database = internal.Database
|
|
|
|
var _manager *internal.Manager
|
|
var _conf *internal.Config
|
|
|
|
// GetConfig 获取配置
|
|
func GetConfig() *Config {
|
|
return _conf
|
|
}
|
|
|
|
// Init 初始化
|
|
func Init(conf *Config) {
|
|
_conf = conf
|
|
_manager = internal.NewManager(_conf)
|
|
}
|
|
|
|
// Restart 重启
|
|
func Restart() {
|
|
if _manager == nil {
|
|
logger.Logger.Error(NotInitError)
|
|
return
|
|
}
|
|
_manager.Restart(_conf)
|
|
}
|
|
|
|
// Close 关闭
|
|
func Close() {
|
|
internal.Close(_manager)
|
|
}
|
|
|
|
// GetDatabase 获取数据库
|
|
// platform: 平台id
|
|
// database: 数据库名称
|
|
func GetDatabase(platform string, database DatabaseType) (*Database, error) {
|
|
if _manager == nil {
|
|
return nil, NotInitError
|
|
}
|
|
|
|
return _manager.GetDatabase(platform, string(database))
|
|
}
|
|
|
|
func GetUserDatabase(platform string) (*Database, error) {
|
|
return GetDatabase(platform, DatabaseUser)
|
|
}
|
|
|
|
func GetLogDatabase(platform string) (*Database, error) {
|
|
return GetDatabase(platform, DatabaseLog)
|
|
}
|
|
|
|
// GetGlobalDatabase 获取全局库
|
|
// database: 数据库名称
|
|
func GetGlobalDatabase(database DatabaseType) (*Database, error) {
|
|
if _manager == nil {
|
|
return nil, NotInitError
|
|
}
|
|
|
|
return _manager.GetDatabase(KeyGlobal, string(database))
|
|
}
|
|
|
|
func GetGlobalUserDatabase() (*Database, error) {
|
|
return GetGlobalDatabase(DatabaseUser)
|
|
}
|
|
|
|
func GetGlobalLogDatabase() (*Database, error) {
|
|
return GetGlobalDatabase(DatabaseLog)
|
|
}
|
|
|
|
// GetGlobalCollection 获取全局库
|
|
// database: 数据库名称
|
|
// collection: 集合名称
|
|
func GetGlobalCollection(database DatabaseType, collection string) (*Collection, error) {
|
|
if _manager == nil {
|
|
return nil, NotInitError
|
|
}
|
|
|
|
return _manager.GetCollection(KeyGlobal, string(database), collection)
|
|
}
|
|
|
|
func GetGlobalUserCollection(collection string) (*Collection, error) {
|
|
return GetGlobalCollection(DatabaseUser, collection)
|
|
}
|
|
|
|
func GetGlobalLogCollection(collection string) (*Collection, error) {
|
|
return GetGlobalCollection(DatabaseLog, collection)
|
|
}
|
|
|
|
// GetCollection 获取平台库
|
|
// platform: 平台id
|
|
// database: 数据库名称
|
|
// collection: 集合名称
|
|
func GetCollection(platform string, database DatabaseType, collection string) (*Collection, error) {
|
|
if _manager == nil {
|
|
return nil, NotInitError
|
|
}
|
|
|
|
return _manager.GetCollection(platform, string(database), collection)
|
|
}
|
|
|
|
func GetUserCollection(platform string, collection string) (*Collection, error) {
|
|
return GetCollection(platform, DatabaseUser, collection)
|
|
}
|
|
|
|
func GetLogCollection(platform string, collection string) (*Collection, error) {
|
|
return GetCollection(platform, DatabaseLog, collection)
|
|
}
|
|
|
|
// GetClient 获取数据库连接
|
|
// 默认获取的是 Global, log 的数据库连接
|
|
func GetClient() (*mongo.Client, error) {
|
|
if _manager == nil {
|
|
return nil, NotInitError
|
|
}
|
|
|
|
c, err := _manager.GetCollection(KeyGlobal, string(DatabaseLog), "empty")
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return c.Database.Client, nil
|
|
}
|