mongo

package module
v0.1.1 Latest Latest
Warning

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

Go to latest
Published: Dec 7, 2023 License: MIT Imports: 13 Imported by: 0

Documentation

Index

Constants

View Source
const Tag = "db"

Variables

View Source
var (
	ErrInvalidModelName = errors.New("invalid model name")
	ErrNoID             = errors.New(`no id. not found primary key from model, defined by tag db:"pk" or bson:"_id"`)
	ErrRecordNotFound   = errors.New("record not found")
)

Functions

func GetID added in v0.0.8

func GetID(model any) any

func GetIdFilter

func GetIdFilter(id any) any

func GetModelName

func GetModelName(model any) string

func NewModelType

func NewModelType(model any) any

func ParseModelIndex

func ParseModelIndex(model any) (modelName string, indexes map[string]bool)

func ParseTag

func ParseTag(tag string) map[string]string

func Pointer

func Pointer[T any](v T) *T

func RandInRange added in v0.0.4

func RandInRange(minInclusive, maxExclusive int) int

RandInRange returns a random positive integer from an inclusive minimum to an exclusive maximum

func SequentialID added in v0.0.4

func SequentialID() string

func ToEntities

func ToEntities[T any](items []M) []*T

func ToEntity

func ToEntity[T any](m M) *T

func ToSnake

func ToSnake(text string) string

Types

type Client

type Client struct {
	*mongo.Client
}

func NewClient

func NewClient(url string) *Client

func (*Client) Close

func (c *Client) Close()

type Database

type Database struct {
	*mongo.Database
	// contains filtered or unexported fields
}

func NewDatabase

func NewDatabase(url string, name string) *Database

func (*Database) Close

func (d *Database) Close()

func (*Database) Indexes added in v0.0.13

func (d *Database) Indexes(ctx context.Context, models ...any) error

func (*Database) Txn

func (d *Database) Txn(ctx context.Context, fn func(txn *Txn) error, multiDoc ...bool) error

By default, MongoDB will automatically abort any multi-document transaction that runs for more than 60 seconds.

type M

type M bson.M

func Map

func Map() M

func (M) Del

func (m M) Del(k string) M

func (M) Get

func (m M) Get(k string) (any, bool)

func (M) Set

func (m M) Set(k string, v any) M

type Model

type Model struct {
	// contains filtered or unexported fields
}

func NewModel

func NewModel(txn *Txn, model any) *Model

func (*Model) Count added in v0.0.2

func (m *Model) Count(filter any) (count int64, err error)

func (*Model) Del

func (m *Model) Del(id any) error

func (*Model) First added in v0.0.5

func (m *Model) First(filter, sort any, projection ...any) (M, error)

func (*Model) Get added in v0.0.7

func (m *Model) Get(id any, projection ...any) (M, error)

func (*Model) Has added in v0.0.3

func (m *Model) Has(id any) (bool, error)

func (*Model) Inc added in v0.0.4

func (m *Model) Inc(id, fields any) error

func (*Model) List

func (m *Model) List(filter M, size int64, cb func(m M, total int64) (bool, error), projection ...any) error

`cb` return `false` will stop iterate

func (*Model) Next added in v0.1.0

func (m *Model) Next(filter, sort M, lastID string, pageSize int64, projection ...any) (list []M, err error)

func (*Model) Pagination

func (m *Model) Pagination(filter, sort any, page, pageSize int64, projection ...any) (total int64, list []M, err error)

func (*Model) Set

func (m *Model) Set(model any) error

func (*Model) Unmarshal

func (m *Model) Unmarshal(id, model any, projection ...any) error

func (*Model) Update

func (m *Model) Update(update any) (newRecord M, err error)

parameter 'update' can be a structure or a Map containing the primary key

type Txn

type Txn struct {
	// contains filtered or unexported fields
}

func (*Txn) Model

func (txn *Txn) Model(model any) *Model

Jump to

Keyboard shortcuts

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