Skip to content

Commit

Permalink
async_pymongo: Allow accessing database and collection as an attribute (
Browse files Browse the repository at this point in the history
#10)

Signed-off-by: wulan17 <wulan17@nusantararom.org>
  • Loading branch information
wulan17 committed Oct 15, 2024
1 parent dff9bd2 commit 41f1d2b
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 0 deletions.
3 changes: 3 additions & 0 deletions async_pymongo/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ def __init__(self, *args: Any, **kwargs: Any) -> None:
# Propagate initialization to base
super().__init__(dispatch)

def __getattr__(self, name: str) -> AsyncDatabase:
return AsyncDatabase(self, self.dispatch[name])

def __getitem__(self, name: str) -> AsyncDatabase:
return AsyncDatabase(self, self.dispatch[name])

Expand Down
3 changes: 3 additions & 0 deletions async_pymongo/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@ def __init__(self, client: "AsyncClient", database: Database) -> None:
def __bool__(self) -> bool:
return self.dispatch is not None

def __getattr__(self, name) -> AsyncCollection:
return AsyncCollection(self, name)

def __getitem__(self, name) -> AsyncCollection:
return AsyncCollection(self, name)

Expand Down

0 comments on commit 41f1d2b

Please sign in to comment.