Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(app): allow configuring firestore instance id #631

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion firebase.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type App struct {
projectID string
serviceAccountID string
storageBucket string
firestoreID string
opts []option.ClientOption
}

Expand All @@ -61,6 +62,7 @@ type Config struct {
ProjectID string `json:"projectId"`
ServiceAccountID string `json:"serviceAccountId"`
StorageBucket string `json:"storageBucket"`
FirestoreID string `json:"firestoreId"`
}

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

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

fid := firestore.DefaultDatabaseID
if config.FirestoreID != "" {
fid = config.FirestoreID
}

return &App{
authOverride: ao,
dbURL: config.DatabaseURL,
projectID: pid,
serviceAccountID: config.ServiceAccountID,
storageBucket: config.StorageBucket,
firestoreID: fid,
opts: o,
}, nil
}
Expand Down
44 changes: 42 additions & 2 deletions firebase_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"testing"
"time"

"cloud.google.com/go/firestore"
"firebase.google.com/go/v4/messaging"
"golang.org/x/oauth2"
"golang.org/x/oauth2/google"
Expand Down Expand Up @@ -282,8 +283,20 @@ func TestFirestore(t *testing.T) {
t.Fatal(err)
}

if c, err := app.Firestore(ctx); c == nil || err != nil {
t.Errorf("Firestore() = (%v, %v); want (auth, nil)", c, err)
c, err := app.Firestore(ctx)
if c == nil || err != nil {
t.Fatalf("Firestore() = (%v, %v); want (auth, nil)", c, err)
}

// verify that the client is using the overridden database ID

doc := c.Doc("foo/bar")
if doc == nil {
t.Fatalf("Doc() = nil; want doc")
}

if !strings.Contains(doc.Path, firestore.DefaultDatabaseID) {
t.Fatalf("Doc().Path = %q; want to contain %q", doc.Path, firestore.DefaultDatabaseID)
}
}

Expand Down Expand Up @@ -338,6 +351,33 @@ func TestFirestoreWithNoProjectID(t *testing.T) {
}
}

func TestFirestoreWithDatabaseID(t *testing.T) {
firestoreid := "my-awesome-firestore-db"

ctx := context.Background()
config := &Config{FirestoreID: firestoreid}
app, err := NewApp(ctx, config, option.WithCredentialsFile("testdata/service_account.json"))
if err != nil {
t.Fatal(err)
}

c, err := app.Firestore(ctx)
if c == nil || err != nil {
t.Fatalf("Firestore() = (%v, %v); want (auth, nil)", c, err)
}

// verify that the client is using the default database ID

doc := c.Doc("foo/bar")
if doc == nil {
t.Fatalf("Doc() = nil; want doc")
}

if !strings.Contains(doc.Path, firestoreid) {
t.Fatalf("Doc().Path = %q; want to contain %q", doc.Path, firestore.DefaultDatabaseID)
}
}

func TestInstanceID(t *testing.T) {
ctx := context.Background()
app, err := NewApp(ctx, nil, option.WithCredentialsFile("testdata/service_account.json"))
Expand Down