Skip to main content

Decorator for motor library

Project description

motor-decorator

This project be a decorator for motor lib. Structure and hide low code realisation to database access

Install:

pip
install
motor - decorator

Create fast database class using a motor-decorator:

from typing import Self
from motor_decorator import MotorDecoratorBaseDB, MotorDecoratorAbstractView, init_collection
from pydantic import Field
from bson import ObjectId


class VendorDatabaseView(MotorDecoratorAbstractView):
    id: ObjectId
    supplier_id: int
    api_key: str
    members: list[int] = Field(default_factory=list)

    @classmethod
    def from_db(cls, data: dict) -> Self:
        data["id"] = data.pop("_id")
        return cls(**data)


class VendorDB(MotorDecoratorBaseDB):
    CLUSTER = "MAIN"
    DATABASE = "F_VENDOR"
    VENDORS = "VENDORS"

    async def get_vendors(self) -> list[VendorDatabaseView]:
        await self.init_collection(self.VENDORS, check_existence=True)
        condition = {
            "KEY_NEW": {"$ne": None},
            "NEW_KEY_DISABLED": False,
            "SUPPLIER_ID": {
                "$ne": 0,
            }
        }
        projection = VendorDatabaseView.projection()
        vendors = await self.controller.do_find_many(condition, projection, VendorDatabaseView)
        return vendors

    # or
    @init_collection(VENDORS, check_existence=True)
    async def get_vendors(self) -> list[VendorDatabaseView]:
        condition = {
            "KEY_NEW": {"$ne": None},
            "NEW_KEY_DISABLED": False,
            "SUPPLIER_ID": {
                "$ne": 0,
            }
        }
        projection = VendorDatabaseView.projection()
        vendors = await self.controller.do_find_many(condition, projection, VendorDatabaseView)
        return vendors

Create a MongoDB clusters set and easily bind your database to the cluster:

# config.py
import os
from motor_decorator import add_cluster, profile_clusters

MONGO_HOST_MAIN = os.getenv("MONGO_HOST_MAIN")
MONGO_PORT_MAIN = os.getenv("MONGO_PORT_MAIN")
MONGO_USER_MAIN = os.getenv("MONGO_USER_MAIN")
MONGO_PASSWORD_MAIN = os.getenv("MONGO_PASSWORD_MAIN")

MONGO_HOST_LOCAL = os.getenv("MONGO_HOST_LOCAL")
MONGO_PORT_LOCAL = os.getenv("MONGO_PORT_LOCAL")
MONGO_USER_LOCAL = os.getenv("MONGO_USER_LOCAL")
MONGO_PASSWORD_LOCAL = os.getenv("MONGO_PASSWORD_LOCAL")


def init_clusters():
    add_cluster(
        cluster_name="MAIN",
        username=MONGO_USER_MAIN,
        password=MONGO_PASSWORD_MAIN,
        host=MONGO_HOST_MAIN,
        port=MONGO_PORT_MAIN,
        response_timeout=10_000
    )
    add_cluster(
        cluster_name="LOCAL",
        username=MONGO_USER_LOCAL,
        password=MONGO_PASSWORD_LOCAL,
        host=MONGO_HOST_LOCAL,
        port=MONGO_PORT_LOCAL,
        response_timeout=5_000
    )
    profile_clusters(extended_logs=True, log_level="DEBUG", ping=True)


init_clusters()

# or

import os
from motor_decorator import add_cluster, profile_clusters

MONGO_HOST_MAIN = os.getenv("MONGO_HOST_MAIN")
MONGO_PORT_MAIN = os.getenv("MONGO_PORT_MAIN")
MONGO_USER_MAIN = os.getenv("MONGO_USER_MAIN")
MONGO_PASSWORD_MAIN = os.getenv("MONGO_PASSWORD_MAIN")

MONGO_HOST_LOCAL = os.getenv("MONGO_HOST_LOCAL")
MONGO_PORT_LOCAL = os.getenv("MONGO_PORT_LOCAL")
MONGO_USER_LOCAL = os.getenv("MONGO_USER_LOCAL")
MONGO_PASSWORD_LOCAL = os.getenv("MONGO_PASSWORD_LOCAL")

add_cluster(
    cluster_name="MAIN",
    username=MONGO_USER_MAIN,
    password=MONGO_PASSWORD_MAIN,
    host=MONGO_HOST_MAIN,
    port=MONGO_PORT_MAIN,
    response_timeout=10_000
)
add_cluster(
    cluster_name="LOCAL",
    username=MONGO_USER_LOCAL,
    password=MONGO_PASSWORD_LOCAL,
    host=MONGO_HOST_LOCAL,
    port=MONGO_PORT_LOCAL,
    response_timeout=5_000
)
profile_clusters(extended_logs=True, log_level="DEBUG", ping=True)

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

motor-decorator-0.0.3.8.tar.gz (13.6 kB view details)

Uploaded Source

Built Distribution

motor_decorator-0.0.3.8-py3-none-any.whl (15.6 kB view details)

Uploaded Python 3

File details

Details for the file motor-decorator-0.0.3.8.tar.gz.

File metadata

  • Download URL: motor-decorator-0.0.3.8.tar.gz
  • Upload date:
  • Size: 13.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.11.7

File hashes

Hashes for motor-decorator-0.0.3.8.tar.gz
Algorithm Hash digest
SHA256 7eb49e9ffa44f2789290b541c865d67ab5424028419befa74121333ef9edd0af
MD5 9528539fdc308a79b9597bd3dcbee23e
BLAKE2b-256 53f67ad75afac589c7bdaef793092c7d0a2126c9f05210c10fb5b380fd43a5cd

See more details on using hashes here.

File details

Details for the file motor_decorator-0.0.3.8-py3-none-any.whl.

File metadata

File hashes

Hashes for motor_decorator-0.0.3.8-py3-none-any.whl
Algorithm Hash digest
SHA256 43e4407d4095dd400ac35385ab6ec28e980fbfcca2d673c16e93ed92d28a62b6
MD5 6f6c7e8df4b19db1ad24b802de46d171
BLAKE2b-256 19f13f5a26ac5d62850fab23c4778bf2836a75c34937d724ef704f441174fffa

See more details on using hashes here.

Supported by

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