mocom

package
v2.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 20, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AddIndex

func AddIndex(ctx context.Context, collName string, index mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error)

AddIndex adds an index to collection

func AddIndexT

func AddIndexT[T Modeler](ctx context.Context, index mongo.IndexModel, opts ...*options.CreateIndexesOptions) (string, error)

AddIndex adds an index to collection model should be implement `CollName() string`

func AddIndexes

func AddIndexes(ctx context.Context, collName string, indexes []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error)

AddIndexes adds indexes to collection

func AddIndexesT

func AddIndexesT[T Modeler](ctx context.Context, indexes []mongo.IndexModel, opts ...*options.CreateIndexesOptions) ([]string, error)

AddIndexes adds indexes to collection model should be implement `CollName() string`

func AddOrUpdate

func AddOrUpdate[T Modeler](ctx context.Context, filter interface{}, model T) (*mongo.UpdateResult, error)

AddOrUpdate adds or updates one document from collection model should be implement `CollName() string`

func Aggregate

func Aggregate(ctx context.Context, collName string, req *AggregationRequest) (res []bson.M, err error)

Aggregate aggregates documents based on Pipeline and Options from request

func AggregateT

func AggregateT[T Modeler](ctx context.Context, req *AggregationRequest) (res []bson.M, err error)

Aggregate aggregates documents based on Pipeline and Options from request model should be implement `CollName() string`

func CollRead

func CollRead(collName string) *mongo.Collection

func CollWrite

func CollWrite(collName string) *mongo.Collection

func Connect

func Connect(ctx context.Context, uri string, dbName string) error

Connect mongodb://localhost:27017/?w=majority&retryWrites=false

func Count

func Count(ctx context.Context, collName string, filter interface{}, opts ...*options.CountOptions) (int64, error)

Count counts documents from collection

func CountT

func CountT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error)

CountT counts documents from collection model should be implement `CollName() string`

func Create

func Create(ctx context.Context, collName string, model any, opts ...*options.InsertOneOptions) (interface{}, error)

func CreateCollection

func CreateCollection[T Modeler](ctx context.Context, opts ...*options.CreateCollectionOptions) error

func CreateMany

func CreateMany(ctx context.Context, collName string, models []any, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func CreateManyT

func CreateManyT[T Modeler](ctx context.Context, models []T, opts ...*options.InsertManyOptions) (*mongo.InsertManyResult, error)

func CreateT

func CreateT[T Modeler](ctx context.Context, model T, opts ...*options.InsertOneOptions) (interface{}, error)

func CreateWithID

func CreateWithID[T IDModeler](ctx context.Context, model T, opts ...*options.InsertOneOptions) error

func Delete

func Delete(ctx context.Context, collName string, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

Delete deletes many documents from collection

func DeleteOne

func DeleteOne(ctx context.Context, collName string, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteOne deletes one document from collection

func DeleteOneT

func DeleteOneT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteOne deletes one document from collection

func DeleteT

func DeleteT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

Delete deletes many documents from collection

func EstimatedCount

func EstimatedCount(ctx context.Context, collName string, opts ...*options.EstimatedDocumentCountOptions) (int64, error)

EstimatedCount counts documents from collection but it is not accurate, still faster than Count

func EstimatedCountT

func EstimatedCountT[T Modeler](ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)

EstimatedCountT counts documents from collection but it is not accurate, still faster than Count model should be implement `CollName() string`

func FindOne

func FindOne[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (res *T, err error)

FindOne finds one document from model's collection model should be implement `CollName() string`

func FindT

func FindT[T Modeler](ctx context.Context, filter interface{}, opts ...*options.FindOptions) (res []T, err error)

Find finds documents from from model's collection model should be implement `CollName() string`

func Flush

func Flush[T Modeler](ctx context.Context) (int64, error)

Flush clears all records of collection and return number of deleted records

func GetClient

func GetClient() *mongo.Client

func ReplaceOne

func ReplaceOne(ctx context.Context, collName string, filter interface{}, model any, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)

ReplaceOne replaces one document from collection upsert = true if you want to upsert or just use mocom.AddOrUpdate

func ReplaceOneT

func ReplaceOneT[T Modeler](ctx context.Context, filter interface{}, model T, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)

ReplaceOne replaces one document from collection upsert = true if you want to upsert, or just use mocom.AddOrUpdate model should be implement `CollName() string`

func SetDatabase

func SetDatabase(database *mongo.Database)

func Tx

func Tx(ctx context.Context, cfg TransactionConfig) (interface{}, error)

Tx executes a MongoDB transaction based on provided configuration (cfg). ctx is the context for the transaction, while cfg includes session configuration, transaction options, and the transaction function. Returns the result of transaction function execution or an error if the client is nil or any issues occurred during the session creation or transaction. If a nested transaction is detected, then this transaction will be executed with the passed-in context.

func TxOptimal

func TxOptimal(ctx context.Context, f func(ctx mongo.SessionContext) (interface{}, error)) (interface{}, error)

TxOptimal will do the transaction with majority write-concern snapshot read-concern, primary read preference

This should be used when transaction does not contain any read If a nested transaction is detected, then this transaction will be executed with the passed-in context.

func UpdateAndReturn

func UpdateAndReturn[T Modeler](ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) (*T, error)

UpdateAndReturn updates one document from collection and return the updated document Pass options to return document AFTER or BEFORE the update

func UpdateMany

func UpdateMany(ctx context.Context, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func UpdateManyT

func UpdateManyT[T Modeler](ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateMany updates many documents from collection model should be implement `CollName() string`

func UpdateOne

func UpdateOne(ctx context.Context, collName string, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

func UpdateOneT

func UpdateOneT[T Modeler](ctx context.Context, filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateOne updates one document from collection

Types

type AggregationRequest

type AggregationRequest struct {
	Pipeline []moper.D
	Options  []*options.AggregateOptions
}

-------- AGGREGATION --------

type Database

type Database struct {
	*mongo.Database
}

type ID

type ID = IDT[any]

type IDModeler

type IDModeler interface {
	Modeler
	IDer
}

type IDT

type IDT[T any] struct {
	ID T `json:"id" bson:"_id,omitempty"`
}

func (*IDT[T]) SetID

func (id *IDT[T]) SetID(t any)

type IDer

type IDer interface {
	SetID(t any)
}

-------- MODEL + ID --------

type IntID

type IntID = IDT[int]

type Modeler

type Modeler interface {
	CollName() string
}

-------- MODEL --------

type ObjectID

type ObjectID = IDT[primitive.ObjectID]

type SessionKey

type SessionKey struct{}

type StringID

type StringID = IDT[string]

type TransactionConfig

type TransactionConfig struct {
	Options    *options.TransactionOptions
	SessConfig *options.SessionOptions
	Func       func(ctx mongo.SessionContext) (interface{}, error)
}

-------- TRANSACTION -------- TransactionConfig, you only need to define the func; the transaction options are not necessary to be defined for simple use cases

Jump to

Keyboard shortcuts

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