database

package
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Sep 10, 2022 License: MIT Imports: 15 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPageNumber  int64 = 1
	DefaultPerPageRows int64 = 20
)

Functions

This section is empty.

Types

type Client added in v1.0.1

type Client struct {
	Connection *mongo.Database
}
var (
	Instance *Client
)

func Init

func Init(mongoDSN, databaseName string, opts ...*options.ClientOptions) *Client

func (*Client) Aggregate added in v1.0.1

func (c *Client) Aggregate(ctx context.Context, collection string, pipeline mongo.Pipeline, aggregateOptions *options.AggregateOptions) (*mongo.Cursor, error)

Aggregate runs a simple aggregation pipeline and returns a cursor if successful or error if any. If no aggregation options are provided, allowDiskUse is set to true by default.

func (*Client) CountDocuments added in v1.0.1

func (c *Client) CountDocuments(ctx context.Context, collection string, filters interface{}) (int, error)

CountDocuments returns a count of all the documents that match the provided filters or error otherwise

func (*Client) FindAll added in v1.0.1

func (c *Client) FindAll(ctx context.Context, collection string, filters []bson.E, projection interface{}, sort bson.D) (*mongo.Cursor, error)

FindAll - returns a list of all the document that match the filter or returns an error. To be used with care as a lot of document could be returned and use up a lot of memory.

func (*Client) FindLast added in v1.0.1

func (c *Client) FindLast(ctx context.Context, collection string, filters []bson.E, projection, target interface{}) error

FindLast returns the most recent document in the collection that matches the provided filters. It sorts based on the mongo objectId

func (*Client) FindLastN added in v1.0.1

func (c *Client) FindLastN(ctx context.Context, collection string, limit int, filters []bson.E, projection interface{}) (*mongo.Cursor, error)

FindLastN returns the N (limit) most recent documents in the collection that matches the provided filters. It sorts based on provided mongo objectId

func (*Client) FindOne added in v1.0.1

func (c *Client) FindOne(ctx context.Context, collection string, filters []bson.E, projection, target interface{}, findOneOptions ...*options.FindOneOptions) error

FindOne searches for a single document that matches the provided filters. If projection is nil, all fields are returned. To specify only select fields, use a bson.M - eg: bson.M{"email":1, "phone":1} Target has to be a pointer to a struct where the document will be unmarshalled into.

func (*Client) FindOneByField added in v1.0.1

func (c *Client) FindOneByField(ctx context.Context, collection, field string, value, projection, target interface{}) error

FindOneByField finds a document that matches the provided field and value pair. If projection is nil, all fields are returned. To specify only select fields, use a bson.M - eg: bson.M{"email":1, "phone":1} Target has to be a pointer to a struct where the document will be unmarshalled into.

func (*Client) FindOneByID added in v1.0.1

func (c *Client) FindOneByID(ctx context.Context, collection string, id primitive.ObjectID, projection, target interface{}) error

FindOneByID finds a document that matches the provided ID in the collection. If projection is nil, all fields are returned. To specify only select fields, use a bson.M - eg: bson.M{"email":1, "phone":1} Target has to be a pointer to a struct where the document will be unmarshalled into.

func (*Client) FindPaginated added in v1.0.1

func (c *Client) FindPaginated(ctx context.Context, collection string, pageOptions PageOpts, filters []bson.E, projection interface{}, sort bson.D) (*PaginatedResult, error)

FindPaginated searches for document that matches the provided filters. PageOpts control CurrentPage and PerPage value. If projection is nil, all fields are returned. To specify only select fields, use a bson.M - eg: bson.M{"email":1, "phone":1} sort should be a bson.D - eg: bson.D{bson.E{Key: "_id", Value: -1}, bson.E{Key: "another, Value: "value"}} FindPaginated will return the Mongo Cursor in the PaginatedResult struct. REMEMBER TO CALL Cursor.Close(ctx) WHEN DONE READING

func (*Client) HardDeleteDocument added in v1.0.1

func (c *Client) HardDeleteDocument(ctx context.Context, collection string, doc interface{}) (*mongo.DeleteResult, error)

HardDeleteDocument deletes a record from the DB. Careful with this as the document is irrecoverable. Use SoftDeleteDocument() instead except you want the document truly gone.

func (*Client) SaveDocument added in v1.0.1

func (c *Client) SaveDocument(ctx context.Context, collection string, doc interface{}) (interface{}, error)

SaveDocument will create a new document or update an existing document if doc is not new. if doc is new and implements the PreCreator interface, the PreCreate hook will fire or return appropriate error. if doc is new and implements the PostCreator interface, the PostCreate hook will fire or return appropriate error. if doc is existing and implements the PreUpdater interface, the PreUpdate hook will fire or return appropriate error. if doc is new and implements the PostUpdater interface, the PostUpdate hook will fire or return appropriate error.

func (*Client) SoftDeleteDocument added in v1.0.1

func (c *Client) SoftDeleteDocument(ctx context.Context, collection string, doc interface{}) (*mongo.SingleResult, error)

SoftDeleteDocument marks a document as deleted and sets the deleted timestamp. This does not remove the item from the DB but it hides it from future queries except deleted records is added to the filters

func (*Client) UpdateMany added in v1.0.1

func (c *Client) UpdateMany(ctx context.Context, collection string, filters []bson.E, updateBuilder *builder.UpdateManyBuilder, updateOptions *options.UpdateOptions) (*mongo.UpdateResult, error)

UpdateMany finds the documents that match the filter and update them based on the operators configured in the UpdateManyBuilder

type PageOpts

type PageOpts struct {
	Page    int64 `json:"page"`
	PerPage int64 `json:"per_page"`
}

type PaginatedResult

type PaginatedResult struct {
	Paginator
	Cursor *mongo.Cursor
}

type Paginator

type Paginator struct {
	CurrentPage int64 `json:"currentPage"`
	NextPage    int64 `json:"nextPage"`
	PrevPage    int64 `json:"prevPage"`
	TotalPages  int64 `json:"totalPages"`
	TotalRows   int64 `json:"totalRows"`
	PerPage     int64 `json:"perPage"`
	Offset      int64 `json:"-"`
}

func NewPaginator

func NewPaginator(opts PageOpts) *Paginator

func (*Paginator) SetNextPage

func (p *Paginator) SetNextPage()

func (*Paginator) SetOffset

func (p *Paginator) SetOffset()

func (*Paginator) SetPrevPage

func (p *Paginator) SetPrevPage()

func (*Paginator) SetTotalPages

func (p *Paginator) SetTotalPages()

Jump to

Keyboard shortcuts

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