Converting the RavenDB Northwind database to a more denormalized form

In a previous blog post I demonstrated how to denormalize the RavenDB sample database and use the DenormalizedReference<T> and INamedDocument types from the RavenDB documentation to make life really sweet. That leaves us with one small problem and that is that the original sample database doesn’t work with our improved document design. With the sample database, small as it is, loading all document as a dynamic type, converting them and saving them would be easy enough but in a real database that would not be practical. So lets look at a better solution fixing the database.

 

Updating the database on the server

Instead of downloading each document, updating the structure and saving it back to the server it is much better to do these sort of actions on the server itself. Fortunately RavenDB has the capability to execute database commands on the server. These update commands can be PatchRequest objects that will let you do a large number of things using a nice C# API. And a the ultimate fallback there is the ScriptedPatchRequest which will let you execute a block of JavaScript code on the server. Why JavaScript? Well RavenDB stores things in JSON and the server is really not dependent on a .NET client.

Using the ScriptedPatchRequest we can either execute a patch on a single document or on a collection of documents. In this case I want to update all Order documents to reflect their new structure. It turns out this is quite simple

 

   1: using (IDocumentStore documentStore = new DocumentStore


   2: {


   3:     ConnectionStringName = "Northwind"


   4: }.Initialize())


   5: {


   6:     var javaScript = @"...";


   7:     


   8:     documentStore.DatabaseCommands.UpdateByIndex(


   9:         "Raven/DocumentsByEntityName",


  10:         new IndexQuery


  11:         {


  12:             Query = "Tag:Orders"


  13:         },


  14:         new ScriptedPatchRequest


  15:         {


  16:             Script = javaScript


  17:         });


  18: }

This code will execute the JavaScript code to patch the document once for each document in the Orders collection.

 

The JavaScript code to execute is quite simple, just make the changes required to the document and you are set.

   1: var company = LoadDocument(this.Company); 


   2: this.Company = {Id: this.Company, Name: company.Name};


   3:  


   4: var employee = LoadDocument(this.Employee);


   5: this.Employee = {Id: this.Employee, Name: employee.FirstName + ' ' + employee.LastName};


   6:  


   7: var shipVia = LoadDocument(this.ShipVia); 


   8: this.ShipVia = {Id: this.ShipVia, Name: shipVia.Name};


   9:  


  10: this.Lines.forEach(function(line){


  11:     var product = LoadDocument(line.Product); 


  12:     line.Product = {Id: line.Product, Name: product.Name};


  13:     delete line.ProductName;


  14: });



 





In this case I am converting the Company, Employee, ShipVia and Product properties to have the new structure. Additionally I am removing the ProductName from the OrderLine as that is no longer needed.



 



Sweet :-)

Leave a Reply

Your email address will not be published. Required fields are marked *


*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>