File: example_query_write_test.go

package info (click to toggle)
golang-gopkg-dancannon-gorethink.v2 2.0.4-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, stretch
  • size: 780 kB
  • ctags: 1,597
  • sloc: makefile: 4
file content (228 lines) | stat: -rw-r--r-- 4,955 bytes parent folder | download | duplicates (6)
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
package gorethink

import (
	"fmt"
)

// Insert a document into the table posts using a struct.
func ExampleTerm_Insert_struct() {
	type Post struct {
		ID      int    `gorethink:"id"`
		Title   string `gorethink:"title"`
		Content string `gorethink:"content"`
	}

	resp, err := DB("examples").Table("posts").Insert(Post{
		ID:      1,
		Title:   "Lorem ipsum",
		Content: "Dolor sit amet",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row inserted", resp.Inserted)

	// Output:
	// 1 row inserted
}

// Insert a document without a defined primary key into the table posts where
// the primary key is id.
func ExampleTerm_Insert_generatedKey() {
	type Post struct {
		Title   string `gorethink:"title"`
		Content string `gorethink:"content"`
	}

	resp, err := DB("examples").Table("posts").Insert(map[string]interface{}{
		"title":   "Lorem ipsum",
		"content": "Dolor sit amet",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row inserted, %d key generated", resp.Inserted, len(resp.GeneratedKeys))

	// Output:
	// 1 row inserted, 1 key generated
}

// Insert a document into the table posts using a map.
func ExampleTerm_Insert_map() {
	resp, err := DB("examples").Table("posts").Insert(map[string]interface{}{
		"id":      2,
		"title":   "Lorem ipsum",
		"content": "Dolor sit amet",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row inserted", resp.Inserted)

	// Output:
	// 1 row inserted
}

// Insert multiple documents into the table posts.
func ExampleTerm_Insert_multiple() {
	resp, err := DB("examples").Table("posts").Insert([]interface{}{
		map[string]interface{}{
			"title":   "Lorem ipsum",
			"content": "Dolor sit amet",
		},
		map[string]interface{}{
			"title":   "Lorem ipsum",
			"content": "Dolor sit amet",
		},
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d rows inserted", resp.Inserted)

	// Output:
	// 2 rows inserted
}

// Insert a document into the table posts, replacing the document if it already
// exists.
func ExampleTerm_Insert_upsert() {
	resp, err := DB("examples").Table("posts").Insert(map[string]interface{}{
		"id":    1,
		"title": "Lorem ipsum 2",
	}, InsertOpts{
		Conflict: "replace",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row replaced", resp.Replaced)

	// Output:
	// 1 row replaced
}

// Update the status of the post with id of 1 to published.
func ExampleTerm_Update() {
	resp, err := DB("examples").Table("posts").Get(2).Update(map[string]interface{}{
		"status": "published",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row replaced", resp.Replaced)

	// Output:
	// 1 row replaced
}

// Update bob's cell phone number.
func ExampleTerm_Update_nested() {
	resp, err := DB("examples").Table("users").Get("bob").Update(map[string]interface{}{
		"contact": map[string]interface{}{
			"phone": "408-555-4242",
		},
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row replaced", resp.Replaced)

	// Output:
	// 1 row replaced
}

// Update the status of all posts to published.
func ExampleTerm_Update_all() {
	resp, err := DB("examples").Table("posts").Update(map[string]interface{}{
		"status": "published",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row replaced", resp.Replaced)

	// Output:
	// 4 row replaced
}

// Increment the field view of the post with id of 1. If the field views does not
// exist, it will be set to 0.
func ExampleTerm_Update_increment() {
	resp, err := DB("examples").Table("posts").Get(1).Update(map[string]interface{}{
		"views": Row.Field("views").Add(1).Default(0),
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row replaced", resp.Replaced)

	// Output:
	// 1 row replaced
}

// Update the status of the post with id of 1 using soft durability.
func ExampleTerm_Update_softDurability() {
	resp, err := DB("examples").Table("posts").Get(2).Update(map[string]interface{}{
		"status": "draft",
	}, UpdateOpts{
		Durability: "soft",
	}).RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row replaced", resp.Replaced)

	// Output:
	// 1 row replaced
}

// Delete a single document from the table posts.
func ExampleTerm_Delete() {
	resp, err := DB("examples").Table("posts").Get(2).Delete().RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d row deleted", resp.Deleted)

	// Output:
	// 1 row deleted
}

// Delete all comments where the field status is published
func ExampleTerm_Delete_many() {
	resp, err := DB("examples").Table("posts").Filter(map[string]interface{}{
		"status": "published",
	}).Delete().RunWrite(session)
	if err != nil {
		fmt.Print(err)
		return
	}

	fmt.Printf("%d rows deleted", resp.Deleted)

	// Output:
	// 4 rows deleted
}