Skip to content

Commit

Permalink
add query.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ekorman committed Jul 26, 2024
1 parent af8b66f commit 913735e
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions affine/query.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
from typing import TYPE_CHECKING, Type

from affine.collection import Collection, Filter, FilterSet

if TYPE_CHECKING:
from affine.engine import Engine


class QueryObject:
def __init__(self, db: "Engine", collection_class: Type[Collection]):
self.db = db
self.collection_class = collection_class

def filter(self, filter_set: FilterSet | Filter) -> list[Collection]:
if isinstance(filter_set, Filter):
filter_set = FilterSet(
filters=[filter_set], collection=filter_set.collection
)
return self.db._query(filter_set)

def all(self) -> list[Collection]:
return self.db._query(
FilterSet(filters=[], collection=self.collection_class.__name__)
)

def get_by_id(self, id_) -> Collection:
return self.db.get_element_by_id(self.collection_class, id_)

0 comments on commit 913735e

Please sign in to comment.