orm

package
v1.4.13 Latest Latest
Warning

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

Go to latest
Published: Apr 17, 2023 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Connect added in v1.4.10

func Connect(ctx context.Context) *mongo.Client

*

  • @title connect mongodb server

func Disconnect added in v1.4.10

func Disconnect(ctx context.Context)

*

  • @title disconnect mongodb server

func Setup added in v1.3.8

func Setup(db, host, port, password string)

setup mongodb connect config

Types

type Eloquent

type Eloquent[t any] struct {
	Collection string
	// contains filtered or unexported fields
}

func NewEloquent

func NewEloquent[T any](collection string) *Eloquent[T]

func (*Eloquent[T]) All

func (e *Eloquent[T]) All(ctx context.Context, opts ...*options.FindOptions) (models []*T, err error)

*

  • @title get all document from collection
  • @return models []*T your model slice
  • @return err error fail message from query

func (*Eloquent[T]) Count

func (e *Eloquent[T]) Count(ctx context.Context, filter any) (count int, err error)

*

  • @title counter document
  • @param filter any you can use struct, bson,etc .., or nil
  • @return count int filter document count
  • @return err error fail message from query

func (*Eloquent[T]) Delete

func (e *Eloquent[T]) Delete(ctx context.Context, id string) (deleteCount int, err error)

*

  • @title delete a document
  • @param id string _id of document
  • @return deleteCount int delete document count
  • @return err error fail message from query

func (*Eloquent[T]) DeleteMultiple

func (e *Eloquent[T]) DeleteMultiple(ctx context.Context, filter any) (deleteCount int, err error)

*

  • @title delete Multiple document
  • @param filter any ex:bson.M{}, struct, bson.D{}
  • @return deleteCount int delete document count
  • @return err error fail message from query

func (*Eloquent[T]) Find

func (e *Eloquent[T]) Find(ctx context.Context, id string) (model *T, err error)

*

  • @title find a document by _id
  • @param id string _id of document
  • @return model struct your model struct
  • @return err error fail message from query

func (*Eloquent[T]) FindMultiple

func (e *Eloquent[T]) FindMultiple(ctx context.Context, filter any, opts ...*options.FindOptions) (models []*T, err error)

*

  • @title find multiple document
  • @param filter any
  • @return models []*T your model slice
  • @return err error fail message from query

func (*Eloquent[T]) GetCollection

func (e *Eloquent[T]) GetCollection() *mongo.Collection

*

  • @title get collection instance

func (*Eloquent[T]) Insert

func (e *Eloquent[T]) Insert(ctx context.Context, data *T) (insertedID string, err error)

*

  • @title insert a document
  • @param data *T your model struct
  • @return insertedID string _id of mongodb
  • @return err error fail message from query

func (*Eloquent[T]) InsertMultiple

func (e *Eloquent[T]) InsertMultiple(ctx context.Context, data []*T) (InsertedIDs []string, err error)

*

  • @title insert multiple document
  • @param data []*T{} your model slice
  • @return InsertedIDs []string _id of mongodb
  • @return err error fail message from query

func (*Eloquent[T]) Paginate

func (e *Eloquent[T]) Paginate(ctx context.Context, limit int, page int, filter any) (paginated *Pagination[T], err error)

*

  • @title create pagination for your model data
  • @param limit int how many data display in each page. default=10
  • @param page int choose page for pagination. default=1
  • @param filter any you can use struct, bson,etc ... . but reject pass nil to filter
  • @return pagination *pagination[T]
  • @return err error fail message from query

func (*Eloquent[T]) Update

func (e *Eloquent[T]) Update(ctx context.Context, id string, data *T) (modifiedCount int, err error)

*

  • @title update a document
  • @param id string _id of mongodb
  • @return modifiedCount int modified document count
  • @return err error fail message from query

func (*Eloquent[T]) UpdateMultiple

func (e *Eloquent[T]) UpdateMultiple(ctx context.Context, filter any, data *T) (modifiedCount int, err error)

*

  • @title update multiple document
  • @param filter any ex:struct, bson
  • @return modifiedCount int modified document count
  • @return err error fail message from query

type IEloquent

type IEloquent[T any] interface {
	GetCollection() *mongo.Collection
	All(ctx context.Context, opts ...*options.FindOptions) (models []*T, err error)
	Find(ctx context.Context, id string) (model *T, err error)
	FindMultiple(ctx context.Context, filter any, opts ...*options.FindOptions) (models []*T, err error)
	Insert(ctx context.Context, data *T) (insertedID string, err error)
	InsertMultiple(ctx context.Context, data []*T) (InsertedIDs []string, err error)
	Delete(ctx context.Context, id string) (deleteCount int, err error)
	DeleteMultiple(ctx context.Context, filter any) (deleteCount int, err error)
	Update(ctx context.Context, id string, data *T) (modifiedCount int, err error)
	UpdateMultiple(ctx context.Context, filter any, data *T) (modifiedCount int, err error)
	Count(ctx context.Context, filter any) (count int, err error)
	Paginate(ctx context.Context, limit int, page int, filter any) (paginated *Pagination[T], err error)
}

type Pagination

type Pagination[T any] struct {
	Total       int  `json:"total"`
	PerPage     int  `json:"per_page"`
	CurrentPage int  `json:"current_page"`
	LastPage    int  `json:"last_page"`
	From        int  `json:"from"`
	To          int  `json:"to"`
	Data        []*T `json:"data"`
}

Jump to

Keyboard shortcuts

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