Data Subscriptions: Common Data Subscription Creation Examples
In this page:
Create subscription on all documents in a collection
Create subscription with filtering
Create subscription with filtering and projection
Create subscription with load document in filter projection
Create revisions enabled subscription
Create subscription on all documents in a collection
Here we create a plain subscription on the Orders collection, without any constraint or transformation.
store.subscriptions.create(Order);
store.subscriptions.create({ query: "from Orders" });
Create subscription with filtering
Here we create a subscription on Orders collection, which total order revenue is greater than 100.
const 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`;
const name = await store.subscriptions.create({ query });
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.
const 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 order as o
where getOrderLinesSum(o) > 100
select projectOrder(o)`;
const name = await store.subscriptions.create({ query });
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.
const 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 order as o
where getOrderLinesSum(o) > 100
select projectOrder(o)`;
const name = await store.subscriptions.create({ query });
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.
const name = await store.subscriptions.createForRevisions(Order);
const name = await store.subscriptions.createForRevisions({
query: "from orders (Revisions = true)"
});