This is an extract of the full documentation available in docs/ (https://rays.bfdy.dev/rays-data-mongo/).
Rays Data Mongo is the official MongoDB integration package for the BeamFoundry Rays framework, bringing Spring Data MongoDB-style repositories, a fluent client builder, and ThreadLocal transactions to Go using the official MongoDB Go Driver.
- Fluent
MongoClientBuilder& Multi-DatabaseConfiguration(pkg/tx): Construct one or more*mongo.Clientand*mongo.DatabaseBeams inside standard RaysConfigurationstructs usingNewMongoClientBuilder(). - Auto-Generated Repository Methods (
pkg/core): EmbedMongoRepository[T, K]and declare unexportedfuncfields likefindByName func(string) *User,findByAddressCity func(string) []User,countByStatus func(string) (int64, Error), orstreamByAgeGreaterThan func(uint) (Stream[User], Error). Rays Data Mongo binds the BSON query implementation automatically. - ThreadLocal Transactions (
pkg/tx): Usinggithub.com/timandy/routine,MongoTransactionalManager(registered automatically byinternal/autoconfigwith no Stereotype of its own) propagates activemongo.SessionContexttransactions across goroutine calls without polluting repository method signatures. - Injection-Safe BSON Queries & Composable Helpers (
pkg/util): Derived queries build structuredbson.M/bson.Dfilters and support variadic...MongoQueryFunchelpers (Paginate,WithLimit,WithSkip,OrderByAsc,OrderByDesc,WithSelect,WithOmit,WithWhere,Combine, etc.). - Centralized
slogObservability (pkg/util): Bridges driver command and error logs into RaysLoggingviaMongoLogSink(with a defaultoptions.LogSinkBeam registered byinternal/autoconfig).
package config
import (
"go.mongodb.org/mongo-driver/mongo"
"go.mongodb.org/mongo-driver/mongo/options"
. "github.com/BeamFoundry/rays-data-mongo/pkg/tx"
. "github.com/BeamFoundry/rays/pkg/core"
. "github.com/BeamFoundry/rays/pkg/lang"
)
type MongoConfig struct {
Configuration
logSink options.LogSink `@:"Inject"`
}
func (c *MongoConfig) Client() (*mongo.Client, Error) {
return NewMongoClientBuilder().
WithHost("127.0.0.1").
WithPort(27017).
WithLogSink(c.logSink).
Build()
}
func (c *MongoConfig) Database(
that struct {
client *mongo.Client `@:"Inject"`
},
) *mongo.Database {
return that.client.Database("myapp")
}
var _, _ = any(&MongoConfig{}).(Stereotype)package repositories
import (
"go.mongodb.org/mongo-driver/bson/primitive"
. "github.com/BeamFoundry/rays-data-mongo/pkg/core"
. "github.com/BeamFoundry/rays/pkg/core"
. "github.com/BeamFoundry/rays/pkg/lang"
)
type User struct {
ID primitive.ObjectID `bson:"_id,omitempty"`
Name string `bson:"name"`
UserName string `bson:"user_name"`
Age uint `bson:"age"`
}
type UserRepository struct {
MongoRepository[User, primitive.ObjectID]
findByUserName func(string) Optional[*User]
}
func (r *UserRepository) FindByUserName(userName string) Optional[*User] {
return r.findByUserName(userName)
}
var _, _ = any(&UserRepository{}).(Stereotype)package main
import (
"os"
"github.com/BeamFoundry/rays"
_ "github.com/BeamFoundry/rays-data-mongo"
_ "myapp/config"
_ "myapp/repositories"
)
func main() {
ctx := rays.ApplicationContext()
if err := ctx.Initialize(); err != nil {
err.LogErrorWithStackTrace()
os.Exit(1)
}
ctx.Run()
}- Documentation (Hugo): Built with Hugo +
hugo-bookindocs/. - Tests: Run with Ginkgo + Testcontainers +
github.com/BeamFoundry/rays/pkg/testing:
GOWORK=off golangci-lint run ./...
GOWORK=off go test -v ./...