Skip to content

Commit

Permalink
docstore/gcpfilestore: add support for non-default databases. (#3345)
Browse files Browse the repository at this point in the history
  • Loading branch information
coryschwartz authored Nov 22, 2023
1 parent c04000b commit fa413a3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
8 changes: 7 additions & 1 deletion docstore/gcpfirestore/fs.go
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,12 @@ func CollectionResourceID(projectID, collPath string) string {
return fmt.Sprintf("projects/%s/databases/(default)/documents/%s", projectID, collPath)
}

// CollectResoureceIDWithDatabase constructs a resource ID for a collection from the project ID, database ID, and the collection path.
// See the OpenCollection example for use.
func CollectionResourceIDWithDatabase(projectID, databaseID, collPath string) string {
return fmt.Sprintf("projects/%s/databases/%s/documents/%s", projectID, databaseID, collPath)
}

// OpenCollection creates a *docstore.Collection representing a Firestore collection.
//
// collResourceID must be of the form "project/<projectID>/databases/(default)/documents/<collPath>".
Expand Down Expand Up @@ -193,7 +199,7 @@ func OpenCollectionWithNameFunc(client *vkit.Client, collResourceID string, name
return docstore.NewCollection(c), nil
}

var resourceIDRE = regexp.MustCompile(`^(projects/[^/]+/databases/\(default\))/documents/.+`)
var resourceIDRE = regexp.MustCompile(`^(projects/[^/]+/databases/[^/]+)/documents/.+`)

func newCollection(client *vkit.Client, collResourceID, nameField string, nameFunc func(docstore.Document) string, opts *Options) (*collection, error) {
if nameField == "" && nameFunc == nil {
Expand Down
2 changes: 1 addition & 1 deletion docstore/gcpfirestore/fs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@ func TestResourceIDRegexp(t *testing.T) {
for _, good := range []string{
"projects/abc-_.309/databases/(default)/documents/C",
"projects/P/databases/(default)/documents/C/D/E",
"projects/P/databases/mydb/documents/E/F/G",
} {
if !resourceIDRE.MatchString(good) {
t.Errorf("%q did not match but should have", good)
Expand All @@ -194,7 +195,6 @@ func TestResourceIDRegexp(t *testing.T) {
"Projects/P/databases/(default)/documents/C",
"P/databases/(default)/documents/C",
"projects/P/Q/databases/(default)/documents/C",
"projects/P/databases/mydb/documents/C",
"projects/P/databases/(default)/C",
"projects/P/databases/(default)/documents/",
"projects/P/databases/(default)",
Expand Down

0 comments on commit fa413a3

Please sign in to comment.