db

package
v0.0.0-...-dfd9f30 Latest Latest
Warning

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

Go to latest
Published: Jan 15, 2020 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Overview

Package db provides tools for using MongoDB databases. However, it wraps mgo types in interfaces that the anser/mocks package provides mocked implementations of for testing facility.

In general, these types are fully functional for most application uses, but do not expose some of the configurability that the mgo equivalents do.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ResolveCursorOne

func ResolveCursorOne(ctx context.Context, iter *mongo.Cursor, result interface{}) error

ResolveCursorOne decodes the first result in a cursor, for use in "FindOne" cases.

func ResultsNotFound

func ResultsNotFound(err error) bool

Types

type Bulk

type Bulk interface {
	Insert(...interface{})
	Remove(...interface{})
	RemoveAll(...interface{})
	Update(...interface{})
	UpdateAll(...interface{})
	Upsert(...interface{})
	Unordered()
	Run() (*BulkResult, error)
}

type BulkResult

type BulkResult struct {
	Matched  int
	Modified int
}

type Change

type Change struct {
	Update    interface{} // The update document
	Upsert    bool        // Whether to insert in case the document isn't found
	Remove    bool        // Whether to remove the document found rather than updating
	ReturnNew bool        // Should the modified document be returned rather than the old one
}

Change represents the options that you can pass to the findAndModify operation.

type ChangeInfo

type ChangeInfo struct {
	Updated    int         // Number of existing documents updated
	Removed    int         // Number of documents removed
	UpsertedId interface{} // Upserted _id field, when not explicitly provided
}

ChangeInfo represents the data returned by Update and Upsert documents. This type mirrors the mgo type.

type Collection

type Collection interface {
	DropCollection() error
	Pipe(interface{}) Results
	Find(interface{}) Query
	FindId(interface{}) Query
	Count() (int, error)
	Insert(...interface{}) error
	Upsert(interface{}, interface{}) (*ChangeInfo, error)
	UpsertId(interface{}, interface{}) (*ChangeInfo, error)
	Update(interface{}, interface{}) error
	UpdateId(interface{}, interface{}) error
	UpdateAll(interface{}, interface{}) (*ChangeInfo, error)
	Remove(interface{}) error
	RemoveId(interface{}) error
	RemoveAll(interface{}) (*ChangeInfo, error)
	Bulk() Bulk
}

Collection provides access to the common query functionality of the mgo.Collection type.

type CombinedCloser

type CombinedCloser struct {
	Iterator
	// contains filtered or unexported fields
}

func (CombinedCloser) Close

func (c CombinedCloser) Close() error

type Database

type Database interface {
	Name() string
	C(string) Collection
	DropDatabase() error
}

Database provides a very limited subset of the mgo.DB type.

type Document

type Document map[string]interface{}

Document is, like bson.M, a wrapper for an un-ordered map type

type Iterator

type Iterator interface {
	Next(interface{}) bool
	Close() error
	Err() error
}

Iterator is a more narrow subset of mgo's Iter type that provides the opportunity to mock results, and avoids a strict dependency between mgo and migrations definitions.

func NewCombinedIterator

func NewCombinedIterator(ses Session, iter Iterator) Iterator

NewCombinedIterator produces a DocumentIterator that is an mgo.Iter, with a modified Close() method that also closes the provided mgo session after closing the iterator.

type MigrationOperation

type MigrationOperation func(Session, bson.RawD) error

MigrationOperation defines the function object that performs the transformation in the manual migration migrations. Register these functions using RegisterMigrationOperation.

Implementors of MigrationOperations are responsible for implementing idempotent operations.

type Processor

type Processor interface {
	Load(Session, model.Namespace, map[string]interface{}) Iterator
	Migrate(Iterator) error
}

Processor defines the process for processing a stream of documents using an Iterator, which resembles mgo's Iter operation.

type Query

type Query interface {
	Count() (int, error)
	Limit(int) Query
	Select(interface{}) Query
	Skip(n int) Query
	Sort(...string) Query
	Apply(Change, interface{}) (*ChangeInfo, error)
	Results
}

type Results

type Results interface {
	All(interface{}) error
	One(interface{}) error
	Iter() Iterator
}

Results reflect the output of a database operation and is part of the query interface, and is returned by the pipeline (e.g aggregation operation.)

type Session

type Session interface {
	Clone() Session
	Copy() Session
	Close()
	DB(string) Database
	SetSocketTimeout(time.Duration)
	Error() error
}

Session provides a subset of the functionality of the *mgo.Session type.

func WrapClient

func WrapClient(ctx context.Context, client *mongo.Client) Session

WrapClient provides the anser database Session interface, which is modeled on mgo's interface but based on new mongo.Client fundamentals.

func WrapSession

func WrapSession(s *mgo.Session) Session

WrapSession takes an mgo.Session and returns an equivalent session object.

Jump to

Keyboard shortcuts

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