Documentation
¶
Index ¶
- Constants
- func CloseConnection() error
- func FilterSearch(search string, fields ...string) primitive.M
- func NewConnection(c *Config, l *zap.Logger) error
- func RequestSort(sort []string) primitive.D
- func StructFilter(m any, fields ...string) primitive.M
- type BaseRepository
- func (r *BaseRepository[T]) FindAll(opts *common.QueryOption, query CustomQueryFn) ([]*T, int64, error)
- func (r *BaseRepository[T]) FindByID(id any) (*T, error)
- func (r *BaseRepository[T]) FindOne(customQuery CustomQueryFn) (*T, error)
- func (r *BaseRepository[T]) Insert(entity *T) error
- func (r *BaseRepository[T]) SoftDelete(id any) error
- func (r *BaseRepository[T]) Update(entity *T, fields ...string) error
- func (r *BaseRepository[T]) WithContext(ctx context.Context) common.BaseRepositoryInterface[T]
- type Collection
- func (c *Collection) Count(filter any) (int64, error)
- func (c *Collection) Create(model Model, opts ...*options.InsertOneOptions) error
- func (c *Collection) Delete(model Model) error
- func (c *Collection) Finds(results any, filter any, opts ...*options.FindOptions) error
- func (c *Collection) GetOne(filter any, model Model, opts ...*options.FindOneOptions) error
- func (c *Collection) Show(id any, model Model, opts ...*options.FindOneOptions) error
- func (c *Collection) Update(model Model, fields ...string) error
- func (c *Collection) WithContext(ctx context.Context) *Collection
- type Config
- type CustomQueryFn
- type Model
Constants ¶
const ID = "_id"
ID is the default MongoDB document ID field name.
Variables ¶
This section is empty.
Functions ¶
func CloseConnection ¶
func CloseConnection() error
func FilterSearch ¶
FilterSearch builds a case-insensitive `$or` regex filter for the specified fields.
Example:
FilterSearch("john", "name", "email")
=>
{
"$or": [
{"name": {"$regex": "john", "$options": "i"}},
{"email": {"$regex": "john", "$options": "i"}}
]
}
func NewConnection ¶
NewConnection sets up the MongoDB client and database.
func RequestSort ¶
RequestSort converts a list of sort keys into a MongoDB sort document.
Prefixing a field with "-" sorts descending. Use "__" to represent nested fields. The special case "id" will be converted to "_id".
Example:
RequestSort([]string{"-created_at", "user__name"})
=>
bson.D{
{"created_at", -1},
{"user.name", 1},
}
Types ¶
type BaseRepository ¶
type BaseRepository[T any] struct { Collection *Collection Context context.Context // contains filtered or unexported fields }
func NewBaseRepository ¶
func NewBaseRepository[T any](col *Collection, searchFields []string, enableSoftDelete bool) *BaseRepository[T]
func (*BaseRepository[T]) FindAll ¶
func (r *BaseRepository[T]) FindAll(opts *common.QueryOption, query CustomQueryFn) ([]*T, int64, error)
func (*BaseRepository[T]) FindByID ¶
func (r *BaseRepository[T]) FindByID(id any) (*T, error)
func (*BaseRepository[T]) FindOne ¶
func (r *BaseRepository[T]) FindOne(customQuery CustomQueryFn) (*T, error)
func (*BaseRepository[T]) Insert ¶
func (r *BaseRepository[T]) Insert(entity *T) error
func (*BaseRepository[T]) SoftDelete ¶
func (r *BaseRepository[T]) SoftDelete(id any) error
func (*BaseRepository[T]) Update ¶
func (r *BaseRepository[T]) Update(entity *T, fields ...string) error
func (*BaseRepository[T]) WithContext ¶
func (r *BaseRepository[T]) WithContext(ctx context.Context) common.BaseRepositoryInterface[T]
type Collection ¶
type Collection struct {
*mongo.Collection
// contains filtered or unexported fields
}
Collection wraps a MongoDB collection with a default context for convenience.
func NewCollection ¶
func NewCollection(name string, opts ...*options.CollectionOptions) *Collection
NewCollection creates and returns a new Collection from the default DB with the given name and options. The returned Collection uses the global default context, but you can override it with WithContext.
func (*Collection) Count ¶
func (c *Collection) Count(filter any) (int64, error)
Count returns the number of documents matching the given filter. Returns the count and any error encountered.
func (*Collection) Create ¶
func (c *Collection) Create(model Model, opts ...*options.InsertOneOptions) error
Create inserts the given model into the collection. Optionally accepts InsertOneOptions. On success, sets the inserted ID back to the model. Returns error if insertion fails.
func (*Collection) Delete ¶
func (c *Collection) Delete(model Model) error
Delete removes a document matching the model's ID from the collection. Returns error if deletion fails.
func (*Collection) Finds ¶
func (c *Collection) Finds(results any, filter any, opts ...*options.FindOptions) error
Finds executes a find query with the given filter and options, and decodes all results into 'results' (must be a pointer to a slice). Returns error if the find or decoding fails.
func (*Collection) GetOne ¶
func (c *Collection) GetOne(filter any, model Model, opts ...*options.FindOneOptions) error
GetOne finds a single document matching the given filter and decodes it into the model. Returns error if not found.
func (*Collection) Show ¶
func (c *Collection) Show(id any, model Model, opts ...*options.FindOneOptions) error
Show finds a document by its ID (string or ObjectID) and decodes it into 'model'. Optional FindOneOptions can be passed. Returns an error if the document is not found or the ID is invalid.
func (*Collection) Update ¶
func (c *Collection) Update(model Model, fields ...string) error
Update updates only the specified fields of the given model document by ID. Returns error if the update fails.
func (*Collection) WithContext ¶
func (c *Collection) WithContext(ctx context.Context) *Collection
WithContext sets a new context for the Collection and returns itself for chaining.