r3bson

package
v0.0.4 Latest Latest
Warning

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

Go to latest
Published: Jul 8, 2026 License: MIT Imports: 6 Imported by: 0

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

Constants

This section is empty.

Variables

View Source
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

func FieldToBSON(f *r3.FieldSpec) string

FieldToBSON converts a FieldSpec to a BSON field name string.

func FieldsToBSON

func FieldsToBSON(fields r3.Fields) bson.D

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

func FiltersToBSON(filters r3.Filters) (bson.D, error)

FiltersToBSON converts a list of r3.Filters into a combined bson.D filter. Multiple filters are combined with $and.

func IsErrDialectorFailure

func IsErrDialectorFailure(err error) bool

IsErrDialectorFailure checks if the given error is a r3bson failure.

func SortToBSON

func SortToBSON(s *r3.SortSpec) (bson.E, error)

SortToBSON converts a SortSpec to a bson.E element (field: 1 or field: -1).

func SortsToBSON

func SortsToBSON(sorts r3.Sorts) (bson.D, error)

SortsToBSON converts a slice of SortSpec to a bson.D sort document.

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.

Jump to

Keyboard shortcuts

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