haigo

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

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

Go to latest
Published: Jul 29, 2016 License: MIT Imports: 8 Imported by: 0

README

Circle CI GoDoc

Haigo

YeSQL-like abstraction layer for Mongo on Go

What is Haigo?

Haigo lets you write and use standard MongoDB queries directly from within your Go application.

Why?

While working on a project that leveraged Mongo's aggregation framework extensively, we found that writing complicated queries in Go was painful and brittle with deeply nested bson.M or map[string]interface{} clauses.

With Haigo, you can use the JSON formatted Mongo queries directly.

Usage
  1. Connect to Mongo (as you would normally)

  2. Load Query File: SEE examples/queries.yml for example query files.
    hf, err := haigo.LoadQueryFile("queries.yml")

  3. Configure Haigo Params:
    params := haigo.Params{"name": "Ali", "age": 32}

  4. Execute Query/Pipe:
    q, err := hf.Queries["FindUser"].Pipe(col, params)

  5. Handle result (as you would normally):
    q.Count()

TODO
  • Parse MongoDB Query YAML
  • Detect Queries
  • Replace Query Params
  • Integrate CI
  • More/Better Tests
  • Better Error Handling

Documentation

Overview

Example

Here is an example of Haigo's basic usage.

In this case we're loading the "basic-select" query from the "examples/queries.yml" MongoDB query file and then executing it against the "mycol" collection on "mydb".

"Query" simply returns a mgo.Query struct and as such supports all of the expected methods (in this case we're calling Count().

// Dial MongoDB Server
sess, _ := mgo.Dial("127.0.0.1")
col := sess.DB("mydb").C("mycol")

// Load MongoDB Query File
hf, _ := LoadQueryFile("examples/queries.yml")
params := Params{
	"type": "Good",
}

// Call "basic-select" query.
res, _ := hf.Queries["basic-select"].Query(col, params)

// Returns mgo.Query struct
cnt, _ := res.Count()

fmt.Println(cnt)
Output:

Index

Examples

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type File

type File struct {
	Queries map[string]*Query
}

YAML formatted file with MongoDB Queries.

---
  - name: basic-select
    description: Basic MongoDB Select
    query: '{"type": {{.type}} }'

  - name: conditional
    description: Conditional Query
    query: '{
       "type": "food",
       "$or": [ { "qty": { "$gt": {{.qty}} } }, { "name": {{.name}} } ]
    }'

func LoadQueryFile

func LoadQueryFile(file string) (*File, error)

Reads in Mongo Query File for use with Haigo.

type Params

type Params map[string]interface{}

type Query

type Query struct {
	Name        string `yaml:"name"`
	Description string `yaml:"description,omitempty"`
	QueryString string `yaml:"query"`
	// contains filtered or unexported fields
}

func (*Query) Map

func (h *Query) Map(params Params) (interface{}, error)

Accepts a params map and returns a map for use with the mgo `find()` function.

func (*Query) Pipe

func (h *Query) Pipe(col *mgo.Collection, params Params) (*mgo.Pipe, error)

Returns configured mgo Pipe.

func (*Query) Print

func (h *Query) Print(params Params) error

Pretty prints the configured query.

func (*Query) Query

func (h *Query) Query(col *mgo.Collection, params Params) (*mgo.Query, error)

Returns configured mgo Query.

func (*Query) String

func (h *Query) String(params Params) (string, error)

Return configured query as a string.

Jump to

Keyboard shortcuts

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