-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadapter.go
87 lines (69 loc) · 2.21 KB
/
adapter.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
package firestoreadapter
import (
"context"
"os"
"path"
"cloud.google.com/go/firestore"
firebase "firebase.google.com/go/v4"
"github.com/mhogar/amber/common"
"github.com/mhogar/amber/config"
"github.com/mhogar/amber/data"
"google.golang.org/api/option"
)
type FirestoreAdapter struct {
cancelFunc context.CancelFunc
Client *firestore.Client
ContextFactory data.ContextFactory
}
// Setup creates a new firestore client using the firestore config.
// Also initializes the adapter's context and cancel function.
// Returns any errors.
func (a *FirestoreAdapter) Setup() error {
cfg := config.GetFirestoreConfig()
//setup context factory
a.ContextFactory.Context, a.cancelFunc = context.WithCancel(context.Background())
a.ContextFactory.Timeout = cfg.Timeout
ctx, cancel := a.ContextFactory.CreateStandardTimeoutContext()
var err error
if os.Getenv("FIRESTORE_EMULATOR_HOST") != "" {
a.Client, err = firestore.NewClient(ctx, "emulator-project-id")
} else {
credsFile := path.Join(config.GetAppRoot(), cfg.ServiceFile)
//create the firebase app
app, appErr := firebase.NewApp(a.ContextFactory.Context, nil, option.WithCredentialsFile(credsFile))
if appErr != nil {
return common.ChainError("error creating firebase app", appErr)
}
a.Client, err = app.Firestore(ctx)
}
cancel()
if err != nil {
return common.ChainError("error creating firestore client", err)
}
return nil
}
// CleanUp closes the firestore client and reset's the adapter's instance.
// The adapter also calls its cancel function to cancel any child requests that may still be running.
// Neither the adapter's client instance or context should be used after calling this function.
// Returns any errors.
func (a *FirestoreAdapter) CleanUp() error {
err := a.Client.Close()
if err != nil {
return common.ChainError("error closing firestore client", err)
}
//cancel any remaining requests that may still be running
a.cancelFunc()
//clean up resources
a.Client = nil
return nil
}
func (a *FirestoreAdapter) GetExecutor() data.DataExecutor {
exec := &FirestoreExecutor{
FirestoreCRUD: FirestoreCRUD{
Client: a.Client,
ContextFactory: a.ContextFactory,
},
}
exec.FirestoreCRUD.DocWriter = exec
return exec
}