diff --git a/docs/cosmos-db.md b/docs/cosmos-db.md new file mode 100644 index 0000000..f02d6dd --- /dev/null +++ b/docs/cosmos-db.md @@ -0,0 +1,71 @@ +# CosmosDB + +CosmosDB can be added to the list of required connections. + +## Config +To enable [Cosmos DB](https://azure.microsoft.com/en-us/services/cosmos-db/) data sources add 'cosmos-db' to the connections config. The host name and password key are required values. + +```js +connections: { + 'cosmos-db': { + 'host': "", + 'key': "" + } +} +``` + +NB. The host name excludes the '.documents.azure.com' suffix. + +## Data Sources +| Property | Type | Value | Description +| :--------|:-----|:------|:------------ +| `id`| `string` || ID of the element +| `type`| `string` | "CosmosDB/Query" | Data source plugin name +| `dependencies`| `object` || A collection of key values referenced by queries +| `params`| `object` || Contains `databaseId`, `collectionId`, `query` and `parameters` +| `calculated` | `function` || Result contains array of `Documents`, `_rid` and `_count` properties. + +## Params +| Property | Type | Description +| :--------|:-----|:------------ +| `databaseId`| `string` | Database Id (default is 'admin') +| `collectionId`| `string` | Collection Id +| `query`| `string` | SQL query string +| `parameters`| `object[]` | Parameterized SQL request + +More info about SQL `query` string and `parameters` are available in the [CosmosDB documentation](https://docs.microsoft.com/en-us/rest/api/documentdb/querying-documentdb-resources-using-the-rest-api). You can try out Cosmos DB queries using the *Query Explorer* in the [Azure portal](https://portal.azure.com/), or learn using the [SQL demo](https://www.documentdb.com/sql/demo). + +## Sample data source +```js +{ + id: "botConversations", + type: "CosmosDB/Query", + dependencies: { timespan: "timespan", queryTimespan: "timespan:queryTimespan" }, + params: { + databaseId: "admin", + collectionId: "conversations", + query: () => `SELECT * FROM conversations WHERE (conversations.state = 0)`, + parameters: [] + }, + calculated: (result) => { + return result; + } +} +``` + +## Sample element + +``` +{ + id: "conversations", + type: "Scorecard", + title: "Conversations", + subtitle: "Total conversations", + size: { w: 4, h: 3 }, + dependencies: { + card_conversations_value: "botConversations:_count", + card_conversations_heading: "::Conversations", + card_conversations_icon: "::chat" + } +} +``` \ No newline at end of file diff --git a/src/data-sources/plugins/CosmosDB/Query.ts b/src/data-sources/plugins/CosmosDB/Query.ts index a80b068..49b9325 100644 --- a/src/data-sources/plugins/CosmosDB/Query.ts +++ b/src/data-sources/plugins/CosmosDB/Query.ts @@ -16,7 +16,7 @@ interface IQueryParams { export default class CosmosDBQuery extends DataSourcePlugin { type = 'CosmosDB-Query'; - defaultProperty = 'values'; + defaultProperty = 'Documents'; connectionType = connectionType.type; constructor(options: IOptions, connections: IDict) { @@ -85,7 +85,9 @@ export default class CosmosDBQuery extends DataSourcePlugin { // NB: CosmosDB prefixes certain keys with '$' which will be removed for the returned result. this.remap(documents); let returnedResults = { - values: documents || null + 'Documents': documents || [], + '_count': json._count || 0, + '_rid': json._rid || undefined }; return dispatch(returnedResults); });