Giter Club home page Giter Club logo

Comments (8)

roman-right avatar roman-right commented on June 4, 2024 1

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.

roman-right avatar roman-right commented on June 4, 2024

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.

Russell-Pollari avatar Russell-Pollari commented on June 4, 2024

@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.

roman-right avatar roman-right commented on June 4, 2024

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.

Russell-Pollari avatar Russell-Pollari commented on June 4, 2024

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.

Russell-Pollari avatar Russell-Pollari commented on June 4, 2024

@roman-right
What about moving towards MongoDB Change Streams as event triggers?

https://www.mongodb.com/docs/manual/changeStreams/

from beanie.

github-actions avatar github-actions commented on June 4, 2024

This issue is stale because it has been open 30 days with no activity.

from beanie.

github-actions avatar github-actions commented on June 4, 2024

This issue was closed because it has been stalled for 14 days with no activity.

from beanie.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.