Saturday, August 11, 2012

Awesome RavenDB Feature of the day: Eval Patching, Part II–Denormalized References

Awesome RavenDB Feature of the day: Eval Patching, Part II–Denormalized References:
I mentioned yesterday that I am keeping the best for today. What I am going to show you is how you can use Eval Patching for keeping track of denormalized references.
In this case, we have Users & Posts. Each Post contains a UserName property as well as the user id. When the user changes his name, we need to update all of the relevant posts.
Here is how you can do this:

store.DatabaseCommands.UpdateByIndex("Posts/ByUser",
new IndexQuery{Query = "UserId:" + userId},
new AdvancedPatchRequest
{
Script = @"
var user = LoadDocument(this.UserId);
this.UserName = user.Name;
"
});



And this is a really simple scenario, the options that this opens, the ability to load a separate document and modify the current document based on its value is really powerful.


DIGITAL JUICE

No comments:

Post a Comment

Thank's!