FuturistiCoder / LiteDB.Realtime

A LiteDB with realtime notifications
MIT License
43 stars 10 forks source link
database litedb netstandard20 no-sql realtime-database xamarin-forms

LiteDB.Realtime

Build Status nuget

LiteDB.Realtime is a LiteDB with realtime notifications.

Get started

You can subscribe to a document or a total collection with System.Reactive easily.

List<Item> receivedItems = null;
Item receivedItem = null;

using (var db = new RealtimeLiteDatabase(new MemoryStream()))
{
    var newItem = new Item
    {
        Id = Guid.NewGuid(),
        Name = "Keyboard",
        Price = 100m
    };

    // docuement subscription
    // subscribe with System.Reactive extensions
    db.Realtime.Collection<Item>("items").Id(newItem.Id).Subscribe(item => receivedItem = item);

    // collection subscription
    // subscribe with System.Reactive extensions 
    db.Realtime.Collection<Item>("items").Subscribe(items => receivedItems = items);

    // insert new item
    db.GetCollection<Item>("items").Insert(newItem);

    // receivedItems: [ newItem ]
    // receivedItem: newItem
}

If you change the collection quickly, and you want to throttle the notifications.

// raw collection subscription
// this subscription will NOT retrieve the list of items each time, but a ILiteCollection<Item> instead.
// you can do what you want with this ILiteCollection<Item>
// subscribe with System.Reactive extensions
db.Reactive.Collection<Item>("items").Raw.Subscribe(col => /* col is type of ILiteCollection<Item> */);

// you can throttle the notifications by 1 second for example.
db.Realtime
    .Collection<Item>("items")
    .Raw
    .Throttle(TimeSpan.FromSecond(1)) // System.Reactive
    .Subscribe(col =>
    {
        var list = col.Query().OrderBy(i => i.Id).Limit(10).ToList();
        Update(list);
    });