Comments (10)
Hi @beratdogan ,
Yes. I plan to publish the first implementation this weekend.
It will support top-level fields only for now. The syntax will look like this:
class Window(Document):
x: int
y: int
class House(Document):
window: Link[Window]
name: str
Features, which it will support:
- fetching on find
- lazy fetching
- insert / cascade insert
- delete / cascade delete
from beanie.
Hello,
I love Beanie but just ran into this limitation in the current project. How far down the todo list is this feature?
It would be really good to have.
Br Johan
from beanie.
Hey @erajohl
Thank you.
Unfortunately, I didn't decide yet, how to implement this guy. The biggest problem is the query builder and trade-offs related to it.
from beanie.
Hi @roman-right - Any updates on this? Currently discussing to migrate our codebase using Beanie. Happy to contribute if you share your implementation thoughts to implement.
I'm stuck where/how should fetching/prefetching be done?
from beanie.
@roman-right -- It's great to hear you're actively working on this. I'm curious about implementation of the fetching/prefetching - it should not cause N+1 fetching problems. As my research on MongoDB drivers and they are all making N+1 queries for fetching from related collections. Making $lookup
aggregations or querying related docs at once by after collecting could help the performance.
Feature need: Support for optional relations Optional[Link[...]]
Naming suggestion: Referenced[]
might be more suitable name choice for this generic.
from beanie.
Hey @beratdogan ,
Yes, for the pre-fetching it will use lookup aggregations. This part is ready for single relations and will be implemented for lists soon. There is a small limitation for now related to this. It will support only top-level fields for now.
Later I'll try to support any field configuration.
from beanie.
+1
from beanie.
https://docs.mongoengine.org/apireference.html#mongoengine.fields.ReferenceField
https://pymongo.readthedocs.io/en/stable/api/bson/dbref.html
I assume we want to only do this explicitly instead of implicitly as it would make inheritance unworkable. Using the dbref field from pymongo has some advantages. Mongoengine's implementation is probably the best reference point. We can make a parallel setup by implementing a subclass of Fields. This would let us catch it and create a similar flow to mongoengine.
class Window(Document):
width: int
height: int
class House(Document):
address : str
windows: List[Window]
favorite_window: Window
class EmbeddedHouse(Document):
address : str
windows: List[Window] = Embed(...)
favorite_window: Window = Embed(...)
May need some kind of argument to handle List. It would be hard to implement anything similar to Lazy Reference without freaking pydantic validation out.
from beanie.
@zrothberg Good point.
I like your idea, thank you.
To solve the problem with pydantic validation it is possible to implement a custom field type. It will look like:
class Window(Document):
width: int
height: int
class House(Document):
address : str
windows: EmbedList[Window]
favorite_window: Embed[Window]
For sure it will have some black magic inside, but the problem can be solved with this.
from beanie.
The feature is in PR.
I'll play with naming a little this weekend and on Monday it will be released.
from beanie.
Related Issues (20)
- [BUG] Document.save() does not trigger Insert event even when saving a new document HOT 8
- [BUG]Pydantic2 Error : `pydantic.error_wrappers:ErrorWrapper` has been removed HOT 1
- [BUG] Inconsistent result type using upsert() HOT 1
- [BUG] values are not updated in the db after modification in @before_event Save event HOT 4
- [BUG] Query using link field, with fetch_links=False HOT 4
- [BUG] jsonable_encoder fails to convert PydanticObjectId for fastapi version 0.100.0 HOT 4
- [BUG] Error when find documents HOT 3
- [BUG] List type fields in updated model record do not get update. HOT 1
- [BUG] Using insert_many does not trigger insert action HOT 2
- [BUG] fetch links query does not recognize criteria with link's id inside $or operator
- Use Annotated to Indexed to complete type hints
- [BUG] Documents aren't being saved to MongoDB when bulk_writer is used with upsert HOT 1
- [BUG] get_settings() does not return the inherited settings HOT 1
- [BUG] Undefined behavior when chaining update methods HOT 1
- [BUG] Revision Id is in Responsemodel HOT 1
- [BUG] ModuleNotFoundError: No module named 'pydantic_settings' HOT 2
- [BUG] int64 behave unexpectedly HOT 5
- [BUG] Custom types like bson.Binary require `__get_pydantic_core_schema__` HOT 2
- merge_models does not merge list even if it does not include Link HOT 2
- [BUG] `Document.save_changes` doesn't work with pydantic v2 HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from beanie.