Skip to content

Commit 984a7b3

Browse files
committed
feat(app): allow configuring firestore instance id
1 parent 85b8461 commit 984a7b3

File tree

2 files changed

+51
-3
lines changed

2 files changed

+51
-3
lines changed

Diff for: firebase.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type App struct {
5151
projectID string
5252
serviceAccountID string
5353
storageBucket string
54+
firestoreID string
5455
opts []option.ClientOption
5556
}
5657

@@ -61,6 +62,7 @@ type Config struct {
6162
ProjectID string `json:"projectId"`
6263
ServiceAccountID string `json:"serviceAccountId"`
6364
StorageBucket string `json:"storageBucket"`
65+
FirestoreID string `json:"firestoreId"`
6466
}
6567

6668
// Auth returns an instance of auth.Client.
@@ -107,7 +109,7 @@ func (a *App) Firestore(ctx context.Context) (*firestore.Client, error) {
107109
if a.projectID == "" {
108110
return nil, errors.New("project id is required to access Firestore")
109111
}
110-
return firestore.NewClient(ctx, a.projectID, a.opts...)
112+
return firestore.NewClientWithDatabase(ctx, a.projectID, a.firestoreID, a.opts...)
111113
}
112114

113115
// InstanceID returns an instance of iid.Client.
@@ -161,12 +163,18 @@ func NewApp(ctx context.Context, config *Config, opts ...option.ClientOption) (*
161163
ao = *config.AuthOverride
162164
}
163165

166+
fid := firestore.DefaultDatabaseID
167+
if config.FirestoreID != "" {
168+
fid = config.FirestoreID
169+
}
170+
164171
return &App{
165172
authOverride: ao,
166173
dbURL: config.DatabaseURL,
167174
projectID: pid,
168175
serviceAccountID: config.ServiceAccountID,
169176
storageBucket: config.StorageBucket,
177+
firestoreID: fid,
170178
opts: o,
171179
}, nil
172180
}

Diff for: firebase_test.go

+42-2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"testing"
3030
"time"
3131

32+
"cloud.google.com/go/firestore"
3233
"firebase.google.com/go/v4/messaging"
3334
"golang.org/x/oauth2"
3435
"golang.org/x/oauth2/google"
@@ -282,8 +283,20 @@ func TestFirestore(t *testing.T) {
282283
t.Fatal(err)
283284
}
284285

285-
if c, err := app.Firestore(ctx); c == nil || err != nil {
286-
t.Errorf("Firestore() = (%v, %v); want (auth, nil)", c, err)
286+
c, err := app.Firestore(ctx)
287+
if c == nil || err != nil {
288+
t.Fatalf("Firestore() = (%v, %v); want (auth, nil)", c, err)
289+
}
290+
291+
// verify that the client is using the overridden database ID
292+
293+
doc := c.Doc("foo/bar")
294+
if doc == nil {
295+
t.Fatalf("Doc() = nil; want doc")
296+
}
297+
298+
if !strings.Contains(doc.Path, firestore.DefaultDatabaseID) {
299+
t.Fatalf("Doc().Path = %q; want to contain %q", doc.Path, firestore.DefaultDatabaseID)
287300
}
288301
}
289302

@@ -338,6 +351,33 @@ func TestFirestoreWithNoProjectID(t *testing.T) {
338351
}
339352
}
340353

354+
func TestFirestoreWithDatabaseID(t *testing.T) {
355+
firestoreid := "my-awesome-firestore-db"
356+
357+
ctx := context.Background()
358+
config := &Config{FirestoreID: firestoreid}
359+
app, err := NewApp(ctx, config, option.WithCredentialsFile("testdata/service_account.json"))
360+
if err != nil {
361+
t.Fatal(err)
362+
}
363+
364+
c, err := app.Firestore(ctx)
365+
if c == nil || err != nil {
366+
t.Fatalf("Firestore() = (%v, %v); want (auth, nil)", c, err)
367+
}
368+
369+
// verify that the client is using the default database ID
370+
371+
doc := c.Doc("foo/bar")
372+
if doc == nil {
373+
t.Fatalf("Doc() = nil; want doc")
374+
}
375+
376+
if !strings.Contains(doc.Path, firestoreid) {
377+
t.Fatalf("Doc().Path = %q; want to contain %q", doc.Path, firestore.DefaultDatabaseID)
378+
}
379+
}
380+
341381
func TestInstanceID(t *testing.T) {
342382
ctx := context.Background()
343383
app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/service_account.json"))

0 commit comments

Comments
 (0)