mongo

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 30, 2024 License: MIT Imports: 16 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 ParseTLSConfig added in v0.2.0

func ParseTLSConfig(pemFile []byte) (*tls.Config, error)

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(connectionURI string, opts ...func(c *ClientOptions)) *Client

func (*Client) Close

func (c *Client) Close() error

type ClientOptions added in v0.2.1

type ClientOptions options.ClientOptions

type Database

type Database struct {
	*Client
	*mongo.Database
}

func NewDatabase

func NewDatabase(url string, name string, opts ...func(c *ClientOptions)) *Database

func (*Database) Close

func (d *Database) Close()

func (*Database) Count added in v0.2.0

func (o *Database) Count(model, filter any) (count int64, err error)

func (*Database) Delete added in v0.2.0

func (o *Database) Delete(model any, id string) error

func (*Database) First added in v0.2.0

func (o *Database) First(model, filter, sort any, projection ...any) (record M, err error)

func (*Database) Has added in v0.2.0

func (o *Database) Has(model, id any) (exists bool, err error)

func (*Database) Indexes added in v0.0.13

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

func (*Database) List added in v0.2.0

func (o *Database) List(ctx context.Context, model any, filter M, cb func(m M) (bool, error), projection ...any) error

func (*Database) Pagination added in v0.2.0

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

func (*Database) Set added in v0.2.0

func (o *Database) Set(record 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.

func (*Database) Unmarshal added in v0.2.0

func (o *Database) Unmarshal(id, model any, projection ...any) error

func (*Database) Update added in v0.2.0

func (o *Database) Update(record any) (newRecord M, err error)

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, cb func(m M) (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