Awesome RavenDB Feature of the dayEval Patching, Part II–Denormalized References

time to read 2 min | 227 words

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:

    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.

More posts in "Awesome RavenDB Feature of the day" series:

  1. (26 Jul 2012) Eval Patching, Part II–Denormalized References
  2. (24 Jul 2012) Evil Patching