|
| 1 | +""" |
| 2 | +This module provides async functionality for managing collections in the Typesense API. |
| 3 | +
|
| 4 | +It contains the AsyncCollections class, which allows for creating, retrieving, and |
| 5 | +accessing individual collections asynchronously. |
| 6 | +
|
| 7 | +Classes: |
| 8 | + AsyncCollections: Manages collections in the Typesense API (async). |
| 9 | +
|
| 10 | +Dependencies: |
| 11 | + - typesense.async_api_call: Provides the AsyncApiCall class for making async API requests. |
| 12 | + - typesense.async_collection: Provides the AsyncCollection class for individual collection operations. |
| 13 | + - typesense.types.collection: Provides CollectionCreateSchema and CollectionSchema types. |
| 14 | + - typesense.types.document: Provides DocumentSchema type. |
| 15 | +
|
| 16 | +Note: This module uses conditional imports to support both Python 3.11+ and earlier versions. |
| 17 | +""" |
| 18 | + |
| 19 | +import sys |
| 20 | + |
| 21 | +if sys.version_info >= (3, 11): |
| 22 | + import typing |
| 23 | +else: |
| 24 | + import typing_extensions as typing |
| 25 | + |
| 26 | +from typesense.async_api_call import AsyncApiCall |
| 27 | +from typesense.async_collection import AsyncCollection |
| 28 | +from typesense.types.collection import CollectionCreateSchema, CollectionSchema |
| 29 | +from typesense.types.document import DocumentSchema |
| 30 | + |
| 31 | +TDoc = typing.TypeVar("TDoc", bound=DocumentSchema, covariant=True) |
| 32 | + |
| 33 | + |
| 34 | +class AsyncCollections(typing.Generic[TDoc]): |
| 35 | + """ |
| 36 | + Manages collections in the Typesense API (async). |
| 37 | +
|
| 38 | + This class provides async methods to create, retrieve, and access individual collections. |
| 39 | + It is generic over the document type TDoc, which should be a subtype of DocumentSchema. |
| 40 | +
|
| 41 | + Attributes: |
| 42 | + resource_path (str): The API endpoint path for collections operations. |
| 43 | + api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests. |
| 44 | + collections (Dict[str, AsyncCollection[TDoc]]): |
| 45 | + A dictionary of AsyncCollection instances, keyed by collection name. |
| 46 | + """ |
| 47 | + |
| 48 | + resource_path: typing.Final[str] = "/collections" |
| 49 | + |
| 50 | + def __init__(self, api_call: AsyncApiCall): |
| 51 | + """ |
| 52 | + Initialize the AsyncCollections instance. |
| 53 | +
|
| 54 | + Args: |
| 55 | + api_call (AsyncApiCall): The AsyncApiCall instance for making async API requests. |
| 56 | + """ |
| 57 | + self.api_call = api_call |
| 58 | + self.collections: typing.Dict[str, AsyncCollection[TDoc]] = {} |
| 59 | + |
| 60 | + async def __contains__(self, collection_name: str) -> bool: |
| 61 | + """ |
| 62 | + Check if a collection exists in Typesense. |
| 63 | +
|
| 64 | + This method tries to retrieve the specified collection to check for its existence, |
| 65 | + utilizing the AsyncCollection.retrieve() method but without caching non-existent collections. |
| 66 | +
|
| 67 | + Args: |
| 68 | + collection_name (str): The name of the collection to check. |
| 69 | +
|
| 70 | + Returns: |
| 71 | + bool: True if the collection exists, False otherwise. |
| 72 | + """ |
| 73 | + if collection_name in self.collections: |
| 74 | + try: |
| 75 | + await self.collections[collection_name].retrieve() |
| 76 | + return True |
| 77 | + except Exception: |
| 78 | + self.collections.pop(collection_name, None) |
| 79 | + return False |
| 80 | + |
| 81 | + try: |
| 82 | + await AsyncCollection(self.api_call, collection_name).retrieve() |
| 83 | + return True |
| 84 | + except Exception: |
| 85 | + return False |
| 86 | + |
| 87 | + def __getitem__(self, collection_name: str) -> AsyncCollection[TDoc]: |
| 88 | + """ |
| 89 | + Get or create an AsyncCollection instance for a given collection name. |
| 90 | +
|
| 91 | + This method allows accessing collections using dictionary-like syntax. |
| 92 | + If the AsyncCollection instance doesn't exist, it creates a new one. |
| 93 | +
|
| 94 | + Args: |
| 95 | + collection_name (str): The name of the collection to access. |
| 96 | +
|
| 97 | + Returns: |
| 98 | + AsyncCollection[TDoc]: The AsyncCollection instance for the specified collection name. |
| 99 | +
|
| 100 | + Example: |
| 101 | + >>> collections = AsyncCollections(async_api_call) |
| 102 | + >>> fruits_collection = collections["fruits"] |
| 103 | + """ |
| 104 | + if not self.collections.get(collection_name): |
| 105 | + self.collections[collection_name] = AsyncCollection( |
| 106 | + self.api_call, |
| 107 | + collection_name, |
| 108 | + ) |
| 109 | + return self.collections[collection_name] |
| 110 | + |
| 111 | + async def create(self, schema: CollectionCreateSchema) -> CollectionSchema: |
| 112 | + """ |
| 113 | + Create a new collection in Typesense. |
| 114 | +
|
| 115 | + Args: |
| 116 | + schema (CollectionCreateSchema): |
| 117 | + The schema defining the structure of the new collection. |
| 118 | +
|
| 119 | + Returns: |
| 120 | + CollectionSchema: |
| 121 | + The schema of the created collection, as returned by the API. |
| 122 | +
|
| 123 | + Example: |
| 124 | + >>> collections = AsyncCollections(async_api_call) |
| 125 | + >>> schema = { |
| 126 | + ... "name": "companies", |
| 127 | + ... "fields": [ |
| 128 | + ... {"name": "company_name", "type": "string"}, |
| 129 | + ... {"name": "num_employees", "type": "int32"}, |
| 130 | + ... {"name": "country", "type": "string", "facet": True}, |
| 131 | + ... ], |
| 132 | + ... "default_sorting_field": "num_employees", |
| 133 | + ... } |
| 134 | + >>> created_schema = await collections.create(schema) |
| 135 | + """ |
| 136 | + call: CollectionSchema = await self.api_call.post( |
| 137 | + endpoint=AsyncCollections.resource_path, |
| 138 | + entity_type=CollectionSchema, |
| 139 | + as_json=True, |
| 140 | + body=schema, |
| 141 | + ) |
| 142 | + return call |
| 143 | + |
| 144 | + async def retrieve(self) -> typing.List[CollectionSchema]: |
| 145 | + """ |
| 146 | + Retrieve all collections from Typesense. |
| 147 | +
|
| 148 | + Returns: |
| 149 | + List[CollectionSchema]: |
| 150 | + A list of schemas for all collections in the Typesense instance. |
| 151 | +
|
| 152 | + Example: |
| 153 | + >>> collections = AsyncCollections(async_api_call) |
| 154 | + >>> all_collections = await collections.retrieve() |
| 155 | + >>> for collection in all_collections: |
| 156 | + ... print(collection["name"]) |
| 157 | + """ |
| 158 | + call: typing.List[CollectionSchema] = await self.api_call.get( |
| 159 | + endpoint=AsyncCollections.resource_path, |
| 160 | + as_json=True, |
| 161 | + entity_type=typing.List[CollectionSchema], |
| 162 | + ) |
| 163 | + return call |
0 commit comments