kip

package module
v0.0.2 Latest Latest
Warning

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

Go to latest
Published: May 15, 2018 License: MIT Imports: 7 Imported by: 1

README

GoDoc

Kip is a Object wrapper for MongoDB.

How to use

Define

Basic usage:

type User struct {
	Name  string `bson:"name"`
	Age   int    `bson:"age"`
	Email string `bson:"email"`
}

kip.Define(&Collection{
	Name: "Users",
	OnCreate: func() interface{} {
		return &User{
			Id:   bson.NewObjectId(),
			Name: "default name",
		}
	},
})

Define indexes:


kip.Define(&Collection{
	Name: "Users",
	OnCreate: func() interface{} {
		return &User{
			Id:   bson.NewObjectId(),
			Name: "default name",
		}
	},
}).EnsureIndex(mgo.Index{
    Key: []string{"email"},
    Unique: true,
    DropDups: true,
    Background: true, // See notes.
    Sparse: true,
})

Create DAO

Definitions can be instantiated as many times as you want :)

users := kip.Create("Users")
users.Database = NewDatabase("localhost", "demo")

CRUD: Create

john := users.Create()

CRUD: Retrieve

Objects can be retrieved in three ways:

  • FindOne
  • FindById
  • Find
FindOne

Retrieve one item based on a query.

If there is no matching objects, nil is returned.

It will panic if an unexpected error happens.

john := users.FindOne(bson.M{"name": "John"})
FindById

Retrieve one item by _id.

It is a particular case of FindOne with the query bson.M{"_id": <id> }.

Find

Retrieve a cursor...

CRUD: Update

TODO!

CRUD: Delete

john.Delete()

Documentation

Index

Constants

This section is empty.

Variables

View Source
var MONGO_DIAL_TIMEOUT = 0 * time.Second
View Source
var MONGO_SOCKET_TIMEOUT = 3 * time.Second
View Source
var MONGO_SYNC_TIMEOUT = 3 * time.Second

Functions

func Close

func Close(addrs string)

func CloseAll

func CloseAll()

func Define

func Define(c *Collection)

Types

type Collection

type Collection struct {
	Name     string
	Sample   interface{}
	OnCreate OnCreate
	Indexes  []mgo.Index
}

type Dao

type Dao struct {
	Collection *Collection
	Database   *Database
}

Dao is the combination of `Collection` definition plus a `Database`

func NewDao

func NewDao(name string, db *Database) *Dao

func (*Dao) Create

func (d *Dao) Create() *Item

*

  • Create a new item for the existing collection

func (*Dao) Delete

func (d *Dao) Delete(query bson.M) (n int, err error)

Delete will remove all items that match with the query

func (*Dao) Find

func (d *Dao) Find(query interface{}) *Query

func (*Dao) FindById

func (d *Dao) FindById(id interface{}) (*Item, error)

*

  • FindById is a particular case of FindOne

func (*Dao) FindOne

func (d *Dao) FindOne(query interface{}) (*Item, error)

*

  • Returned values:
  • - *Item -> All works
  • - nil -> Item not found
  • - panic() -> Some kind of uncontrolled error happened

func (*Dao) Insert

func (d *Dao) Insert(o *Item) error

type Database

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

func NewDatabase

func NewDatabase(mongourl string) (*Database, error)

func (*Database) C

func (d *Database) C(collection string) *mgo.Collection

func (*Database) Clone

func (d *Database) Clone() *Database

func (*Database) Close

func (d *Database) Close()

func (*Database) GetName

func (d *Database) GetName() string

type Item

type Item struct {
	Dao   *Dao
	Value interface{}
	// contains filtered or unexported fields
}

func (*Item) Delete

func (i *Item) Delete() error

func (*Item) GetId

func (i *Item) GetId() interface{}

func (*Item) Patch

func (i *Item) Patch(p *Patch) error

func (*Item) Reload

func (i *Item) Reload() error

func (*Item) Save

func (i *Item) Save() error

func (*Item) Where

func (i *Item) Where(condition bson.M) *Item

type Kip

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

func Global

func Global() *Kip

func NewKip

func NewKip() *Kip

func (*Kip) Define

func (k *Kip) Define(c *Collection)

func (*Kip) NewDao

func (k *Kip) NewDao(name string, database *Database) *Dao

type OnCreate

type OnCreate func() interface{}

type Patch

type Patch struct {
	Operation string      `json:"operation"`
	Key       string      `json:"key"`
	Value     interface{} `json:"value"`
}

type Query

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

func (*Query) All

func (q *Query) All(result interface{}) error

Finalizers

func (*Query) Count

func (q *Query) Count() (n int, err error)

func (*Query) ForEach

func (q *Query) ForEach(f func(*Item)) error

func (*Query) Iter

func (q *Query) Iter() (*mgo.Iter, *Database)

func (*Query) Limit

func (q *Query) Limit(n int) *Query

func (*Query) One

func (q *Query) One(result interface{}) error

func (*Query) Select

func (q *Query) Select(projection interface{}) *Query

func (*Query) Skip

func (q *Query) Skip(n int) *Query

func (*Query) Snapshot

func (q *Query) Snapshot() *Query

func (*Query) Sort

func (q *Query) Sort(fields ...string) *Query

Jump to

Keyboard shortcuts

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