Skip to main content

This package contains base code for all services e.g. Query DTO and Model Enums

Project description

QueryDTO and Related Classes

This repository contains a Python implementation of classes used for querying and filtering data models with the help of the Pydantic library. These classes aim to provide a standardized way to build flexible and reusable query objects, with support for sorting, filtering, pagination, and field population.


Table of Contents


Features

  • Model Identification: Enumerate specific models using the Model enum class.
  • Sorting: Specify fields and order (ascending/descending).
  • Filtering: Create flexible queries using operators, fields, and values.
  • Population: Specify which fields to include in the query.
  • Pagination: Limit results by defining page numbers and sizes.
  • Logical Operations: Define whether filters use AND or OR operations.

Classes

Model Enum

The Model Enum class defines constants for different models or entities.

class Model(Enum):
    blog = 1
    appliance = 2
    brand = 3
    product = 4
    faq = 5
    contact = 6
    advice_article = 7

Sort Class

Defines sorting parameters for a query.

class Sort(BaseModel):
    field: str  # Field to sort by
    order: str  # Order ('asc' for ascending, 'desc' for descending)

Filter Class

Defines filters to be applied to a query.

class Filter(BaseModel):
    operator: str  # Logical operator (e.g., 'eq', 'eqi', 'contains', 'notNull', 'and','or')
    field: str     # Field to filter by
    value: str     # Value to compare the field with

Populate Class

Specifies fields to include in the query response.

class Populate(BaseModel):
    field: str  # Field name to populate

Pagination Class

Defines pagination parameters for a query.

class Pagination(BaseModel):
    page: int      # Current page number
    pagesize: int  # Number of records per page

QueryDTO Class

The main Data Transfer Object (DTO) class for constructing queries. It includes options for model identification, filters, sorting, pagination, and field population.

class QueryDTO(BaseModel):
    model: Optional[int] = None  # Entity or module name (from the Model enum)
    id: Optional[int] = None  # -1 for list, >0 for specific record ID
    slug: Optional[str] = None  # Slug URLs
    sorts: Optional[List[Sort]] = None  # List of sorting parameters
    filters: Optional[List[Filter]] = None  # List of filters
    union_filter_operator: Optional[str] = "and"  # Logical operator ('and', 'or')
    populate: Optional[List[Populate]] = None  # Fields to include
    pagination: Optional[Pagination] = None  # Pagination details

Usage

To use these classes, import them into your project and instantiate them as needed. For example:

from your_module import QueryDTO, Sort, Filter, Populate, Pagination

# Example query
query = QueryDTO(
    model=1,  # Refers to 'blog'
    id=-1,  # Fetch a list
    sorts=[Sort(field="created_at", order="desc")],
    filters=[
        Filter(operator="=", field="status", value="published"),
        Filter(operator=">=", field="created_at", value="2023-01-01")
    ],
    union_filter_operator="and",
    populate=[Populate(field="author")],
    pagination=Pagination(page=1, pagesize=10)
)

Installation

  1. Clone the repository.
  2. Ensure you have Python 3.7 or later installed.
  3. Install Pydantic:
    pip install pydantic
    

License

This project is licensed under the MIT License. Feel free to use and modify it as needed.

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

ecomm_base-1.1.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

ecomm_base-1.1.0-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file ecomm_base-1.1.0.tar.gz.

File metadata

  • Download URL: ecomm_base-1.1.0.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for ecomm_base-1.1.0.tar.gz
Algorithm Hash digest
SHA256 85da876e23fe7a0ef1b3f8026c5fc2a36578a7b86ae7481cae7ff8001981ae9a
MD5 e0a5c579e1ee0ded775386088a493758
BLAKE2b-256 f5a230402e87b395a69ae82ce0548672083004596204355d41068151bd19e48e

See more details on using hashes here.

File details

Details for the file ecomm_base-1.1.0-py3-none-any.whl.

File metadata

  • Download URL: ecomm_base-1.1.0-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.1

File hashes

Hashes for ecomm_base-1.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2e059c2fdf3196eca77794932b490f342e636cff3add6e312d5fb3184ab9362b
MD5 197f22eb4d7746c850ce09e8a4234a11
BLAKE2b-256 5a85e8555f40a18f37d8d7085f89e7df2edd85d726be1b8e182ea278c3315f39

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