{"id":1964,"date":"2011-09-07T15:16:06","date_gmt":"2011-09-07T14:16:06","guid":{"rendered":"http:\/\/www.gamlor.info\/wordpress\/?p=1964"},"modified":"2021-07-03T13:37:48","modified_gmt":"2021-07-03T12:37:48","slug":"ravendb-bundles-cascade-deletion-document-versioning","status":"publish","type":"post","link":"https:\/\/www.gamlor.info\/wordpress\/2011\/09\/ravendb-bundles-cascade-deletion-document-versioning\/","title":{"rendered":"RavenDB: Bundles, Cascade Deletion, Document Versioning"},"content":{"rendered":"<p>Today we take a look at how we can install additional functionality to RavenDB with bundles. Then we take a brief look at the cascading delete and versioning bundles.<\/p>\n<h2>Bundles \/ Plugins<\/h2>\n<p>The default RavenDB installation only includes basic features. More advanced functionality can be installed with bundles, like versioning of documents, expiration of documents, authorization or cascade delete functionality. Take a look at this page for a <a href=\"http:\/\/ravendb.net\/bundles\">list of bundles<\/a>.<\/p>\n<div id=\"attachment_1980\" style=\"width: 212px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDBBundles.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1980\" class=\"size-medium wp-image-1980\" title=\"RavenDBBundles\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDBBundles-202x300.png\" alt=\"RavenDB Bundle\" width=\"202\" height=\"300\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDBBundles-202x300.png 202w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDBBundles.png 400w\" sizes=\"(max-width: 202px) 100vw, 202px\" \/><\/a><p id=\"caption-attachment-1980\" class=\"wp-caption-text\">RavenDB Bundle<\/p><\/div>\n<p>&nbsp;<\/p>\n<p>A bundle is easy to install. Create a \u2018Plugins\u2019 folder next to RavenDB server location and put the bundle-assembly in there. The bundles shipped with RavenDB are in the \u2018Bundles\u2019 directory of the RavenDB distribution.<\/p>\n<h2>Cascading Deletion Bundle<\/h2>\n<p>RavenDB itself doesn\u2019t have any cascading delete functionally. However the <a href=\"http:\/\/ravendb.net\/bundles\/cascade-delete\">cascade delete bundle<\/a> brings basic support for that. We just need to drop the \u2018Raven.Bundles.CascadeDelete.dll\u2019 assembly into the \u2018Plugins\u2019 directory and start up the server.<\/p>\n<p>After that we specify which documents are deleted by adding the ids to a special meta-data field. For example we can add ids of comments to the blog post. When the post document is deleted the comment documents will be deleted as well:<\/p>\n<script src=\"https:\/\/gist.github.com\/1195751.js?file=CascadeDeleteDocument.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">var comment = new Comment()\n     {\n          PostId = post.Id\n     };\nsession.Store(comment);\n\nsession.Advanced.GetMetadataFor(post)[&quot;Raven-Cascade-Delete-Documents&quot;] \n\t= new RavenJArray(new []{comment.Id});<\/code><\/pre><\/noscript>\n<p>It\u2019s also possible to delete attachments with this functionality:<\/p>\n<script src=\"https:\/\/gist.github.com\/1195751.js?file=CascadeDeleteAttachement.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">session.Advanced.GetMetadataFor(post)[&quot;Raven-Cascade-Delete-Attachments&quot;] =  \n\tnew RavenJArray(new[] { &quot;picture\/1&quot; });<\/code><\/pre><\/noscript>\n<div id=\"attachment_1984\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/CascadeDelete.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1984\" class=\"size-medium wp-image-1984\" title=\"CascadeDelete\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/CascadeDelete-300x282.png\" alt=\"RavenDB Cascade Delete\" width=\"300\" height=\"282\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/CascadeDelete-300x282.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/CascadeDelete.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1984\" class=\"wp-caption-text\">RavenDB Cascade Delete<\/p><\/div>\n<p>&nbsp;<\/p>\n<h2>Document Versioning<\/h2>\n<p>Another functionality that we can add is <a href=\"http:\/\/ravendb.net\/bundles\/versioning\">document versioning<\/a>. This bundle will keep a copy of the document whenever we store, update or delete a document. This enables us to track changes over time.<\/p>\n<p>Again add the \u2018Raven.Bundles.Versioning.dll\u2019 to the Plugins directory to install it on the server. Also add the \u2018Raven.Bundles.Versioning.dll\u2019 and the \u2018Raven.Client.Versioning.dll\u2019 assembly to your project.<\/p>\n<p>To configure we store a configuration document. The VersioningConfiguration-instance with the Id \u2018Raven\/Versioning\/DefaultConfiguration\u2019 represents the default. When we set the Exclude flag to true, then documents are not versioned by default. Otherwise as many revisions as specified in MaxRevisions are kept:<\/p>\n\n<p>We can configure the versioning behavior for each document type. We do this by storing a VersioningConfiguration instance with an id which refers to the document collection. Like this:<\/p>\n<script src=\"https:\/\/gist.github.com\/1195751.js?file=Revisions-For-Specific-Type.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">session.Store(new VersioningConfiguration\n{\n\tExclude = false,\n\tId = &quot;Raven\/Versioning\/BlogPosts&quot;,\n\tMaxRevisions = 15\n});<\/code><\/pre><\/noscript>\n<div id=\"attachment_1982\" style=\"width: 310px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDB-History.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-1982\" class=\"size-medium wp-image-1982\" title=\"RavenDB-History\" src=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDB-History-300x289.png\" alt=\"RavenDB Revisions\" width=\"300\" height=\"289\" srcset=\"https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDB-History-300x289.png 300w, https:\/\/www.gamlor.info\/wordpress\/wp-content\/uploads\/2011\/09\/RavenDB-History.png 600w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><p id=\"caption-attachment-1982\" class=\"wp-caption-text\">RavenDB Revisions<\/p><\/div>\n<p>&nbsp;<\/p>\n<h2>Accessing the Versions<\/h2>\n<p>Now RavenDB starts to version our documents. Versions are just stored as regular documents with a special id. However they are hidden from the indexers, so they don\u2019t show up in any query. So how do we access these documents? We access them by loading them by id. Like this:<\/p>\n<script src=\"https:\/\/gist.github.com\/1195751.js?file=Getting-Old-Revision.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">var oldRevision = session.Load&lt;BlogPost&gt;(&quot;blogposts\/1\/revisions\/1&quot;);<\/code><\/pre><\/noscript>\n<p>The revision number is just attached to the document id:<\/p>\n<script src=\"https:\/\/gist.github.com\/1195751.js?file=Getting-Old-Revision-via-Id.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">var oldRevision = session.Load&lt;BlogPost&gt;(post.Id + &quot;\/revisions\/1&quot;);<\/code><\/pre><\/noscript>\n<p>And how do we find out what the latest revision is? We can use the \u2018GetRevisionsFor\u2018 extension method. We specify the id of the document, where we want to start and how many revisions we want to get. Like this:<\/p>\n<script src=\"https:\/\/gist.github.com\/1195751.js?file=Last-Revsions.cs\"><\/script><noscript><pre><code class=\"language-c# c#\">\/\/ Get the last three revisions\nvar lastThreeVersions = session.Advanced.GetRevisionsFor&lt;BlogPost&gt;(post.Id, 0, 3);<\/code><\/pre><\/noscript>\n<h2>Conclusion<\/h2>\n<p>RavenDB Bundles give us optional, useful features. Make sure that you look for a <a href=\"http:\/\/ravendb.net\/bundles\">bundle<\/a> before implementing some functionality yourself.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Today we take a look at how we can install additional functionality to RavenDB with bundles. Then we take a brief look at the cascading delete and versioning bundles. Bundles&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":"","_links_to":"","_links_to_target":""},"categories":[126,231],"tags":[21,296],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1964"}],"collection":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/comments?post=1964"}],"version-history":[{"count":19,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1964\/revisions"}],"predecessor-version":[{"id":3901,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/posts\/1964\/revisions\/3901"}],"wp:attachment":[{"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/media?parent=1964"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/categories?post=1964"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gamlor.info\/wordpress\/wp-json\/wp\/v2\/tags?post=1964"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}