Files

65 lines
3.5 KiB
Go

package repository
import (
"context"
"git.sendico.io/tech/sxchnge/shared/database/internal/repository"
"git.sendico.io/tech/sxchnge/shared/database/record"
"git.sendico.io/tech/sxchnge/shared/database/repository/builder"
rd "git.sendico.io/tech/sxchnge/shared/database/repository/decoder"
ri "git.sendico.io/tech/sxchnge/shared/database/repository/index"
"git.sendico.io/tech/sxchnge/shared/database/source"
"git.sendico.io/tech/sxchnge/shared/database/transaction"
"go.mongodb.org/mongo-driver/v2/bson"
"go.mongodb.org/mongo-driver/v2/mongo"
)
type (
// FilterQuery selects documents to operate on.
FilterQuery = builder.Query
// PatchDoc defines field/value modifications for partial updates.
PatchDoc = builder.Patch
)
type Repository interface {
Aggregate(ctx context.Context, builder builder.Pipeline, decoder rd.DecodingFunc) error
Insert(ctx context.Context, obj storable.Storable, getFilter builder.Query) error
InsertMany(ctx context.Context, objects []storable.Storable) error
Get(ctx context.Context, id bson.ObjectID, result storable.Storable) error
FindOneByFilter(ctx context.Context, builder builder.Query, result storable.Storable) error
FindFirstByFilter(ctx context.Context, builder builder.Query, result storable.Storable) error
FindManyByFilter(ctx context.Context, builder builder.Query, decoder rd.DecodingFunc) error
// FindAllByFilter decodes all matching documents into result, which must be a pointer to a slice.
FindAllByFilter(ctx context.Context, builder builder.Query, result any) error
CountByFilter(ctx context.Context, builder builder.Query) (int64, error)
Update(ctx context.Context, obj storable.Storable) error
// ReplaceOneByFilter replaces one matching document with obj and reports whether a document matched the filter.
// The repository must persist obj as provided and must not mutate its timestamps or identity before replacement.
ReplaceOneByFilter(ctx context.Context, filter builder.Query, obj storable.Storable) (bool, error)
// Upsert replaces the document identified by obj ID, inserting it when no document exists.
Upsert(ctx context.Context, obj storable.Storable) error
// Patch applies partial updates defined by patch to the document identified by id.
Patch(ctx context.Context, id bson.ObjectID, patch PatchDoc) error
// PatchOneByFilter atomically patches one document matching filter and decodes the updated document into result.
PatchOneByFilter(ctx context.Context, filter FilterQuery, patch PatchDoc, result storable.Storable) (bool, error)
// PatchMany applies partial updates defined by patch to all documents matching filter and returns the number of updated documents.
PatchMany(ctx context.Context, filter FilterQuery, patch PatchDoc) (int, error)
Delete(ctx context.Context, id bson.ObjectID) error
DeleteMany(ctx context.Context, query builder.Query) error
CreateIndex(def *ri.Definition) error
DropIndex(ctx context.Context, name string) error
ListIndexDefinitions(ctx context.Context) ([]*ri.Definition, error)
ListIDs(ctx context.Context, query builder.Query) ([]bson.ObjectID, error)
WithTransaction(ctx context.Context, cb transaction.Callback) (any, error)
Collection() string
}
func CreateRepository(db source.Source, collection string) Repository {
return repositoryimp.NewRepository(db, collection)
}
// CreateMongoRepository adapts an externally owned fixed database.
func CreateMongoRepository(db *mongo.Database, collection string) Repository {
return CreateRepository(source.Fixed(db), collection)
}