Skip to content

afterLoad callback #219

Closed
Closed

Description

@dirkmc

Hi, thanks for creating this module, works great!

It would be nice if the afterLoad hook took a callback like the before hooks. For example if you need to retrieve something else from the database and add it to the object.

Activity

  1. dresende commented on Jul 5, 2013

    @dresende
    Owner

    I see, I'll see what we can do.

  2. dirkmc commented on Jul 5, 2013

    @dirkmc
    Author

    Thanks! Another thing that would be handy would be an afterFetch hook that gets triggered after related models have been autoFetched. If you like I can make an attempt to add these and create a pull request

  3. dresende commented on Jul 5, 2013

    @dresende
    Owner

    This one wasn't easy, found 2 (not related to this) edge cases. Please test it. I'm not sure it's 100% working but it seems so. About afterFetch, you mean when autoFetch = true and after getting associations?

  4. dirkmc commented on Jul 5, 2013

    @dirkmc
    Author

    Whoa, that was fast. Thanks! I'll test it out this morning.

    With respect to an afterFetch hook, yes, I found that when I tried to access associated models in afterLoad they were null.

    For example I have tables called Channel and User with a many-to-many relationship between them. I want to query for messages in all of the channels Fred is subscribed to. To make this query perform faster, when I load a User, I want to get the ids of all the channels that user is in and set that as a property on the User object, so that I can write a query like this:

    Message.find({ channel_id: user.channel_ids })
    
  5. dirkmc commented on Jul 5, 2013

    @dirkmc
    Author

    afterLoad works well, thank you!

  6. dresende commented on Jul 5, 2013

    @dresende
    Owner

    Here's an update. I called it afterAutoFetch to keep consistency with autoFetch option. Please remember this hook does not mean there are any associations, it just means that if exist, they were fetched.

  7. dirkmc commented on Jul 6, 2013

    @dirkmc
    Author

    Wow, thanks so much for your fast work on this, I really appreciate it!

    I have a couple of documentation suggestions:

    • Next to many of the hook method names it says "(no parameters)", but actually some of them take a next parameter
    • I needed to take a look at the source code to figure out that for a many to many relation
      • I could specify the name of the joining table (eg UserChannel)
      • I could specify the column name on the joining table (eg channel_id, user_id)
      • by default it is s_id (eg channels_id)

    It would be great if this were in the documentation in the many to many section.

    Muito obrigado diego!

  8. dresende commented on Jul 6, 2013

    @dresende
    Owner

    Thanks.

    Yes, the documentation needs some affection. About the hooks part, the next parameter is actually optional. It works (non blocking) without it :)

  9. dirkmc commented on Jul 13, 2013

    @dirkmc
    Author

    Hi Diego,
    I was thinking about this a bit more. When there are associated models with an auto fetch, I think it probably makes sense to wait until the associated models have been auto-fetched before calling the callback on find.
    For example

    Channel.find(3, function() { console.log(this.users) // at the moment this is null });
    

    Does that make sense to you?

  10. dresende commented on Jul 14, 2013

    @dresende
    Owner

    That should be what is happening. If not I have to take a look at it.

  11. dirkmc commented on Jul 16, 2013

    @dirkmc
    Author

    I haven't been able to reproduce this bug since I reported it. You can go ahead and close it and I'll reopen if I can create a reproducible test case.

  12. dresende commented on Jul 16, 2013

    @dresende
    Owner

    Ok :)

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

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions