Documentation ¶
Index ¶
- func WithTransaction[T SessionLike](ctx context.Context, sess T, fn SessionFunc, ...) (any, error)
- type AfterCreateHook
- type AfterDeleteHook
- type AfterFindHook
- type AfterSaveHook
- type AfterUpdateHook
- type AfterValidateHook
- type BeforeCreateHook
- type BeforeDeleteHook
- type BeforeFindHook
- type BeforeSaveHook
- type BeforeUpdateHook
- type BeforeValidateHook
- type DefaultSchema
- func (s *DefaultSchema) GenerateCreatedAt()
- func (s *DefaultSchema) GenerateID()
- func (s *DefaultSchema) GenerateUpdatedAt()
- func (s DefaultSchema) GetCreatedAt() time.Time
- func (s DefaultSchema) GetID() primitive.ObjectID
- func (s DefaultSchema) GetUpdatedAt() time.Time
- func (s DefaultSchema) GetUpdatedAtTag(t string) string
- func (s *DefaultSchema) SetUpdatedAt(t time.Time)
- type Document
- func (doc *Document[T, P]) BSON() (bson.M, error)
- func (doc *Document[T, P]) Collection() *mongo.Collection
- func (doc *Document[T, P]) Delete(ctx context.Context) error
- func (doc *Document[T, P]) IsModified(field string) bool
- func (doc *Document[T, P]) IsNew() bool
- func (doc *Document[T, P]) JSON() (map[string]any, error)
- func (doc *Document[T, P]) MarshalBSON() ([]byte, error)
- func (doc *Document[T, P]) MarshalJSON() ([]byte, error)
- func (doc *Document[T, P]) Model() *Model[T, P]
- func (doc *Document[T, P]) Save(ctx context.Context) error
- func (doc *Document[T, P]) UnmarshalBSON(data []byte) error
- type HookArg
- type IDefaultSchema
- type Model
- func (model *Model[T, P]) Collection() *mongo.Collection
- func (model *Model[T, P]) CreateMany(ctx context.Context, docs []T, opts ...*mopt.InsertManyOptions) ([]*Document[T, P], error)
- func (model *Model[T, P]) CreateOne(ctx context.Context, doc T, opts ...*mopt.InsertOneOptions) (*Document[T, P], error)
- func (model *Model[T, P]) DeleteMany(ctx context.Context, query bson.M, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (model *Model[T, P]) DeleteOne(ctx context.Context, query bson.M, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (model *Model[T, P]) Find(ctx context.Context, query bson.M, opts ...*mopt.FindOptions) ([]*Document[T, P], error)
- func (model *Model[T, P]) FindById(ctx context.Context, id any, opts ...*mopt.FindOneOptions) (*Document[T, P], error)
- func (model *Model[T, P]) FindOne(ctx context.Context, query bson.M, opts ...*mopt.FindOneOptions) (*Document[T, P], error)
- func (model *Model[T, P]) NewDocument(data T) *Document[T, P]
- func (model *Model[T, P]) UpdateMany(ctx context.Context, query bson.M, update bson.M, ...) (*mongo.UpdateResult, error)
- func (model *Model[T, P]) UpdateOne(ctx context.Context, query bson.M, update bson.M, ...) (*mongo.UpdateResult, error)
- type Query
- type QueryOperation
- type Schema
- type SessionFunc
- type SessionLike
- type ValidateHook
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func WithTransaction ¶
func WithTransaction[T SessionLike](ctx context.Context, sess T, fn SessionFunc, opts ...*options.TransactionOptions) (any, error)
WithTransaction executes the callback function in a transaction. When a transaction is started with mongo.SessionContext options are ignored because the session is already created. This method only closes the session if it was created by this method.
Types ¶
type AfterCreateHook ¶
type AfterCreateHook[T Schema] interface { AfterCreate(ctx context.Context, arg *HookArg[T]) error }
AfterCreateHook runs after documents are written to the database when executing Model.CreateOne and Model.CreateMany operations. AfterSaveHook will run before this hook runs. HookArg.Data will return ptr to Document(s) created.
type AfterDeleteHook ¶
type AfterDeleteHook[T Schema] interface { AfterDelete(ctx context.Context, arg *HookArg[T]) error }
AfterDeleteHook runs after Document(s) are removed from the database. HookArg.Data will return ptr to deleted Document if this is called on a Document, otherwise it will return ptr to mongo.DeleteResult.
type AfterFindHook ¶
AfterFindHook runs after a find operation is executed. HookArg.Data will return ptr to found Document(s).
type AfterSaveHook ¶
AfterSaveHook runs after Document(s) are written to the database when using Model.CreateOne, Model.CreateMany or Document.Save. This hook doesn't run on all Model Update operations. HookArg.Data will return ptr to Document(s) being saved.
type AfterUpdateHook ¶
type AfterUpdateHook[T Schema] interface { AfterUpdate(ctx context.Context, arg *HookArg[T]) error }
AfterUpdateHook runs after a document is updated in the database. This hook also runs on `replace` operations. HookArg.Data will return ptr to updated *Document if called on a Document, otherwise it will return ptr to mongo.UpdateResult.
type AfterValidateHook ¶
type AfterValidateHook[T Schema] interface { AfterValidate(ctx context.Context, arg *HookArg[T]) error }
AfterValidateHook runs after validate hook is called. HookArg.Data will return ptr to validated Document.
type BeforeCreateHook ¶
type BeforeCreateHook[T Schema] interface { BeforeCreate(ctx context.Context, arg *HookArg[T]) error }
BeforeCreateHook runs before executing [Model.Create] and Model.CreateMany operations. BeforeSaveHook will run after this hook runs. HookArg.Data will return ptr to Document(s) being created.
type BeforeDeleteHook ¶
type BeforeDeleteHook[T Schema] interface { BeforeDelete(ctx context.Context, arg *HookArg[T]) error }
BeforeDeleteHook runs before Document(s) are removed from the database. HookArg.Data will return ptr to Query being executed if this is called on a Model, otherwise it will return ptr to Document being deleted.
type BeforeFindHook ¶
BeforeFindHook runs before any find QueryOperation is executed. HookArg.Data will return ptr to Query being ececuted.
type BeforeSaveHook ¶
BeforeSaveHook runs before Document(s) are written to the database when using Model.CreateOne, Model.CreateMany or Document.Save. This hook doesn't run on all Model Update operations. HookArg.Data will return ptr to Document(s) being saved.
type BeforeUpdateHook ¶
type BeforeUpdateHook[T Schema] interface { BeforeUpdate(ctx context.Context, arg *HookArg[T]) error }
BeforeUpdateHook runs before Document(s) are updated in the database. This hook also runs on `replace` operations. HookArg.Data will return ptr to Document being updated if called on a Document, otherwise it will return ptr to Query.
type BeforeValidateHook ¶
type BeforeValidateHook[T Schema] interface { BeforeValidate(ctx context.Context, arg *HookArg[T]) error }
BeforeValidateHook runs before validate hook is called. HookArg.Data will return ptr to Document being validated.
type DefaultSchema ¶
type DefaultSchema struct { ID *primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"` CreatedAt *time.Time `json:"createdAt,omitempty" bson:"createdAt,omitempty"` UpdatedAt *time.Time `json:"updatedAt,omitempty" bson:"updatedAt,omitempty"` }
DefaultSchema is a struct that implements the IDefaultSchema interface. It contains the default fields (ID,CreatedAt,UpdatedAt) that are added to a document.
func (*DefaultSchema) GenerateCreatedAt ¶
func (s *DefaultSchema) GenerateCreatedAt()
func (*DefaultSchema) GenerateID ¶
func (s *DefaultSchema) GenerateID()
func (*DefaultSchema) GenerateUpdatedAt ¶
func (s *DefaultSchema) GenerateUpdatedAt()
func (DefaultSchema) GetCreatedAt ¶
func (s DefaultSchema) GetCreatedAt() time.Time
func (DefaultSchema) GetID ¶
func (s DefaultSchema) GetID() primitive.ObjectID
func (DefaultSchema) GetUpdatedAt ¶
func (s DefaultSchema) GetUpdatedAt() time.Time
func (DefaultSchema) GetUpdatedAtTag ¶
func (s DefaultSchema) GetUpdatedAtTag(t string) string
func (*DefaultSchema) SetUpdatedAt ¶
func (s *DefaultSchema) SetUpdatedAt(t time.Time)
type Document ¶
type Document[T Schema, P IDefaultSchema] struct { IDefaultSchema `json:"-" bson:"-"` Doc *T `json:",inline" bson:",inline"` // contains filtered or unexported fields }
Document is a struct that represents a document in a MongoDB collection. Do not use this struct directly, instead use the Model.NewDocument method.
func (*Document[T, P]) Collection ¶
func (doc *Document[T, P]) Collection() *mongo.Collection
Returns the collection that doc belongs to.
func (*Document[T, P]) Delete ¶
Deletes Document (doc) from the database atomically. This operation fails if any of the hooks return an error.
func (*Document[T, P]) IsModified ¶
IsModified returns true if the field has been modified.
func (*Document[T, P]) IsNew ¶
IsNew returns true if the document is new and has never been written to the database. Otherwise, it returns false
func (*Document[T, P]) MarshalBSON ¶
Returns doc as BSON bytes.
func (*Document[T, P]) MarshalJSON ¶
Returns doc as JSON bytes.
func (*Document[T, P]) Save ¶
Saves [Documet] (doc) to the database atomically. This method creates a new document if the document is not already existing, Otherwise, it updates the existing document. This operation fails if any of the hooks return an error.
func (*Document[T, P]) UnmarshalBSON ¶
Unmarshals data into the doc.
type HookArg ¶
type HookArg[T Schema] struct { // contains filtered or unexported fields }
HookArg represents arguments passed to a hook method. It is advisable to only modify HookArg.Data and not the Hook receiver itself, doing so may cause unexpected behaviors. To avoid modifying the receiver, ensure it's not a pointer.
func (*HookArg[T]) Data ¶
func (arg *HookArg[T]) Data() interface{}
Data returns the data associated with this hook.
func (*HookArg[T]) Operation ¶
func (arg *HookArg[T]) Operation() QueryOperation
Operation returns the QueryOperation being executed.
type IDefaultSchema ¶
type IDefaultSchema interface { // Generates a new ObjectID and sets it to the ID field. GenerateID() // Generates a new time.Time and sets it to the CreatedAt field. GenerateCreatedAt() // Generates a new time.Time and sets it to the UpdatedAt field. GenerateUpdatedAt() // GetID returns the ID field. GetID() primitive.ObjectID // GetCreatedAt returns the CreatedAt field. GetCreatedAt() time.Time // GetUpdatedAt returns the UpdatedAt field. GetUpdatedAt() time.Time // Sets the UpdatedAt field to t. SetUpdatedAt(t time.Time) // Returns the tag name for the UpdatedAt field. t can be either "json", "bson" or any custom tag. // This is useful for setting the UpdatedAt field when updating with [Model.UpdateOne] and [Model.UpdateMany]. GetUpdatedAtTag(t string) string }
IDefaultSchema is an interface that represents the default fields that are added to a document. Only structs that implement this interface can be used as a default schema.
type Model ¶
type Model[T Schema, P IDefaultSchema] struct { // contains filtered or unexported fields }
func NewModel ¶
func NewModel[T Schema, P IDefaultSchema](collection *mongo.Collection) *Model[T, P]
NewModel creates a new model. T represents the schema type while P represents default schema type. Panics if T or P is not a struct .
func (*Model[T, P]) Collection ¶
func (model *Model[T, P]) Collection() *mongo.Collection
Collection returns the *mongo.Collection that the model is using.
func (*Model[T, P]) CreateMany ¶
func (model *Model[T, P]) CreateMany(ctx context.Context, docs []T, opts ...*mopt.InsertManyOptions) ([]*Document[T, P], error)
CreateMany creates multiple documents in the collection. It returns the created documents or an error if one occurred. Documents are not created if any of the hooks return an error.
func (*Model[T, P]) CreateOne ¶
func (model *Model[T, P]) CreateOne(ctx context.Context, doc T, opts ...*mopt.InsertOneOptions) (*Document[T, P], error)
CreateOne creates a single document in the collection. It returns the created document or an error if one occurred. Document is not created if any of the hooks return an error.
func (*Model[T, P]) DeleteMany ¶
func (model *Model[T, P]) DeleteMany(ctx context.Context, query bson.M, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteMany deletes multiple documents from the collection. It returns the deleted result or an error if one occurred. Documents are not deleted if any of the hooks return an error.
func (*Model[T, P]) DeleteOne ¶
func (model *Model[T, P]) DeleteOne(ctx context.Context, query bson.M, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne deletes a single document from the collection. It returns the deleted result or an error if one occurred. Document is not deleted if any of the hooks return an error.
func (*Model[T, P]) Find ¶
func (model *Model[T, P]) Find(ctx context.Context, query bson.M, opts ...*mopt.FindOptions) ([]*Document[T, P], error)
Find finds multiple documents from the collection. It returns the documents or an error if one occurred.
func (*Model[T, P]) FindById ¶
func (model *Model[T, P]) FindById(ctx context.Context, id any, opts ...*mopt.FindOneOptions) (*Document[T, P], error)
FindById finds a single document by its id. It returns the document or an error if one occurred. If no document is found, it returns mongo.ErrNoDocuments.
func (*Model[T, P]) FindOne ¶
func (model *Model[T, P]) FindOne(ctx context.Context, query bson.M, opts ...*mopt.FindOneOptions) (*Document[T, P], error)
FindOne finds a single document from the collection. It returns the document or an error if one occurred. If no document is found, it returns mongo.ErrNoDocuments.
func (*Model[T, P]) NewDocument ¶
NewDocument creates a new *Document with the given data.
func (*Model[T, P]) UpdateMany ¶
func (model *Model[T, P]) UpdateMany(ctx context.Context, query bson.M, update bson.M, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateMany updates multiple documents in the collection. It returns the update result or an error if one occurred. Documents are not updated if any of the hooks return an error.
func (*Model[T, P]) UpdateOne ¶
func (model *Model[T, P]) UpdateOne(ctx context.Context, query bson.M, update bson.M, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOne updates a single document in the collection. It returns the update result or an error if one occurred. Document is not updated if any of the hooks return an error.
type Query ¶
type Query[T Schema] struct { // The document filter for this operation Filter *bson.M // Update payload if Operation is an update operation Update *bson.M // Options specific to the current operation Options interface{} // Operation being executed Operation QueryOperation }
Query is a struct that holds information about the current operation beign executed on a model.
func (*Query[T]) SetOperation ¶
func (q *Query[T]) SetOperation(o QueryOperation) *Query[T]
SetOperation sets the Query Operation field.
func (*Query[T]) SetOptions ¶
SetOptions sets the Query Options field.
type QueryOperation ¶
type QueryOperation string
Model Query Operation
const ( CreateOne QueryOperation = "create-one" CreateMany QueryOperation = "create-many" FindMany QueryOperation = "find-many" FindOne QueryOperation = "find-one" FindOneAndUpdate QueryOperation = "find-one-and-update" Replace QueryOperation = "replace" UpdateOne QueryOperation = "update-one" UpdateMany QueryOperation = "update-many" DeleteOne QueryOperation = "delete-one" DeleteMany QueryOperation = "delete-many" )
type Schema ¶
type Schema interface{}
Schema is an interface that represents the structure of a document in a MongoDB collection. It must be a struct.
type SessionFunc ¶
type SessionFunc func(sessCtx mongo.SessionContext) (interface{}, error)
SessionFunc is a callback function that's executed within a session transaction.
type SessionLike ¶
type SessionLike interface { *mongo.Database | *mongo.Collection | *mongo.SessionContext | *mongo.Client | *mongo.Session }
SessionLike is a Union type of all mongo types that a seeion can be derived from.
type ValidateHook ¶
ValidateHook is used to register validation for a schema. This hook runs before a Document is saved to the database. It only runs on Model.CreateOne, Model.CreateMany and Document.Save operations. HookArg.Data will return ptr to Document being validated.