Skip to content

Commit 22bcacc

Browse files
committed
Added more documentation and examples for GetAll
Fixes #266
1 parent fc32b6f commit 22bcacc

File tree

4 files changed

+83
-1
lines changed

4 files changed

+83
-1
lines changed

CHANGELOG.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@ This project adheres to [Semantic Versioning](http://semver.org/).
44

55
## Unreleased
66

7+
### Added
8+
- Added more documentation and examples for `GetAll`.
9+
710
### Fixed
811
- Fixed `RunWrite` not defering its call to `Cursor.Close()`. This could cause issues if an error occurred when decoding the result.
912

example_query_select_test.go

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,79 @@ func ExampleTerm_Get() {
3030
// Output: Superman
3131
}
3232

33+
// Find a document by ID.
34+
func ExampleTerm_GetAll() {
35+
// Fetch the row from the database
36+
res, err := DB("examples").Table("heroes").GetAll(2).Run(session)
37+
if err != nil {
38+
fmt.Print(err)
39+
return
40+
}
41+
defer res.Close()
42+
43+
if res.IsNil() {
44+
fmt.Print("Row not found")
45+
return
46+
}
47+
48+
var hero map[string]interface{}
49+
err = res.One(&hero)
50+
if err != nil {
51+
fmt.Printf("Error scanning database result: %s", err)
52+
return
53+
}
54+
fmt.Print(hero["name"])
55+
56+
// Output: Superman
57+
}
58+
59+
// Find a document by ID.
60+
func ExampleTerm_GetAll_multiple() {
61+
// Fetch the row from the database
62+
res, err := DB("examples").Table("heroes").GetAll(1, 2).Run(session)
63+
if err != nil {
64+
fmt.Print(err)
65+
return
66+
}
67+
defer res.Close()
68+
69+
var heroes []map[string]interface{}
70+
err = res.All(&heroes)
71+
if err != nil {
72+
fmt.Printf("Error scanning database result: %s", err)
73+
return
74+
}
75+
fmt.Print(heroes[0]["name"])
76+
77+
// Output: Superman
78+
}
79+
80+
// Find all document with an indexed value.
81+
func ExampleTerm_GetAllByIndex() {
82+
// Fetch the row from the database
83+
res, err := DB("examples").Table("heroes").GetAllByIndex("code_name", "man_of_steel").Run(session)
84+
if err != nil {
85+
fmt.Print(err)
86+
return
87+
}
88+
defer res.Close()
89+
90+
if res.IsNil() {
91+
fmt.Print("Row not found")
92+
return
93+
}
94+
95+
var hero map[string]interface{}
96+
err = res.One(&hero)
97+
if err != nil {
98+
fmt.Printf("Error scanning database result: %s", err)
99+
return
100+
}
101+
fmt.Print(hero["name"])
102+
103+
// Output: Superman
104+
}
105+
33106
// Find a document and merge another document with it.
34107
func ExampleTerm_Get_merge() {
35108
// Fetch the row from the database

query_select.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,10 @@ func (t Term) Get(args ...interface{}) Term {
6161
return constructMethodTerm(t, "Get", p.Term_GET, args, map[string]interface{}{})
6262
}
6363

64-
// GetAll gets all documents where the given value matches the value of the primary index.
64+
// GetAll gets all documents where the given value matches the value of the primary
65+
// index. Multiple values can be passed this function if you want to select multiple
66+
// documents. If the documents you are fetching have composite keys then each
67+
// argument should be a slice. For more information see the examples.
6568
func (t Term) GetAll(keys ...interface{}) Term {
6669
return constructMethodTerm(t, "GetAll", p.Term_GET_ALL, keys, map[string]interface{}{})
6770
}

testdata_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ func setupTestData() {
2424
[]interface{}{Row.Field("date"), Row.Field("title")},
2525
).Exec(session)
2626

27+
DB("examples").Table("heroes").IndexCreate("code_name").Exec(session)
28+
DB("examples").Table("heroes").IndexWait().Exec(session)
29+
2730
DB("examples").Table("games").IndexCreate("type").Exec(session)
2831
DB("examples").Table("games").IndexWait().Exec(session)
2932

0 commit comments

Comments
 (0)