Documentation ¶
Index ¶
- Variables
- func DateTimeNow() primitive.DateTime
- func IsFindOneError(err error) bool
- func IsNoDocumentsError(err error) bool
- func LinkModel[T ModelData](model *Model[T], db *mongo.Database)
- func NewId() primitive.ObjectID
- func NewUUid() string
- type Client
- type ConnectionCredentials
- type Model
- func (coll *Model[T]) Aggregate(pipeline interface{}, opts ...*options.AggregateOptions) ([]bson.M, error)
- func (coll *Model[T]) Count(filter interface{}, opts ...*options.CountOptions) (int64, error)
- func (coll *Model[T]) CountAggregate(pipeline interface{}, opts ...*options.AggregateOptions) (int, error)
- func (coll *Model[T]) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
- func (coll *Model[T]) Exists(filter interface{}) (bool, error)
- func (coll *Model[T]) Find(filter interface{}, opts ...*options.FindOptions) ([]T, error)
- func (coll *Model[T]) FindAs(result interface{}, filter interface{}, opts ...*options.FindOptions) error
- func (coll *Model[T]) FindOne(filter interface{}, opts ...*options.FindOneOptions) (T, error)
- func (coll *Model[T]) FindOneAs(result interface{}, filter interface{}, opts ...*options.FindOneOptions) error
- func (coll *Model[T]) FindOneAsHelper(filter interface{}, opts ...*options.FindOneOptions) (ModelHelper[T], error)
- func (coll *Model[T]) FindOneById(id primitive.ObjectID, opts ...*options.FindOneOptions) (T, error)
- func (coll *Model[T]) GetPublicFields(model ModelData) bson.M
- func (coll *Model[T]) GetPublicFieldsAnd(model ModelData, interceptor func(data bson.M) bson.M) bson.M
- func (coll *Model[T]) Helpers(model T) ModelHelper[T]
- func (coll *Model[T]) Paginate(page int, perPage int, query interface{}, opts ...*options.FindOptions) (*Paginated[any], error)
- func (coll *Model[T]) PaginateAggregate(page int, perPage int, query []interface{}) (*Paginated[any], error)
- func (coll *Model[T]) Pick(data ModelData, keys []string) bson.M
- func (coll *Model[T]) ProjectPublicFields() bson.M
- func (coll *Model[T]) ProjectPublicFieldsAnd(keys []string) bson.M
- func (coll *Model[T]) ProjectPublicFieldsWithout(keys []string) bson.M
- func (coll *Model[T]) ToBsonMap(data ModelData) bson.M
- func (coll *Model[T]) ToJsonMap(data ModelData) bson.M
- func (coll *Model[T]) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
- type ModelData
- type ModelHelper
- func (m ModelHelper[T]) Delete() (*mongo.DeleteResult, error)
- func (m ModelHelper[T]) GetID() primitive.ObjectID
- func (m ModelHelper[T]) GetPublicFields() bson.M
- func (m ModelHelper[T]) Update(set bson.M) (*mongo.UpdateResult, error)
- func (m ModelHelper[T]) UpdateRaw(update bson.M) (*mongo.UpdateResult, error)
- type Paginated
- type PaginatedMeta
Constants ¶
This section is empty.
Variables ¶
var Projection = struct { OmitKeys func(keys []string) map[string]any PickKeys func(keys []string) map[string]any OmitIdAnd func(keys []string) map[string]any OmitIdAndPick func(keys []string) map[string]any }{ OmitKeys: omitKeys, PickKeys: pickKeys, OmitIdAnd: omitIdAnd, OmitIdAndPick: omitIdAndPick, }
Functions ¶
func DateTimeNow ¶
DateTimeNow - Get the current date and time
func IsFindOneError ¶ added in v0.1.1
IsFindOneError - Check if the error exists but is not a mongo.ErrNoDocuments error
func IsNoDocumentsError ¶ added in v0.1.1
IsNoDocumentsError - Check if the error exists and is a mongo.ErrNoDocuments error
Types ¶
type Client ¶
type Client struct { MongoClient *mongo.Client Database *mongo.Database // contains filtered or unexported fields }
func ConnectUsingCredentials ¶
func ConnectUsingCredentials(credentials *ConnectionCredentials) (*Client, error)
func ConnectUsingString ¶
func (*Client) IsConnected ¶
type ConnectionCredentials ¶
type Model ¶
type Model[T ModelData] struct { CollectionName string PublicFields []string Native func() *mongo.Collection }
func CreateModel ¶ added in v0.1.6
CreateModel - Create a new model with default values Note: Created model will not have a collection and will throw an error if `.Native()` is called
func (*Model[T]) Aggregate ¶ added in v0.1.1
func (coll *Model[T]) Aggregate(pipeline interface{}, opts ...*options.AggregateOptions) ([]bson.M, error)
Aggregate - Aggregate
func (*Model[T]) Count ¶ added in v0.1.1
func (coll *Model[T]) Count(filter interface{}, opts ...*options.CountOptions) (int64, error)
Count - Count documents in database
func (*Model[T]) CountAggregate ¶
func (coll *Model[T]) CountAggregate(pipeline interface{}, opts ...*options.AggregateOptions) (int, error)
CountAggregate - Count aggregate
func (*Model[T]) DeleteOne ¶
func (coll *Model[T]) DeleteOne(filter interface{}, opts ...*options.DeleteOptions) (*mongo.DeleteResult, error)
DeleteOne Delete - Delete model from database
func (*Model[T]) Find ¶ added in v0.1.1
func (coll *Model[T]) Find(filter interface{}, opts ...*options.FindOptions) ([]T, error)
Find - Find documents
func (*Model[T]) FindAs ¶ added in v0.1.1
func (coll *Model[T]) FindAs(result interface{}, filter interface{}, opts ...*options.FindOptions) error
FindAs - Find documents and decode it into a different struct
func (*Model[T]) FindOne ¶
func (coll *Model[T]) FindOne(filter interface{}, opts ...*options.FindOneOptions) (T, error)
FindOne - Find one document and decode it into the same struct
func (*Model[T]) FindOneAs ¶
func (coll *Model[T]) FindOneAs(result interface{}, filter interface{}, opts ...*options.FindOneOptions) error
FindOneAs - Find one document and decode it into a different struct
func (*Model[T]) FindOneAsHelper ¶ added in v0.1.1
func (coll *Model[T]) FindOneAsHelper(filter interface{}, opts ...*options.FindOneOptions) (ModelHelper[T], error)
FindOneAsHelper - Find one document and decode it into the same struct
func (*Model[T]) FindOneById ¶ added in v0.1.1
func (coll *Model[T]) FindOneById(id primitive.ObjectID, opts ...*options.FindOneOptions) (T, error)
FindOneById - Find one document by ID
func (*Model[T]) GetPublicFields ¶
GetPublicFields - Get public fields
func (*Model[T]) GetPublicFieldsAnd ¶ added in v0.1.1
func (coll *Model[T]) GetPublicFieldsAnd(model ModelData, interceptor func(data bson.M) bson.M) bson.M
GetPublicFieldsAnd - Get public fields
func (*Model[T]) Helpers ¶ added in v0.1.1
func (coll *Model[T]) Helpers(model T) ModelHelper[T]
Helpers - get model helper
func (*Model[T]) Paginate ¶ added in v0.1.1
func (coll *Model[T]) Paginate( page int, perPage int, query interface{}, opts ...*options.FindOptions, ) (*Paginated[any], error)
Paginate - Paginate Find
func (*Model[T]) PaginateAggregate ¶
func (coll *Model[T]) PaginateAggregate(page int, perPage int, query []interface{}) (*Paginated[any], error)
PaginateAggregate - Paginate aggregate
func (*Model[T]) ProjectPublicFields ¶
ProjectPublicFields - Project public fields
func (*Model[T]) ProjectPublicFieldsAnd ¶
ProjectPublicFieldsAnd - Project public fields including some keys
func (*Model[T]) ProjectPublicFieldsWithout ¶
ProjectPublicFieldsWithout - Project public fields excluding some keys
func (*Model[T]) UpdateOne ¶
func (coll *Model[T]) UpdateOne(filter interface{}, update interface{}, opts ...*options.UpdateOptions) (*mongo.UpdateResult, error)
UpdateOne - Update model in database
type ModelHelper ¶ added in v0.1.1
ModelHelper - A helper for models, this struct includes all the functions for a model instance
func GetModelHelper ¶ added in v0.1.1
func GetModelHelper[T ModelData](model Model[T], data T) ModelHelper[T]
GetModelHelper - Get a model helper for a model instance
func (ModelHelper[T]) Delete ¶ added in v0.1.1
func (m ModelHelper[T]) Delete() (*mongo.DeleteResult, error)
Delete - Delete a model instance
func (ModelHelper[T]) GetID ¶ added in v0.1.1
func (m ModelHelper[T]) GetID() primitive.ObjectID
GetID - Get the ID of a model instance
func (ModelHelper[T]) GetPublicFields ¶ added in v0.1.1
func (m ModelHelper[T]) GetPublicFields() bson.M
GetPublicFields - Get the public fields of a model instance
func (ModelHelper[T]) Update ¶ added in v0.1.1
func (m ModelHelper[T]) Update(set bson.M) (*mongo.UpdateResult, error)
Update - Update a model instance
func (ModelHelper[T]) UpdateRaw ¶ added in v0.1.1
func (m ModelHelper[T]) UpdateRaw(update bson.M) (*mongo.UpdateResult, error)
UpdateRaw - Update a model instance with raw data
type Paginated ¶
type Paginated[T any] struct { Meta PaginatedMeta `json:"meta"` Data T `json:"data"` }