mqb

package module
v0.0.0-...-dd85316 Latest Latest
Warning

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

Go to latest
Published: Oct 27, 2015 License: Apache-2.0 Imports: 12 Imported by: 0

README

mqb GoDoc

MongoDB Query Builder

Documentation

Overview

Package mqb creates mgo queries from HTTP requests.

Let's say you have a collection represented by the following type:

type Person struct {
   Name string
   Age  int
}

then this package creates from a http request with parameters like:

/?name=peter&age=10&field=name&limit=10&offset=2&sort=-name&sort=age

a mgo query like:

s := session.DB("dbname")
q := s.C("people").Find(bson.M{"name": bson.RegExp{Pattern: "peter", Options: ""}, "age": 10}).Select(bson.M{"name": 1}).Limit(10).Skip(2).Sort("-name", "age")

Index

Constants

This section is empty.

Variables

View Source
var (
	DefaultPageSize uint = 20 // DefaultPageSize defines how many elements a page contains per default.
)

Functions

This section is empty.

Types

type MongoQuery

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

MongoQuery can be used to to create mgo.Query from http request parameters.

func NewMongoQuery

func NewMongoQuery(endPointStruct interface{}, database *mgo.Database) *MongoQuery

NewMongoQuery returns a new MongoQuery.

func (*MongoQuery) AddOrOverwriteValidParameter

func (mq *MongoQuery) AddOrOverwriteValidParameter(name string, value reflect.Kind)

AddOrOverwriteValidParameter adds or overwrites a valid parmeter with name and reflect.Kind.

func (*MongoQuery) CreateQuery

func (mq *MongoQuery) CreateQuery(req *http.Request) (*mgo.Query, error)

CreateQuery creates a mgo.Query from a HTTP Request for a collection represented by endpointStruct.

Examples:

mq := NewMongoQuery(People{}, db)
q, _ := mq.CreateQuery(req) // creates a query from the request for the people collection

mq.DisableParameters("name", "sort")
q, _ := mq.CreateQuery(req) // creates a query from the request for the people collection with the parameters "name" and "sort" disabled.

func (*MongoQuery) DisableParameters

func (mq *MongoQuery) DisableParameters(paramters ...string)

DisableParameters disables paramters. If a URL query contains any of those paramters, an error is returned.

func (*MongoQuery) Run

func (mq *MongoQuery) Run(req *http.Request) (*Response, error)

Run runs the query on the database and returns a *Response.

type Page

type Page struct {
	Size    uint `json:"size"`    // Size defines how many elements a page contains.
	Items   uint `json:"items"`   // Items defines the total number of items the corresponding query returns.
	Last    uint `json:"last"`    // Last represents total number of pages a query generates (depends on the page size and the total number of elements returned by the query).
	Current uint `json:"current"` // Current is the current page nuber for the query.
}

Page the paging information.

type Response

type Response struct {
	Content interface{} `json:"content,omitempty"`
	Page    Page        `json:"page"`
}

Response contains the result of the query, including the Page information.

Jump to

Keyboard shortcuts

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