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

[Core] Rewrite saved objects in typescript #36829

Merged
merged 37 commits into from
Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
37 commits
Select commit Hold shift + click to select a range
10c42ca
Convert simple files to TS
mikecote Mar 26, 2019
b8e0334
Fix jest tests
mikecote Mar 26, 2019
975ead0
Rename saved_objects_client{.js => .ts}
rudolf Apr 30, 2019
cd2220c
WIP saved_objects_client
rudolf May 2, 2019
b6677f2
saved_objects repository{.js => .ts}
rudolf May 2, 2019
ed64822
includedFields support string[] for type paramater
rudolf May 9, 2019
3b46d5c
Repository/saved_objects_client -> TS
rudolf May 13, 2019
fb94a0a
Fix tests and dependencies
rudolf May 13, 2019
3902f47
Fix saved objects type errors and simplify
rudolf May 23, 2019
891e5ba
saved_objects/index saved_objects/service/index -> ts
rudolf May 23, 2019
2e62461
Fix saved objects export test after switching to typed mock
rudolf May 23, 2019
d18e7b3
Workaround type error
rudolf May 23, 2019
857f284
Revert "Workaround type error"
rudolf May 23, 2019
35b8ab9
Correctly type Server.savedObjects.SaveObjectsClient constructor
rudolf May 23, 2019
928ffe9
saved_objects/service/lib/index.{js -> ts}
rudolf May 23, 2019
dbf30f8
saved_objects/service/lib/scoped_client_provider{js -> ts}
rudolf May 23, 2019
b0a6b3b
Typescriptify scoped_client_provider
rudolf May 23, 2019
9eaab2f
Fix x-pack jest imports
rudolf May 24, 2019
60a091a
Add lodash/internal/toPath typings to xpath
rudolf May 24, 2019
8dcff01
Introduce SavedObjectsClientContract
rudolf May 24, 2019
897685e
Cleanup and simplify types
rudolf May 24, 2019
1baef0e
Fix repository#delete should return {}
rudolf May 27, 2019
88f725a
Add SavedObjects repository test for uncovered bug
rudolf May 27, 2019
fbc1bbf
SavedObject.updated_at: string and unify saved_object / serializer types
rudolf May 28, 2019
0233e27
Cleanup
rudolf May 28, 2019
24c2d36
Address code review feedback
rudolf May 29, 2019
2baf7a3
Don't mock errors helpers in SavedObjectsClient Mock
rudolf May 29, 2019
8fcbd87
Address CR feedback
rudolf May 29, 2019
3357a69
CR Feedback #2
rudolf Jun 3, 2019
89be456
Merge branch 'master' into ts-saved-objects-lib
rudolf Jun 3, 2019
be54402
Add kibana-platform as code owners of Saved Objects
rudolf Jun 3, 2019
72e9555
Merge branch 'master' into ts-saved-objects-lib
rudolf Jun 3, 2019
1cddd03
Better typings for SavedObjectsClient.errors
rudolf Jun 4, 2019
db98b09
Use unknown as default for generic type request paramater
rudolf Jun 4, 2019
cef7ffe
Bump @types/elasticsearch
rudolf Jun 4, 2019
0ad2b17
Fix types for isForbiddenError
rudolf Jun 4, 2019
a52d038
Bump x-pack @types/elasticsearch
rudolf Jun 5, 2019
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
2 changes: 2 additions & 0 deletions .github/CODEOWNERS
Validating CODEOWNERS rules …
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@

# Platform
/src/core/ @elastic/kibana-platform
/src/legacy/server/saved_objects/ @elastic/kibana-platform
/src/legacy/ui/public/saved_objects @elastic/kibana-platform

# Security
/x-pack/plugins/security/ @elastic/kibana-security
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,7 @@
"@types/d3": "^3.5.41",
"@types/dedent": "^0.7.0",
"@types/delete-empty": "^2.0.0",
"@types/elasticsearch": "^5.0.30",
"@types/elasticsearch": "^5.0.33",
"@types/enzyme": "^3.1.12",
"@types/eslint": "^4.16.6",
"@types/execa": "^0.9.0",
Expand Down
2 changes: 1 addition & 1 deletion src/legacy/server/kbn_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ import configCompleteMixin from './config/complete';
import optimizeMixin from '../../optimize';
import * as Plugins from './plugins';
import { indexPatternsMixin } from './index_patterns';
import { savedObjectsMixin } from './saved_objects';
import { savedObjectsMixin } from './saved_objects/saved_objects_mixin';
rudolf marked this conversation as resolved.
Show resolved Hide resolved
import { sampleDataMixin } from './sample_data';
import { capabilitiesMixin } from './capabilities';
import { urlShorteningMixin } from './url_shortening';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,10 @@
*/

import { getSortedObjectsForExport } from './get_sorted_objects_for_export';
import { SavedObjectsClientMock } from '../service/saved_objects_client.mock';

describe('getSortedObjectsForExport()', () => {
const savedObjectsClient = {
errors: {} as any,
find: jest.fn(),
bulkGet: jest.fn(),
create: jest.fn(),
bulkCreate: jest.fn(),
delete: jest.fn(),
get: jest.fn(),
update: jest.fn(),
};
const savedObjectsClient = SavedObjectsClientMock.create();

afterEach(() => {
savedObjectsClient.find.mockReset();
Expand All @@ -48,8 +40,10 @@ describe('getSortedObjectsForExport()', () => {
{
id: '2',
type: 'search',
attributes: {},
references: [
{
name: 'name',
type: 'index-pattern',
id: '1',
},
Expand All @@ -58,9 +52,12 @@ describe('getSortedObjectsForExport()', () => {
{
id: '1',
type: 'index-pattern',
attributes: {},
references: [],
},
],
per_page: 1,
page: 0,
});
const response = await getSortedObjectsForExport({
savedObjectsClient,
Expand All @@ -70,15 +67,18 @@ describe('getSortedObjectsForExport()', () => {
expect(response).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "name",
"type": "index-pattern",
},
],
Expand Down Expand Up @@ -118,19 +118,24 @@ Array [
{
id: '2',
type: 'search',
attributes: {},
references: [
{
type: 'index-pattern',
name: 'name',
id: '1',
},
],
},
{
id: '1',
type: 'index-pattern',
attributes: {},
references: [],
},
],
per_page: 1,
page: 0,
});
await expect(
getSortedObjectsForExport({
Expand All @@ -147,16 +152,19 @@ Array [
{
id: '2',
type: 'search',
attributes: {},
references: [
{
type: 'index-pattern',
id: '1',
name: 'name',
type: 'index-pattern',
},
],
},
{
id: '1',
type: 'index-pattern',
attributes: {},
references: [],
},
],
Expand All @@ -179,15 +187,18 @@ Array [
expect(response).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "name",
"type": "index-pattern",
},
],
Expand Down Expand Up @@ -227,9 +238,11 @@ Array [
{
id: '2',
type: 'search',
attributes: {},
references: [
{
type: 'index-pattern',
name: 'name',
id: '1',
},
],
Expand All @@ -241,6 +254,7 @@ Array [
{
id: '1',
type: 'index-pattern',
attributes: {},
references: [],
},
],
Expand All @@ -260,15 +274,18 @@ Array [
expect(response).toMatchInlineSnapshot(`
Array [
Object {
"attributes": Object {},
"id": "1",
"references": Array [],
"type": "index-pattern",
},
Object {
"attributes": Object {},
"id": "2",
"references": Array [
Object {
"id": "1",
"name": "name",
"type": "index-pattern",
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import Boom from 'boom';
import { SavedObjectsClient } from '../service/saved_objects_client';
import { SavedObjectsClientContract } from '../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { SavedObjectsClientContract } from '..';

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Our codebase has a mix of both and the style guide includes an example with ../ (although it doesn't express any preference for it https://github.com/elastic/kibana/blob/master/style_guides/js_style_guide.md#import-only-top-level-modules)

I don't have a preference for either style, but I think it's probably better if we make the style preference explicit with a linter rule.

import { injectNestedDependencies } from './inject_nested_depdendencies';
import { sortObjects } from './sort_objects';

Expand All @@ -30,7 +30,7 @@ interface ObjectToExport {
interface ExportObjectsOptions {
types?: string[];
objects?: ObjectToExport[];
savedObjectsClient: SavedObjectsClient;
savedObjectsClient: SavedObjectsClientContract;
exportSizeLimit: number;
includeReferencesDeep?: boolean;
}
Expand All @@ -44,7 +44,7 @@ async function fetchObjectsToExport({
objects?: ObjectToExport[];
types?: string[];
exportSizeLimit: number;
savedObjectsClient: SavedObjectsClient;
savedObjectsClient: SavedObjectsClientContract;
}) {
if (objects) {
if (objects.length > exportSizeLimit) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import Boom from 'boom';
import { SavedObject, SavedObjectsClient } from '../service/saved_objects_client';
import { SavedObject, SavedObjectsClientContract } from '../service/saved_objects_client';

export function getObjectReferencesToFetch(savedObjectsMap: Map<string, SavedObject>) {
const objectsToFetch = new Map<string, { type: string; id: string }>();
Expand All @@ -34,7 +34,7 @@ export function getObjectReferencesToFetch(savedObjectsMap: Map<string, SavedObj

export async function injectNestedDependencies(
savedObjects: SavedObject[],
savedObjectsClient: SavedObjectsClient
savedObjectsClient: SavedObjectsClientContract
) {
const savedObjectsMap = new Map<string, SavedObject>();
for (const savedObject of savedObjects) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
*/

import { Readable } from 'stream';
import { SavedObjectsClient } from '../service';
import { collectSavedObjects } from './collect_saved_objects';
import { extractErrors } from './extract_errors';
import { ImportError } from './types';
import { validateReferences } from './validate_references';
import { SavedObjectsClientContract } from '../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { SavedObjectsClientContract } from '..';


interface ImportSavedObjectsOptions {
readStream: Readable;
objectLimit: number;
overwrite: boolean;
savedObjectsClient: SavedObjectsClient;
savedObjectsClient: SavedObjectsClientContract;
supportedTypes: string[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import { Readable } from 'stream';
import { SavedObjectsClient } from '../service';
import { SavedObjectsClientContract } from '../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { SavedObjectsClientContract } from '..';

import { collectSavedObjects } from './collect_saved_objects';
import { createObjectsFilter } from './create_objects_filter';
import { extractErrors } from './extract_errors';
Expand All @@ -29,7 +29,7 @@ import { validateReferences } from './validate_references';
interface ResolveImportErrorsOptions {
readStream: Readable;
objectLimit: number;
savedObjectsClient: SavedObjectsClient;
savedObjectsClient: SavedObjectsClientContract;
retries: Retry[];
supportedTypes: string[];
}
Expand Down
6 changes: 3 additions & 3 deletions src/legacy/server/saved_objects/import/validate_references.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*/

import Boom from 'boom';
import { SavedObject, SavedObjectsClient } from '../service';
import { SavedObject, SavedObjectsClientContract } from '../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { SavedObject, SavedObjectsClientContract } from '..';

import { ImportError } from './types';

const REF_TYPES_TO_VLIDATE = ['index-pattern', 'search'];
Expand All @@ -29,7 +29,7 @@ function filterReferencesToValidate({ type }: { type: string }) {

export async function getNonExistingReferenceAsKeys(
savedObjects: SavedObject[],
savedObjectsClient: SavedObjectsClient
savedObjectsClient: SavedObjectsClientContract
) {
const collector = new Map();
// Collect all references within objects
Expand Down Expand Up @@ -77,7 +77,7 @@ export async function getNonExistingReferenceAsKeys(

export async function validateReferences(
savedObjects: SavedObject[],
savedObjectsClient: SavedObjectsClient
savedObjectsClient: SavedObjectsClientContract
) {
const errorMap: { [key: string]: ImportError } = {};
const nonExistingReferenceKeys = await getNonExistingReferenceAsKeys(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,8 @@
* under the License.
*/

export { savedObjectsMixin } from './saved_objects_mixin';
export { SavedObjectsClient } from './service';
export * from './service';

export { SavedObjectsSchema } from './schema';

export { SavedObjectsManagement } from './management';
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ import Boom from 'boom';
import _ from 'lodash';
import cloneDeep from 'lodash.clonedeep';
import Semver from 'semver';
import { MigrationVersion, RawSavedObjectDoc } from '../../serialization';
import { RawSavedObjectDoc } from '../../serialization';
import { MigrationVersion } from '../../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { MigrationVersion } from '../..';

import { LogFn, Logger, MigrationLogger } from './migration_logger';

export type TransformFn = (doc: RawSavedObjectDoc, log?: Logger) => RawSavedObjectDoc;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,9 @@

import _ from 'lodash';
import { IndexMapping } from '../../../mappings';
import { MigrationVersion } from '../../serialization';
import { MigrationVersion } from '../../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { MigrationVersion } from '../..';

import { AliasAction, CallCluster, NotFound, RawDoc, ShardsInfo } from './call_cluster';

// @ts-ignore untyped dependency
import { getTypes } from '../../../mappings';

const settings = { number_of_shards: 1, auto_expand_replicas: '0-1' };

export interface FullIndexInfo {
Expand Down
17 changes: 5 additions & 12 deletions src/legacy/server/saved_objects/routes/bulk_create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,14 @@
import Hapi from 'hapi';
import { createMockServer } from './_mock_server';
import { createBulkCreateRoute } from './bulk_create';
import { SavedObjectsClientMock } from '../service/saved_objects_client.mock';

describe('POST /api/saved_objects/_bulk_create', () => {
let server: Hapi.Server;
const savedObjectsClient = {
errors: {} as any,
bulkCreate: jest.fn(),
bulkGet: jest.fn(),
create: jest.fn(),
delete: jest.fn(),
find: jest.fn(),
get: jest.fn(),
update: jest.fn(),
};
const savedObjectsClient = SavedObjectsClientMock.create();

beforeEach(() => {
savedObjectsClient.bulkCreate.mockImplementation(() => Promise.resolve(''));
savedObjectsClient.bulkCreate.mockImplementation(() => Promise.resolve('' as any));
server = createMockServer();

const prereqs = {
Expand Down Expand Up @@ -75,7 +67,8 @@ describe('POST /api/saved_objects/_bulk_create', () => {
id: 'abc123',
type: 'index-pattern',
title: 'logstash-*',
version: 2,
attributes: {},
version: '2',
references: [],
},
],
Expand Down
4 changes: 2 additions & 2 deletions src/legacy/server/saved_objects/routes/bulk_create.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@

import Hapi from 'hapi';
import Joi from 'joi';
import { SavedObjectAttributes, SavedObjectsClient } from '../';
import { SavedObjectAttributes, SavedObjectsClientContract } from '../';
Copy link
Contributor

Choose a reason for hiding this comment

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

Remove trailing slash for consistency:

import { SavedObjectAttributes, SavedObjectsClientContract } from '..';

import { Prerequisites, SavedObjectReference, WithoutQueryAndParams } from './types';

interface SavedObject {
Expand All @@ -33,7 +33,7 @@ interface SavedObject {

interface BulkCreateRequest extends WithoutQueryAndParams<Hapi.Request> {
pre: {
savedObjectsClient: SavedObjectsClient;
savedObjectsClient: SavedObjectsClientContract;
};
query: {
overwrite: boolean;
Expand Down
Loading