Comments (8)
Change Streams are not supported by Beanie yet. I plan to add them soon. Then, yes, I'll check if it is possible to have events based on them.
from beanie.
Hi! Can you please try using the Save
event in addition to Insert
in the list of events for the before_event
decorator
from beanie.
@roman-right That would trigger the action, but it would also trigger the same action when saving an existing document
For my use case above, I'd only like to set created_at
on insert.
In my project, I can just stick to intentionally using document.insert
for new documents. But your documentation around events needs to be updated. Happy to submit a PR
However, looking at the implementation of document.update()
, it creates a new _id if one is not on the object
if self.id is not None:
find_query: Dict[str, Any] = {"_id": self.id}
else:
find_query = {"_id": PydanticObjectId()}
This find_query with a new objectId will always return nothing, correct? If so, then I think we can call document.insert()
if there is no _id
instead of an update with upsert=True
Does that make sense? If so, happy to put a PR together
from beanie.
Hi @Russell-Pollari,
I wouldn't use the insert
method for this case because, even if there is an id, it doesn't guarantee that such an object exists in the database. It could be, for example, a UUID generated by the default factory during object creation. In that case, we would have two different database operations for the same scenario. I prefer to keep it consistent. However, I think it would be nice to handle upsert events. I'm not sure how it is currently possible, but I'll think about it.
Regarding the documentation improvements - yes, sure. It would be great if you could submit a PR.
from beanie.
In that case, we would have two different database operations for the same scenario. I prefer to keep it consistent.
Fair point. I'm inclined to agree
However, I think it would be nice to handle upsert events. I'm not sure how it is currently possible, but I'll think about it.
I will too. I feel like an After Upsert
event would be do-able, given the response from the db operation.
But not sure how to go about before upsert, other than first doing a query to determine if the document exists already. Which basically... makes using upsert
redundant
from beanie.
@roman-right
What about moving towards MongoDB Change Streams as event triggers?
https://www.mongodb.com/docs/manual/changeStreams/
from beanie.
This issue is stale because it has been open 30 days with no activity.
from beanie.
This issue was closed because it has been stalled for 14 days with no activity.
from beanie.
Related Issues (20)
- [BUG] Frozen fields not supported (Pydantic 2) - exception thrown on `.save()` HOT 4
- [BUG] Pylance: Every `.save()`, `.insert()`, `.update()` operation reports `Argument missing for parameter "self"` HOT 1
- [BUG] `Document.save` failed if the `_id` field is a customized field HOT 1
- [DOCUMENTATION] Update & Deleting give wrong example command HOT 1
- Discord link is broken in Documentation. HOT 1
- [BUG] Slient invalid document link resolution HOT 1
- [BUG] Migration documentation leads to misfunctioning migrations HOT 1
- [BUG] Migrations don't skip __init__.py file HOT 1
- [BUG] Multilevel related document lookups don't work HOT 1
- [BUG] Typing errors whenever `Link[SomeDoc]` is used (Pylance) HOT 7
- [BUG] From 1.24.0 the exception RevisionIdWasChanged is raised differently from 1.23.6 HOT 4
- [BUG] before_event, after_event can not trigger HOT 2
- find_one with update ignores sorting HOT 7
- [BUG] Argument of type `Callable` to `json_schema_extra` raises `AttributeError` HOT 1
- [BUG] Backlinks are not populated HOT 10
- [BUG] Fetching Links in view raises AttributeError: _database_major_version HOT 2
- [BUG] Double return in sources code of class beanie.odm.fields.Link HOT 4
- [BUG] Beanie projection and Pydantic Schema do not play well together HOT 6
- [BUG] before_event not working with validate_on_save = True HOT 2
- [BUG] ModuleNotFoundError when importing models in a migration module HOT 1
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.