Skip to content

Latest commit

 

History

History
77 lines (49 loc) · 2.83 KB

README.md

File metadata and controls

77 lines (49 loc) · 2.83 KB

Unit 3, Lesson 1 - Revisions

Hello and welcome to Unit 3. You already know the basics of using RavenDB. It is time to get started using some more advanced features.

In this lesson you will learn how to enable document revisions on your databases.

What are Revisions?

Every time you update, or even delete, a document, RavenDB 4 creates a snapshot (revision) of the previous state. More than that, it's useful when you need to track the history of the documents or when you need a full audit trail. 

You can choose to keep track of the last N revisions. If necessary, you could "track everything".

How to Enable Revisions

You can configure the revisions feature using the Studio: 

Enabling revisions

When activated, by default, RavenDB will track the history for all documents. Also, by default, RavenDB will never purge old revisions. 

As the administrator, you can configure this for all collections.  But you could specify a different setup for a specific collection. By default, RavenDB will track history for all documents.

The options you have are;

  • Purge on Delete: delete the revisions upon the document delete.
  • Limit # of revisions: How much revisions to keep.
  • Limit # of revisions by age: Configure a minimum retention time before the revisions can be expired.

You can configure it programmatically following the recommendations from the online documentation.

Exercise: Enable revisions for the Employees collection

Let's start enabling revisions to the Employees Collection.

Step 1: Open the Northwind database

Open the RavenDB Management Studio, then open the Northwind database.

Step 2: Opening the Document Revisions form

On the left panel, select the Settings section and then Document Revisions.

Step 3: Set up revisions for the Employees collection

  • On the top right, click on the Add a collection specific configuration option.
  • Select the Employees collection.
  • Enable Limit # of revisions and inform that you want to save 10 revisions.
  • Click on OK.
  • Click on Save

Limit of revisions

You can test revisions editing a document directly on the Studio. Edit any document and see the list of revisions.

Revisions list

Retrieving Revisions from a Document

Retrieving revisions for a document is pretty simple.

static void Main(string[] args)
{
    using (var session = DocumentStoreHolder.Store.OpenSession())
    {
        var revisions = session.Advanced.Revisions
            .GetFor<Employee>("employees/7-A");

        foreach (var revision in revisions)
        {
            // process revision
        }
    }
}

Great Job!

Let's move onto Lesson 2.