goose

package module
v0.0.0-...-08ce0a7 Latest Latest
Warning

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

Go to latest
Published: Jan 30, 2017 License: MIT Imports: 3 Imported by: 0

README

Goose v0.1

Version history

Version Date
0.1 31st Jan 2017

Introduction

Goose is an ODM(Object Data Mapper) for Golang built on top of the mgo library.

Goal

Mirror all the features of Mongoose.

Features

Currently Goose supports the following.

  • Connect to a single mongoDB instance
  • Attach a schema (currently there are no schema validations being performed)
  • Insert
  • Paginated Fetch
  • Update (whole document)
  • Delete
  • Add Indexs
Connect

func Init() *Goose

func (goose *Goose) Connect(connectionString, dbName string) (*mgo.Session, *mgo.Database)

To connect first we have to initialize goose and then use the Connect() function

goose := goose.Init()
goose.Connect("localhost", "golang")

Calling goose.Init() will return you the global goose instance.

Attach Schema

func (goose *Goose) Definition(name string) schema

The Definitions() function is used to attach a schema.

schema := goose.Definition("people")

Please note, currently goose doesn't perform any schema level validations.

Insert document

func (sch *schema) Save(doc interface{}) error

The Save() function is used to insert a new document.

type People struct {
	Name string
	Age  int
}

goose := goose.Init()
schema.Save(&People{"Apple", 10})
Paginated fetch

func (sch *schema) Index(page, count int, selectData []string, filter []byte) []bson.M

filter := []byte(`{"age":20}`)
selectQuery := []string{"name", "_id"}
resultSet := schema.Index(1, 1, selectQuery, filter)

This returns an array of bsons.

Count

func (sch *schema) Count(filter []byte) int

Given a filter, this function returns the number of matching documents.

Get

func (sch *schema) Get(id string, selectData []string) bson.M

Fetches the document based on the id. If selectData is provided, then only those attributes of the document is retrieved.

Update

func (sch *schema) Update(id string, _doc interface{}) (interface{}, bson.M)

Updates a document with _doc where the _id matches id

Delete

func (sch *schema) Delete(id string) error

Deletes the document where _id is the same as id

Set Index

func (sch *schema) SetIndex(keys []string, unique, dropDups, backgroud, sparse bool)

Sets the indexes for the collection. Read more about how to set the indexes under mgo package func (*Collection) EnsureIndex

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Goose

type Goose struct {
	Session *mgo.Session
	DB      *mgo.Database
	Schemas map[string]schema
}

func Init

func Init() *Goose

func (*Goose) Connect

func (goose *Goose) Connect(connectionString, dbName string) (*mgo.Session, *mgo.Database)

func (*Goose) Definition

func (goose *Goose) Definition(name string) schema

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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