repo

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jun 18, 2024 License: GPL-3.0 Imports: 4 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrFindOne    = fmt.Errorf("find one error")
	ErrFind       = fmt.Errorf("find error")
	ErrFindStream = fmt.Errorf("find stream error")
	ErrInsertOne  = fmt.Errorf("insert one error")
	ErrInsertMany = fmt.Errorf("insert many error")
	ErrUpdateOne  = fmt.Errorf("update one error")
	ErrUpdateByID = fmt.Errorf("update by ID error")
	ErrUpdateMany = fmt.Errorf("update many error")
	ErrDeleteOne  = fmt.Errorf("delete one error")
	ErrDeleteMany = fmt.Errorf("delete many error")
	ErrCount      = fmt.Errorf("count error")
)

Functions

This section is empty.

Types

type Model

type Model interface {
	GetDatabaseName() string
	GetCollectionName() string
}

Model is a generic model.

example:

type User struct {
	ID       primitive.ObjectID `bson:"_id"`
	Username string             `bson:"username"`
	Password string             `bson:"password"`
}

func (u *User) GetDatabaseName() string {
	return "users_db"
}

func (u *User) GetCollectionName() string {
	return "users_col"
}

type Repository

type Repository[M Model, I any] struct {
	// contains filtered or unexported fields
}

Repository is a generic repository for a model.

func NewRepository

func NewRepository[M Model, I any](client *mongo.Client) *Repository[M, I]

NewRepository creates a new repository for a model. The model must implement the Model interface. e.g. usersRepo := NewRepository[*User](client)

func (*Repository[M, I]) Count added in v0.0.4

func (r *Repository[M, I]) Count(ctx context.Context, filter any, opts ...*options.CountOptions) (int64, error)

Count returns the number of documents that match the filter.

func (*Repository[M, I]) CountEstimate added in v0.0.4

func (r *Repository[M, I]) CountEstimate(ctx context.Context, opts ...*options.EstimatedDocumentCountOptions) (int64, error)

CountEstimate returns the estimated number of documents that match the filter.

func (*Repository[M, I]) DeleteMany

func (r *Repository[M, I]) DeleteMany(
	ctx context.Context,
	filter any,
	opts ...*options.DeleteOptions,
) (*mongo.DeleteResult, error)

DeleteMany deletes multiple documents by their filter.

func (*Repository[M, I]) DeleteOne

func (r *Repository[M, I]) DeleteOne(
	ctx context.Context,
	filter any,
	opts ...*options.DeleteOptions,
) (*mongo.DeleteResult, error)

DeleteOne deletes a single document by its filter.

func (*Repository[M, I]) Find

func (r *Repository[M, I]) Find(
	ctx context.Context,
	filter any,
	opts ...*options.FindOptions,
) ([]M, error)

Find returns all documents that match the filter.

func (*Repository[M, I]) FindOne

func (r *Repository[M, I]) FindOne(
	ctx context.Context,
	filter any,
	opts ...*options.FindOneOptions,
) (M, error)

FindOne returns the first document that matches the filter.

func (*Repository[M, I]) FindStream

func (r *Repository[M, I]) FindStream(
	ctx context.Context,
	filter any,
	opts ...*options.FindOptions,
) (chan M, chan error, chan struct{}, error)

FindStream works like Find, but returns a channel of results and a channel of errors. both values and error channels are closed when the cursor is exhausted, an error occurs or the cancel channel is closed. the cancel channel can be used to stop the stream before it is exhausted by closing it. the caller is responsible for closing the cancel channel.

func (*Repository[M, I]) InsertMany

func (r *Repository[M, I]) InsertMany(
	ctx context.Context,
	documents []M,
	opts ...*options.InsertManyOptions,
) ([]I, error)

InsertMany inserts multiple documents into the collection.

func (*Repository[M, I]) InsertOne

func (r *Repository[M, I]) InsertOne(
	ctx context.Context,
	document M,
	opts ...*options.InsertOneOptions,
) (I, error)

InsertOne inserts a single document into the collection.

func (*Repository[M, I]) UpdateByID

func (r *Repository[M, I]) UpdateByID(
	ctx context.Context,
	id I,
	update any,
	opts ...*options.UpdateOptions,
) (*UpdateResult[I], error)

UpdateByID updates a single document by its ID.

func (*Repository[M, I]) UpdateMany

func (r *Repository[M, I]) UpdateMany(
	ctx context.Context,
	filter any,
	update any,
	opts ...*options.UpdateOptions,
) (*UpdateResult[I], error)

UpdateMany updates multiple documents by their filter.

func (*Repository[M, I]) UpdateOne

func (r *Repository[M, I]) UpdateOne(
	ctx context.Context,
	filter any,
	update any,
	opts ...*options.UpdateOptions,
) (*UpdateResult[I], error)

UpdateOne updates a single document by its filter.

type UpdateResult

type UpdateResult[I any] struct {
	MatchedCount  int64 // The number of documents matched by the filter.
	ModifiedCount int64 // The number of documents modified by the operation.
	UpsertedCount int64 // The number of documents upserted by the operation.
	UpsertedID    *I    // The _id field of the upserted document, or nil if no upsert was done.
}

UpdateResult is the same as the mongo.UpdateResult type, but with generic support.

Jump to

Keyboard shortcuts

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