From 6fa4057914e0d320b6aac41d6521d1f5921f37c3 Mon Sep 17 00:00:00 2001 From: Marcos Huck Date: Thu, 1 Jun 2023 16:58:56 -0300 Subject: [PATCH] Make NewFirestoreRepository receive the collection name --- repository/firestore/firestore.go | 10 ++++++---- repository/firestore/firestore_test.go | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/repository/firestore/firestore.go b/repository/firestore/firestore.go index 7087647..16c0b3d 100644 --- a/repository/firestore/firestore.go +++ b/repository/firestore/firestore.go @@ -10,7 +10,8 @@ import ( // firestoreRepository implements Repository using the firestore client. type firestoreRepository[T repository.Model] struct { - client *firestore.Client + client *firestore.Client + collection string } // FirstOrCreate is not implemented. @@ -32,7 +33,7 @@ func (r *firestoreRepository[T]) CreateBulk(entities []repository.Model) ([]repo // output: will contain the result of the query. It must be a pointer to a slice. // options: configuration options for the search. func (r *firestoreRepository[T]) Find(output interface{}, options ...repository.Option) error { - col := r.client.Collection(r.Model().TableName()) + col := r.client.Collection(r.collection) r.applyOptions(&col.Query, options...) iter := col.Documents(context.Background()) docs, err := iter.GetAll() @@ -92,8 +93,9 @@ func (r *firestoreRepository[T]) applyOptions(q *firestore.Query, opts ...reposi } // NewFirestoreRepository initializes a new Repository implementation for Firestore collections. -func NewFirestoreRepository[T repository.Model](client *firestore.Client) repository.Repository { +func NewFirestoreRepository[T repository.Model](client *firestore.Client, collection string) repository.Repository { return &firestoreRepository[T]{ - client: client, + client: client, + collection: collection, } } diff --git a/repository/firestore/firestore_test.go b/repository/firestore/firestore_test.go index e955fb7..56d2fe0 100644 --- a/repository/firestore/firestore_test.go +++ b/repository/firestore/firestore_test.go @@ -41,7 +41,7 @@ func (suite *FirestoreRepositoryTestSuite) SetupSuite() { suite.fs, err = suite.client.Firestore(ctx) suite.Require().NoError(err) - suite.repository = NewFirestoreRepository[Test](suite.fs) + suite.repository = NewFirestoreRepository[Test](suite.fs, "test") } func (suite *FirestoreRepositoryTestSuite) clearFirestoreData() {