-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
65 lines (58 loc) · 1.81 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
package firestorm
import (
"cloud.google.com/go/firestore"
"context"
mapper "github.com/jschoedt/go-structmapper"
)
// FSClient is the client used to perform the CRUD actions
type FSClient struct {
Client *firestore.Client
MapToDB *mapper.Mapper
MapFromDB *mapper.Mapper
IDKey, ParentKey string
Cache *cacheWrapper
IsEntity func(i interface{}) bool
}
// NewRequest creates a new CRUD Request to firestore
func (fsc *FSClient) NewRequest() *Request {
r := &Request{}
r.FSC = fsc
r.mapperFunc = func(i map[string]interface{}) {
return
}
return r
}
// New creates a firestorm client. Supply the names of the id and parent fields of your model structs
// Leave parent blank if sub-collections are not used.
func New(client *firestore.Client, id, parent string) *FSClient {
c := &FSClient{}
c.Client = client
c.MapToDB = mapper.New()
c.MapToDB.MapFunc = c.DefaultToDBMapperFunc
c.MapFromDB = mapper.New()
c.MapFromDB.MapFunc = c.DefaultFromDBMapperFunc
c.MapFromDB.CaseSensitive = false
c.IDKey = id
c.ParentKey = parent
c.Cache = newCacheWrapper(client, newDefaultCache(), nil)
c.IsEntity = isEntity(c.IDKey)
return c
}
// SetCache sets a second level cache besides the session cache. Use it for eg. memcache or redis
func (fsc *FSClient) SetCache(cache Cache) {
fsc.Cache = newCacheWrapper(fsc.Client, newDefaultCache(), cache)
}
// getCache gets the transaction cache when inside a transaction - otherwise the global cache
func (fsc *FSClient) getCache(ctx context.Context) *cacheWrapper {
if c, ok := ctx.Value(transCacheKey).(*cacheWrapper); ok {
return c
}
return fsc.Cache
}
// isEntity tests if the i is a firestore entity
func isEntity(id string) func(i interface{}) bool {
return func(i interface{}) bool {
_, err := getIDValue(id, i)
return err == nil
}
}