orm

package
v1.3.8 Latest Latest
Warning

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

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

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

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(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]) Close

func (e *Eloquent[T]) Close(client *mongo.Client)

*

  • @title Close connect

func (*Eloquent[T]) Connect

func (e *Eloquent[T]) Connect() (client *mongo.Client)

*

  • @title creates a new Client connect

func (*Eloquent[T]) Count

func (e *Eloquent[T]) Count(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(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(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(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(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(client *mongo.Client) *mongo.Collection

*

  • @title get collection instance

func (*Eloquent[T]) Insert

func (e *Eloquent[T]) Insert(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(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(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(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(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 {
	Connect() (client *mongo.Client)
	Close(client *mongo.Client)
	GetCollection(client *mongo.Client) *mongo.Collection
	All(opts ...*options.FindOptions) (models []*T, err error)
	Find(id string) (model *T, err error)
	FindMultiple(filter any, opts ...*options.FindOptions) (models []*T, err error)
	Insert(data *T) (insertedID string, err error)
	InsertMultiple(data []*T) (InsertedIDs []string, err error)
	Delete(id string) (deleteCount int, err error)
	DeleteMultiple(filter any) (deleteCount int, err error)
	Update(id string, data *T) (modifiedCount int, err error)
	UpdateMultiple(filter any, data *T) (modifiedCount int, err error)
	Count(filter any) (count int, err error)
	Paginate(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