diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.initialize.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.initialize.md
index 2e1e409e2d7f9..61091306d0dbc 100644
--- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.initialize.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.initialize.md
@@ -4,7 +4,7 @@
## SavedObjectsIncrementCounterOptions.initialize property
-Sets all the counter fields to 0 if they don't already exist
+(default=false) If true, sets all the counter fields to 0 if they don't already exist. Existing fields will be left as-is and won't be incremented.
Signature:
diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.md
index a885982194fa6..68e9bb09456cd 100644
--- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.md
@@ -15,7 +15,7 @@ export interface SavedObjectsIncrementCounterOptions extends SavedObjectsBaseOpt
| Property | Type | Description |
| --- | --- | --- |
-| [initialize](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.initialize.md) | boolean
| Sets all the counter fields to 0 if they don't already exist |
-| [migrationVersion](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.migrationversion.md) | SavedObjectsMigrationVersion
| |
-| [refresh](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.refresh.md) | MutatingOperationRefreshSetting
| The Elasticsearch Refresh setting for this operation |
+| [initialize](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.initialize.md) | boolean
| (default=false) If true, sets all the counter fields to 0 if they don't already exist. Existing fields will be left as-is and won't be incremented. |
+| [migrationVersion](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.migrationversion.md) | SavedObjectsMigrationVersion
| [SavedObjectsMigrationVersion](./kibana-plugin-core-server.savedobjectsmigrationversion.md) |
+| [refresh](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.refresh.md) | MutatingOperationRefreshSetting
| (default='wait\_for') The Elasticsearch refresh setting for this operation. See [MutatingOperationRefreshSetting](./kibana-plugin-core-server.mutatingoperationrefreshsetting.md) |
diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.migrationversion.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.migrationversion.md
index 417db99fd5a27..aff80138d61cf 100644
--- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.migrationversion.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.migrationversion.md
@@ -4,6 +4,8 @@
## SavedObjectsIncrementCounterOptions.migrationVersion property
+[SavedObjectsMigrationVersion](./kibana-plugin-core-server.savedobjectsmigrationversion.md)
+
Signature:
```typescript
diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.refresh.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.refresh.md
index 31d957ca30a3e..4f217cc223d46 100644
--- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.refresh.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsincrementcounteroptions.refresh.md
@@ -4,7 +4,7 @@
## SavedObjectsIncrementCounterOptions.refresh property
-The Elasticsearch Refresh setting for this operation
+(default='wait\_for') The Elasticsearch refresh setting for this operation. See [MutatingOperationRefreshSetting](./kibana-plugin-core-server.mutatingoperationrefreshsetting.md)
Signature:
diff --git a/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.incrementcounter.md b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.incrementcounter.md
index 4022467832660..dc62cacf6741b 100644
--- a/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.incrementcounter.md
+++ b/docs/development/core/server/kibana-plugin-core-server.savedobjectsrepository.incrementcounter.md
@@ -16,20 +16,22 @@ incrementCounter(type: string, id: string, counterFieldNames: string[], options?
| Parameter | Type | Description |
| --- | --- | --- |
-| type | string
| |
-| id | string
| |
-| counterFieldNames | string[]
| |
-| options | SavedObjectsIncrementCounterOptions
| |
+| type | string
| The type of saved object whose fields should be incremented |
+| id | string
| The id of the document whose fields should be incremented |
+| counterFieldNames | string[]
| An array of field names to increment |
+| options | SavedObjectsIncrementCounterOptions
| [SavedObjectsIncrementCounterOptions](./kibana-plugin-core-server.savedobjectsincrementcounteroptions.md) |
Returns:
`Promise`
-{promise}
+The saved object after the specified fields were incremented
## Remarks
-\* When supplying a field name like `stats.api.counter` the field name will be used as-is to create a document like: `{attributes: {'stats.api.counter': 1}}` It will not create a nested structure like: `{attributes: {stats: {api: {counter: 1}}}}` When using incrementCounter for collecting usage data, you need to ensure that usage collection happens on a best-effort basis and doesn't negatively affect your plugin or users. See https://github.com/elastic/kibana/blob/master/src/plugins/usage\_collection/README.md\#tracking-interactions-with-incrementcounter)
+When supplying a field name like `stats.api.counter` the field name will be used as-is to create a document like: `{attributes: {'stats.api.counter': 1}}` It will not create a nested structure like: `{attributes: {stats: {api: {counter: 1}}}}`
+
+When using incrementCounter for collecting usage data, you need to ensure that usage collection happens on a best-effort basis and doesn't negatively affect your plugin or users. See https://github.com/elastic/kibana/blob/master/src/plugins/usage\_collection/README.md\#tracking-interactions-with-incrementcounter)
## Example
@@ -37,10 +39,17 @@ incrementCounter(type: string, id: string, counterFieldNames: string[], options?
```ts
const repository = coreStart.savedObjects.createInternalRepository();
+// Initialize all fields to 0
repository
.incrementCounter('dashboard_counter_type', 'counter_id', [
'stats.apiCalls',
'stats.sampleDataInstalled',
+ ], {initialize: true});
+
+// Increment the apiCalls field counter
+repository
+ .incrementCounter('dashboard_counter_type', 'counter_id', [
+ 'stats.apiCalls',
])
```
diff --git a/src/core/server/saved_objects/service/lib/repository.ts b/src/core/server/saved_objects/service/lib/repository.ts
index 6d4576bfce14d..2f09ad71de558 100644
--- a/src/core/server/saved_objects/service/lib/repository.ts
+++ b/src/core/server/saved_objects/service/lib/repository.ts
@@ -101,10 +101,17 @@ export interface SavedObjectsRepositoryOptions {
* @public
*/
export interface SavedObjectsIncrementCounterOptions extends SavedObjectsBaseOptions {
- /** Sets all the counter fields to 0 if they don't already exist */
+ /**
+ * (default=false) If true, sets all the counter fields to 0 if they don't
+ * already exist. Existing fields will be left as-is and won't be incremented.
+ */
initialize?: boolean;
+ /** {@link SavedObjectsMigrationVersion} */
migrationVersion?: SavedObjectsMigrationVersion;
- /** The Elasticsearch Refresh setting for this operation */
+ /**
+ * (default='wait_for') The Elasticsearch refresh setting for this
+ * operation. See {@link MutatingOperationRefreshSetting}
+ */
refresh?: MutatingOperationRefreshSetting;
}
@@ -1521,11 +1528,12 @@ export class SavedObjectsRepository {
* if one doesn't exist for the given id.
*
* @remarks
- * * When supplying a field name like `stats.api.counter` the field name will
+ * When supplying a field name like `stats.api.counter` the field name will
* be used as-is to create a document like:
* `{attributes: {'stats.api.counter': 1}}`
* It will not create a nested structure like:
* `{attributes: {stats: {api: {counter: 1}}}}`
+ *
* When using incrementCounter for collecting usage data, you need to ensure
* that usage collection happens on a best-effort basis and doesn't
* negatively affect your plugin or users. See https://github.com/elastic/kibana/blob/master/src/plugins/usage_collection/README.md#tracking-interactions-with-incrementcounter)
@@ -1534,19 +1542,25 @@ export class SavedObjectsRepository {
* ```ts
* const repository = coreStart.savedObjects.createInternalRepository();
*
+ * // Initialize all fields to 0
* repository
* .incrementCounter('dashboard_counter_type', 'counter_id', [
* 'stats.apiCalls',
* 'stats.sampleDataInstalled',
+ * ], {initialize: true});
+ *
+ * // Increment the apiCalls field counter
+ * repository
+ * .incrementCounter('dashboard_counter_type', 'counter_id', [
+ * 'stats.apiCalls',
* ])
* ```
*
- * @param {string} type
- * @param {string} id
- * @param {string} counterFieldNames
- * @param {object} [options={}]
- * @property {object} [options.migrationVersion=undefined]
- * @returns {promise}
+ * @param type - The type of saved object whose fields should be incremented
+ * @param id - The id of the document whose fields should be incremented
+ * @param counterFieldNames - An array of field names to increment
+ * @param options - {@link SavedObjectsIncrementCounterOptions}
+ * @returns The saved object after the specified fields were incremented
*/
async incrementCounter(
type: string,