static createRepository factory function (#52056) (#52187)

* static createRepository factory function

* Fix API docs after master merge
This commit is contained in:
Rudolf Meijering 2019-12-05 10:37:15 +01:00 committed by GitHub
parent 6cfa632ec2
commit e06dcd5720
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 1823 additions and 1846 deletions

View file

@ -4,6 +4,7 @@
## SavedObjectsRepository class
<b>Signature:</b>
```typescript
@ -25,7 +26,3 @@ export declare class SavedObjectsRepository
| [incrementCounter(type, id, counterFieldName, options)](./kibana-plugin-server.savedobjectsrepository.incrementcounter.md) | | Increases a counter field by one. Creates the document if one doesn't exist for the given id. |
| [update(type, id, attributes, options)](./kibana-plugin-server.savedobjectsrepository.update.md) | | Updates an object |
## Remarks
The constructor for this class is marked as internal. Third-party code should not call the constructor directly or create subclasses that extend the `SavedObjectsRepository` class.

View file

@ -35,12 +35,11 @@ import { migrationsRetryCallCluster } from '../elasticsearch/retry_call_cluster'
import { SavedObjectsConfigType } from './saved_objects_config';
import { KibanaRequest } from '../http';
import { SavedObjectsClientContract } from './types';
import { ISavedObjectsRepository } from './service/lib/repository';
import { ISavedObjectsRepository, SavedObjectsRepository } from './service/lib/repository';
import {
SavedObjectsClientFactory,
SavedObjectsClientWrapperFactory,
} from './service/lib/scoped_client_provider';
import { createRepository } from './service/lib/create_repository';
import { Logger } from '..';
/**
@ -237,7 +236,7 @@ export class SavedObjectsService
}));
const createSORepository = (callCluster: APICaller, extraTypes: string[] = []) => {
return createRepository(
return SavedObjectsRepository.createRepository(
migrator,
savedObjectSchemas,
setupDeps.legacyPlugins.pluginExtendedConfig,

View file

@ -1,61 +0,0 @@
/*
* Licensed to Elasticsearch B.V. under one or more contributor
* license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright
* ownership. Elasticsearch B.V. licenses this file to you under
* the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import { APICaller } from 'src/core/server/elasticsearch';
import { retryCallCluster } from '../../../elasticsearch/retry_call_cluster';
import { KibanaMigrator } from '../../migrations';
import { SavedObjectsSchema } from '../../schema';
import { getRootPropertiesObjects } from '../../mappings';
import { SavedObjectsSerializer } from '../../serialization';
import { SavedObjectsRepository } from '.';
import { LegacyConfig } from '../../../legacy/config';
export const createRepository = (
migrator: KibanaMigrator,
schema: SavedObjectsSchema,
config: LegacyConfig,
indexName: string,
callCluster: APICaller,
extraTypes: string[] = []
) => {
const mappings = migrator.getActiveMappings();
const allTypes = Object.keys(getRootPropertiesObjects(mappings));
const serializer = new SavedObjectsSerializer(schema);
const visibleTypes = allTypes.filter(type => !schema.isHiddenType(type));
const missingTypeMappings = extraTypes.filter(type => !allTypes.includes(type));
if (missingTypeMappings.length > 0) {
throw new Error(
`Missing mappings for saved objects types: '${missingTypeMappings.join(', ')}'`
);
}
const allowedTypes = [...new Set(visibleTypes.concat(extraTypes))];
return new SavedObjectsRepository({
index: indexName,
config,
migrator,
mappings,
schema,
serializer,
allowedTypes,
callCluster: retryCallCluster(callCluster),
});
};

View file

@ -17,11 +17,7 @@
* under the License.
*/
export {
ISavedObjectsRepository,
SavedObjectsRepository,
SavedObjectsRepositoryOptions,
} from './repository';
export { ISavedObjectsRepository, SavedObjectsRepository } from './repository';
export {
SavedObjectsClientWrapperFactory,

View file

@ -18,6 +18,7 @@
*/
import { omit } from 'lodash';
import { retryCallCluster } from '../../../elasticsearch/retry_call_cluster';
import { APICaller } from '../../../elasticsearch/';
import { getRootPropertiesObjects, IndexMapping } from '../../mappings';
import { getSearchDsl } from './search_dsl';
@ -123,8 +124,50 @@ export class SavedObjectsRepository {
private _unwrappedCallCluster: APICaller;
private _serializer: SavedObjectsSerializer;
/** @internal */
constructor(options: SavedObjectsRepositoryOptions) {
/**
* A factory function for creating SavedObjectRepository instances.
*
* @internalRemarks
* Tests are located in ./repository_create_repository.test.ts
*
* @internal
*/
public static createRepository(
migrator: KibanaMigrator,
schema: SavedObjectsSchema,
config: LegacyConfig,
indexName: string,
callCluster: APICaller,
extraTypes: string[] = [],
injectedConstructor: any = SavedObjectsRepository
) {
const mappings = migrator.getActiveMappings();
const allTypes = Object.keys(getRootPropertiesObjects(mappings));
const serializer = new SavedObjectsSerializer(schema);
const visibleTypes = allTypes.filter(type => !schema.isHiddenType(type));
const missingTypeMappings = extraTypes.filter(type => !allTypes.includes(type));
if (missingTypeMappings.length > 0) {
throw new Error(
`Missing mappings for saved objects types: '${missingTypeMappings.join(', ')}'`
);
}
const allowedTypes = [...new Set(visibleTypes.concat(extraTypes))];
return new injectedConstructor({
index: indexName,
config,
migrator,
mappings,
schema,
serializer,
allowedTypes,
callCluster: retryCallCluster(callCluster),
});
}
private constructor(options: SavedObjectsRepositoryOptions) {
const {
index,
config,

View file

@ -20,11 +20,12 @@ import { SavedObjectsRepository } from './repository';
import { mockKibanaMigrator } from '../../migrations/kibana/kibana_migrator.mock';
import { SavedObjectsSchema } from '../../schema';
import { KibanaMigrator } from '../../migrations';
import { createRepository } from './create_repository';
import { LegacyConfig } from '../../../legacy/config';
jest.mock('./repository');
describe('#createRepository', () => {
const { SavedObjectsRepository: originalRepository } = jest.requireActual('./repository');
describe('SavedObjectsRepository#createRepository', () => {
const callAdminCluster = jest.fn();
const schema = new SavedObjectsSchema({
nsAgnosticType: { isNamespaceAgnostic: true },
@ -64,7 +65,7 @@ describe('#createRepository', () => {
it('should not allow a repository with an undefined type', () => {
try {
createRepository(
originalRepository.createRepository(
(migrator as unknown) as KibanaMigrator,
schema,
{} as LegacyConfig,
@ -80,12 +81,14 @@ describe('#createRepository', () => {
});
it('should create a repository without hidden types', () => {
const repository = createRepository(
const repository = originalRepository.createRepository(
(migrator as unknown) as KibanaMigrator,
schema,
{} as LegacyConfig,
'.kibana-test',
callAdminCluster
callAdminCluster,
[],
SavedObjectsRepository
);
expect(repository).toBeDefined();
expect(RepositoryConstructor.mock.calls[0][0].allowedTypes).toMatchInlineSnapshot(`
@ -98,13 +101,14 @@ describe('#createRepository', () => {
});
it('should create a repository with a unique list of hidden types', () => {
const repository = createRepository(
const repository = originalRepository.createRepository(
(migrator as unknown) as KibanaMigrator,
schema,
{} as LegacyConfig,
'.kibana-test',
callAdminCluster,
['hiddenType', 'hiddenType', 'hiddenType']
['hiddenType', 'hiddenType', 'hiddenType'],
SavedObjectsRepository
);
expect(repository).toBeDefined();
expect(RepositoryConstructor.mock.calls[0][0].allowedTypes).toMatchInlineSnapshot(`

File diff suppressed because it is too large Load diff