# Path: virtool/types.py



import asyncio
import math
import re
from typing import Dict, List, Optional, Tuple, Union
from aiohttp.web import Request
from multidict import MultiDictProxy
from virtool.types import Projection
from virtool.utils import coerce_list, to_bool
def compose_regex_query(term, fields: List[str]) -> Dict[str, List[Dict[str, dict]]]:
    """
    Compose a MongoDB query that checks if the values of the passed `fields` match the
    passed search `term`.

    :param term: the term to search
    :param fields: the list of field to match against
    :return: a query

    """
    if not isinstance(fields, (list, tuple)):
        raise TypeError("Type of 'fields' must be one of 'list' or 'tuple'")

    # Stringify fields.
    fields = [str(field) for field in coerce_list(fields)]

    term = re.escape(term)

    # Compile regex, making is case-insensitive.
    regex = re.compile(str(term), re.IGNORECASE)

    # Compose and return $or-based query.
    return {"$or": [{field: {"$regex": regex}} for field in fields]}


async def paginate(
    collection,
    db_query: Union[Dict, MultiDictProxy[str]],
    url_query: Union[Dict, MultiDictProxy[str]],
    sort: Optional[Union[List[Tuple[str, int]], str]] = None,

# Path: virtool/types.py



# Path: virtool/api/utils.py
import asyncio
import math
import re
from typing import Dict, List, Optional, Tuple, Union
from aiohttp.web import Request
from multidict import MultiDictProxy
from virtool.types import Projection
from virtool.utils import coerce_list, to_bool

def compose_regex_query(term, fields: List[str]) -> Dict[str, List[Dict[str, dict]]]:
    """
    Compose a MongoDB query that checks if the values of the passed `fields` match the
    passed search `term`.

    :param term: the term to search
    :param fields: the list of field to match against
    :return: a query

    """
    if not isinstance(fields, (list, tuple)):
        raise TypeError("Type of 'fields' must be one of 'list' or 'tuple'")

    # Stringify fields.
    fields = [str(field) for field in coerce_list(fields)]

    term = re.escape(term)

    # Compile regex, making is case-insensitive.
    regex = re.compile(str(term), re.IGNORECASE)

    # Compose and return $or-based query.
    return {"$or": [{field: {"$regex": regex}} for field in fields]}


async def paginate(
    collection,
    db_query: Union[Dict, MultiDictProxy[str]],
    url_query: Union[Dict, MultiDictProxy[str]],
    sort: Optional[Union[List[Tuple[str, int]], str]] = None,

    projection: Optional[Projection] = None,