Skip to content

Commit

Permalink
add updatedAt to SimpleSavedObject (#126359)
Browse files Browse the repository at this point in the history
  • Loading branch information
orouz authored Mar 9, 2022
1 parent eb4cd4a commit 06e453e
Show file tree
Hide file tree
Showing 6 changed files with 102 additions and 12 deletions.
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 @@ -1389,7 +1389,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 @@ -1416,6 +1416,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;
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;
});
} 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

0 comments on commit 06e453e

Please sign in to comment.