
    	,h
                     t    U d dl mZ d dlmZ d dlmZ d dlmZ d dlm	Z	  e	e
      ZdZeed<    G d d	      Zy)
    )MongoClient)
Collection)Database)settings)
get_loggerN	db_clientc                   ~     e Zd ZU dZdZeed<    fdZdefdZ	de
defdZdedefd	Zde
d
edefdZdefdZ xZS )MongoDBSingletonN_clientc                     | j                   St        | 	  |       | _         t        j                  }t        |      | j                   _        t        j                  d       | j                   S )NzConnected to MongoDB)		_instancesuper__new__r   MONGODB_URIr   r   loginfo)clsconnection_string	__class__s     2/var/www/html/RetailAPI/app/database/main/mongo.pyr   zMongoDBSingleton.__new__   sR    == !GOC0CM ( 4 4$/0A$BCMM!HH+,}}    returnc                 @    t         j                  }| j                  |   S )N)r   
MONGODB_DBr   )selfdb_names     r   get_main_dbzMongoDBSingleton.get_main_db   s    %%||G$$r   collection_namec                 r    | j                         }||j                         vr|j                  |       ||   S )z(Get a collection from the main database.)r   list_collection_namescreate_collection)r   r   dbs      r   get_collectionzMongoDBSingleton.get_collection   s:    "":":"<<  1/""r   
collectionc                 (    |j                  d|i      S )a  
        Retrieves an existing document from a MongoDB collection.

        Args:
            collection (Collection): The MongoDB collection.
            identifier: The identifier of the document.

        Returns:
            dict: The existing document if found, else None.
        _id)find_one)r   r$   
identifiers      r   get_existing_documentz&MongoDBSingleton.get_existing_document#   s     ""E:#677r   r(   c                     t        |t        t        f      st        d      | j	                  |      }|j                  d|i      duS )a  
        Checks if data exists in a MongoDB collection.

        Args:
            collection_name (str): The name of the collection.
            identifier: The identifier of the document.

        Returns:
            bool: True if data exists, False otherwise.
        z(Identifier must be an integer or string.r&   N)
isinstanceintstr	TypeErrorr#   r'   )r   r   r(   r$   s       r   is_data_in_dbzMongoDBSingleton.is_data_in_db0   sI     *sCj1FGG((9
""E:#67tCCr   c                    K   | j                  ||      }|r|j                  d|id|i       y d|i|}|j                  |       y w)Nr&   z$set)r)   
update_one
insert_one)r   r$   r(   dataexisting_documentnew_documents         r   insert_or_update_documentz*MongoDBSingleton.insert_or_update_documentB   sR      66z:N!!5*"5~F!:66L!!,/s   AA)__name__
__module____qualname__r   r   r   __annotations__r   r   r   r-   r   r#   dictr)   r,   boolr/   r6   __classcell__)r   s   @r   r
   r
      ss    IG[%X %#c #j #8
 84 8DS Dc Dd D$0* 0r   r
   )pymongor   pymongo.collectionr   pymongo.databaser   
app.configr   app.logr   r7   r   r   r:   r
    r   r   <module>rD      s2     ) %  	; <0 <0r   