query #
Base method used to send queries to Kuzzle, following the API Documentation.
This is a low-level method, exposed to allow advanced SDK users to bypass high-level methods.
Arguments #
query(request, [options]);
Argument | Type | Description |
---|---|---|
request | object | API request |
options | object | Optional query options |
request #
All properties necessary for the Kuzzle API can be added in the request object. The following properties are the most common.
Property | Type | Description |
---|---|---|
controller | string | Controller name (mandatory) |
action | string | Action name (mandatory) |
body | object | Query body for this action |
index | string | Index name for this action |
collection | string | Collection name for this action |
_id | string | id for this action |
volatile | object | Additional information to send to Kuzzle |
options #
Additional query options
Property | Type (default) | Description |
---|---|---|
queuable | boolean ( true ) | Make this request queuable or not |
timeout | number ( -1 ) | Time (in ms) during which a request will still be waited to be resolved. Set it -1 if you want to wait indefinitely |
triggerEvents | boolean ( false ) | If set to true , will trigger events even if using Embeded SDK. You should always ensure that your events/pipes does not create an infinite loop. Available since Kuzzle 2.31.0 |
timeout #
::: warn The timeout option can only be used to prevent the SDK from being frozen if Kuzzle take too long to resolve a request, this will not prevent the request from being executed and Kuzzle will still resolve it at some point.
triggerEvents #
::: warn The triggerEvents option can only be used to prevent the EmbededSDK from escaping events such as a pipe or a hook. Use it can lead to infinite loop if used wrongly. (ex: a pipe on document creation that create a document that triggers same pipe again and again)
Resolves #
Resolve to the raw Kuzzle API response. See the API Documentation.
Usage #
const request = {
controller: 'document',
action: 'create',
index: 'nyc-open-data',
collection: 'yellow-taxi',
_id: 'my-custom-document-id',
refresh: 'wait_for', // Additional property allowed for this API action
body: {
trip_distance: 4.23,
passenger_count: 2
}
};
try {
const response = await kuzzle.query(request);
console.log(response);
/*
{ requestId: '49ffb6db-bdff-45b9-b3f6-00442f472393',
status: 200,
error: null,
controller: 'document',
action: 'create',
collection: 'yellow-taxi',
index: 'nyc-open-data',
volatile: { sdkName: 'javascript@7.0.0' },
room: '49ffb6db-bdff-45b9-b3f6-00442f472393',
result:
{ _index: 'nyc-open-data',
_type: 'yellow-taxi',
_id: 'my-custom-document-id',
_version: 1,
result: 'created',
_shards: { total: 2, successful: 1, failed: 0 },
created: true,
_source:
{ trip_distance: 4.23,
passenger_count: 2,
_kuzzle_info:
{ author: '-1',
createdAt: 1532529302225,
updatedAt: null,
updater: null } } } }
*/
console.log('Document created');
} catch (error) {
console.error(error);
}