How to use Bunnet Document along with Beanie Document #9
Replies: 2 comments
-
Hey @yoshiya0503 , Technically it can not be inherited from both keeping both interfaces, as there are overlaps. I'm thinking about a solution to make it possible to switch the interfaces for each object/model. But as the original lib (Beanie) has already a significant number of features, there are many corner cases to be covered by this solution. For now, I would suggest splitting schemas and actual models + adding a factory. The implementation can vary - it depends on the use case. Here is an example of a simple case: import asyncio
from datetime import datetime
import motor.motor_asyncio
from beanie import Document as BeanieDocument, init_beanie
from bunnet import Document as BunnetDocument, init_bunnet
from pydantic import BaseModel, Field
from pymongo import MongoClient
class ModelSchema(BaseModel):
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
str_field: str
class SyncModel(BunnetDocument, ModelSchema):
...
class AsyncModel(BeanieDocument, ModelSchema):
...
class Model:
def __new__(cls, *args, is_sync: bool = True, **kwargs):
if is_sync:
return SyncModel(*args, **kwargs)
else:
return AsyncModel(*args, **kwargs)
async def main():
client = motor.motor_asyncio.AsyncIOMotorClient(
"mongodb://beanie:beanie@localhost:27017")
await init_beanie(
client.test_sync_async,
document_models=[AsyncModel]
)
client = MongoClient("mongodb://beanie:beanie@localhost:27017")
init_bunnet(
client.test_sync_async,
document_models=[SyncModel]
)
sync_doc = Model(is_sync=True, str_field="TEST VALUE")
print(sync_doc)
print(type(sync_doc))
# >>> created_at=datetime.datetime(2022, 12, 6, 9, 55, 32, 677951) updated_at=datetime.datetime(2022, 12, 6, 9, 55, 32, 677954) str_field='TEST VALUE' id=None revision_id=None
# >>> <class '__main__.SyncModel'>
async_doc = Model(is_sync=False, str_field="TEST VALUE")
print(async_doc)
print(type(async_doc))
# >>> created_at=datetime.datetime(2022, 12, 6, 9, 56, 5, 834904) updated_at=datetime.datetime(2022, 12, 6, 9, 56, 5, 834905) str_field='TEST VALUE' id=None revision_id=None
# >>> <class '__main__.AsyncModel'>
asyncio.run(main()) |
Beta Was this translation helpful? Give feedback.
-
Thanks for your reply.
I got a good hint. But I'm bothersome to redefine all our schemas,
so I try to switch schemas somehow dynamically.
Thanks in advance.
--------------------------------
# 株式会社 funnyface
# CEO: 伊藤 吉弥
# address: 東京都品川区北品川 1- 3- 16 プライムメゾン品川 315
# tel: 080-6023-4134
# mail: ***@***.***
# web: https://github.com/yoshiya0503
2022年12月6日(火) 19:02 Roman Right ***@***.***>:
… Hey @yoshiya0503 <https://github.com/yoshiya0503> ,
These libs are not compatible. It provides different interfaces for
different scenarios.
Technically it can not be inherited from both keeping both interfaces, as
there are overlaps. I'm thinking about a solution to make it possible to
switch the interfaces for each object/model. But as the original lib
(Beanie) has already a significant number of features, there are many
corner cases to be covered by this solution.
For now, I would suggest splitting schemas and actual models + adding a
factory. The implementation can vary - it depends on the use case. Here is
an example of a simple case:
import asynciofrom datetime import datetime
import motor.motor_asynciofrom beanie import Document as BeanieDocument, init_beaniefrom bunnet import Document as BunnetDocument, init_bunnetfrom pydantic import BaseModel, Fieldfrom pymongo import MongoClient
class ModelSchema(BaseModel):
created_at: datetime = Field(default_factory=datetime.utcnow)
updated_at: datetime = Field(default_factory=datetime.utcnow)
str_field: str
class SyncModel(BunnetDocument, ModelSchema):
...
class AsyncModel(BeanieDocument, ModelSchema):
...
class Model:
def __new__(cls, *args, is_sync: bool = True, **kwargs):
if is_sync:
return SyncModel(*args, **kwargs)
else:
return AsyncModel(*args, **kwargs)
async def main():
client = motor.motor_asyncio.AsyncIOMotorClient(
***@***.***:27017")
await init_beanie(
client.test_sync_async,
document_models=[AsyncModel]
)
client = ***@***.***:27017")
init_bunnet(
client.test_sync_async,
document_models=[SyncModel]
)
sync_doc = Model(is_sync=True, str_field="TEST VALUE")
print(sync_doc)
print(type(sync_doc))
# >>> created_at=datetime.datetime(2022, 12, 6, 9, 55, 32, 677951) updated_at=datetime.datetime(2022, 12, 6, 9, 55, 32, 677954) str_field='TEST VALUE' id=None revision_id=None
# >>> <class '__main__.SyncModel'>
async_doc = Model(is_sync=False, str_field="TEST VALUE")
print(async_doc)
print(type(async_doc))
# >>> created_at=datetime.datetime(2022, 12, 6, 9, 56, 5, 834904) updated_at=datetime.datetime(2022, 12, 6, 9, 56, 5, 834905) str_field='TEST VALUE' id=None revision_id=None
# >>> <class '__main__.AsyncModel'>
asyncio.run(main())
—
Reply to this email directly, view it on GitHub
<#9 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/ABIWNO7RZXXSECNLCU4N6D3WL4FMVANCNFSM6AAAAAASVHOTOQ>
.
You are receiving this because you were mentioned.Message ID:
***@***.***>
|
Beta Was this translation helpful? Give feedback.
-
Using bienie with fastAPI, it will work very fine.
But using with Typer(task runner of fastapi, it has 'sync' interface), it will not working, so we required 'Sync' interface.
However, we have to define database model inherit BunnetDocument along with BeanieDocument...
Therefore I try like this.
but not working.
Beta Was this translation helpful? Give feedback.
All reactions