Getting started

Starting with version 5.5, Siaqodb provides a full featured DocumentStore that allows you to store Documents (concept from Document Databases ) – full object graph is stored together as a unit/snapshot.

Documents are stored in buckets. A Bucket is a collection of Documents and it can be associated with tables in relational databases. Serialization engine of a Document is plug-able, so you can use JSON, BSON, Protocol Buffers, MsgPack or any other serialization engines.

To set your desired serialization plugin you have to implement Sqo.IDocumentSerializer interface and set it via SiaqodbConfigurator.SetDocumentSerializer(…).

So let's first create our desired serializer:

using Sqo;
using Newtonsoft.Json;

 internal class MyJsonSerializer : IDocumentSerializer
    {

        public object Deserialize(Type type, byte[] objectBytes)
        {
            string jsonStr = Encoding.UTF8.GetString(objectBytes);
            return JsonConvert.DeserializeObject(jsonStr.TrimEnd('\0'), type);
        }
        public byte[] Serialize(object obj)
        {
            string jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented);
            return Encoding.UTF8.GetBytes(jsonStr);
        }

    }

Now let's set the serializer so Siaqodb will use it to serialize/deserialize Documents:

Sqo.SiaqodbConfigurator.SetDocumentSerializer(new MyJsonSerializer());

Get a reference to a Bucket object

Once we have set the serializer, the next step is to open a Siaqodb database and get the reference to an IBucket:

using Sqo;
using Sqo.Documents;
....

Siaqodb siaqodb = new Siaqodb(@"c:\mydb\");
IBucket bucket=siaqodb.Documents["invoices"];

Create and store a document

Let's define a type of which instances will be stored as Document content:

public class Invoice
    {
        public string CustomerName { get; set; }
        public int InvoiceNumber { get; set; }
        public decimal Total { get; set; }
        public DateTime InvoiceDate { get; set; }

    }

And now lets create and store the document


 Invoice inv = new Invoice { CustomerName = "My Company", InvoiceDate=DateTime.Now, Total = 2390 };

 Document document=new Document();
 document.Key="Invoice-324r";
 document.SetContent<Invoice>(inv);

 bucket.Store(document);

If you choose to work directly with POCO objects, it's even simpler:


Invoice inv = new Invoice { CustomerName = "My Company", InvoiceDate=DateTime.Now, Total = 2390 };

bucket.Store("Invoice-324r",inv );

Find an existing Document

In this example we will read back an Invoice assuming we know the Key value:


Invoice inv = bucket.Load<Invoice>("Invoice-324r");

If you would need full Document, you can get it like this:


Document document = bucket.Load("Invoice-324r");
Invoice invoice = document.GetContent<Invoice>();

Update an existing Document


Document document = bucket.Load("Invoice-324r");

Invoice invoice = document.GetContent<Invoice>();
invoice.InvoiceDate=DateTime.Now.AddDays(-1);
document.SetContent<Invoice>(invoice);

bucket.Store(document);

Delete an existing Document

To delete an existing Document from a bucket you will have to do:

bucket.Delete("Invoice-324r");

Full Sample Program

using Newtonsoft.Json;
using Sqo;
using Sqo.Documents;
using System;
using System.Text;


namespace ConsoleProgram1
{
    internal class MyJsonSerializer : IDocumentSerializer
    {

        public object Deserialize(Type type, byte[] objectBytes)
        {
            string jsonStr = Encoding.UTF8.GetString(objectBytes);
            return JsonConvert.DeserializeObject(jsonStr.TrimEnd('\0'), type);
        }
        public byte[] Serialize(object obj)
        {
            string jsonStr = JsonConvert.SerializeObject(obj, Formatting.Indented);
            return Encoding.UTF8.GetBytes(jsonStr);
        }

    }
    public class Invoice
    {
        public string CustomerName { get; set; }
        public int InvoiceNumber { get; set; }
        public decimal Total { get; set; }
        public DateTime InvoiceDate { get; set; }

    }

    static class Program2
    {
        public static void Main(string[] args)
        {

            SiaqodbConfigurator.SetDocumentSerializer(new MyJsonSerializer());

            using (Siaqodb siaqodb = new Siaqodb(@"c:\db\"))
            {
                IBucket bucket = siaqodb.Documents["invoices"];

                Invoice inv = new Invoice { CustomerName = "My Company", InvoiceDate = DateTime.Now, Total = 2390 };

                Document document = new Document();
                document.Key = "Invoice-324r";
                document.SetContent<Invoice>(inv);

                bucket.Store(document);

                Document documentLoaded = bucket.Load("Invoice-324r");
                Invoice invoiceLoaded = documentLoaded.GetContent<Invoice>();
                invoiceLoaded.InvoiceDate = DateTime.Now.AddDays(-1);
                documentLoaded.SetContent<Invoice>(invoiceLoaded);

                bucket.Store(documentLoaded);

                bucket.Delete(documentLoaded);
            }




        }
    }
}