Skip to content

Commit 47285fc

Browse files
committed
feat(collections): add __contains__ method for collection existence
- implement `__contains__` method to check if collection exists - enable use of `in` operator with collection names - handle exceptions gracefully when unable to retrieve collections
1 parent 58df75e commit 47285fc

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

src/typesense/collections.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,22 @@ def __init__(self, api_call: ApiCall):
5757
self.api_call = api_call
5858
self.collections: typing.Dict[str, Collection[TDoc]] = {}
5959

60+
def __contains__(self, collection_name: str) -> bool:
61+
"""
62+
Check if a collection exists in Typesense.
63+
64+
Args:
65+
collection_name (str): The name of the collection to check.
66+
67+
Returns:
68+
bool: True if the collection exists, False otherwise.
69+
"""
70+
try:
71+
all_collections = self.retrieve()
72+
except Exception:
73+
return False
74+
return any(coll["name"] == collection_name for coll in all_collections)
75+
6076
def __getitem__(self, collection_name: str) -> Collection[TDoc]:
6177
"""
6278
Get or create a Collection instance for a given collection name.

0 commit comments

Comments
 (0)