wrap

package module
v0.4.0 Latest Latest
Warning

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

Go to latest
Published: Nov 4, 2019 License: MIT Imports: 9 Imported by: 1

README

wrap

Wrap enables the full MongoDB feature set without ever having to touch BSON or other weird libraries. Everything is structured in a way that BSON and MongoDB stay completly hidden.

install

go get github.com/lucacasonato/wrap
import "github.com/lucacasonato/wrap"

usage

connect
client, err := wrap.Connect("mongodb://localhost:27017", 5*time.Second)
if err != nil {
  panic(err)
}
open a database
db := client.Database("production")

note: you are only getting a refrence to the database here. you are not actually creating it yet

get a collection
users := db.Collection("users")

note: you are only getting a refrence to the collection here. you are not actually creating it yet

add data
doc, err := users.Add(&User{
  Name:            "Luca Casonato",
  Email:           "luca.casonato@antipy.com",
  FavoriteNumbers: []int{5, 10, 15},
  LastEdited:      time.Now(),
})
if err != nil {
  panic(err)
}
get data
data, err := doc.Get()
if err != nil {
  panic(err)
}

user := User{}

data.DataTo(&user)

fmt.Println(user.Name)
update data
err = doc.Update(update.Set("email", "luca@antipy.com"), true)
if err != nil {
  panic(err)
}
create index
err = users.CreateIndex(map[string]wrap.Index{
  "name":  wrap.TextIndex,
  "email": wrap.AscendingIndex,
})
if err != nil {
  panic(err)
}
get filtered data
iterator, err := users.
  Where(filter.AND(
    filter.TextSearch("luca"),
    filter.Equal("email", "luca.casonato@antipy.com"),
  )).
  DocumentIterator()
if err != nil {
  panic(err)
}
defer iterator.Close()

for iterator.Next() {
  user := User{}
  err := iterator.DataTo(&user)
  if err != nil {
    panic(err)
  }

  fmt.Println(user)
}
get structurally modified data (aggregation)
iterator, err = users.All().
  Modify(map[string]interface{}{
    "email": expressions.Exclude,
  }).
  AddFields(map[string]interface{}{
    "averagefavoritenumber": expressions.MathAvg(expressions.Value("favoritenumbers")),
  }).
  DocumentIterator()

if err != nil {
  panic(err)
}
defer iterator.Close()

for iterator.Next() {
  user := map[string]interface{}{}
  err := iterator.DataTo(&user)
  if err != nil {
    panic(err)
  }

  fmt.Println(user)
}
transactions
err := users.Transaction(func(users *wrap.Collection) error {
  now := time.Now()

  err := users.Document(luca.ID).Update(true, update.Set("lastedited", now))
  if err != nil {
    return err
  }

  err = users.Document(jaap.ID).Update(true, update.Set("lastedited", now))
  if err != nil {
    return err
  }

  return nil
})
if err != nil {
  err = nil
}
example

A full example can be found in the "example" folder.

planning

  • implement schema filters (im lazy)
  • automatic index creation
  • more tests

contributing

to build start a mongo server on localhost:27017

run tests with go test

  1. open an issue about your idea / suggestion / bug
  2. wait for response
  3. have someone fix it or pr yourself
  4. thanks

licence

Copyright (c) 2019 Luca Casonato

This project is licenced under the MIT licence. More details in the LICENCE file.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type BulkCollection added in v0.3.0

type BulkCollection struct {
	ID         string
	Collection *Collection
	// contains filtered or unexported fields
}

BulkCollection is a collection which is used for bulk writing

func (*BulkCollection) Add added in v0.3.0

func (c *BulkCollection) Add(data interface{})

Add a document with a certain value

func (*BulkCollection) DeleteDocumentsWhere added in v0.3.0

func (c *BulkCollection) DeleteDocumentsWhere(filter filter.Filter)

DeleteDocumentsWhere the filter matches

func (*BulkCollection) Document added in v0.3.0

func (c *BulkCollection) Document(id string) *BulkDocument

Document get a single document from a collection

func (*BulkCollection) UpdateDocumentsWhere added in v0.3.0

func (c *BulkCollection) UpdateDocumentsWhere(filter filter.Filter, upsert bool, updates ...update.Update) error

UpdateDocumentsWhere the filter matches

type BulkDocument added in v0.3.0

type BulkDocument struct {
	ID         string
	Collection *BulkCollection
}

BulkDocument is a document which is used for bulk writing

func (*BulkDocument) Delete added in v0.3.0

func (d *BulkDocument) Delete() error

Delete a document from a collection

func (*BulkDocument) Set added in v0.3.0

func (d *BulkDocument) Set(data interface{}) error

Set a document to a certain value

func (*BulkDocument) Update added in v0.3.0

func (d *BulkDocument) Update(upsert bool, updates ...update.Update) error

Update a document using the update operators

type Client

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

Client wraps the mongo client

func Connect

func Connect(mongoURI string, timeout time.Duration) (*Client, error)

Connect to a mongo instance

func (*Client) Database

func (c *Client) Database(id string) *Database

Database gets a database instance from a client

func (*Client) Transaction added in v0.3.0

func (c *Client) Transaction(run func(client *Client) error) error

Transaction means all operations executed in the run function are atomic

type Collection

type Collection struct {
	ID string

	Database *Database
	// contains filtered or unexported fields
}

Collection is a collection on the database

func (*Collection) Add

func (c *Collection) Add(data interface{}) (*Document, error)

Add a document with a certain value

func (*Collection) All added in v0.2.0

func (c *Collection) All() *CollectionQuery

All returns an abstract of the collection of all documents

func (*Collection) Bulk added in v0.3.0

func (c *Collection) Bulk(run func(collection *BulkCollection) error, ordered bool) error

Bulk is used to do bulk writes

func (*Collection) CreateIndex

func (c *Collection) CreateIndex(fields map[string]Index) error

CreateIndex for a single or group of fields

func (*Collection) Delete

func (c *Collection) Delete() error

Delete a collection

func (*Collection) DeleteDocumentsWhere added in v0.3.0

func (c *Collection) DeleteDocumentsWhere(filter filter.Filter) error

DeleteDocumentsWhere the filter matches

func (*Collection) Document

func (c *Collection) Document(id string) *Document

Document get a single document from a collection

func (*Collection) Transaction added in v0.3.0

func (c *Collection) Transaction(run func(c *Collection) error) error

Transaction means all operations executed in the run function are atomic

func (*Collection) UpdateDocumentsWhere added in v0.3.0

func (c *Collection) UpdateDocumentsWhere(filter filter.Filter, upsert bool, updates ...update.Update) error

UpdateDocumentsWhere the filter matches

func (*Collection) Where

func (c *Collection) Where(filter filter.Filter) *CollectionQuery

Where returns an abstract of the collection of documents that match the filter

type CollectionQuery

type CollectionQuery struct {
	Collection *Collection
	// contains filtered or unexported fields
}

CollectionQuery is a filtered abstraction of a group of documents

func (*CollectionQuery) AddFields added in v0.2.0

func (cq *CollectionQuery) AddFields(spec map[string]interface{}) *CollectionQuery

AddFields adds some fields to the returned documents

func (*CollectionQuery) Count added in v0.2.0

func (cq *CollectionQuery) Count(field string) *CollectionQuery

Count returns the amount of documents in a single document under the field 'field'

func (*CollectionQuery) DocumentIterator

func (cq *CollectionQuery) DocumentIterator() (*Iterator, error)

DocumentIterator gives you an iterator to loop over the documents

func (*CollectionQuery) Join added in v0.2.0

func (cq *CollectionQuery) Join(localField string, foreignCollection string, foreignField string, as string) *CollectionQuery

Join gets all documents from a foreign collection in this database, filters them by checking if the value of foreignField matches the value of localField and if so adding these to the original document as an array under the 'as' key

func (*CollectionQuery) Limit added in v0.2.0

func (cq *CollectionQuery) Limit(n int) *CollectionQuery

Limit to only return n documents

func (*CollectionQuery) Modify added in v0.2.0

func (cq *CollectionQuery) Modify(spec map[string]interface{}) *CollectionQuery

Modify changes the data structure of the field like specified by the specification

func (*CollectionQuery) Sample added in v0.2.0

func (cq *CollectionQuery) Sample(n int) *CollectionQuery

Sample returns n amount of documents randomly picked from the document pool

func (*CollectionQuery) Skip added in v0.2.0

func (cq *CollectionQuery) Skip(n int) *CollectionQuery

Skip skips the first n documents

func (*CollectionQuery) Sort added in v0.4.0

func (cq *CollectionQuery) Sort(sorters ...*Sorter) *CollectionQuery

Sort sorts a collection by a certain order

func (*CollectionQuery) Transaction added in v0.3.0

func (cq *CollectionQuery) Transaction(run func(cq *CollectionQuery) error) error

Transaction means all operations executed in the run function are atomic

type Database

type Database struct {
	ID string

	Client *Client
	// contains filtered or unexported fields
}

Database is a database instance

func (*Database) Collection

func (d *Database) Collection(id string) *Collection

Collection in the database by id

func (*Database) Delete

func (d *Database) Delete() error

Delete a database

func (*Database) Transaction added in v0.3.0

func (db *Database) Transaction(run func(db *Database) error) error

Transaction means all operations executed in the run function are atomic

type Document

type Document struct {
	ID         string
	Collection *Collection
}

Document is a document in a collection

func (*Document) Delete

func (d *Document) Delete() error

Delete a document from a collection

func (*Document) Get

func (d *Document) Get() (*DocumentData, error)

Get the contents of a document

func (*Document) Set

func (d *Document) Set(data interface{}) error

Set a document to a certain value

func (*Document) Transaction added in v0.3.0

func (d *Document) Transaction(run func(d *Document) error) error

Transaction means all operations executed in the run function are atomic

func (*Document) Update

func (d *Document) Update(upsert bool, updates ...update.Update) error

Update a document using the update operators

type DocumentData

type DocumentData struct {
	Document *Document
	// contains filtered or unexported fields
}

DocumentData is the data in a document

func (*DocumentData) Data

func (d *DocumentData) Data() (interface{}, error)

Data decodes some data and returns an interface

func (*DocumentData) DataTo

func (d *DocumentData) DataTo(data interface{}) error

DataTo decodes some data into an interface

type Index

type Index interface{}

Index used to index data

var (
	// AscendingIndex goes from start to end
	AscendingIndex Index = 1
	// DescendingIndex goes from end to start
	DescendingIndex Index = -1
	// TextIndex indexes text
	TextIndex Index = "text"
)

type Iterator

type Iterator struct {
	Collection *Collection
	// contains filtered or unexported fields
}

Iterator to iterate over documents

func (*Iterator) Close

func (i *Iterator) Close() error

Close stops the iterator

func (*Iterator) Data

func (i *Iterator) Data() (interface{}, error)

Data decodes some data and returns an interface

func (*Iterator) DataTo

func (i *Iterator) DataTo(data interface{}) error

DataTo decodes some data into an interface

func (*Iterator) ID added in v0.3.3

func (i *Iterator) ID() string

ID gets the ID of the current iterator item

func (*Iterator) Next

func (i *Iterator) Next() bool

Next means go to the next document in the iterator

type Sorter added in v0.4.0

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

Sorter is for sorting

func Ascending added in v0.4.0

func Ascending(field string) *Sorter

Ascending means sorted from small to big

func Descending added in v0.4.0

func Descending(field string) *Sorter

Descending means sorted from big to small

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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