database

package
v2.0.10 Latest Latest
Warning

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

Go to latest
Published: Dec 6, 2020 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func NewDatabase

func NewDatabase(client *mongox.Client, dbname string) (db mongox.Database)

NewDatabase function creates new database instance with mongo client and empty context

Types

type Database

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

Database handler

func (*Database) Client

func (d *Database) Client() (client *mongox.Client)

Client function returns a mongo client

func (*Database) Context

func (d *Database) Context() (ctx context.Context)

Context function returns a context

func (*Database) Count

func (d *Database) Count(target interface{}, filters ...interface{}) (result int64, err error)

Count function counts documents in the database by query target is used only to get collection by tag so it'd be better to use nil ptr here

func (*Database) DeleteArray

func (d *Database) DeleteArray(target interface{}, filters ...interface{}) (err error)

DeleteArray removes documents list from a database by their ids

func (*Database) DeleteOne

func (d *Database) DeleteOne(target interface{}, filters ...interface{}) (err error)

DeleteOne removes a document from a database and then returns it into target

func (*Database) GetCollectionOf

func (d *Database) GetCollectionOf(document interface{}) (collection *mongox.Collection)

GetCollectionOf returns the collection object by the «collection» tag of the given document; the «collection» tag should exists, e.g.:

type Foobar struct {
    base.ObjectID `bson:",inline" json:",inline" collection:"foobars"`
	   ...

Will panic if there is no «collection» tag

func (*Database) IndexEnsure

func (d *Database) IndexEnsure(cfg interface{}, document interface{}) (err error)

IndexEnsure function ensures index in mongo collection of document

`index:""` -- https://docs.mongodb.com/manual/indexes/#create-an-index
`index:"-"` -- (descending)
`index:"-,+foo,+-bar"` -- https://docs.mongodb.com/manual/core/index-compound
`index:"-,+foo,+-bar,unique"` -- https://docs.mongodb.com/manual/core/index-unique
`index:"-,+foo,+-bar,unique,allowNull"` -- https://docs.mongodb.com/manual/core/index-partial
`index:"-,unique,allowNull,expireAfter=86400"` -- https://docs.mongodb.com/manual/core/index-ttl
`index:"-,unique,allowNull,expireAfter={{.Expire}}"` -- evaluate index as a golang template with `cfg` arguments

func (*Database) LoadArray

func (d *Database) LoadArray(target interface{}, filters ...interface{}) (err error)

LoadArray loads an array of documents from the database by query

func (*Database) LoadOne

func (d *Database) LoadOne(target interface{}, filters ...interface{}) (err error)

LoadOne function loads a first single target document by a query

func (*Database) LoadStream

func (d *Database) LoadStream(target interface{}, filters ...interface{}) (loader mongox.StreamLoader, err error)

LoadStream function loads documents one by one into a target channel

func (*Database) Name

func (d *Database) Name() (name string)

Name function returns a database name

func (*Database) New

func (d *Database) New(ctx context.Context) (db mongox.Database)

New function creates new database context with same client

func (*Database) SaveOne

func (d *Database) SaveOne(source interface{}, filters ...interface{}) (err error)

SaveOne saves a single source document to the database

func (*Database) UpdateOne added in v2.0.9

func (d *Database) UpdateOne(target interface{}, filters ...interface{}) (err error)

UpdateOne updates a single document in the database and loads it into target

type StreamLoader

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

StreamLoader is a controller for a database cursor

func (*StreamLoader) Close

func (l *StreamLoader) Close() (err error)

Close stream loader and the underlying cursor

func (*StreamLoader) Cursor

func (l *StreamLoader) Cursor() (cursor *mongox.Cursor)

Cursor returns the underlying cursor

func (*StreamLoader) DecodeMsg added in v2.0.10

func (l *StreamLoader) DecodeMsg(i interface{}) (err error)

DecodeMsg decodes the current cursor document into an interface

func (*StreamLoader) DecodeNextMsg added in v2.0.10

func (l *StreamLoader) DecodeNextMsg(i interface{}) (err error)

DecodeNextMsg decodes the next document to an interface or returns an error

func (*StreamLoader) Err

func (l *StreamLoader) Err() (err error)

Err returns the last error

func (*StreamLoader) Next

func (l *StreamLoader) Next() (err error)

Next loads next documents but doesn't perform decoding

Jump to

Keyboard shortcuts

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