Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add updatedAt to SimpleSavedObject #126359

Merged
merged 3 commits into from
Mar 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@ Constructs a new instance of the `SimpleSavedObject` class
<b>Signature:</b>

```typescript
constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, }: SavedObjectType<T>);
constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated_at: updatedAt, }: SavedObjectType<T>);
```

## Parameters

| Parameter | Type | Description |
| --- | --- | --- |
| client | SavedObjectsClientContract | |
| { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, } | SavedObjectType&lt;T&gt; | |
| { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated\_at: updatedAt, } | SavedObjectType&lt;T&gt; | |

Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ export declare class SimpleSavedObject<T = unknown>

| 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 <code>SimpleSavedObject</code> 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 <code>SimpleSavedObject</code> class |

## Properties

Expand All @@ -33,6 +33,7 @@ export declare class SimpleSavedObject<T = unknown>
| [namespaces](./kibana-plugin-core-public.simplesavedobject.namespaces.md) | | SavedObjectType&lt;T&gt;\['namespaces'\] | Space(s) that this saved object exists in. This attribute is not used for "global" saved object types which are registered with <code>namespaceType: 'agnostic'</code>. |
| [references](./kibana-plugin-core-public.simplesavedobject.references.md) | | SavedObjectType&lt;T&gt;\['references'\] | |
| [type](./kibana-plugin-core-public.simplesavedobject.type.md) | | SavedObjectType&lt;T&gt;\['type'\] | |
| [updatedAt](./kibana-plugin-core-public.simplesavedobject.updatedat.md) | | SavedObjectType&lt;T&gt;\['updated\_at'\] | |

## Methods

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!-- Do not edit this file. It is automatically generated by API Documenter. -->

[Home](./index.md) &gt; [kibana-plugin-core-public](./kibana-plugin-core-public.md) &gt; [SimpleSavedObject](./kibana-plugin-core-public.simplesavedobject.md) &gt; [updatedAt](./kibana-plugin-core-public.simplesavedobject.updatedat.md)

## SimpleSavedObject.updatedAt property

<b>Signature:</b>

```typescript
updatedAt: SavedObjectType<T>['updated_at'];
```
4 changes: 3 additions & 1 deletion src/core/public/public.api.md
Original file line number Diff line number Diff line change
Expand Up @@ -1401,7 +1401,7 @@ export class ScopedHistory<HistoryLocationState = unknown> implements History_2<

// @public
export class SimpleSavedObject<T = unknown> {
constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, }: SavedObject<T>);
constructor(client: SavedObjectsClientContract, { id, type, version, attributes, error, references, migrationVersion, coreMigrationVersion, namespaces, updated_at: updatedAt, }: SavedObject<T>);
// (undocumented)
attributes: T;
// (undocumented)
Expand All @@ -1428,6 +1428,8 @@ export class SimpleSavedObject<T = unknown> {
// (undocumented)
type: SavedObject<T>['type'];
// (undocumented)
updatedAt: SavedObject<T>['updated_at'];
// (undocumented)
_version?: SavedObject<T>['version'];
}

Expand Down
63 changes: 63 additions & 0 deletions src/core/public/saved_objects/simple_saved_object.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
29 changes: 21 additions & 8 deletions src/core/public/saved_objects/simple_saved_object.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export class SimpleSavedObject<T = unknown> {
public coreMigrationVersion: SavedObjectType<T>['coreMigrationVersion'];
public error: SavedObjectType<T>['error'];
public references: SavedObjectType<T>['references'];
public updatedAt: SavedObjectType<T>['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'`.
Expand All @@ -48,6 +49,7 @@ export class SimpleSavedObject<T = unknown> {
migrationVersion,
coreMigrationVersion,
namespaces,
updated_at: updatedAt,
}: SavedObjectType<T>
) {
this.id = id;
Expand All @@ -58,6 +60,7 @@ export class SimpleSavedObject<T = unknown> {
this.migrationVersion = migrationVersion;
this.coreMigrationVersion = coreMigrationVersion;
this.namespaces = namespaces;
this.updatedAt = updatedAt;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needed in order to show the value in a table column

if (error) {
this.error = error;
}
Expand All @@ -77,15 +80,25 @@ export class SimpleSavedObject<T = unknown> {

public save(): Promise<SimpleSavedObject<T>> {
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;
});
Comment on lines +87 to +90
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wasn't sure if this change warrants adding a test. thoughts?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that the current test suite is pretty lackluster, but let's not make the situation worse, and let's add tests on new enhancements if that's fine with you.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

happy to help. don't mind iterating too if needed 👍

} 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;
});
}
}

Expand Down