mongox

package module
v0.0.0-...-8e33440 Latest Latest
Warning

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

Go to latest
Published: Sep 21, 2022 License: Apache-2.0 Imports: 13 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ProtoCodecFunc = func(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
	if !val.IsValid() || val.Type() != tsProtoType {
		return bsoncodec.ValueEncoderError{Name: "ObjectIDEncodeValue", Types: []reflect.Type{tsProtoType}, Received: val}
	}
	s := val.Interface().(*timestamppb.Timestamp)

	return vw.WriteDateTime(s.Seconds * 1000)
}

ProtoCodecFunc is the Proto Encoding Function

View Source
var ProtoDeCodecFunc = func(_ bsoncodec.DecodeContext, vr bsonrw.ValueReader, val reflect.Value) error {

	read, err := vr.ReadDateTime()
	if err != nil {
		return err
	}

	val.Interface().(*timestamppb.Timestamp).Seconds = read / 1000
	return nil
}

ProtoDeCodecFunc is the Proto Dencoding Function

View Source
var StrCodecFunc = func(_ bsoncodec.EncodeContext, vw bsonrw.ValueWriter, val reflect.Value) error {
	if !val.IsValid() || val.Type() != tsStrType {
		return bsoncodec.ValueEncoderError{Name: "ObjectIDEncodeValue", Types: []reflect.Type{tsProtoType}, Received: val}
	}
	s := val.Interface().(string)
	t, err := time.Parse(time.RFC3339Nano, s)
	if err != nil {
		return vw.WriteString(s)
	}
	return vw.WriteDateTime(t.Unix() * 1000)
}

StrCodecFunc is the String Encoding Function

Functions

func BuildBulkOperation

func BuildBulkOperation(filter interface{}, update interface{}, data interface{}) *mongo.UpdateOneModel

BuildBulkOperation create a single bulk operation

func CodecRegistry

func CodecRegistry(options *Options) *bsoncodec.RegistryBuilder

CodecRegistry create a new bsoncode

func GetLogLevel

func GetLogLevel(data interface{}) int

GetLogLevel returns log level from data value

Types

type Client

type Client struct {
	*mgo.Client
	Database   string
	Collection string
}

Client is the internal Mongo Client

func (*Client) GetCollection

func (c *Client) GetCollection() CollectionAPI

GetCollection return a CollectionAPI

func (*Client) SetCollection

func (c *Client) SetCollection(collection string)

SetCollection set collection name for the internal client

func (*Client) SetCursor

func (c *Client) SetCursor(cursor *mongo.Cursor) CursorAPI

SetCursor return a CursorAPI

func (*Client) SetSingleResult

func (c *Client) SetSingleResult(singleresult *mongo.SingleResult) SingleResultAPI

SetSingleResult return a SingleResultAPI

type ClientAPI

type ClientAPI interface {
	SetCollection(collection string)
	GetCollection() CollectionAPI
	SetCursor(*mongo.Cursor) CursorAPI
	SetSingleResult(*mongo.SingleResult) SingleResultAPI
	Connect(context.Context) error
	Ping(context.Context, *readpref.ReadPref) error
}

ClientAPI is the internal Client API interface

func GetClient

func GetClient(ctx context.Context, m *MongoDB) (ClientAPI, error)

GetClient return the mongo client recorded or create a new instance

type ClientCollection

type ClientCollection struct {
	*mgo.Collection
}

ClientCollection is the internal Mongo Collection

func (*ClientCollection) GetIndex

func (c *ClientCollection) GetIndex() IndexAPI

GetIndex return an IndexAPI from collection indexes

type ClientCursor

type ClientCursor struct {
	*mgo.Cursor
}

ClientCursor is the internal Mongo Cursor

type ClientDatabase

type ClientDatabase struct {
	*mgo.Database
}

ClientDatabase is the internal Mongo Database

type ClientIndex

type ClientIndex struct {
	mgo.IndexView
}

ClientIndex is the internal Client Index

type ClientSingleResult

type ClientSingleResult struct {
	*mgo.SingleResult
}

ClientSingleResult is the internal Single Result

type CollectionAPI

type CollectionAPI interface {
	GetIndex() IndexAPI
	InsertOne(context.Context, interface{}, ...*mgoOtions.InsertOneOptions) (*mongo.InsertOneResult, error)
	BulkWrite(context.Context, []mongo.WriteModel, ...*mgoOtions.BulkWriteOptions) (*mongo.BulkWriteResult, error)
	Find(context.Context, interface{}, ...*options.FindOptions) (*mongo.Cursor, 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)
	ReplaceOne(context.Context, interface{}, interface{}, ...*options.ReplaceOptions) (*mongo.UpdateResult, error)
	FindOneAndUpdate(context.Context, interface{}, interface{}, ...*options.FindOneAndUpdateOptions) *mongo.SingleResult
	Aggregate(context.Context, interface{}, ...*options.AggregateOptions) (*mongo.Cursor, error)
}

CollectionAPI is the internal Collection API interface

type CursorAPI

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

CursorAPI is the internal Cursor API interface

type IndexAPI

type IndexAPI interface {
	ListSpecifications(ctx context.Context, opts ...*options.ListIndexesOptions) ([]*mongo.IndexSpecification, error)
	DropOne(ctx context.Context, name string, opts ...*options.DropIndexesOptions) (bson.Raw, error)
	CreateOne(context.Context, mongo.IndexModel, ...*options.CreateIndexesOptions) (string, error)
	CreateMany(context.Context, []mongo.IndexModel, ...*options.CreateIndexesOptions) ([]string, error)
}

IndexAPI is the internal Index API interface

type MockClient

type MockClient struct {
	ClientAPI
	ErrConnect              error
	ErrPing                 error
	ErrInsertOne            error
	ErrBulkWrite            error
	InsertOneResult         mongo.InsertOneResult
	BulkWriteResult         mongo.BulkWriteResult
	ErrFind                 error
	ErrDeleteOne            error
	ErrDeleteMany           error
	ErrorCursorAll          error
	ErrUpdateOne            error
	ErrReplaceOne           error
	UpdateOneResult         mongo.UpdateResult
	ReplaceOneResult        mongo.UpdateResult
	FindOneAndUpdateResult  mongo.SingleResult
	ErrorSingleResultDecode error
	AggregateResult         mongo.Cursor
	ErrAggregate            error
	CreateIndexResult       string
	CreateManyIndexResult   []string
	ErrCreateIndex          error
	ErrCreateManyIndex      error
	ListSpecificationResult []*mongo.IndexSpecification
	ErrListSpecifications   error
	DropOneResult           bson.Raw
	ErrDropOne              error
}

MockClient is the internal mock client

func (*MockClient) Connect

func (p *MockClient) Connect(ctx context.Context) error

Connect is an internal mock method

func (*MockClient) GetCollection

func (p *MockClient) GetCollection() CollectionAPI

GetCollection is an internal mock method

func (*MockClient) Ping

func (p *MockClient) Ping(ctx context.Context, rp *readpref.ReadPref) error

Ping is an internal mock method

func (*MockClient) SetCollection

func (p *MockClient) SetCollection(collection string)

SetCollection is an internal mock method

func (*MockClient) SetCursor

func (p *MockClient) SetCursor(*mongo.Cursor) CursorAPI

SetCursor is an internal mock method

func (*MockClient) SetSingleResult

func (p *MockClient) SetSingleResult(*mongo.SingleResult) SingleResultAPI

SetSingleResult is an internal mock method

type MockCollection

type MockCollection struct {
	CollectionAPI
	ErrInsertOne            error
	ErrBulkWrite            error
	ErrFind                 error
	ErrDeleteOne            error
	ErrDeleteMany           error
	InsertOneResult         mongo.InsertOneResult
	BulkWriteResult         mongo.BulkWriteResult
	DeleteResult            mongo.DeleteResult
	ErrUpdateOne            error
	ErrReplaceOne           error
	UpdateOneResult         mongo.UpdateResult
	ReplaceOneResult        mongo.UpdateResult
	FindOneAndUpdateResult  mongo.SingleResult
	AggregateResult         mongo.Cursor
	ErrAggregate            error
	CreateIndexResult       string
	CreateManyIndexResult   []string
	ErrCreateIndex          error
	ErrCreateManyIndex      error
	ListSpecificationResult []*mongo.IndexSpecification
	ErrListSpecifications   error
	DropOneResult           bson.Raw
	ErrDropOne              error
}

MockCollection is the internal mock collection

func (*MockCollection) Aggregate

func (c *MockCollection) Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) (*mongo.Cursor, error)

Aggregate is an internal mock method

func (*MockCollection) BulkWrite

BulkWrite is an internal mock method

func (*MockCollection) DeleteMany

func (c *MockCollection) DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)

DeleteMany is an internal mock method

func (*MockCollection) DeleteOne

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

DeleteOne is an internal mock method

func (*MockCollection) Find

func (c *MockCollection) Find(ctx context.Context, filter interface{}, opts ...*mgoOtions.FindOptions) (*mongo.Cursor, error)

Find is an internal mock method

func (*MockCollection) FindOneAndUpdate

func (c *MockCollection) FindOneAndUpdate(ctx context.Context, filter interface{}, update interface{}, opts ...*options.FindOneAndUpdateOptions) *mongo.SingleResult

FindOneAndUpdate is an internal mock method

func (*MockCollection) GetIndex

func (c *MockCollection) GetIndex() IndexAPI

GetIndex is an internal mock method

func (*MockCollection) InsertOne

func (c *MockCollection) InsertOne(ctx context.Context, document interface{}, opts ...*mgoOtions.InsertOneOptions) (*mongo.InsertOneResult, error)

InsertOne is an internal mock method

func (*MockCollection) ReplaceOne

func (c *MockCollection) ReplaceOne(ctx context.Context, filter interface{}, replacement interface{}, opts ...*options.ReplaceOptions) (*mongo.UpdateResult, error)

ReplaceOne is an internal mock method

func (*MockCollection) UpdateOne

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

UpdateOne is an internal mock method

type MockCursor

type MockCursor struct {
	CursorAPI
	ErrorCursorAll error
}

MockCursor is the internal mock cursor

func (*MockCursor) All

func (c *MockCursor) All(ctx context.Context, results interface{}) error

All is an internal mock method

func (*MockCursor) Decode

func (c *MockCursor) Decode(v interface{}) error

Decode is an internal mock method

func (*MockCursor) Next

func (c *MockCursor) Next(ctx context.Context) bool

Next is an internal mock method

type MockIndex

type MockIndex struct {
	CursorAPI
	CreateIndexResult       string
	CreateManyIndexResult   []string
	ListSpecificationResult []*mongo.IndexSpecification
	DropOneResult           bson.Raw
	ErrCreateIndex          error
	ErrCreateManyIndex      error
	ErrListSpecifications   error
	ErrDropOne              error
}

MockIndex is the internal mock index

func (*MockIndex) CreateMany

func (i *MockIndex) CreateMany(_ context.Context, _ []mongo.IndexModel, _ ...*options.CreateIndexesOptions) ([]string, error)

CreateMany is an internal mock method

func (*MockIndex) CreateOne

CreateOne is an internal mock method

func (*MockIndex) DropOne

func (i *MockIndex) DropOne(_ context.Context, _ string, _ ...*options.DropIndexesOptions) (bson.Raw, error)

DropOne is an internal mock method

func (*MockIndex) ListSpecifications

func (i *MockIndex) ListSpecifications(_ context.Context, _ ...*options.ListIndexesOptions) ([]*mongo.IndexSpecification, error)

ListSpecifications is an internal mock method

type MockSingleResult

type MockSingleResult struct {
	SingleResultAPI
	ErrorSingleResultDecode error
}

MockSingleResult is the internal mock single result

func (*MockSingleResult) Decode

func (s *MockSingleResult) Decode(v interface{}) error

Decode is an internal mock method

type MockbsonReader

type MockbsonReader struct {
	bsonrw.ValueReader
	ErrReadDateTime error
}

MockbsonReader structure used by codec

func (*MockbsonReader) ReadDateTime

func (b *MockbsonReader) ReadDateTime() (int64, error)

ReadDateTime API

type MockbsonWriter

type MockbsonWriter struct {
	bsonrw.ValueWriter
}

MockbsonWriter structure used by codec

func (*MockbsonWriter) WriteDateTime

func (b *MockbsonWriter) WriteDateTime(dt int64) error

WriteDateTime API

func (*MockbsonWriter) WriteString

func (b *MockbsonWriter) WriteString(string) error

WriteDateTime API

type Mongo

type Mongo struct {
	// Username for database authentication
	Username string `mapstructure:"username"`
	// Password for database authentication
	Password string `mapstructure:"password"`
	// Name of the database
	Name string `mapstructure:"name"`
	// URL of the database
	URL string `mapstructure:"url"`
	// Name of the authorisation database
	AuthSource string `mapstructure:"authSource"`
}

Mongo input structure

func (*Mongo) New

func (cfg *Mongo) New(opts ...Option) MongoAPI

New return a mongo instance

type MongoAPI

type MongoAPI interface {
	SetCollection(string) MongoAPI
	SetOptions(...Option) MongoAPI
	Connect() error
	Get(interface{}, interface{}, ...*options.FindOptions) error
	Insert(interface{}) error
	InsertBulk([]*mongo.UpdateOneModel) error
	Delete(interface{}) error
	DeleteAll() error
	Update(interface{}, interface{}) error
	Upsert(interface{}, interface{}) error
	FindAndUpdate(interface{}, interface{}, interface{}) error
	Aggregate(mongo.Pipeline, interface{}) error
	CreateIndexes(mongo.IndexModel) error
	CreateManyIndexes([]mongo.IndexModel) error
	ListIndexes() ([]string, error)
	DropIndex(string) error
	Incr(string) (int64, error)
}

MongoAPI is the public Mongo API interface

type MongoDB

type MongoDB struct {
	ClientAPI

	// From user selection
	Collection string
	// contains filtered or unexported fields
}

MongoDB is the public Mongo Instance

func (*MongoDB) Aggregate

func (m *MongoDB) Aggregate(pipeline mongo.Pipeline, data interface{}) error

Aggregate operator over mongo client

func (*MongoDB) Connect

func (m *MongoDB) Connect() error

Connect public client to the internal client

func (*MongoDB) CreateIndexes

func (m *MongoDB) CreateIndexes(opt mongo.IndexModel) error

CreateIndexes create index based on input mongo index model

func (*MongoDB) CreateManyIndexes

func (m *MongoDB) CreateManyIndexes(opt []mongo.IndexModel) error

CreateManyIndexes create indexes based on input mongo index model array

func (*MongoDB) Delete

func (m *MongoDB) Delete(filter interface{}) error

Delete remove documents from collection based on input filter setting

func (*MongoDB) DeleteAll

func (m *MongoDB) DeleteAll() error

DeleteAll remove all documents inside collection

func (*MongoDB) DropIndex

func (m *MongoDB) DropIndex(index string) error

DropIndex remove index

func (*MongoDB) FindAndUpdate

func (m *MongoDB) FindAndUpdate(filter interface{}, update interface{}, data interface{}) error

FindAndUpdate single document from a collection based on filter input

func (*MongoDB) Get

func (m *MongoDB) Get(filter interface{}, data interface{}, findOptions ...*options.FindOptions) error

Get return documents from collection based on input filter

func (*MongoDB) Incr

func (m *MongoDB) Incr(key string) (int64, error)

Incr behaves a counter based on a key string value

func (*MongoDB) Insert

func (m *MongoDB) Insert(value interface{}) error

Insert single document value inside collection

func (*MongoDB) InsertBulk

func (m *MongoDB) InsertBulk(operations []*mongo.UpdateOneModel) error

InsertBulk multiple documents value inside collection

func (*MongoDB) ListIndexes

func (m *MongoDB) ListIndexes() ([]string, error)

ListIndexes list indexes

func (*MongoDB) SetCollection

func (m *MongoDB) SetCollection(collection string) MongoAPI

SetCollection set collection name for the public client

func (*MongoDB) SetOptions

func (m *MongoDB) SetOptions(opts ...Option) MongoAPI

SetOptions set options for the public client

func (*MongoDB) Update

func (m *MongoDB) Update(filter interface{}, update interface{}) error

Update single document from a collection based on filter input

func (*MongoDB) Upsert

func (m *MongoDB) Upsert(filter interface{}, update interface{}) error

Upsert single document from a collection based on filter input

type Option

type Option func(*Options)

Option ...

func Timeout

func Timeout(t time.Duration) Option

Timeout option

func WithProtoCodec

func WithProtoCodec() Option

WithProtoCodec option

func WithStrCodec

func WithStrCodec() Option

WithStrCodec option

type Options

type Options struct {
	ProtoCodec bool
	StrCodec   bool
	Timeout    time.Duration
}

Options ...

func NewOptions

func NewOptions(opts ...Option) Options

NewOptions ...

type SingleResultAPI

type SingleResultAPI interface {
	Decode(interface{}) error
}

SingleResultAPI is the internal Single Result API interface

Jump to

Keyboard shortcuts

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