Skip to main content

It is an extension of the bisslog library to support processes with the pymongo driver library of the mongo db database.

Project description

bisslog-pymongo

It is an extension of the bisslog library to support processes with the pymongo driver library of the mongo db database.

Example usage

import os
from pymongo import MongoClient, DESCENDING
from abc import ABC, abstractmethod
from bisslog import Division, bisslog_db
from bisslog_pymongo import BasicPymongoHelper, bisslog_exc_mapper_pymongo


class MarketingDivision(Division, ABC):

    @abstractmethod
    def find_sales_per_client(self, id_client: int):
        raise NotImplementedError("find_sales_per_client must be implemented")


class MarketingMongoDivision(MarketingDivision, BasicPymongoHelper):

    @bisslog_exc_mapper_pymongo
    def find_sales_per_client(self, id_client: int):
        res = self.get_collection("sales").find({"id_client": id_client}).sort(
            {'created_at': DESCENDING})
        return self.stringify_list_identifier(res)


marketing_div = MarketingMongoDivision(MongoClient(os.environ["ATLAS_URI"]),
                                       database_name="marketing")

bisslog_db.register_adapters(marketing=marketing_div)

Components

PymongoHelper

It is just a class that receives the client and has some methods to facilitate the creation of pymongo adapters. the creation of pymongo adapters

bisslog_exception_handler_pymongo

Decorator to map PyMongo exceptions to their corresponding Bisslog exceptions.

Excepción de PyMongo Excepción de Bisslog
DuplicateKeyError IntegrityErrorExtException
BSONError InvalidDataExtException
CollectionInvalid ProgrammingErrorExtException
ConfigurationError ConfigurationExtException
NetworkTimeout TimeoutExtException
WriteError IntegrityErrorExtException
ConnectionFailure ConnectionExtException
OperationFailure OperationalErrorExtException
PyMongoError ExternalInteractionError

BasicPymongoHelper

BasicPymongoHelper is a helper class for interacting with MongoDB databases using PyMongo.
It provides convenient methods for performing CRUD operations while maintaining traceability through TransactionTraceable.

Initialization

BasicPymongoHelper(mongo_client: MongoClient, database_name: str = None)

Initializes the helper with a MongoClient instance.

If database_name is not provided, it uses the default database.

Methods

  • get_collection
get_collection(collection_name: str, database_name: str = None) -> Collection

Returns the collection object from the specified database.

If database_name is not provided, it defaults to the instance's database.

  • insert_one
insert_one(collection: str, obj: object = None, *, save_id: bool = False, **kwargs) -> str

Inserts a document into the specified collection.

If save_id is False, it removes the _id field before insertion.

Returns the inserted document's ID as a string.

  • find_one
find_one(collection: str, query: dict, selection: dict) -> dict

Retrieves a single document from the specified collection based on query and selection.

Converts the document’s _id to a string before returning.

  • stringify_identifier
@classmethod
stringify_identifier(cls, obj: dict) -> dict

Converts an ObjectId _id field to a string in a single document.

  • stringify_list_identifier
@classmethod
stringify_list_identifier(cls, list_obj: Iterator) -> list

Converts the _id field to a string in a list of documents.

  • verify_query
@staticmethod
verify_query(query: dict) -> dict

Validates and processes a query:

Converts _id field to ObjectId if necessary.

Ensures the query is a dictionary.

Returns None if validation fails.

  • get_length
get_length(collection: str, query: dict) -> int

Returns the number of documents matching the given query in a collection.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bisslog_pymongo-0.0.1.tar.gz (15.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bisslog_pymongo-0.0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file bisslog_pymongo-0.0.1.tar.gz.

File metadata

  • Download URL: bisslog_pymongo-0.0.1.tar.gz
  • Upload date:
  • Size: 15.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for bisslog_pymongo-0.0.1.tar.gz
Algorithm Hash digest
SHA256 5a547d85b74823a6ac8473489c945e68cfa611b2873a605455f3ad69240b39d7
MD5 d93e52f4ee51954469e3a6ca51c8398e
BLAKE2b-256 9adfa4da39e638c573de457d55339ccff80c618124c13a5f3f3a4f064b374485

See more details on using hashes here.

File details

Details for the file bisslog_pymongo-0.0.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bisslog_pymongo-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cb2c75440cf41d8c332f350499abaf22f82bff27570496e416f04412405756f4
MD5 695ed32d212acde324da46a1117594fb
BLAKE2b-256 1afb794bd55e95172b6416c0641f00cc84e7dd2adba06416167b85ff60fc0e2e

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page