-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
241 lines (193 loc) · 5.3 KB
/
example_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package corm_test
import (
"context"
"fmt"
"github.com/serkansipahi/corm"
"log"
)
// 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.
func ExampleNewOrm_orm() {
// 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
}
// Here is an example for Authentication an user
func ExampleNewClient() {
// 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
}
// Save a document with "auto generated" id by couchDB
func ExampleOrm_Save_save1() {
// 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
}
// Save a document with "predefined" id
func ExampleOrm_Save_save2() {
// 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
}
// Update an document by given Id and Rev
func ExampleOrm_Update() {
// 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
}
// Read document
func ExampleOrm_Read() {
// 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"}
}
// Read document with options
func ExampleOrm_Read2() {
// example coming soon
}
// Delete document
func ExampleOrm_Delete() {
// 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)
}
}