Documentation ¶
Index ¶
- func CheckDocument(doc interface{}) (structName, docId, docRev, docType string, err error)
- type Client
- type ClientConfig
- type Config
- type DB
- type Options
- type Orm
- func (c *Orm) Delete(ctx context.Context, docId string, docRev ...string) (newRev string, err error)
- func (c *Orm) Read(ctx context.Context, docId string, doc interface{}, options ...Options) (row *Row, err error)
- func (c *Orm) Save(ctx context.Context, doc interface{}) (docId string, docRev string, err error)
- func (c *Orm) Update(ctx context.Context, doc interface{}) (docId, newRev string, err error)
- type OrmOptions
- type Row
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CheckDocument ¶
CheckDocument checks if the required fields Id, Rev and Type are exists. When everything gone right, it return the extracted fields Id, Rev and Type. When it fails it return an error.
Types ¶
type Client ¶
func NewClient ¶
func NewClient(ctx context.Context, config ClientConfig) (*Client, error)
NewClient returns a client instance that can be used for e.g. CouchDb´ s build in authentication. Here are the full method list of the client instance: https://godoc.org/github.com/flimzy/kivik#Client
Example ¶
Here is an example for Authentication an user
package main import ( "context" "fmt" "github.com/serkansipahi/corm" "log" ) func main() { // create client client, err := corm.NewClient(context.TODO(), corm.ClientConfig{ Host: "http://localhost:5984/", DriverName: "couch", }) // log when it fails if err != nil { log.Fatal(err) } // define credentials type Credentials struct { Name string `json:"name"` Password string `json:"password"` } // authenticate user err = client.Authenticate(context.TODO(), Credentials{ Name: "myname", Password: "somepassword", }) fmt.Println(err) }
Output: nil
type ClientConfig ¶
type ClientConfig struct { Host string // default "couch" DriverName string // default "http://localhost:5984/" }
type Config ¶
type Config struct { Host string // default "couch" DriverName string // default "http://localhost:5984/" DBName string }
Config can be passed in corm.New(...)
type Orm ¶
type Orm struct {
Db *DB
}
Orm contains the Db instance that be used inside of corm.NewOrm(...)
func New ¶
New returns a db instance connection by passed DBName. When it fails it will return an error. Optionally "Host" and DriverName can be passed with Config struct.
ctx := context.TODO() db, err := corm.New(ctx, Config{ DBName: "honeyglass", })
func NewOrm ¶
NewOrm creates a new Orm instance by passed db instance. Note: not relevant for you! It will be used inside of corm.New(...) but if you really want to create your own orm, please see [Example].
Example (Orm) ¶
You dont need this step when using corm. But if want to know how to init an custom orm, please follow the example step by step.
package main import ( "context" "fmt" "github.com/serkansipahi/corm" "log" ) func main() { // type Product struct { // Id string `json:"_id,omitempty"` // required in this style // Rev string `json:"_rev,omitempty"` // required in this style // Type string `json:"type"` // required in this style // Name string `json:"vendorId"` // } // create client client, err := corm.NewClient(context.TODO(), corm.ClientConfig{ Host: "http://localhost:5984/", DriverName: "couch", }) if err != nil { log.Fatal(err) } // create db db, err := client.DB(context.TODO(), "mydbname") // create orm orm := corm.NewOrm(db) if err != nil { log.Fatal(err) } // define any struct type Person struct { Name string Surename string Age int } // save person id, rev, err := orm.Save(context.TODO(), Person{ Name: "Serkan", Surename: "Sipahi", }) fmt.Println(id, rev, err) }
Output: 889c9653a6b490cc24c85d78b10076c7, 1-68a533f5dc76a65b56b7329b9d4086ab, nil
func (*Orm) Delete ¶
func (c *Orm) Delete(ctx context.Context, docId string, docRev ...string) (newRev string, err error)
Delete deletes the document by passed id and revision (rev). When id and revision (rev) are empty it will return an error. When everything works fine it will return the new deleted revision (newRev).
Example ¶
Delete document
package main import ( "context" "github.com/serkansipahi/corm" "log" ) func main() { // create orm orm, err := corm.New(context.TODO(), corm.Config{ DBName: "mydbname", }) // log when it fails if err != nil { log.Fatal(err) } // delete doc _, err = orm.Delete(context.TODO(), "889c9653a6b490cc24c85d78b10076c7", "2-68a533f5dc76a65b56b7329b9d4086ab", ) // log when it fails if err != nil { log.Fatal(err) } }
Output:
func (*Orm) Read ¶
func (c *Orm) Read(ctx context.Context, docId string, doc interface{}, options ...Options) (row *Row, err error)
Read reads document from database by passed id and doc struct. When it fails it will return an empty Row and error and when everything works fine, it will unmarshal the result (row) to passed doc struct.
Example ¶
Read document
// type Product struct { // Id string `json:"_id,omitempty"` // required in this style // Rev string `json:"_rev,omitempty"` // required in this style // Type string `json:"type"` // required: tag this but don´t touch it // Name string `json:"name"` // } // create orm orm, err := corm.New(context.TODO(), corm.Config{ DBName: "mydbname", }) // log when it fails if err != nil { log.Fatal(err) } var product Product _, err = orm.Read(context.TODO(), "889c9653a6b490cc24c85d78b10076c7", &product) fmt.Println(product)
Output: Product{Name: "Bar", Id: "889c9653a6b490cc24c85d78b10076c7", Rev: "2-68a533f5dc76a65b56b7329b9d4086ab"}
func (*Orm) Save ¶
Save saves new doc (struct) in the database. When it saved correctly it will return an id and revision. On fail it will return an error. When doc contain the Id property, the database will interpret it as predefined unique Id and when its omitted it will generate it automatically.
Example (Save1) ¶
Save a document with "auto generated" id by couchDB
// type Product struct { // Id string `json:"_id,omitempty"` // required in this style // Rev string `json:"_rev,omitempty"` // required in this style // Type string `json:"type"` // required: tag this but don´t touch it // Name string `json:"vendorId"` // } // create orm orm, err := corm.New(context.TODO(), corm.Config{ DBName: "mydbname", }) if err != nil { log.Fatal(err) } // save document docId, rev, err := orm.Save(context.TODO(), Product{ Name: "Foo", }) // log when it fails if err != nil { log.Fatal(err) } fmt.Println(docId, rev, nil)
Output: 889c9653a6b490cc24c85d78b10076c7, 1-68a533f5dc76a65b56b7329b9d4086ab, nil
Example (Save2) ¶
Save a document with "predefined" id
// type Product struct { // Id string `json:"_id,omitempty"` // required in this style // Rev string `json:"_rev,omitempty"` // required in this style // Type string `json:"type"` // required: tag this but don´t touch it // Name string `json:"name"` // } // create orm orm, err := corm.New(context.TODO(), corm.Config{ DBName: "mydbname", }) // create document with predefined id docId, rev, err := orm.Save(context.TODO(), Product{ Id: "123456", Name: "Foo", }) // log when it fails if err != nil { log.Fatal(err) } fmt.Println(docId, rev, err)
Output: 123456, 1-68a533f5dc76a65b56b7329b9d4086ab, nil
func (*Orm) Update ¶
Update updates doc (struct) as document in the database. The doc must contains the doc Id and Rev property. When its saved correctly it will return a new revision. Otherwise it will return an error.
Example ¶
Update an document by given Id and Rev
// type Product struct { // Id string `json:"_id,omitempty"` // required in this style // Rev string `json:"_rev,omitempty"` // required in this style // Type string `json:"type"` // required: tag this but don´t touch it // Name string `json:"name"` // } // create orm orm, err := corm.New(context.TODO(), corm.Config{ DBName: "mydbname", }) // log when it fails if err != nil { log.Fatal(err) } // update document rev, err := orm.Update(context.TODO(), Product{ Id: "889c9653a6b490cc24c85d78b10076c7", Rev: "1-68a533f5dc76a65b56b7329b9d4086ab", Name: "Bar", }) // log when it fails if err != nil { log.Fatal(err) } fmt.Println(rev, err)
Output: 2-68a533f5dc76a65b56b7329b9d4086ab, nil
type OrmOptions ¶
type OrmOptions map[string]interface{}
OrmOptions contains options as key value pair.