Skip to content

timestamps support in embedded_schema #160

Open
@AlexKovalevych

Description

@AlexKovalevych

I wasn't able to get timestamps support in embedded_schema. In result timestamp fields are nil for all embedded documents but it works fine for regular documents. Do i miss something, is there a way to get timestamps support for embedded_schema?

Activity

bdtomlin

bdtomlin commented on Dec 6, 2021

@bdtomlin

I realize this issue is really old, but it is still an issue which I assume is why it's still open. Since mongoldb_ecto 1.0 is in beta, I assume this is a bug that would need to be fixed before the final release.

When using the timestamps macro from Ecto.Schema in an embedded schema, the inserted_at and updated_at field values are always nil/null.

scottmessinger

scottmessinger commented on Dec 8, 2021

@scottmessinger
Collaborator

@bdtomlin This is a good question! I'm not quite sure how that would be implemented, but my guess is it wouldn't be straightforward conceptually or technically and possibly impossible. Timestamps can be set on the root document whenever it's created/updated, but embedded documents don't have quite as clean of a lifecycle. For instance, I could insert a bunch of comments into a post document and then create the post -- what timestamp do the comments get? Is it the timestamp of when they were added to the post struct or when the post was inserted into the DB? In terms of implementation, my guess is the implementation of timestamps on the root document is straight forward (e.g. add a $set on all the insert* or update*` functions) but how do you implement it on embedded documents that might be embedded deep in a tree?

I'm going to close this for now as I don't see a path forward for this feature. However, if anyone has any ideas on how to implement this, I would welcome the PR and would love to hear ideas on how to make it happen!

bdtomlin

bdtomlin commented on Dec 8, 2021

@bdtomlin

@scottmessinger,

Mongoid handles this with what it calls cascading callbacks. That name gives a pretty decent hint of a way to handle it. https://docs.mongodb.com/mongoid/current/tutorials/mongoid-relations/#cascading-callbacks

I will try to come back to this in the future, but I think it would help to leave it open so it still has visibility for me or anyone else who may need this feature and has some time to work on it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @bdtomlin@scottmessinger@AlexKovalevych@joeapearson

        Issue actions

          timestamps support in embedded_schema · Issue #160 · elixir-mongo/mongodb_ecto