/
main.go
71 lines (61 loc) · 1.46 KB
/
main.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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"cloud.google.com/go/firestore"
"github.com/mchmarny/oven"
"github.com/mchmarny/oven/examples/book"
"github.com/mchmarny/oven/pkg/id"
)
func main() {
projectID := os.Getenv("PROJECT_ID")
if projectID == "" {
log.Fatal("PROJECT_ID not set")
}
ctx := context.Background()
// create firestore client
client, err := firestore.NewClient(ctx, projectID)
if err != nil {
log.Fatal(err)
}
bookCollectionSize := 10
bookAuthor := "Douglas Adams"
// save books
for i := 0; i < bookCollectionSize; i++ {
b := &book.Book{
BookID: id.NewID(),
Name: fmt.Sprintf("Galaxy, volume %d", i),
Author: bookAuthor,
Published: time.Now().AddDate(-1, -2, -i).UTC(),
Pages: book.SampleBookNumOfPages,
HardCover: false,
}
if err = oven.Save(ctx, client, book.CollectionName, b.BookID, b); err != nil {
log.Fatalf("failed to save: %v", err)
}
}
// get all books by the author
q := &oven.Criteria{
Collection: book.CollectionName,
Criterions: []*oven.Criterion{
{
Path: "author", // `firestore:"author"`
Operation: oven.OperationTypeEqual,
Value: bookAuthor,
},
},
OrderBy: "published", // `firestore:"published"`
Desc: true,
Limit: bookCollectionSize,
}
list, err := oven.Query[book.Book](ctx, client, q)
if err != nil {
log.Fatalf("failed to query: %v", err)
}
for i, b := range list {
fmt.Printf("book[%d]: %+v\n", i, b)
}
}