Documentation
¶
Index ¶
- type BaseDoc
- type BaseRepository
- func (r *BaseRepository) Aggregate(ctx context.Context, pipeline interface{}, allowDiskUse bool) (*mongo.Cursor, error)
- func (r *BaseRepository) Create(ctx context.Context, doc Document, allowDiskUse bool) (*mongo.InsertOneResult, error)
- func (r *BaseRepository) CreateMany(ctx context.Context, docs []Document, allowDiskUse bool) (*mongo.InsertManyResult, error)
- func (r *BaseRepository) DeleteById(ctx context.Context, id string) (*mongo.DeleteResult, error)
- func (r *BaseRepository) Disconnect(ctx context.Context)
- func (r *BaseRepository) Find(ctx context.Context, query interface{}, rtn interface{}, ...) error
- func (r *BaseRepository) FindById(ctx context.Context, id string, rtn interface{}) error
- func (r *BaseRepository) FindByIdList(ctx context.Context, ids []string, rtn interface{}) error
- func (r *BaseRepository) FindCursor(ctx context.Context, query interface{}, opts ...*options.FindOptions) (*mongo.Cursor, error)
- func (r *BaseRepository) FindDistinct(ctx context.Context, fieldName string, query interface{}) ([]interface{}, error)
- func (r *BaseRepository) FindOne(ctx context.Context, query interface{}, rtn interface{}) error
- func (r *BaseRepository) QuickDeleteById(ctx context.Context, id string)
- func (r *BaseRepository) Update(ctx context.Context, query interface{}, update interface{}, multi bool) (*mongo.UpdateResult, error)
- func (r *BaseRepository) UpdateBase(ctx context.Context, params MongoUpdateParams) (*mongo.UpdateResult, error)
- func (r *BaseRepository) UpdateById(ctx context.Context, id string, update interface{}, autoSet bool, ...) (*mongo.UpdateResult, error)
- func (r *BaseRepository) UpdateByIdList(ctx context.Context, ids []string, update interface{}, status []DocStatus) (*mongo.UpdateResult, error)
- func (r *BaseRepository) Watch(ctx context.Context) (*mongo.ChangeStream, error)
- type DocStatus
- type Document
- type MongoUpdateParams
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 (*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 ¶
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) 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 ¶
func (r *BaseRepository) Watch(ctx context.Context) (*mongo.ChangeStream, error)
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.
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