database

package
v0.0.0-...-6524a5b Latest Latest
Warning

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

Go to latest
Published: Oct 13, 2022 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BaseDoc

type BaseDoc struct {
	Id           primitive.ObjectID `json:"_id" bson:"_id"`
	Status       DocStatus          `json:"status"`
	CreatedDate  *time.Time         `json:"createdDate,omitempty" bson:"createdDate,omitempty"`
	ModifiedDate *time.Time         `json:"modifiedDate,omitempty" bson:"modifiedDate,omitempty"`
}

Every repository's root struct (DeviceRepository => Device, EntityRepository => Entity) has the BaseDoc embedded into it which provides the standard document fields, as well as every root struct to implement the Document interface.

func (*BaseDoc) GetBaseDoc

func (b *BaseDoc) GetBaseDoc() *BaseDoc

func (*BaseDoc) InitializeBaseDoc

func (b *BaseDoc) InitializeBaseDoc()

func (*BaseDoc) RemoveObjectId

func (b *BaseDoc) RemoveObjectId()

BASEDOC: Methods for BaseDoc to comply with the Document interface

func (*BaseDoc) UpdateModifiedDate

func (b *BaseDoc) UpdateModifiedDate()

type BaseRepository

type BaseRepository struct {
	Client         *mongo.Client
	Database       *mongo.Database
	CollectionName string
}

The BaseRepository is embedded into every other repository struct and contains the shared MongoDB client (allows for sharing a MongoDB connection instance between multiple repositories) as well as the repositories collection name and the client.Database.

func NewBaseRepository

func NewBaseRepository(client *mongo.Client, databaseName string, collectionName string) (*BaseRepository, error)

Called by every other NewRepository function to grab the MongoDB client and set up a repository struct with it.

func (*BaseRepository) Aggregate

func (r *BaseRepository) Aggregate(ctx context.Context, pipeline interface{}, allowDiskUse bool) (*mongo.Cursor, error)

func (*BaseRepository) Create

func (r *BaseRepository) Create(ctx context.Context, doc Document, allowDiskUse bool) (*mongo.InsertOneResult, error)

func (*BaseRepository) CreateMany

func (r *BaseRepository) CreateMany(ctx context.Context, docs []Document, allowDiskUse bool) (*mongo.InsertManyResult, error)

func (*BaseRepository) DeleteById

func (r *BaseRepository) DeleteById(ctx context.Context, id string) (*mongo.DeleteResult, error)

Deletes a document with a corresponding _id

func (*BaseRepository) Disconnect

func (r *BaseRepository) Disconnect(ctx context.Context)

used mainly in testing to avoid capping out the concurrent connections on MongoDB

func (*BaseRepository) Find

func (r *BaseRepository) Find(ctx context.Context, query interface{}, rtn interface{}, opts ...*options.FindOptions) error

returns all documents that match the provided query

func (*BaseRepository) FindById

func (r *BaseRepository) FindById(ctx context.Context, id string, rtn interface{}) error

returns the first document that has an _id that matches the id parameter

func (*BaseRepository) FindByIdList

func (r *BaseRepository) FindByIdList(ctx context.Context, ids []string, rtn interface{}) error

returns all documents where the _id is matches an id specified in the ids []string parameter

func (*BaseRepository) FindCursor

func (r *BaseRepository) FindCursor(ctx context.Context, query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)

returns all documents that match the provided query and return the cursor

func (*BaseRepository) FindDistinct

func (r *BaseRepository) FindDistinct(ctx context.Context, fieldName string, query interface{}) ([]interface{}, error)

func (*BaseRepository) FindOne

func (r *BaseRepository) FindOne(ctx context.Context, query interface{}, rtn interface{}) error

returns the first document that matches the provided query

func (*BaseRepository) QuickDeleteById

func (r *BaseRepository) QuickDeleteById(ctx context.Context, id string)

Works similar to DeleteById but doesn't return an op result or an error

func (*BaseRepository) Update

func (r *BaseRepository) Update(ctx context.Context, query interface{}, update interface{}, multi bool) (*mongo.UpdateResult, error)

func (*BaseRepository) UpdateBase

func (r *BaseRepository) UpdateBase(ctx context.Context, params MongoUpdateParams) (*mongo.UpdateResult, error)

DEPRECATED: We may not need this anymore, will implement in the future if otherwise

func (*BaseRepository) UpdateById

func (r *BaseRepository) UpdateById(ctx context.Context, id string, update interface{}, autoSet bool, status []DocStatus) (*mongo.UpdateResult, error)

Updates a single document whose _id matches what is provided in the `id` parameter

func (*BaseRepository) UpdateByIdList

func (r *BaseRepository) UpdateByIdList(ctx context.Context, ids []string, update interface{}, status []DocStatus) (*mongo.UpdateResult, error)

Updates a set of documents whose _ids match what is provided in the `ids` parameter

func (*BaseRepository) Watch

type DocStatus

type DocStatus int8

The DocStatus type is used to 'delete' documents in MongoDB. Documents are never actually deleted but instead are set to 'inactive' where they are filtered out in most other queries.

const (
	Active   DocStatus = 1
	Inactive DocStatus = 0
)

type Document

type Document interface {
	GetBaseDoc() *BaseDoc
	RemoveObjectId()
	InitializeBaseDoc()
}

The Document interface allows ever root struct to have common helper functions, for example before inserting a new document into MongoDB we call InitializeBaseDoc which creates a new primitive.ObjectID and sets the CreatedDate and ModifiedDate

type MongoUpdateParams

type MongoUpdateParams struct {
	Query    interface{}
	Update   interface{}
	Document Document
	Multi    bool
	Upsert   bool
}

DEPRECATED: see BaseRepository.UpdateBase method

Jump to

Keyboard shortcuts

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