mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 09:48:58 -04:00
[Data views] Generate ID if non-persisted (#134780)
* [Data views] Generate ID if non-persisted * Use version to infer isPersisted * Fix create test
This commit is contained in:
parent
48db1ec0f0
commit
4db96c5f26
4 changed files with 38 additions and 3 deletions
|
@ -265,6 +265,10 @@ export class DataView implements DataViewBase {
|
|||
};
|
||||
}
|
||||
|
||||
isPersisted() {
|
||||
return typeof this.version === 'string';
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates static representation of the data view.
|
||||
*/
|
||||
|
|
|
@ -235,6 +235,31 @@ describe('IndexPatterns', () => {
|
|||
|
||||
await indexPatterns.create({ title });
|
||||
expect(indexPatterns.refreshFields).toBeCalled();
|
||||
expect(indexPattern.id).toBeDefined();
|
||||
expect(indexPattern.isPersisted()).toBe(false);
|
||||
});
|
||||
|
||||
test('createSavedObject', async () => {
|
||||
const title = 'kibana-*';
|
||||
const version = '8.4.0';
|
||||
const dataView = await indexPatterns.create({ title }, true);
|
||||
|
||||
savedObjectsClient.find = jest.fn().mockResolvedValue([]);
|
||||
savedObjectsClient.create = jest.fn().mockResolvedValue({
|
||||
...savedObject,
|
||||
id: dataView.id,
|
||||
version,
|
||||
attributes: {
|
||||
...savedObject.attributes,
|
||||
title: dataView.title,
|
||||
},
|
||||
});
|
||||
|
||||
const indexPattern = await indexPatterns.createSavedObject(dataView);
|
||||
expect(indexPattern).toBeInstanceOf(DataView);
|
||||
expect(indexPattern.id).toBe(dataView.id);
|
||||
expect(indexPattern.title).toBe(title);
|
||||
expect(indexPattern.isPersisted()).toBe(true);
|
||||
});
|
||||
|
||||
test('find', async () => {
|
||||
|
|
|
@ -11,6 +11,7 @@ import { PublicMethodsOf } from '@kbn/utility-types';
|
|||
import { castEsToKbnFieldTypeName } from '@kbn/field-types';
|
||||
import { FieldFormatsStartCommon, FORMATS_UI_SETTINGS } from '@kbn/field-formats-plugin/common';
|
||||
import { SavedObjectNotFound } from '@kbn/kibana-utils-plugin/common';
|
||||
import uuid from 'uuid';
|
||||
import { DATA_VIEW_SAVED_OBJECT_TYPE, DEFAULT_ASSETS_TO_IGNORE } from '..';
|
||||
import { SavedObjectsClientCommon } from '../types';
|
||||
|
||||
|
@ -52,7 +53,7 @@ export type IndexPatternListSavedObjectAttrs = Pick<
|
|||
*/
|
||||
export interface DataViewListItem {
|
||||
/**
|
||||
* Saved object id
|
||||
* Saved object id (or generated id if in-memory only)
|
||||
*/
|
||||
id: string;
|
||||
/**
|
||||
|
@ -762,10 +763,15 @@ export class DataViewsService {
|
|||
* @param skipFetchFields if true, will not fetch fields
|
||||
* @returns DataView
|
||||
*/
|
||||
async create(spec: DataViewSpec, skipFetchFields = false): Promise<DataView> {
|
||||
async create({ id, ...restOfSpec }: DataViewSpec, skipFetchFields = false): Promise<DataView> {
|
||||
const shortDotsEnable = await this.config.get(FORMATS_UI_SETTINGS.SHORT_DOTS_ENABLE);
|
||||
const metaFields = await this.config.get(META_FIELDS);
|
||||
|
||||
const spec = {
|
||||
id: id ?? uuid.v4(),
|
||||
...restOfSpec,
|
||||
};
|
||||
|
||||
const indexPattern = new DataView({
|
||||
spec,
|
||||
fieldFormats: this.fieldFormats,
|
||||
|
|
|
@ -429,7 +429,7 @@ export type DataViewFieldMap = Record<string, FieldSpec>;
|
|||
// eslint-disable-next-line @typescript-eslint/consistent-type-definitions
|
||||
export type DataViewSpec = {
|
||||
/**
|
||||
* Saved object id
|
||||
* Saved object id (or generated id if in-memory only)
|
||||
*/
|
||||
id?: string;
|
||||
/**
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue