Closed maletor closed 5 years ago
Hey, @maletor.
Can you nest BatchLoaders?
Yes, it should be possible. Here is some code example https://github.com/exAspArk/batch-loader/issues/23#issuecomment-417729982. And here is a spec for that:
Different example from a MessageType object with Users and Receipts (join table for users and messages).
I don't fully understand your code example, will need a bit more information about the context: what obj
instances are, which BatchLoader
belongs to which type, what are the relations in DB.
Hey @exAspArk,
If you have two objects you want to cache is there a way to prime
like with facebook dataloader?
If you have a has_many through relationship so in order to map the entities you have to go through a middle relation, a join table. How do you later access that join table query?
BatchLoader.for(message.id).batch(default_value: []) do |ids, loader|
receipts = MessageParticipant.where(message_id: ids)
receipts = Pundit.policy_scope!(auth_context, receipts)
receipts.each do |r|
loader.call(r.notification_id) do |memo|
memo << ::UserGenerator.by_id(auth_context, r.receiver_id)
end
end
I can't come back to receipts
now. It's just lost. Can I add it to another loader in the call
block? I tried to unsuccessfully. Suppose later I want to be able to do get the receipts by message id but instead of returning users return receipts, the join table.
Hey @maletor,
It depends on your specific situation. If you'd like to "cache" receipts
and use them in different loaders, you could try something like:
def cached_receipts(ids)
BatchLoader.for(message_ids).batch do |ids_ids, loader|
ids_ids.each do |ids|
receipts = MessageParticipant.where(message_id: ids)
receipts = Pundit.policy_scope!(auth_context, receipts)
loader.call(ids, receipts)
end
end
end
BatchLoader.for(message.id).batch(default_value: []) do |ids, loader|
cached_receipts(ids).each { |r| ... }
end
BatchLoader.for(message.id).batch(default_value: []) do |ids, loader|
cached_receipts(ids).each { |r| ... }
end
Going to close the issue.
If you have a Post with many Comments and you also want to query individual Comments outside of a given Post, it's not possible to batch all of those Comments together.
I'm running into a situation where I need a universal "cache" of objects; it's not enough to just nest them under one association.
Different example from a MessageType object with Users and Receipts (join table for users and messages).
How do you make it so that when a sender is also a recipient, it's not broken into a separate query? Can you nest BatchLoaders? I wasn't able to get any success with that.