base_clients

package
v0.0.0-...-e71a76f Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2025 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 ActivityResourceClient

type ActivityResourceClient[T any] struct {
	Collection  *mongo.Collection
	ExtraFilter bson.M
	Pagination  *db.Pagination
	Search      *db.SearchParams
}

ActivityResourceClient is a type of base client that adds methods to manage activities in the database.

func (*ActivityResourceClient[T]) AddActivity

func (client *ActivityResourceClient[T]) AddActivity(id, activity string) error

AddActivity adds an activity to the model with the given ID. If the activity already exists, it will be overwritten.

func (*ActivityResourceClient[T]) AddExtraFilter

func (client *ActivityResourceClient[T]) AddExtraFilter(filter bson.D) *ActivityResourceClient[T]

AddExtraFilter adds an extra filter to the client.

func (*ActivityResourceClient[T]) ClearActivities

func (client *ActivityResourceClient[T]) ClearActivities(id string) error

ClearActivities clears all activities from the model with the given ID.

func (*ActivityResourceClient[T]) IsDoingActivity

func (client *ActivityResourceClient[T]) IsDoingActivity(id, activity string) (bool, error)

IsDoingActivity returns whether the model with the given ID is doing the given activity.

func (*ActivityResourceClient[T]) RemoveActivity

func (client *ActivityResourceClient[T]) RemoveActivity(id, activity string) error

RemoveActivity removes an activity from the model with the given ID. If the activity does not exist, nothing will happen.

type OnlyID

type OnlyID struct {
	ID string `bson:"id"`
}

OnlyID is a type that only contains an ID. This is useful when only the ID is needed. This should be paired with a projection that only includes the ID, such as bson.D{{"id", 1}}.

type OnlyName

type OnlyName struct {
	Name string `bson:"name"`
}

OnlyName is a type that only contains a name. This is useful when only the name is needed. This should be paired with a projection that only includes the name, such as bson.D{{"name", 1}}.

type Resource

type Resource interface {
}

type ResourceClient

type ResourceClient[T Resource] struct {
	Collection     *mongo.Collection
	IncludeDeleted bool
	ExtraFilter    bson.M
	Pagination     *db.Pagination
	Search         *db.SearchParams
	SortBy         *db.SortBy
}

ResourceClient is a type of base client that adds methods to manage a model in the database. It includes many useful operations such as listing, searching, and pagination.

func (*ResourceClient[T]) AddExtraFilter

func (client *ResourceClient[T]) AddExtraFilter(filter bson.D) *ResourceClient[T]

AddExtraFilter adds an extra filter to the client.

func (*ResourceClient[T]) Count

func (client *ResourceClient[T]) Count() (int, error)

Count returns the number of resources that match the given filter.

func (*ResourceClient[T]) CountDistinct

func (client *ResourceClient[T]) CountDistinct(field string) (int, error)

CountDistinct returns the number of distinct values for the given field.

func (*ResourceClient[T]) CreateIfUnique

func (client *ResourceClient[T]) CreateIfUnique(id string, resource *T, filter bson.D) error

CreateIfUnique creates a model with the given ID and model, but only if a model with the given filter does not already exist.

func (*ResourceClient[T]) Delete

func (client *ResourceClient[T]) Delete() error

Delete deletes all resources that match the given filter. It only sets the deletedAt field to the current time (which will cause it be to be treated as a deleted model), and does not remove the resources from the database.

func (*ResourceClient[T]) DeleteByID

func (client *ResourceClient[T]) DeleteByID(id string) error

DeleteByID deletes a model with the given ID. It only sets the deletedAt field to the current time (which // will cause it be to be treated as a deleted model), and does not remove the model from the database.

func (*ResourceClient[T]) Deleted

func (client *ResourceClient[T]) Deleted(id string) (bool, error)

Deleted returns whether a model with the given ID has been deleted.

func (*ResourceClient[T]) Erase

func (client *ResourceClient[T]) Erase() error

Erase deletes all resources that match the given filter. It removes the resources from the database.

func (*ResourceClient[T]) EraseByID

func (client *ResourceClient[T]) EraseByID(id string) error

EraseByID deletes a model with the given ID. It removes the model from the database.

func (*ResourceClient[T]) ExistsAny

func (client *ResourceClient[T]) ExistsAny() (bool, error)

ExistsAny returns whether any resources exist with the given filter.

func (*ResourceClient[T]) ExistsByID

func (client *ResourceClient[T]) ExistsByID(id string) (bool, error)

ExistsByID returns whether a model with the given ID exists.

func (*ResourceClient[T]) ExistsByName

func (client *ResourceClient[T]) ExistsByName(name string) (bool, error)

ExistsByName returns whether a model with the given name exists.

func (*ResourceClient[T]) ExistsWithFilter

func (client *ResourceClient[T]) ExistsWithFilter(filter bson.D) (bool, error)

ExistsWithFilter returns whether a model with the given filter exists.

func (*ResourceClient[T]) Get

func (client *ResourceClient[T]) Get() (*T, error)

Get returns a model with the given filter.

func (*ResourceClient[T]) GetByID

func (client *ResourceClient[T]) GetByID(id string) (*T, error)

GetByID returns a model with the given ID.

func (*ResourceClient[T]) GetByName

func (client *ResourceClient[T]) GetByName(name string) (*T, error)

GetByName returns a model with the given name.

func (*ResourceClient[T]) GetID

func (client *ResourceClient[T]) GetID() (*string, error)

GetID returns the ID of a model with the given filter. It returns a OnlyID type, which only contains the ID.

func (*ResourceClient[T]) GetName

func (client *ResourceClient[T]) GetName(id string) (*string, error)

GetName returns the name of a model with the given filter. It returns a OnlyName type, which only contains the name.

func (*ResourceClient[T]) GetWithFilterAndProjection

func (client *ResourceClient[T]) GetWithFilterAndProjection(filter, projection bson.D) (*T, error)

GetWithFilterAndProjection returns a model with the given filter and projection. It should be used as a base method for other methods to use, and not called directly.

func (*ResourceClient[T]) List

func (client *ResourceClient[T]) List() ([]T, error)

List returns any resources that match the given filter.

func (*ResourceClient[T]) ListIDs

func (client *ResourceClient[T]) ListIDs() ([]string, error)

ListIDs returns the IDs of all resources that match the given filter.

func (*ResourceClient[T]) ListNames

func (client *ResourceClient[T]) ListNames() ([]string, error)

ListNames returns the names of all resources that match the given filter.

func (*ResourceClient[T]) ListWithFilterAndProjection

func (client *ResourceClient[T]) ListWithFilterAndProjection(filter, projection bson.D) ([]T, error)

ListWithFilterAndProjection returns any resources that match the given filter and projection. It should be used as a base method for other methods to use, and not called directly.

func (*ResourceClient[T]) SetWithBSON

func (client *ResourceClient[T]) SetWithBSON(update bson.D) error

SetWithBSON sets the given fields to the given values in a model.

func (*ResourceClient[T]) SetWithBsonByFilter

func (client *ResourceClient[T]) SetWithBsonByFilter(filter bson.D, update bson.D) error

SetWithBsonByFilter sets the given fields to the given values in a model with the given filter.

func (*ResourceClient[T]) SetWithBsonByID

func (client *ResourceClient[T]) SetWithBsonByID(id string, update bson.D) error

SetWithBsonByID sets the given fields to the given values in a model with the given ID.

func (*ResourceClient[T]) SetWithBsonByName

func (client *ResourceClient[T]) SetWithBsonByName(name string, update bson.D) error

SetWithBsonByName sets the given fields to the given values in a model with the given name.

func (*ResourceClient[T]) UnsetByID

func (client *ResourceClient[T]) UnsetByID(id string, fields ...string) error

UnsetByID unsets the given fields from a model with the given ID.

func (*ResourceClient[T]) UnsetByName

func (client *ResourceClient[T]) UnsetByName(name string, fields ...string) error

UnsetByName unsets the given fields from a model with the given name.

func (*ResourceClient[T]) UnsetWithBSON

func (client *ResourceClient[T]) UnsetWithBSON(fields ...string) error

UnsetWithBSON unsets the given fields from a model.

func (*ResourceClient[T]) UpdateWithBSON

func (client *ResourceClient[T]) UpdateWithBSON(update bson.D) error

UpdateWithBSON updates a model with the given BSON update.

func (*ResourceClient[T]) UpdateWithBsonByFilter

func (client *ResourceClient[T]) UpdateWithBsonByFilter(filter bson.D, update bson.D) error

UpdateWithBsonByFilter updates a model with the given filter and BSON update.

func (*ResourceClient[T]) UpdateWithBsonByID

func (client *ResourceClient[T]) UpdateWithBsonByID(id string, update bson.D) error

UpdateWithBsonByID updates a model with the given ID and BSON update.

func (*ResourceClient[T]) UpdateWithBsonByName

func (client *ResourceClient[T]) UpdateWithBsonByName(name string, update bson.D) error

UpdateWithBsonByName updates a model with the given name and BSON update.

type TimestampedResourceClient

type TimestampedResourceClient[T any] struct {
	Collection   *mongo.Collection
	ExtraFilter  bson.M
	MaxDocuments int
}

TimestampedResourceClient is a type of base client that adds methods to manage timestamped resources in the database. These resources are characterized by having data paired a timestamp, such as polled status

func (*TimestampedResourceClient[T]) List

func (c *TimestampedResourceClient[T]) List() ([]T, error)

List fetches resources from the database that are within the limit

func (*TimestampedResourceClient[T]) Save

func (c *TimestampedResourceClient[T]) Save(resource *T) error

Save saves a resource to the database and delete old resources if the number of resources exceeds the limit

Jump to

Keyboard shortcuts

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