Documentation
¶
Overview ¶
Package r3bson provides a BSON dialect for converting r3 types to MongoDB BSON documents.
Category: Data store dialect.
This is the MongoDB equivalent of the r3sql package for SQL databases. It translates r3.FilterSpec, r3.SortSpec, and other query types into bson.D documents suitable for use with the MongoDB Go driver.
Index ¶
- Variables
- func CursorToBSON(values r3.CursorValues, sorts r3.Sorts, direction r3.CursorDirection) (bson.D, error)
- func FieldToBSON(f *r3.FieldSpec) string
- func FieldsToBSON(fields r3.Fields) bson.D
- func FilterToBSON(f *r3.FilterSpec) (bson.D, error)
- func FiltersToBSON(filters r3.Filters) (bson.D, error)
- func IsErrDialectorFailure(err error) bool
- func SortToBSON(s *r3.SortSpec) (bson.E, error)
- func SortsToBSON(sorts r3.Sorts) (bson.D, error)
- type BSONOperator
Constants ¶
This section is empty.
Variables ¶
var ErrBSONDialectorFailed = errors.New("bson dialector failed")
ErrBSONDialectorFailed is a generic error for r3bson dialector failures.
Functions ¶
func CursorToBSON ¶
func CursorToBSON(values r3.CursorValues, sorts r3.Sorts, direction r3.CursorDirection) (bson.D, error)
CursorToBSON generates a keyset pagination filter from decoded cursor values and the current sort specification.
For a single sort column (e.g., created_at DESC), it produces:
{created_at: {$lt: value}}
For multiple sort columns (e.g., created_at DESC, id ASC), it produces:
{$or: [{created_at: {$lt: v1}}, {$and: [{created_at: {$eq: v1}}, {id: {$gt: v2}}]}]}
The direction parameter controls whether comparison operators are $gt (forward) or $lt (backward), flipped based on the sort direction of each column.
func FieldToBSON ¶
FieldToBSON converts a FieldSpec to a BSON field name string.
func FieldsToBSON ¶
FieldsToBSON converts r3.Fields to a bson.D projection document. Each field is set to 1 (include). The _id field is always included.
func FilterToBSON ¶
func FilterToBSON(f *r3.FilterSpec) (bson.D, error)
FilterToBSON converts a FilterSpec to a bson.D filter document.
func FiltersToBSON ¶
FiltersToBSON converts a list of r3.Filters into a combined bson.D filter. Multiple filters are combined with $and.
func IsErrDialectorFailure ¶
IsErrDialectorFailure checks if the given error is a r3bson failure.
func SortToBSON ¶
SortToBSON converts a SortSpec to a bson.E element (field: 1 or field: -1).
Types ¶
type BSONOperator ¶
type BSONOperator string
BSONOperator represents a MongoDB query operator (e.g. "$eq", "$regex", "$in").
const ( BSONOperatorEq BSONOperator = "$eq" BSONOperatorNe BSONOperator = "$ne" BSONOperatorGt BSONOperator = "$gt" BSONOperatorGte BSONOperator = "$gte" BSONOperatorLt BSONOperator = "$lt" BSONOperatorLte BSONOperator = "$lte" BSONOperatorIn BSONOperator = "$in" BSONOperatorNin BSONOperator = "$nin" BSONOperatorRegex BSONOperator = "$regex" BSONOperatorExists BSONOperator = "$exists" BSONOperatorOptions BSONOperator = "$options" BSONOperatorNot BSONOperator = "$not" BSONOperatorAnd BSONOperator = "$and" BSONOperatorOr BSONOperator = "$or" )
List of supported BSON operators.
func OperatorToBSON ¶
func OperatorToBSON(op r3.FilterOperatorSpec) (BSONOperator, error)
OperatorToBSON converts a FilterOperatorSpec to a BSONOperator.
func (BSONOperator) String ¶
func (op BSONOperator) String() string
String so we implement fmt.Stringer.