Documentation
¶
Index ¶
- func CollName(m Model) string
- func Ctx() context.Context
- func Disconnect()
- func Init(conf *Config, dbName string, opts ...*options.ClientOptions) (err error)
- func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)
- func NewCtx(timeout time.Duration) context.Context
- func ResetDefaultConfig()
- func UpsertTrueOption() *options.UpdateOptions
- type Collection
- func (c *Collection) Create(model Model, opts ...*options.InsertOneOptions) (interface{}, error)
- func (c *Collection) CreateWithCtx(ctx context.Context, model Model, opts ...*options.InsertOneOptions) (interface{}, error)
- func (c *Collection) Delete(model Model) error
- func (c *Collection) DeleteWithCtx(ctx context.Context, model Model) error
- func (c *Collection) FindAll(results interface{}, filter interface{}, opts ...*options.FindOptions) error
- func (c *Collection) FindAllWithCtx(ctx context.Context, results interface{}, filter interface{}, ...) error
- func (c *Collection) FindByID(id interface{}, model Model, opts ...*options.FindOneOptions) error
- func (c *Collection) FindByIDWithCtx(ctx context.Context, id interface{}, model Model, ...) error
- func (c *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error
- func (c *Collection) FirstWithCtx(ctx context.Context, filter interface{}, model Model, ...) error
- func (c *Collection) Patch(model Model, fields map[string]interface{}, opts ...*options.UpdateOptions) error
- func (c *Collection) PatchWithCtx(ctx context.Context, model Model, fields map[string]interface{}, ...) error
- func (c *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error
- func (c *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)
- func (c *Collection) SimpleAggregateCursorWithCtx(ctx context.Context, stages ...interface{}) (*mongo.Cursor, error)
- func (c *Collection) SimpleAggregateFirst(result interface{}, stages ...interface{}) (bool, error)
- func (c *Collection) SimpleAggregateFirstWithCtx(ctx context.Context, result interface{}, stages ...interface{}) (bool, error)
- func (c *Collection) SimpleAggregateWithCtx(ctx context.Context, results interface{}, stages ...interface{}) error
- func (c *Collection) Update(model Model, opts ...*options.UpdateOptions) error
- func (c *Collection) UpdateWithCtx(ctx context.Context, model Model, opts ...*options.UpdateOptions) error
- type CollectionGetter
- type CollectionNameGetter
- type Config
- type CreatedHook
- type CreatingHook
- type DateFields
- type DefaultModel
- type DefaultTenantModel
- type DeletedHook
- type DeletingHook
- type IDField
- type Model
- type SavedHook
- type SavingHook
- type TenantIdField
- type UpdatedHook
- type UpdatingHook
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CollName ¶
CollName returns a model's collection name. The `CollectionNameGetter` will be used if the model implements this interface. Otherwise, the collection name is inferred based on the model's type using reflection.
func Disconnect ¶
func Disconnect()
func Init ¶
func Init(conf *Config, dbName string, opts ...*options.ClientOptions) (err error)
Init initializes the client and database using the specified configuration values, or default.
func NewClient ¶
func NewClient(opts ...*options.ClientOptions) (*mongo.Client, error)
NewClient returns a new mongodb client.
func ResetDefaultConfig ¶
func ResetDefaultConfig()
ResetDefaultConfig resets the configuration values, client and database.
func UpsertTrueOption ¶
func UpsertTrueOption() *options.UpdateOptions
UpsertTrueOption returns new instance of UpdateOptions with the upsert property set to true.
Types ¶
type Collection ¶
type Collection struct {
*mongo.Collection
}
Collection performs operations on models and the given Mongodb collection
func Coll ¶
func Coll(m Model, opts ...*options.CollectionOptions) *Collection
Coll returns the collection associated with a model.
func CollectionByName ¶
func CollectionByName(name string, opts ...*options.CollectionOptions) *Collection
CollectionByName returns a new collection using the current configuration values.
func NewCollection ¶
func NewCollection(db *mongo.Database, name string, opts ...*options.CollectionOptions) *Collection
NewCollection returns a new collection with the supplied database.
func (*Collection) Create ¶
func (c *Collection) Create(model Model, opts ...*options.InsertOneOptions) (interface{}, error)
Create method inserts a new model into the database.
func (*Collection) CreateWithCtx ¶
func (c *Collection) CreateWithCtx(ctx context.Context, model Model, opts ...*options.InsertOneOptions) (interface{}, error)
func (*Collection) Delete ¶
func (c *Collection) Delete(model Model) error
Delete method deletes a model (doc) from a collection using the specified context. To perform additional operations when deleting a model you should use hooks rather than overriding this method.
func (*Collection) DeleteWithCtx ¶
func (c *Collection) DeleteWithCtx(ctx context.Context, model Model) error
func (*Collection) FindAll ¶
func (c *Collection) FindAll(results interface{}, filter interface{}, opts ...*options.FindOptions) error
FindAll finds, decodes and returns the results using the specified context.
func (*Collection) FindAllWithCtx ¶
func (c *Collection) FindAllWithCtx(ctx context.Context, results interface{}, filter interface{}, opts ...*options.FindOptions) error
func (*Collection) FindByID ¶
func (c *Collection) FindByID(id interface{}, model Model, opts ...*options.FindOneOptions) error
FindByID method finds a doc and decodes it to a model, otherwise returns an error. The id field can be any value that if passed to the `PrepareID` method, it returns a valid ID (e.g.string, bson.ObjectId).
func (*Collection) FindByIDWithCtx ¶
func (c *Collection) FindByIDWithCtx(ctx context.Context, id interface{}, model Model, opts ...*options.FindOneOptions) error
func (*Collection) First ¶
func (c *Collection) First(filter interface{}, model Model, opts ...*options.FindOneOptions) error
First method searches and returns the first document in the search results.
func (*Collection) FirstWithCtx ¶
func (c *Collection) FirstWithCtx(ctx context.Context, filter interface{}, model Model, opts ...*options.FindOneOptions) error
func (*Collection) Patch ¶
func (c *Collection) Patch(model Model, fields map[string]interface{}, opts ...*options.UpdateOptions) error
Patch function persists the given fields in a model to the database using the specified context. Calling this method also invokes the model's mdu updating, updated, saving, and saved hooks.
func (*Collection) PatchWithCtx ¶
func (c *Collection) PatchWithCtx(ctx context.Context, model Model, fields map[string]interface{}, opts ...*options.UpdateOptions) error
func (*Collection) SimpleAggregate ¶
func (c *Collection) SimpleAggregate(results interface{}, stages ...interface{}) error
SimpleAggregate performs a simple aggregation, decodes the aggregate result and returns the list using the provided result parameter. The value of `stages` can be Operator|bson.M Note: you can not use this method in a transaction because it does not accept a context. To participate in transactions, please use the regular aggregation method.
func (*Collection) SimpleAggregateCursor ¶
func (c *Collection) SimpleAggregateCursor(stages ...interface{}) (*mongo.Cursor, error)
SimpleAggregateCursor performs a simple aggregation and returns a cursor over the resulting documents. Note: you can not use this method in a transaction because it does not accept a context. To participate in transactions, please use the regular aggregation method.
func (*Collection) SimpleAggregateCursorWithCtx ¶
func (*Collection) SimpleAggregateFirst ¶
func (c *Collection) SimpleAggregateFirst(result interface{}, stages ...interface{}) (bool, error)
SimpleAggregateFirst performs a simple aggregation, decodes the first aggregate result and returns it using the provided result parameter. The value of `stages` can be Operator|bson.M Note: you can not use this method in a transaction because it does not accept a context. To participate in transactions, please use the regular aggregation method.
func (*Collection) SimpleAggregateFirstWithCtx ¶
func (c *Collection) SimpleAggregateFirstWithCtx(ctx context.Context, result interface{}, stages ...interface{}) (bool, error)
func (*Collection) SimpleAggregateWithCtx ¶
func (c *Collection) SimpleAggregateWithCtx(ctx context.Context, results interface{}, stages ...interface{}) error
func (*Collection) Update ¶
func (c *Collection) Update(model Model, opts ...*options.UpdateOptions) error
Update function persists the changes made to a model to the database using the specified context. Calling this method also invokes the model's mdu updating, updated, saving, and saved hooks.
func (*Collection) UpdateWithCtx ¶
func (c *Collection) UpdateWithCtx(ctx context.Context, model Model, opts ...*options.UpdateOptions) error
type CollectionGetter ¶
type CollectionGetter interface { // Collection method return collection Collection() *Collection }
CollectionGetter interface contains a method to return a model's custom collection.
type CollectionNameGetter ¶
type CollectionNameGetter interface { // CollectionName method return model collection's name. CollectionName() string }
CollectionNameGetter interface contains a method to return the collection name of a model.
type CreatedHook ¶
CreatedHook is called after a model has been created
type CreatingHook ¶
CreatingHook is called before saving a new model to the database
type DateFields ¶
type DateFields struct { CreatedAt time.Time `json:"created_at" bson:"created_at"` UpdatedAt time.Time `json:"updated_at" bson:"updated_at"` }
DateFields struct contains the `created_at` and `updated_at` fields that autofill when inserting or updating a model.
type DefaultModel ¶
type DefaultModel struct { IDField `bson:",inline"` DateFields `bson:",inline"` }
DefaultModel struct contains a model's default fields.
type DefaultTenantModel ¶ added in v0.5.0
type DefaultTenantModel struct { IDField `bson:",inline"` DateFields `bson:",inline"` TenantIdField `bson:",inline"` }
DefaultTenantModel struct contains a model's default fields. This is useful for multi tenant systems.
type DeletedHook ¶
type DeletedHook interface {
Deleted(ctx context.Context, result *mongo.DeleteResult) error
}
DeletedHook is called after a model is deleted
type DeletingHook ¶
DeletingHook is called before a model is deleted
type IDField ¶
type IDField struct {
ID string `json:"id" bson:"_id,omitempty"`
}
type Model ¶
type Model interface { // PrepareID converts the id value if needed, then // returns it (e.g.convert string to objectId). PrepareID(id string) (string, error) GetID() string SetID(id string) }
Model interface contains base methods that must be implemented by each model. If you're using the `DefaultModel` struct in your model, you don't need to implement any of these methods.
type SavingHook ¶
SavingHook is called before a model (new or existing) is saved to the database.
type TenantIdField ¶ added in v0.5.0
type TenantIdField struct {
TenantId string `json:"tenantId" bson:"tenantId,omitempty"`
}
func (*TenantIdField) GetTenantId ¶ added in v0.6.0
func (f *TenantIdField) GetTenantId() string
GetTenantId method returns a model's TenantId
func (*TenantIdField) SetTenantId ¶ added in v0.6.0
func (f *TenantIdField) SetTenantId(tenantId string)
SetTenantId sets the value of a model's TenantId field.
type UpdatedHook ¶
type UpdatedHook interface {
Updated(ctx context.Context, result *mongo.UpdateResult) error
}
UpdatedHook is called after a model is updated
type UpdatingHook ¶
UpdatingHook is called before updating a model