This repository has been archived by the owner on Apr 2, 2024. It is now read-only.
generated from mrz1836/go-template
-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
model_get.go
189 lines (161 loc) · 5.4 KB
/
model_get.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
package bux
import (
"context"
"errors"
"time"
"github.com/BuxOrg/bux/cachestore"
"github.com/BuxOrg/bux/datastore"
)
// Get will retrieve a model from the Cachestore or Datastore using the provided conditions
//
// use bypassCache to skip checking the Cachestore for the record
func Get(
ctx context.Context,
model ModelInterface,
conditions map[string]interface{},
_ bool,
timeout time.Duration,
forceWriteDB bool,
) error {
if timeout == 0 {
timeout = defaultDatabaseReadTimeout
}
/*
// todo: add cache support here for basic model lookups
*/
// Attempt to Get the model (by model fields & given conditions)
return model.Client().Datastore().GetModel(ctx, model, conditions, timeout, forceWriteDB)
}
// getModels will retrieve model(s) from the Cachestore or Datastore using the provided conditions
//
// use bypassCache to skip checking the Cachestore for the record
func getModels(
ctx context.Context,
datastore datastore.ClientInterface,
models interface{},
conditions map[string]interface{},
queryParams *datastore.QueryParams,
timeout time.Duration,
) error {
// Attempt to Get the model (by model fields & given conditions)
return datastore.GetModels(ctx, models, conditions, queryParams, nil, timeout)
}
// getModelsAggregate will retrieve a count of the model(s) from the Cachestore or Datastore using the provided conditions
func getModelsAggregate(
ctx context.Context,
datastore datastore.ClientInterface,
models interface{},
conditions map[string]interface{},
aggregateColumn string,
timeout time.Duration,
) (map[string]interface{}, error) {
// Attempt to Get the model (by model fields & given conditions)
return datastore.GetModelsAggregate(ctx, models, conditions, aggregateColumn, timeout)
}
// getModelCount will retrieve a count of the model from the Cachestore or Datastore using the provided conditions
func getModelCount(
ctx context.Context,
datastore datastore.ClientInterface,
model interface{},
conditions map[string]interface{},
timeout time.Duration, //nolint:nolintlint,unparam // default timeout is passed most of the time
) (int64, error) {
// Attempt to Get the model (by model fields & given conditions)
return datastore.GetModelCount(ctx, model, conditions, timeout)
}
// getModelsByConditions will get models by given conditions
func getModelsByConditions(ctx context.Context, modelName ModelName, modelItems interface{},
metadata *Metadata, conditions *map[string]interface{}, queryParams *datastore.QueryParams,
opts ...ModelOps) error {
dbConditions := map[string]interface{}{}
if metadata != nil {
dbConditions[metadataField] = metadata
}
if conditions != nil && len(*conditions) > 0 {
and := make([]map[string]interface{}, 0)
if _, ok := dbConditions["$and"]; ok {
and = dbConditions["$and"].([]map[string]interface{})
}
and = append(and, *conditions)
dbConditions["$and"] = and
}
// Get the records
if err := getModels(
ctx, NewBaseModel(modelName, opts...).Client().Datastore(),
modelItems, dbConditions, queryParams, defaultDatabaseReadTimeout,
); err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil
}
return err
}
return nil
}
// getModelsAggregateByConditions will get aggregates of models by given conditions
func getModelsAggregateByConditions(ctx context.Context, modelName ModelName, models interface{},
metadata *Metadata, conditions *map[string]interface{}, aggregateColumn string,
opts ...ModelOps) (map[string]interface{}, error) {
dbConditions := map[string]interface{}{}
if metadata != nil {
dbConditions[metadataField] = metadata
}
if conditions != nil && len(*conditions) > 0 {
and := make([]map[string]interface{}, 0)
if _, ok := dbConditions["$and"]; ok {
and = dbConditions["$and"].([]map[string]interface{})
}
and = append(and, *conditions)
dbConditions["$and"] = and
}
// Get the records
results, err := getModelsAggregate(
ctx, NewBaseModel(modelName, opts...).Client().Datastore(),
models, dbConditions, aggregateColumn, defaultDatabaseReadTimeout,
)
if err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return nil, nil
}
return nil, err
}
return results, nil
}
// getModelCountByConditions will get model counts (sums) from given conditions
func getModelCountByConditions(ctx context.Context, modelName ModelName, model interface{},
metadata *Metadata, conditions *map[string]interface{}, opts ...ModelOps) (int64, error) {
dbConditions := map[string]interface{}{}
if metadata != nil {
dbConditions[metadataField] = metadata
}
if conditions != nil && len(*conditions) > 0 {
and := make([]map[string]interface{}, 0)
if _, ok := dbConditions["$and"]; ok {
and = dbConditions["$and"].([]map[string]interface{})
}
and = append(and, *conditions)
dbConditions["$and"] = and
}
// Get the records
count, err := getModelCount(
ctx, NewBaseModel(modelName, opts...).Client().Datastore(),
model, dbConditions, defaultDatabaseReadTimeout,
)
if err != nil {
if errors.Is(err, datastore.ErrNoResults) {
return 0, nil
}
return 0, err
}
return count, nil
}
// getModelFromCache will attempt to get a model from cache
func getModelFromCache(ctx context.Context, cacheClient cachestore.ClientInterface,
key string, model ModelInterface) (bool, error) { // Success if the key was found
if err := cacheClient.GetModel(ctx, key, model); err != nil {
if errors.Is(err, cachestore.ErrKeyNotFound) {
return false, nil
}
return false, err
}
return true, nil
}