gmongo

package module
v0.1.9 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Apr 27, 2024 License: MIT Imports: 14 Imported by: 0

README

gmongo

My personal MongoDB Util package for Go.

Installation

go get github.com/trapcodeio/gmongo

Usage

For now, see the tests for usage examples.

Test

Make sure you have a MongoDB instance running on localhost:27017

go test -v

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

func DateTimeNow() primitive.DateTime

DateTimeNow - Get the current date and time

func IsFindOneError added in v0.1.1

func IsFindOneError(err error) bool

IsFindOneError - Check if the error exists but is not a mongo.ErrNoDocuments error

func IsNoDocumentsError added in v0.1.1

func IsNoDocumentsError(err error) bool

IsNoDocumentsError - Check if the error exists and is a mongo.ErrNoDocuments error

func LinkModel added in v0.1.6

func LinkModel[T ModelData](model *Model[T], db *mongo.Database)

LinkModel - Link model to a database

func NewId

func NewId() primitive.ObjectID

NewId - Generate a new ID

func NewUUid

func NewUUid() string

NewUUid - Generate a new UUID

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 ConnectUsingString(connectionString string, database string) (*Client, error)

func (*Client) IsConnected

func (c *Client) IsConnected() bool

type ConnectionCredentials

type ConnectionCredentials struct {
	DbServer   string
	DbName     string
	DbPassword string
}

type Model

type Model[T ModelData] struct {
	CollectionName string
	PublicFields   []string
	Native         func() *mongo.Collection
}

func CreateModel added in v0.1.6

func CreateModel[T ModelData](collectionName string) *Model[T]

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 MakeModel

func MakeModel[T ModelData](db *mongo.Database, collectionName string) Model[T]

MakeModel - Create a new model with a database connection

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]) Exists added in v0.1.4

func (coll *Model[T]) Exists(filter interface{}) (bool, error)

Exists - Check if document exists

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

func (coll *Model[T]) GetPublicFields(model ModelData) bson.M

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]) Pick

func (coll *Model[T]) Pick(data ModelData, keys []string) bson.M

func (*Model[T]) ProjectPublicFields

func (coll *Model[T]) ProjectPublicFields() bson.M

ProjectPublicFields - Project public fields

func (*Model[T]) ProjectPublicFieldsAnd

func (coll *Model[T]) ProjectPublicFieldsAnd(keys []string) bson.M

ProjectPublicFieldsAnd - Project public fields including some keys

func (*Model[T]) ProjectPublicFieldsWithout

func (coll *Model[T]) ProjectPublicFieldsWithout(keys []string) bson.M

ProjectPublicFieldsWithout - Project public fields excluding some keys

func (*Model[T]) ToBsonMap

func (coll *Model[T]) ToBsonMap(data ModelData) bson.M

func (*Model[T]) ToJsonMap

func (coll *Model[T]) ToJsonMap(data ModelData) bson.M

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 ModelData

type ModelData interface {
	// GetID - A function that returns the ID of the model
	GetID() primitive.ObjectID
}

type ModelHelper added in v0.1.1

type ModelHelper[T ModelData] struct {
	Data  T
	Model func() Model[T]
}

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"`
}

type PaginatedMeta

type PaginatedMeta struct {
	Total    int `json:"total"`
	PerPage  int `json:"perPage"`
	Page     int `json:"page"`
	LastPage int `json:"lastPage"`
}

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL