mongo

package
v0.0.0-...-d4a7eef Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 23, 2025 License: Apache-2.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	CreateRecordIfNotExist = options.Update().SetUpsert(true)
)

Functions

func NewGlobalHelper

func NewGlobalHelper(opts ...Option) error

Types

type Auth

type Auth struct {
	Enable   bool   `json:"enable" yaml:"enable"`
	Source   string `json:"source" yaml:"source"`
	Username string `json:"username" yaml:"username"`
	Password string `json:"password" yaml:"password"`
}

type Client

type Client interface {
	Database(string, ...*options.DatabaseOptions) *mongo.Database
	StartSession(...*options.SessionOptions) (mongo.Session, error)
	Disconnect(context.Context) error
}

type CursorClient

type CursorClient interface {
	All(context.Context, any) error
	Next(context.Context) bool
}

type DBClient

type DBClient interface {
	Collection(string, ...*options.CollectionOptions) *mongo.Collection
	ListCollectionNames(context.Context, any, ...*options.ListCollectionsOptions) ([]string, error)
}

type Helper

type Helper struct {
	Client
	Options
}

func GetGlobalHelper

func GetGlobalHelper() *Helper

func NewHelper

func NewHelper(opts ...Option) (*Helper, error)

func (*Helper) Close

func (h *Helper) Close()

func (*Helper) CreateExpirationIndex

func (h *Helper) CreateExpirationIndex(db, coll string, keys bson.D, seconds int32) error

func (*Helper) DeleteAll

func (h *Helper) DeleteAll(db, coll string, filter any) error

func (*Helper) DeleteOne

func (h *Helper) DeleteOne(db, coll string, filter any) error

func (*Helper) Get

func (h *Helper) Get(db, coll string, filter bson.M) (*mongo.SingleResult, error)

func (*Helper) GetAllCollections

func (h *Helper) GetAllCollections(db string) ([]string, error)

func (*Helper) GetCount

func (h *Helper) GetCount(db, coll string, filter bson.M) (int64, error)

func (*Helper) GetQueryCursor

func (h *Helper) GetQueryCursor(db, coll string, query bson.M, opts ...*options.FindOptions) (*mongo.Cursor, error)

func (*Helper) Insert

func (h *Helper) Insert(db, coll string, data any) error

func (*Helper) InsertMany

func (h *Helper) InsertMany(db, coll string, data []any) error

func (*Helper) NewCollCli

func (h *Helper) NewCollCli(db, coll string) (CollClient, error)

func (*Helper) NewDBCli

func (h *Helper) NewDBCli(db string) (DBClient, error)

func (*Helper) NewTxnCli

func (h *Helper) NewTxnCli() (TxnClient, error)

func (*Helper) SetMongoClient

func (h *Helper) SetMongoClient() error

func (*Helper) UpdateMany

func (h *Helper) UpdateMany(db, coll string, filter any, data any) error

func (*Helper) UpdateOne

func (h *Helper) UpdateOne(db, coll string, filter any, data any, opts ...*options.UpdateOptions) error

type Option

type Option func(*Options)

func AuthEnable

func AuthEnable(enable bool) Option

func AuthPassword

func AuthPassword(password string) Option

func AuthSource

func AuthSource(source string) Option

func AuthUsername

func AuthUsername(username string) Option

func Collection

func Collection(collection string) Option

func Collections

func Collections(collections map[string]string) Option

func Connect

func Connect(connect string) Option

func Database

func Database(database string) Option

func Databases

func Databases(databases map[string]string) Option

func Host

func Host(host string) Option

func Port

func Port(port int) Option

func ReplicaSet

func ReplicaSet(replicaSet string) Option

func Uri

func Uri(uri string) Option

type Options

type Options struct {
	Uri        string `json:"uri" yaml:"uri"`
	Host       string `json:"host" yaml:"host"`
	Port       int    `json:"port" yaml:"port"`
	Auth       `json:"auth" yaml:"auth"`
	ReplicaSet string `json:"replicaSet" yaml:"replicaSet"`
	Connect    string `json:"connect" yaml:"connect"`

	Database    string            `json:"database" yaml:"database"`
	Collection  string            `json:"collection" yaml:"collection"`
	Databases   map[string]string `json:"databases" yaml:"databases"`
	Collections map[string]string `json:"collections" yaml:"collections"`
}
var (
	Opts *Options
)

type TxnClient

type TxnClient interface {
	WithTransaction(context.Context, func(mongo.SessionContext) (any, error), ...*options.TransactionOptions) (any, error)
	EndSession(context.Context)
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL