Code Monkey home page Code Monkey logo

documentdb-backups-csharp's Introduction

Azure DocumentDB - C# (6.0) library

Azure DocumentDB, or shortly DocumentDB, is a NoSQL document database service offered by Microsoft.

This library offers a set of classes to work with DocumentDB using .NET C# 6.0.

Backups

The framework also has a DocumentDB history backup solution. The backups will be created in a different collection (in the same database or in a different one).

The implementation is very simple and will only backup the documents. A complete Microsoft backup solution is already planned.

####Service definition:

    public class FoodService : DocumentDBService
    {
        public FoodService()
            : base(endPointUrl, authorizationKey, database)
        {
            // Create collection service
            ShoppingList = CreateCollectionService<Item>("ShoppingListCollection").Result;

            // Create a backup service for the shopping list collection
            BackupService = CreateCollectionBackupService(ShoppingList);
        }

        public ICollectionService<Item> ShoppingList { get; }
        public IBackupService BackupService { get; }
    }

####Create and restore backups:

using (var foodService = new FoodService())
{
    Console.WriteLine("\n> Create some documents:");
    foodService.ShoppingList.CreateDocument(new Item { Name = "Milk", Description = "Skimmed milk" }).Wait();
    foodService.ShoppingList.CreateDocument(new Item { Name = "Milk", Description = "Whole milk" }).Wait();
    foodService.ShoppingList.CreateDocument(new Item { Name = "Water", Description = "Mineral" }).Wait();
    
    foreach (var item in foodService.ShoppingList.AllDocuments)
    {
        Console.WriteLine($"- {item.Name}- {item.Description}- {item.Id}");
    }

    Console.WriteLine("\n> Find all items with name = Milk:");
    foreach (var item in foodService.ShoppingList.Where(d => d.Name.Equals("Milk")))
    {
        Console.WriteLine($"- {item.Name}- {item.Description}- {item.Id}");
    }

    Console.WriteLine("\n> Create a backup:");
    var backup = foodService.BackupService.CreateBackup("my backup").Result;
    Console.WriteLine("Backup created at" + backup.Timestamp);

    Console.WriteLine("\n> Delete water:");
    foreach (var item in foodService.ShoppingList.Where(d => d.Name.Equals("Water")))
    {
        Console.WriteLine($"Delete: {item.Name} - {item.Id}");
        foodService.ShoppingList.DeleteDocument(item.SelfLink).Wait();
    }

    Console.WriteLine("\n> Restore backup:");
    foodService.BackupService.RestoreBackup(backup);
    Console.WriteLine("Backup restored!");

    Console.WriteLine("\n> List all the documents:");
    foreach (var item in foodService.ShoppingList.AllDocuments)
    {
        Console.WriteLine($"- {item.Name}- {item.Description}- {item.Id}");
    }

    Console.WriteLine("\n> Delete database:");
    foodService.ShoppingList.DatabaseService.DeleteDatabase().Wait();
}

Class diagram

alt tag

Read more on my blog: http://softwarejuancarlos.com

documentdb-backups-csharp's People

Contributors

softwarejc avatar

Watchers

James Cloos avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.