-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample_create_document_test.go
59 lines (49 loc) · 1.15 KB
/
example_create_document_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package sofa_test
import (
"fmt"
"time"
"github.com/joelnb/sofa"
)
type Fruit struct {
sofa.DocumentMetadata
Name string `json:"name"`
Type string `json:"type"`
}
// ExampleCreateDocument creates a
func Example_createDocument() {
conn, err := sofa.NewConnection("http://localhost:5984", 10*time.Second, sofa.NullAuthenticator())
if err != nil {
panic(err)
}
// Open the previously-created database
db := conn.Database("example_db")
// Create a document to save
doc := &Fruit{
DocumentMetadata: sofa.DocumentMetadata{
ID: "fruit1",
},
Name: "apple",
Type: "fruit",
}
// Create the document on the CouchDB server
rev, err := db.Put(doc)
if err != nil {
panic(err)
}
// Retrieve the document which was created
getDoc := &Fruit{}
getRev, err := db.Get(getDoc, "fruit1", "")
if err != nil {
panic(err)
}
// Is the document we put there the same revision we got back?
if getRev != rev {
panic("someone changed the document while this was running")
}
// Show the struct which was returned from the DB
fmt.Printf("Name: %s\n", getDoc.Name)
fmt.Printf("Type: %s\n", getDoc.Type)
// Output:
// Name: apple
// Type: fruit
}