One of my favorite features within RavenDB is the Index Replication Bundle. We usually replicate our tables based on similarities in term of table schemas. In non-relational database is totally different, as there are no relationships between data, replications to a relational database could be difficult, prior to the domain complexity. The Index Replication Bundle is not about the actual replication, it is about the ability to extract necessary information that could be mapped to a relational database table, and then perform the replication.
Simply drop in the “Raven.Bundles.IndexReplication.dll” to your ~/Plugins directory.
Now we need to tell RavenDB, where we want to replicate to, in our case, we wanted to replicate to Microsoft SQL Server. You are not restricted to replicate to SQL Server, but you can replicate to almost every RDMS, for instance, replicating to Oracle, MySQL, PostgresSQL and many more.
NOTE: Make sure you stop your RavenDB running instance, before updating your configuration file “Raven.Server.exe.config”
Browse to your RavenDB Server directory, and open up the “Raven.Server.exe.config”
In your configuration, add in your ConnectionString settings.
That’s all, the necessary configurations are completed. Now let’s create our relational database, in this case, I’ll use Microsoft SQL Server.
SQL database schema.
Let’s create our “Customer” table, and set our table schema.
Note that the Id is an nvarchar, as RavenDB will replicate the Ids as strings “customers/5”.
RavenDB Index Creation.
As I have mentioned previously, that Replications from relational to non-relational is not possible, as in relational databases, you would map a table to another table that you want to replicate to.
In RavenDB, you have to extract the necessary information from your documents, and then you would be able to replicate to a relational database.
The extraction process is done through an Index creation. In an index we need to define which fields we are interested in.
That the index created, so know we need to initialize our RavenDB DocumentStore, and instruct RavenDB to create our Index.
Persisting Index Replication Destination.
At this stage, all we need to specify our replication destination settings. In RavenDB, we use the “IndexReplicationDestination” that has several properties, which allow us to define the necessary settings and once done, we’ll need to persist the settings to RavenDB.
Below is some explanation of each property:
Id: This is used to give the document that will hold you replication settings pre-defined id
ColumnsMapping: Is a KeyValuePair, which will allow you to map your RavenDB document field against relational database table column. The left string, represent your RavenDB document field, while the other refers to your relational database column.
PrimaryKeyColumnName: That is used to tell RavenDB that the column Id is my primary key on my relational database table.
Table: That’s your relational database table name.
After we have created our replication settings, now we need to store the replication settings to RavenDB.
First line of code is used to check that the database exists.
That’s all, RavenDB will replicate If any changes occurs to your documents (in my case Customer).
Source code available on: RavenDB IndexReplication.