mongo

package
v0.0.0-...-5625908 Latest Latest
Warning

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

Go to latest
Published: Feb 25, 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 CollClient

type CollClient interface {
	Find(context.Context, interface{}, ...*options.FindOptions) (*mongo.Cursor, error)
	FindOne(context.Context, interface{}, ...*options.FindOneOptions) *mongo.SingleResult
	FindOneAndUpdate(context.Context, interface{}, interface{}, ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
	FindOneAndDelete(context.Context, interface{}, ...*options.FindOneAndDeleteOptions) *mongo.SingleResult
	InsertOne(context.Context, interface{}, ...*options.InsertOneOptions) (*mongo.InsertOneResult, error)
	InsertMany(context.Context, []interface{}, ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)
	DeleteOne(context.Context, interface{}, ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	DeleteMany(context.Context, interface{}, ...*options.DeleteOptions) (*mongo.DeleteResult, error)
	UpdateOne(context.Context, interface{}, interface{}, ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	UpdateMany(context.Context, interface{}, interface{}, ...*options.UpdateOptions) (*mongo.UpdateResult, error)
	CountDocuments(context.Context, interface{}, ...*options.CountOptions) (int64, error)
}

type CursorClient

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

type DBClient

type DBClient interface {
	Collection(string, ...*options.CollectionOptions) *mongo.Collection
	ListCollectionNames(context.Context, interface{}, ...*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) DeleteAll

func (h *Helper) DeleteAll(db, coll string, filter interface{}) error

func (*Helper) DeleteOne

func (h *Helper) DeleteOne(db, coll string, filter interface{}) 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 interface{}) error

func (*Helper) InsertMany

func (h *Helper) InsertMany(db, coll string, data []interface{}) 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 interface{}, data interface{}) error

func (*Helper) UpdateOne

func (h *Helper) UpdateOne(db, coll string, filter interface{}, data interface{}, 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 ReplicaSet

func ReplicaSet(replicaSet string) Option

func Uri

func Uri(uri string) Option

type Options

type Options struct {
	Uri        string `json:"uri" yaml:"uri"`
	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) (interface{}, error), ...*options.TransactionOptions) (interface{}, error)
	EndSession(context.Context)
}

Jump to

Keyboard shortcuts

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