-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathexample_test.go
188 lines (153 loc) · 5.78 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
package tormenta_test
import (
"fmt"
"time"
"github.com/jpincas/gouuidv6"
"github.com/jpincas/tormenta"
)
type Product struct {
tormenta.Model
Code string
Name string
Price float32
StartingStock int
Tags []string
}
type Order struct {
tormenta.Model
Customer string
Department int
ShippingFee float64
ProductID gouuidv6.UUID
Product Product `tormenta:"-"`
}
func printlinef(formatString string, x interface{}) {
fmt.Println(fmt.Sprintf(formatString, x))
}
func Example() {
// Open the DB
db, _ := tormenta.OpenTestWithOptions("data/tests", testDBOptions)
defer db.Close()
// Create some products
product1 := Product{
Code: "SKU1",
Name: "Product1",
Price: 1.00,
StartingStock: 50}
product2 := Product{
Code: "SKU2",
Name: "Product2",
Price: 2.00,
StartingStock: 100}
// Save
n, _ := db.Save(&product1, &product2)
printlinef("Saved %v records", n)
// Get
var nonExistentID gouuidv6.UUID
var product Product
// No such record
ok, _ := db.Get(&product, nonExistentID)
printlinef("Get record? %v", ok)
// Get by entity
ok, _ = db.Get(&product1)
printlinef("Got record? %v", ok)
// Get with optional separately specified ID
ok, _ = db.Get(&product, product1.ID)
printlinef("Get record with separately specified ID? %v", ok)
// Delete
db.Delete(&product1)
fmt.Println("Deleted 1 record")
// Basic query
var products []Product
n, _ = db.Find(&products).Run()
printlinef("Found %v record(s)", n)
// Date range query
// Make some fullStructs with specific creation times
var ttsToSave []tormenta.Record
dates := []time.Time{
// Specific years
time.Date(2009, time.January, 1, 1, 0, 0, 0, time.UTC),
time.Date(2010, time.January, 1, 1, 0, 0, 0, time.UTC),
time.Date(2011, time.January, 1, 1, 0, 0, 0, time.UTC),
time.Date(2012, time.January, 1, 1, 0, 0, 0, time.UTC),
time.Date(2013, time.January, 1, 1, 0, 0, 0, time.UTC),
}
for i, date := range dates {
ttsToSave = append(ttsToSave, &Order{
// You wouln't normally do this manually
// This is just for illustration
Model: tormenta.Model{
ID: gouuidv6.NewFromTime(date),
},
Customer: fmt.Sprintf("customer-%v", i), // "customer-0", "customer-1"
ShippingFee: float64(i),
})
}
// Save the fullStructs
db.Save(ttsToSave...)
var fullStructs []Order
var fullStruct Order
mid2009 := time.Date(2009, time.June, 1, 1, 0, 0, 0, time.UTC)
mid2010 := time.Date(2010, time.June, 1, 1, 0, 0, 0, time.UTC)
mid2012 := time.Date(2012, time.June, 1, 1, 0, 0, 0, time.UTC)
// Basic date range query
n, _ = db.Find(&fullStructs).From(mid2009).To(mid2012).Run()
printlinef("Basic date range query: %v records found", n)
// First
n, _ = db.First(&fullStruct).From(mid2009).To(mid2012).Run()
printlinef("Basic date range query, first only: %v record(s) found", n)
// First (not found)
n, _ = db.First(&fullStruct).From(time.Now()).To(time.Now()).Run()
printlinef("Basic date range query, first only: %v record(s) found", n)
// Count only (fast!)
c, _ := db.Find(&fullStructs).From(mid2009).To(mid2012).Count()
printlinef("Basic date range query, count only: counted %v", c)
// Limit
n, _ = db.Find(&fullStructs).From(mid2009).To(mid2012).Limit(2).Run()
printlinef("Basic date range query, 2 limit: %v record(s) found", n)
// Offset
n, _ = db.Find(&fullStructs).From(mid2009).To(mid2012).Limit(2).Offset(1).Run()
printlinef("Basic date range query, 2 limit, 1 offset: %v record(s) found", n)
// Reverse, count
c, _ = db.Find(&fullStructs).Reverse().From(mid2009).To(mid2012).Count()
printlinef("Basic date range query, reverse, count: %v record(s) counted", c)
// Secondary index on 'customer' - exact index match
n, _ = db.First(&fullStruct).Match("Customer", "customer-2").Run()
printlinef("Index query, exact match: %v record(s) found", n)
// Secondary index on 'customer' - prefix match
n, _ = db.First(&fullStruct).StartsWith("Customer", "customer-").Run()
printlinef("Index query, starts with: %v record(s) found", n)
// Index range, Sum (based on index)
var sum float64
db.Find(&fullStructs).Range("ShippingFee", 0.00, 10.00).From(mid2009).To(mid2012).Sum(&sum, "ShippingFee")
printlinef("Index range, date range, index sum query. Sum: %v", sum)
// Secondary index on 'customer' - index range and count
c, _ = db.Find(&fullStructs).Range("Customer", "customer-1", "customer-3").Count()
printlinef("Index range, count: %v record(s) counted", c)
// Secondary index on 'customer' - exact index match, count and date range
c, _ = db.Find(&fullStructs).Match("Customer", "customer-3").From(mid2009).To(time.Now()).Count()
printlinef("Index exact match, date range, count: %v record(s) counted", c)
// Secondary index on 'customer' - index range AND date range
c, _ = db.Find(&fullStructs).Range("Customer", "customer-1", "customer-3").From(mid2009).To(mid2010).Count()
printlinef("Index range, date range, count: %v record(s) counted", c)
// Output:
// Saved 2 records
// Get record? false
// Got record? true
// Get record with separately specified ID? true
// Deleted 1 record
// Found 1 record(s)
// Basic date range query: 3 records found
// Basic date range query, first only: 1 record(s) found
// Basic date range query, first only: 0 record(s) found
// Basic date range query, count only: counted 3
// Basic date range query, 2 limit: 2 record(s) found
// Basic date range query, 2 limit, 1 offset: 2 record(s) found
// Basic date range query, reverse, count: 3 record(s) counted
// Index query, exact match: 1 record(s) found
// Index query, starts with: 1 record(s) found
// Index range, date range, index sum query. Sum: 6
// Index range, count: 3 record(s) counted
// Index exact match, date range, count: 1 record(s) counted
// Index range, date range, count: 1 record(s) counted
}