Documentation
¶
Overview ¶
Package r3mongo provides an r3.CRUD[T, ID] driver backed by the official MongoDB Go driver v2.
Driver: go.mongodb.org/mongo-driver/v2 Source: https://github.com/mongodb/mongo-go-driver
Supported r3 features:
- Full CRUD (Create, Get, List, Update, Patch, Delete)
- Filters, Sorts, Pagination via the r3 BSON dialect
- Field selection (projection)
- Thread-safe default queries (SetDefaultListQuery, SetDefaultGetQuery)
- Soft-delete support (via r3:"soft_delete" struct tag)
- Restore and HardDelete via the features/softdelete SoftDeleter interface
- Relation preloading (has-many, belongs-to) via separate queries
- Raw escape hatch (BaseRaw) for arbitrary MongoDB operations
Model structs should use `bson` struct tags for field mapping, or `r3` tags. The primary key field defaults to `_id`. Use `r3:"pk"` or `bson:"_id"` to customize.
Example:
type User struct {
ID primitive.ObjectID `bson:"_id,omitempty" r3:"pk"`
Name string `bson:"name"`
Age int `bson:"age"`
}
client, _ := mongo.Connect(options.Client().ApplyURI("mongodb://localhost:27017"))
db := client.Database("mydb")
userRepo := r3mongo.NewMongoCRUD[User, primitive.ObjectID](db.Collection("users"))
// Or let the collection name be derived from the struct name:
userRepo := r3mongo.NewMongoCRUDFromDB[User, primitive.ObjectID](db)
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func NewMongoQuerier ¶
func NewMongoQuerier[T any, ID comparable](coll *mongo.Collection, opts ...r3.Option) r3.Querier[T, ID]
NewMongoQuerier creates a read-only MongoDB repository from a *mongo.Collection. Returns r3.Querier — a compile-time guarantee of read-only access.
func NewMongoQuerierFromDB ¶
func NewMongoQuerierFromDB[T any, ID comparable](db *mongo.Database, opts ...r3.Option) r3.Querier[T, ID]
NewMongoQuerierFromDB creates a read-only MongoDB repository from a *mongo.Database, deriving the collection name from the struct type T. Returns r3.Querier — a compile-time guarantee of read-only access.
Types ¶
type MongoCRUD ¶
type MongoCRUD[T any, ID comparable] struct { *enginemongo.BaseCRUD[T, ID] }
MongoCRUD is a CRUD repository based on MongoDB via the official Go driver v2. It embeds enginemongo.BaseCRUD which provides the full r3.CRUD implementation.
func NewMongoCRUD ¶
func NewMongoCRUD[T any, ID comparable](coll *mongo.Collection, opts ...r3.Option) *MongoCRUD[T, ID]
NewMongoCRUD creates a new MongoDB-based CRUD repository from a *mongo.Collection. Models should use `bson` struct tags for field mapping.
Accepts optional r3.Option values for framework-level configuration.
func NewMongoCRUDFromDB ¶
func NewMongoCRUDFromDB[T any, ID comparable](db *mongo.Database, opts ...r3.Option) *MongoCRUD[T, ID]
NewMongoCRUDFromDB creates a new MongoDB-based CRUD repository from a *mongo.Database. The collection name is derived automatically from the struct type T (e.g., User -> "users", CityTranslation -> "city_translations").
Accepts optional r3.Option values for framework-level configuration.
func (*MongoCRUD[T, ID]) Collection ¶
func (r *MongoCRUD[T, ID]) Collection() *mongo.Collection
Collection returns the underlying *mongo.Collection for advanced usage.
func (*MongoCRUD[T, ID]) Raw ¶
func (r *MongoCRUD[T, ID]) Raw() *enginemongo.BaseRaw[T, ID]
Raw returns the BaseRaw escape hatch for custom MongoDB operations.
type MongoRaw
deprecated
type MongoRaw[T any, ID any] struct { *enginemongo.BaseRaw[T, ID] }
MongoRaw is a thin wrapper around enginemongo.BaseRaw for backward compatibility.
Deprecated: Use enginemongo.BaseRaw directly via MongoCRUD.Raw().
func NewMongoRaw ¶
func NewMongoRaw[T any, ID comparable](coll *mongo.Collection) *MongoRaw[T, ID]
NewMongoRaw creates a new MongoRaw instance.