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
126 lines (106 loc) · 3.34 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
package bux
import (
"context"
"errors"
"time"
"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)
}
// 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: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)
}
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
}
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
}