Documentation ¶
Overview ¶
Package mgod implements ODM logic for MongoDB in Go.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ConfigureDefaultConnection ¶ added in v0.3.0
func ConfigureDefaultConnection(cfg *ConnectionConfig, dbName string, opts ...*options.ClientOptions) error
ConfigureDefaultConnection opens a new connection using the provided config options and sets it as a default connection to be used by the package.
func GetSchemaCacheKey ¶
GetSchemaCacheKey returns the cache key for the schema of a model. The cache key is in the format of <collection_name>_<model_name_or_value>. Unless we are storing the schema of a union type based on the discriminator value, the second parameter will be the model name only.
func SetDefaultConnection ¶ added in v0.3.0
SetDefaultConnection sets the default connection to be used by the package.
Types ¶
type ConnectionConfig ¶ added in v0.3.0
type ConnectionConfig struct { // Timeout is the timeout for various operations performed on the MongoDB server like Connect, Ping, Session etc. Timeout time.Duration }
ConnectionConfig is the configuration options available for a MongoDB connection.
type EntityMongoModel ¶
type EntityMongoModel[T any] interface { // GetDocToInsert returns the bson.D doc to be inserted in the collection for the provided struct object. // This function is mainly used while creating a doc to be inserted for Union Type models because the underlying type of a union // type model is interface{}, so it's not possible to identify the underlying concrete type to validate and insert the doc. GetDocToInsert(ctx context.Context, model T) (bson.D, error) // InsertOne inserts a single document in the collection. // Model is kept as interface{} to support Union Type models i.e. accept both bson.D (generated using GetDocToInsert()) and struct object. InsertOne(ctx context.Context, model interface{}, opts ...*options.InsertOneOptions) (T, error) // InsertMany inserts multiple documents in the collection. // Docs is kept as interface{} to support Union Type models i.e. accept both []bson.D (generated using GetDocToInsert()) and []struct objects. InsertMany(ctx context.Context, docs interface{}, opts ...*options.InsertManyOptions) ([]T, error) // UpdateMany updates multiple filtered documents in the collection based on the provided update query. UpdateMany(ctx context.Context, filter, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error) // BulkWrite performs multiple write operations on the collection at once. // Currently, only InsertOne, UpdateOne, and UpdateMany operations are supported. BulkWrite(ctx context.Context, bulkWrites []mongo.WriteModel, opts ...*options.BulkWriteOptions) (*mongo.BulkWriteResult, error) // Find returns all documents in the collection matching the provided filter. Find(ctx context.Context, filter interface{}, opts ...*options.FindOptions) ([]T, error) // FindOne returns a single document from the collection matching the provided filter. FindOne(ctx context.Context, filter interface{}, opts ...*options.FindOneOptions) (*T, error) // FindOneAndUpdate returns a single document from the collection based on the provided filter and updates it. FindOneAndUpdate(ctx context.Context, filter, update interface{}, opts ...*options.FindOneAndUpdateOptions) (T, error) // DeleteOne deletes a single document in the collection based on the provided filter. DeleteOne(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) // DeleteMany deletes multiple documents in the collection based on the provided filter. DeleteMany(ctx context.Context, filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error) // CountDocuments returns the number of documents in the collection for the provided filter. CountDocuments(ctx context.Context, filter interface{}, opts ...*options.CountOptions) (int64, error) // Distinct returns the distinct values for the provided field name in the collection for the provided filter. Distinct(ctx context.Context, fieldName string, filter interface{}, opts ...*options.DistinctOptions) ([]interface{}, error) // Aggregate performs an aggregation operation on the collection and returns the results. Aggregate(ctx context.Context, pipeline interface{}, opts ...*options.AggregateOptions) ([]bson.D, error) }
EntityMongoModel is a generic interface of available wrapper functions on MongoDB collection.
func NewEntityMongoModel ¶
func NewEntityMongoModel[T any](modelType T, schemaOpts schemaopt.SchemaOptions) (EntityMongoModel[T], error)
NewEntityMongoModel returns a new instance of EntityMongoModel for the provided model type and options.
Directories ¶
Path | Synopsis |
---|---|
Package bsondoc builds on an existing bson doc according to the provided entity model schema.
|
Package bsondoc builds on an existing bson doc according to the provided entity model schema. |
Package dateformatter provides utilities to get date time in different formats.
|
Package dateformatter provides utilities to get date time in different formats. |
Package schema provides utilities to build the schema tree for the entity model.
|
Package schema provides utilities to build the schema tree for the entity model. |
fieldopt
Package fieldopt provides custom options for schema fields.
|
Package fieldopt provides custom options for schema fields. |
metafield
Package metafield defines and provide functions on custom meta fields for the schema.
|
Package metafield defines and provide functions on custom meta fields for the schema. |
transformer
Package transformer provides custom transformers for schema fields.
|
Package transformer provides custom transformers for schema fields. |