db

package
v0.0.0-...-071ea55 Latest Latest
Warning

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

Go to latest
Published: Sep 4, 2020 License: GPL-3.0 Imports: 8 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type AggregateResponse

type AggregateResponse struct {
	ID              interface{} `json:"_id"`
	MinPublications int         `json:"min_publications"`
}

AggregateResponse has the output from a aggregation.

type FindResponse

type FindResponse struct {
	Results []bson.M `json:"results,omitempty"`
	Errors  string   `json:"errors,omitempty"`
}

FindResponse returns the data found in database.

type HealthResponse

type HealthResponse struct {
	Databases []string `json:"databases"`
}

HealthResponse shows the databases available.

type HomeResponse

type HomeResponse struct {
	Hello string `json:"hello"`
}

HomeResponse is just a dummy JSON to indicate the server is up.

type InsertResponse

type InsertResponse struct {
	InsertedID interface{} `json:"InsertedID"`
}

InsertResponse gives the ObjectID of the inserted data.

type MongoDBProxy

type MongoDBProxy struct {
	URI string
	// contains filtered or unexported fields
}

MongoDBProxy manages everything related to MongoDB connection, queries etc.

func (*MongoDBProxy) Aggregate

func (m *MongoDBProxy) Aggregate(dbName, collName string, filter interface{}) (*AggregateResponse, error)

Aggregate will compare all entries in a collection and returns the consolidated data..

func (*MongoDBProxy) DBWrapperFunc

func (m *MongoDBProxy) DBWrapperFunc(db, clt string, req []byte,
	f func(ctx context.Context, c *mongo.Client, db, clt string, req []byte) ([]byte, error)) ([]byte, error)

DBWrapperFunc is responsible to setup and clean up database connections. You should bind this function to the routes in the API, and pass the particular func as parameter. func (m *MongoDBProxy) DBWrapperFunc(f func(ctx context.Context, c *mongo.Client) ([]string, error)) ([]string, error) {

func (*MongoDBProxy) Find

func (m *MongoDBProxy) Find(dbName, collName string, filter interface{}) (*FindResponse, error)

Find will fetch all documents that match filter. Find("okr", "okr_coll", []byte(`{ "id": 1 }`), *client, ctx)

func (*MongoDBProxy) GetURI

func (m *MongoDBProxy) GetURI() string

GetURI returns the URI for the database connection.

func (*MongoDBProxy) HealthCheck

func (m *MongoDBProxy) HealthCheck() (*HealthResponse, error)

HealthCheck will return the existing databases if connection is OK.

func (*MongoDBProxy) Insert

func (m *MongoDBProxy) Insert(dbName, collName string, entry Quote) (*InsertResponse, error)

Insert will create a new document in collection collName in database dbName. Insert("okr", "okr_coll", []byte(`{"id": 1,"name": "A green door","price": 12.50,"tags": ["home", "green"]}`), *client, ctx)

func (*MongoDBProxy) Update

func (m *MongoDBProxy) Update(database, collection string, filter, entry interface{}) (*UpdateResponse, error)

Update will modify the fields defined in update in all documents that match filter.

type Proxy

type Proxy interface {
	GetURI() string
	HealthCheck() (*HealthResponse, error)
	Aggregate(database, collection string, filter interface{}) (*AggregateResponse, error)
	Insert(database, collection string, entry Quote) (*InsertResponse, error)
	Find(database, collection string, filter interface{}) (*FindResponse, error)
	Update(database, collection string, filter, entry interface{}) (*UpdateResponse, error)
}

Proxy is the abstraction of what you can do with the database.

func NewConnection

func NewConnection(hostname string, port int, username, password string) (Proxy, error)

NewConnection instantiates the MongoDB proxy connector (client, context etc.)

type Quote

type Quote struct {
	Publications    int    `json:"publications,omitempty" bson:"publications,omitempty"`
	LastPublished   int64  `json:"last_published,omitempty" bson:"last_published,omitempty"`
	OriginalTitle   string `json:"original_title"`
	OriginalQuote   string `json:"original_quote"`
	TranslatedTitle string `json:"translated_title"`
	TranslatedQuote string `json:"translated_quote"`
	Author          string `json:"author"`
}

Quote represents the central collection of the solution, where the quotes used by the Twitter bot is used.

type UpdateResponse

type UpdateResponse struct {
	Results *mongo.UpdateResult `json:"results"`
}

UpdateResponse gives the result of the update.

Jump to

Keyboard shortcuts

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