Skip to content

Commit

Permalink
feat: included optional 'operator' parameters in methods of class Que…
Browse files Browse the repository at this point in the history
…ryManager
  • Loading branch information
AntonioVentilii committed Mar 30, 2024
1 parent 8a56559 commit f54ce52
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
16 changes: 10 additions & 6 deletions firestore_wrapper/query_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,32 @@ def __init__(self, credentials_path: str, database: str = None):
"""
super().__init__(credentials_path=credentials_path, database=database)

def get_collection_document_by_field(self, collection_name: str, field_name: str, field_value: str):
def get_collection_document_by_field(self, collection_name: str, field_name: str, field_value: str,
operator: str = '=='):
"""
Retrieves documents from a collection where the specified field has the specified value.
Retrieves documents from a collection where the field matches a value using the specified operator.
:param collection_name: The name of the collection.
:param field_name: The name of the field to filter by.
:param field_value: The value to match for the specified field.
:param operator: Optional operator to use for the filter; defaults to '=='.
:return: An iterable of DocumentSnapshot objects for documents matching the criteria.
"""
field_filter = FieldFilter(field_name, '==', field_value)
field_filter = FieldFilter(field_name, operator, field_value)
return self.db.collection(collection_name).where(filter=field_filter).stream()

def get_collection_data_by_field(self, collection_name: str, field_name: str, field_value: str) -> list[dict]:
def get_collection_data_by_field(self, collection_name: str, field_name: str, field_value: str,
operator: str = '==') -> list[dict]:
"""
Retrieves data for documents in a specified collection where the field matches a specified value.
Retrieves data for documents from a collection where the field matches a value using the specified operator.
:param collection_name: The name of the collection.
:param field_name: The field name to filter documents by.
:param field_value: The field value to search for.
:param operator: Optional operator to use for the filter; defaults to '=='.
:return: A list of dictionaries containing the data of matching documents.
"""
data = self.get_collection_document_by_field(collection_name, field_name, field_value)
data = self.get_collection_document_by_field(collection_name, field_name, field_value, operator=operator)
return [doc.to_dict() for doc in data]
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

setup(
name='firestore_wrapper',
version='0.2.1',
version='0.2.2',
packages=find_packages(),
description='A custom wrapper for Google Firestore.',
long_description=open('README.md').read(),
Expand Down

0 comments on commit f54ce52

Please sign in to comment.