Data Subscriptions: Common Data Subscription Creation Examples



Create subscription on all documents in a collection

Here we create a plain subscription on the Orders collection, without any constraint or transformation.

name = await store.Subscriptions.CreateAsync(new SubscriptionCreationOptions<Order>
{
    Name = "OrdersProcessingSubscription"                
});
name = await store.Subscriptions.CreateAsync(new SubscriptionCreationOptions()
{
    Query = "From Orders"
});

Create subscription with filtering

Here we create a subscription on Orders collection, which total order revenue is greater than 100.

name = await store.Subscriptions.CreateAsync<Order>(x =>
    x.Lines.Sum(line => line.PricePerUnit * line.Quantity) > 100);
name = await store.Subscriptions.CreateAsync(new SubscriptionCreationOptions()
{
    Query = @"
                declare function getOrderLinesSum(doc){
                    var sum = 0;
                    for (var i in doc.Lines) { sum += doc.Lines[i];}
                    return sum;
                }
                From Orders as o 
                Where getOrderLinesSum(o) > 100"
});

Create subscription with filtering and projection

Here we create a subscription on Orders collection, which total order revenue is greater than 100, and return only ID and total revenue.

name = store.Subscriptions.Create(
    new SubscriptionCreationOptions<Order>()
    {
        Filter = x => x.Lines.Sum(line => line.PricePerUnit * line.Quantity) > 100,
        Projection = x => new
        {
            Id = x.Id,
            Total = x.Lines.Sum(line => line.PricePerUnit * line.Quantity)
        }
    });
name = await store.Subscriptions.CreateAsync(new SubscriptionCreationOptions()
{
    Query = @"declare function getOrderLinesSum(doc){
                    var sum = 0;
                    for (var i in doc.Lines) { sum += doc.Lines[i];}
                    return sum;
                }

                declare function projectOrder(doc){
                    return {
                        Id: order.Id,
                        Total: getOrderLinesSum(order)
                    };
                }

                From Orders as o 
                Where getOrderLinesSum(o) > 100
                Select projectOrder(o)"
});

Create subscription with load document in filter projection

Here we create a subscription on Orders collection, which total order revenue is greater than 100, and return ID, total revenue, shipping address and responsible employee name.

name = store.Subscriptions.Create(
    new SubscriptionCreationOptions<Order>()
    {
        Filter = x => x.Lines.Sum(line => line.PricePerUnit * line.Quantity) > 100,
        Projection = x => new
        {
            Id = x.Id,
            Total = x.Lines.Sum(line => line.PricePerUnit * line.Quantity),
            ShipTo = x.ShipTo,
            EmployeeName = RavenQuery.Load<Employee>(x.Employee).FirstName + " " +
                           RavenQuery.Load<Employee>(x.Employee).LastName
        }
    });
name = await store.Subscriptions.CreateAsync(new SubscriptionCreationOptions()
{
    Query = @"declare function getOrderLinesSum(doc){
                    var sum = 0;
                    for (var i in doc.Lines) { sum += doc.Lines[i];}
                    return sum;
                }

                declare function projectOrder(doc){
                    var employee = LoadDocument(doc.Employee);
                    return {
                        Id: order.Id,
                        Total: getOrderLinesSum(order),
                        ShipTo: order.ShipTo,
                        EmployeeName: employee.FirstName + ' ' + employee.LastName

                    };
                }

                From Orders as o 
                Where getOrderLinesSum(o) > 100
                Select projectOrder(o)"
});

Create revisions enabled subscription

Here we create a subscription on Orders collection, which returns current and previous version of the subscriptions. Please see the page dedicated to subscriptions with revisions for more details and examples.

name = store.Subscriptions.Create(
    new SubscriptionCreationOptions<Revision<Order>>());
name = await store.Subscriptions.CreateAsync(new SubscriptionCreationOptions()
{
    Query = @"From Orders (Revisions = true)"
});