Create and Update Subscription API
Create subscription
Subscriptions can be created using the following create
methods available through the subscriptions
property of the DocumentStore
.
// Available overloads:
// ====================
create(options);
create(options, database);
create(documentType);
Parameter | Type | Description |
---|---|---|
options | object |
The subscription creation options. |
database | string |
The name of the database where the subscription task will be created. If null , the default database configured in the DocumentStore will be used. |
documentType | object |
The class type from which the collection of documents managed by the subscription will be derived. |
Return value | Description |
---|---|
Promise |
A Promise that resolves to the name of the created data subscription (a string ).If the name was provided in the subscription creation options, it will be returned. Otherwise, a unique name will be generated by server. |
Examples for creating subscriptions are available here.
Subscription creation options
// The SubscriptionCreationOptions object:
// =======================================
{
name;
query;
includes;
changeVector;
mentorNode;
pinToMentorNode;
disabled;
documentType;
}
Member | Type | Description |
---|---|---|
name | string |
User defined name for the subscription. The name must be unique in the database. |
query | string |
RQL query that defines the subscription. This RQL comes with additional support to JavaScript functions inside the where clause and special semantics for subscriptions on documents revisions.Learn more in subscription RQL. |
includes | (builder) => void |
A function that accepts a builder object, which allows you to include related documents, counters, and time series in the batch that is sent to the client. See Include methods. |
changeVector | string |
Allows to define a change vector from which the subscription will start processing. Useful for ad-hoc processes that need to process only recent changes. In such cases, you can set the field to "LastDocument" to start processing from the latest document in the collection. |
mentorNode | string |
Allows to define a specific node in the cluster to handle the subscription. Useful when you prefer a specific server due to its stronger hardware, closer geographic proximity to clients, or other reasons. |
pinToMentorNode | boolean |
true - task will only be handled by the specified mentor node.false - When the specified mentor node is down, the cluster selects another node from the Database Group to handle the task.Learn more in pinning a task. |
disabled | boolean |
true - the created subscription will be in a disabled state.false (default) - the created subscription will be enabled. |
documentType | object |
The class type from which the collection of documents managed by the subscription will be derived. |
Include methods
Including documents:
includeDocuments(path);
Parameter | Type | Description |
---|---|---|
path | string |
Path to the property which contains ID of document to include. |
An example of including documents is available here.
Including counters:
// Include a single counter
includeCounter(name);
// Include multiple counters
includeCounters(names);
// Include ALL counters from ALL documents that match the subscription criteria
includeAllCounters();
Parameter | Type | Description |
---|---|---|
name | string |
The name of a counter. The subscription will include all counters with this name that are contained in the documents the subscription retrieves. |
names | string[] |
Array of counter names. |
An example of including counters is available here.
Including time series:
includeTimeSeries(name, type, time);
includeTimeSeries(name, type, count);
includeTimeSeries(names, type, time);
includeTimeSeries(names, type, count);
includeAllTimeSeries(type, time);
includeAllTimeSeries(type, count);
Parameter | Type | Description |
---|---|---|
name | string |
The name of the time series to include. |
names | string[] |
The names of the time series to include. |
type | string |
Indicates how to retrieve the time series entries. Range type can be: "None" or "Last" .When set to Last, retrieve the last X entries, where X is determined by count. |
time | TimeValue |
The time range to consider when retrieving time series entries. E.g.: TimeValue.ofDays(7) |
count | number |
The maximum number of entries to take when retrieving time series entries. |
Update subscription
Existing subscriptions can be modified using the following update
methods available through the subscriptions
property of the DocumentStore
.
// Available overloads:
// ====================
update(options);
update(options, database);
Parameter | Type | Description |
---|---|---|
options | object |
The subscription update options. |
database | string |
The name of the database where the subscription task resides. If null , the default database configured in the DocumentStore will be used. |
Return value | Description |
---|---|
Promise |
A Promise that resolves to the name of the updated data subscription (a string ). |
Examples for updating an existing subscription are available here.
Subscription update options
The subscription update options object extends the creation options object and adds two additional fields:
// The SubscriptionUpdateOptions object:
// =====================================
{
id;
createNew;
}
Parameter | Type | Description |
---|---|---|
id | number |
The unique ID that was assigned to the subscription by the server at creation time. You can retrieve it by getting the subscription status. When updating, the id can be used instead of the name field, and takes precedence over it. This allows you to modify the subscription's name: provide the id and submit a new name in the name field. |
createNew | boolean |
Determines the behavior when the subscription you wish to update does Not exist.true - a new subscription is created with the provided option parameters.false - an exception will be thrown.Default: false |
Subscribe to revisions
To define a data subscription that sends document revisions to the client,
you must first configure revisions
for the specific collection managed by the subscription.
Create a subscription that sends document revisions using the following createForRevisions
methods:
// Available overloads:
// ====================
createForRevisions(options);
createForRevisions(options, database);
Parameter | Type | Description |
---|---|---|
options | object |
The subscription creation options. |
database | string |
The name of the database where the subscription task will be created. If null , the default database configured in the DocumentStore will be used. |
When providing raw RQL to the query
param in the options object,
concatenate the (Revisions = true)
clause to the collection being queried.
For example: From Orders(Revisions = true) as o
Learn more about subscribing to revisions in revisions support.
Subscription RQL
All subscriptions are eventually translated to an RQL-like statement. These statements have the following parts:
-
Functions definition part, like in ordinary RQL. Those functions can contain any JavaScript code, and also supports
load
andinclude
operations. -
From statement, defining the documents source, ex:
from Orders
. The from statement can only address collections, therefore, indexes are not supported. -
Where statement describing the criteria according to which it will be decided to either send the documents to the worker or not. Those statements support either RQL like
equality
operations (=
,==
) ,
plain JavaScript expressions or declared function calls, allowing to perform complex filtering logic.
The subscriptions RQL does not support any of the known RQL searching keywords. -
Select statement, that defines the projection to be performed. The select statements can contain function calls, allowing complex transformations.
-
Include statement allowing to define include path in document.
Keywords
Although subscription's query syntax has an RQL-like structure, it supports only the declare
, select
and where
keywords, usage of all other RQL keywords is not supported.
Usage of JavaScript ES5 syntax is supported.
Paths
Paths in subscriptions RQL statements are treated as JavaScript indirections and not like regular RQL paths.
It means that a query that in RQL would look like:
from Orders as o
where o.Lines[].Product = "products/1-A"
Will look like that in subscriptions RQL:
declare function filterLines(doc, productId)
{
if (!!doc.Lines){
return doc.Lines.filter(x=>x.Product == productId).length >0;
}
return false;
}
from Orders as o
where filterLines(o, "products/1-A")