diff --git a/docs/development/core/public/kibana-plugin-core-public.simplesavedobject._constructor_.md b/docs/development/core/public/kibana-plugin-core-public.simplesavedobject._constructor_.md index f53b6e5292861..412154f7ac2e3 100644 --- a/docs/development/core/public/kibana-plugin-core-public.simplesavedobject._constructor_.md +++ b/docs/development/core/public/kibana-plugin-core-public.simplesavedobject._constructor_.md @@ -9,7 +9,7 @@ Constructs a new instance of the `SimpleSavedObject` class Signature: ```typescript -constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, }: SavedObjectType); +constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated_at: updatedAt, }: SavedObjectType); ``` ## Parameters @@ -17,5 +17,5 @@ constructor(client: SavedObjectsClientContract, { id, type, version, attributes, | Parameter | Type | Description | | --- | --- | --- | | client | SavedObjectsClientContract | | -| { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, } | SavedObjectType<T> | | +| { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated\_at: updatedAt, } | SavedObjectType<T> | | diff --git a/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.md b/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.md index 2aac93f9b5bc1..512fc74d538e3 100644 --- a/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.md +++ b/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.md @@ -18,7 +18,7 @@ export declare class SimpleSavedObject | Constructor | Modifiers | Description | | --- | --- | --- | -| [(constructor)(client, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, })](./kibana-plugin-core-public.simplesavedobject._constructor_.md) | | Constructs a new instance of the SimpleSavedObject class | +| [(constructor)(client, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated\_at: updatedAt, })](./kibana-plugin-core-public.simplesavedobject._constructor_.md) | | Constructs a new instance of the SimpleSavedObject class | ## Properties @@ -33,6 +33,7 @@ export declare class SimpleSavedObject | [namespaces](./kibana-plugin-core-public.simplesavedobject.namespaces.md) | | SavedObjectType<T>\['namespaces'\] | Space(s) that this saved object exists in. This attribute is not used for "global" saved object types which are registered with namespaceType: 'agnostic'. | | [references](./kibana-plugin-core-public.simplesavedobject.references.md) | | SavedObjectType<T>\['references'\] | | | [type](./kibana-plugin-core-public.simplesavedobject.type.md) | | SavedObjectType<T>\['type'\] | | +| [updatedAt](./kibana-plugin-core-public.simplesavedobject.updatedat.md) | | SavedObjectType<T>\['updated\_at'\] | | ## Methods diff --git a/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.updatedat.md b/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.updatedat.md new file mode 100644 index 0000000000000..80b1f95969934 --- /dev/null +++ b/docs/development/core/public/kibana-plugin-core-public.simplesavedobject.updatedat.md @@ -0,0 +1,11 @@ + + +[Home](./index.md) > [kibana-plugin-core-public](./kibana-plugin-core-public.md) > [SimpleSavedObject](./kibana-plugin-core-public.simplesavedobject.md) > [updatedAt](./kibana-plugin-core-public.simplesavedobject.updatedat.md) + +## SimpleSavedObject.updatedAt property + +Signature: + +```typescript +updatedAt: SavedObjectType['updated_at']; +``` diff --git a/src/core/public/public.api.md b/src/core/public/public.api.md index ed119620ac08b..6145cce3912fd 100644 --- a/src/core/public/public.api.md +++ b/src/core/public/public.api.md @@ -1389,7 +1389,7 @@ export class ScopedHistory implements History_2< // @public export class SimpleSavedObject { - constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, }: SavedObject); + constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated_at: updatedAt, }: SavedObject); // (undocumented) attributes: T; // (undocumented) @@ -1416,6 +1416,8 @@ export class SimpleSavedObject { // (undocumented) type: SavedObject['type']; // (undocumented) + updatedAt: SavedObject['updated_at']; + // (undocumented) _version?: SavedObject['version']; } diff --git a/src/core/public/saved_objects/simple_saved_object.test.ts b/src/core/public/saved_objects/simple_saved_object.test.ts index b9338f4dc38bf..432bc215a5b4c 100644 --- a/src/core/public/saved_objects/simple_saved_object.test.ts +++ b/src/core/public/saved_objects/simple_saved_object.test.ts @@ -45,4 +45,67 @@ describe('SimpleSavedObject', () => { const savedObject = new SimpleSavedObject(client, { version } as SavedObject); expect(savedObject._version).toEqual(version); }); + + it('save() changes updatedAt field on existing SimpleSavedObject with an id', async function () { + const date = new Date(); + const initialDate = date.toISOString(); + date.setDate(date.getDate() + 1); + const secondDate = date.toISOString(); + + const config = { + attributes: {}, + id: 'id', + type: 'type', + }; + + const initialSavedObject = new SimpleSavedObject(client, { + ...config, + updated_at: initialDate, + } as SavedObject); + + const updatedSavedObject = new SimpleSavedObject(client, { + ...config, + updated_at: secondDate, + } as SavedObject); + + (client.update as jest.Mock).mockReturnValue(Promise.resolve(updatedSavedObject)); + + const initialValue = initialSavedObject.updatedAt; + await initialSavedObject.save(); + const updatedValue = updatedSavedObject.updatedAt; + + expect(initialValue).not.toEqual(updatedValue); + expect(initialSavedObject.updatedAt).toEqual(updatedValue); + }); + + it('save() changes updatedAt field on existing SimpleSavedObject without an id', async () => { + const date = new Date(); + const initialDate = date.toISOString(); + date.setDate(date.getDate() + 1); + const secondDate = date.toISOString(); + + const config = { + attributes: {}, + type: 'type', + }; + + const initialSavedObject = new SimpleSavedObject(client, { + ...config, + updated_at: initialDate, + } as SavedObject); + + const updatedSavedObject = new SimpleSavedObject(client, { + ...config, + updated_at: secondDate, + } as SavedObject); + + (client.create as jest.Mock).mockReturnValue(Promise.resolve(updatedSavedObject)); + + const initialValue = initialSavedObject.updatedAt; + await initialSavedObject.save(); + const updatedValue = updatedSavedObject.updatedAt; + + expect(initialValue).not.toEqual(updatedValue); + expect(initialSavedObject.updatedAt).toEqual(updatedValue); + }); }); diff --git a/src/core/public/saved_objects/simple_saved_object.ts b/src/core/public/saved_objects/simple_saved_object.ts index 449d3d7943fca..512c6c7656741 100644 --- a/src/core/public/saved_objects/simple_saved_object.ts +++ b/src/core/public/saved_objects/simple_saved_object.ts @@ -30,6 +30,7 @@ export class SimpleSavedObject { public coreMigrationVersion: SavedObjectType['coreMigrationVersion']; public error: SavedObjectType['error']; public references: SavedObjectType['references']; + public updatedAt: SavedObjectType['updated_at']; /** * Space(s) that this saved object exists in. This attribute is not used for "global" saved object types which are registered with * `namespaceType: 'agnostic'`. @@ -48,6 +49,7 @@ export class SimpleSavedObject { migrationVersion, coreMigrationVersion, namespaces, + updated_at: updatedAt, }: SavedObjectType ) { this.id = id; @@ -58,6 +60,7 @@ export class SimpleSavedObject { this.migrationVersion = migrationVersion; this.coreMigrationVersion = coreMigrationVersion; this.namespaces = namespaces; + this.updatedAt = updatedAt; if (error) { this.error = error; } @@ -77,15 +80,25 @@ export class SimpleSavedObject { public save(): Promise> { if (this.id) { - return this.client.update(this.type, this.id, this.attributes, { - references: this.references, - }); + return this.client + .update(this.type, this.id, this.attributes, { + references: this.references, + }) + .then((sso) => { + this.updatedAt = sso.updatedAt; + return sso; + }); } else { - return this.client.create(this.type, this.attributes, { - migrationVersion: this.migrationVersion, - coreMigrationVersion: this.coreMigrationVersion, - references: this.references, - }); + return this.client + .create(this.type, this.attributes, { + migrationVersion: this.migrationVersion, + coreMigrationVersion: this.coreMigrationVersion, + references: this.references, + }) + .then((sso) => { + this.updatedAt = sso.updatedAt; + return sso; + }); } }