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 = store.subscriptions().create(Order.class);
SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery("from Orders");
name = store.subscriptions().create(options);

Create subscription with filtering

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

SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery("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 ");

name = store.subscriptions().create(options);

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.

SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery(" 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 order as o " +
    " where getOrderLinesSum(o) > 100 " +
    " select projectOrder(o)");

name = store.subscriptions().create(options);

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.

SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery(" 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 order as o " +
    " where getOrderLinesSum(o) > 100 " +
    " select projectOrder(o)");

name = store.subscriptions().create(options);

Create subscription with include statement

Here we create a subscription on Orders collection, which returns the orders and brings along all products mentioned in the order as included documents. See the usage example here.

Include statements supported only with raw RQL. Include statements come in two forms, like in any other RQL statements:
1. Include statement in the end of the query, starting with the include keyword, followed by paths to the field containing the ids of the documents to include.
If projection is performed, the mechanism will look for the paths in the projected result, rather then the original document.
It is recommended to prefer this approach when possible both because of clarity of the query and slightly better performance.
2. Include function call inside a 'declared' function.

SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery("from Orders include Lines[].Product");
store.subscriptions().create(options);
SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery("declare function includeProducts(doc) " +
    "   {" +
    "       doc.IncludedFields=0;" +
    "       doc.LinesCount = doc.Lines.length;" +
    "       for (let i=0; i< doc.Lines.length; i++)" +
    "       {" +
    "           doc.IncludedFields++;" +
    "           include(doc.Lines[i].Product);" +
    "       }" +
    "       return doc;" +
    "   }" +
    "   from Orders as o select includeProducts(o)");
store.subscriptions().create(options);

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().createForRevisions(Order.class);
SubscriptionCreationOptions options = new SubscriptionCreationOptions();
options.setQuery("from orders (Revisions = true)");
name = store.subscriptions().createForRevisions(Order.class, options);