Skip to content

Commit

Permalink
DH-4537/sorting the tables and column (#166)
Browse files Browse the repository at this point in the history
  • Loading branch information
MohammadrezaPourreza authored Sep 18, 2023
1 parent 6773689 commit e7cb445
Show file tree
Hide file tree
Showing 4 changed files with 8 additions and 4 deletions.
2 changes: 1 addition & 1 deletion dataherald/db/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def find_by_id(self, collection: str, id: str) -> dict:
pass

@abstractmethod
def find(self, collection: str, query: dict) -> list:
def find(self, collection: str, query: dict, sort: list = None) -> list:
pass

@abstractmethod
Expand Down
4 changes: 3 additions & 1 deletion dataherald/db/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,9 @@ def find_by_id(self, collection: str, id: str) -> dict:
return self._data_store[collection].find_one({"_id": ObjectId(id)})

@override
def find(self, collection: str, query: dict) -> list:
def find(self, collection: str, query: dict, sort: list = None) -> list:
if sort:
return self._data_store[collection].find(query).sort(sort)
return self._data_store[collection].find(query)

@override
Expand Down
4 changes: 3 additions & 1 deletion dataherald/db_scanner/repository/base.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from typing import List

from bson.objectid import ObjectId
from pymongo import ASCENDING

from dataherald.db_scanner.models.types import TableSchemaDetail

Expand Down Expand Up @@ -68,10 +69,11 @@ def find_all(self) -> list[TableSchemaDetail]:

def find_by(self, query: dict) -> list[TableSchemaDetail]:
query = {k: v for k, v in query.items() if v}
rows = self.storage.find(DB_COLLECTION, query)
rows = self.storage.find(DB_COLLECTION, query, sort=[("table_name", ASCENDING)])
result = []
for row in rows:
obj = TableSchemaDetail(**row)
obj.columns = sorted(obj.columns, key=lambda x: x.name)
obj.id = str(row["_id"])
result.append(obj)
return result
2 changes: 1 addition & 1 deletion dataherald/tests/db/test_db.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def find_by_id(self, collection: str, id: str) -> dict:
return None

@override
def find(self, collection: str, query: dict) -> list:
def find(self, collection: str, query: dict, sort: list = None) -> list:
return []

@override
Expand Down

0 comments on commit e7cb445

Please sign in to comment.