Skip to content

Commit

Permalink
Fixed unused variables. Added a few methods to the SO adapter
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolas Chaulet <n.chaulet@gmail.com>
  • Loading branch information
mattapperson and nchaulet committed Jul 23, 2019
1 parent a8d5111 commit db5fc8f
Show file tree
Hide file tree
Showing 4 changed files with 202 additions and 18 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/*
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one
* or more contributor license agreements. Licensed under the Elastic License;
* you may not use this file except in compliance with the Elastic License.
*/
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
*/

import * as t from 'io-ts';
import { DateFromString } from '../../../../common/types/io_ts';

export const RuntimeDatasourceInput = t.interface(
{
Expand Down Expand Up @@ -48,13 +47,14 @@ export const NewRuntimeBackupConfigurationFile = t.interface(
'BackupConfigurationFile'
);

const ExistingDocument = t.partial({
const ExistingDocument = t.interface({
id: t.string,
shared_id: t.string,
version: t.number,
updated_at: DateFromString,
active: t.boolean,
updated_at: t.string,
created_by: t.union([t.undefined, t.string]),
updated_on: DateFromString,
updated_on: t.string,
updated_by: t.union([t.undefined, t.string]),
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,33 +5,97 @@
*/

import { SODatabaseAdapter } from '../so_database/default';
import { RuntimeConfigurationFile, NewConfigurationFile } from './adapter_types';

import {
ConfigurationFile,
NewConfigurationFile,
DatasourceInput,
BackupConfigurationFile,
} from './adapter_types';
import { ConfigurationFile, DatasourceInput, BackupConfigurationFile } from './adapter_types';

export class ConfigAdapter {
constructor(private readonly so: SODatabaseAdapter) {}

public async create(
configuration: NewConfigurationFile
): Promise<{ id: string; shared_id: string; version: number }> {
const newSo = await this.so.create<ConfigurationFile>(
'configurations',
(configuration as any) as ConfigurationFile
);

return {
id: 'fsdfsdf',
shared_id: 'wjkhefkjhfkjs',
version: 0,
id: newSo.id,
shared_id: newSo.attributes.shared_id,
version: newSo.attributes.version,
};
}

public async get(sharedID: string, version?: number): Promise<ConfigurationFile> {
return {} as ConfigurationFile;
public async get(id: string): Promise<ConfigurationFile> {
const config = await this.so.get<any>('configurations', id);

if (config.error) {
throw new Error(config.error.message);
}

if (!config.attributes) {
throw new Error(`No configuration found with ID of ${id}`);
}
if (RuntimeConfigurationFile.decode(config.attributes).isRight()) {
return config.attributes as ConfigurationFile;
} else {
throw new Error(`Invalid ConfigurationFile data. == ${config.attributes}`);
}
}

public async list(sharedID: string, version?: number): Promise<ConfigurationFile[]> {
return [{} as ConfigurationFile];
public async list(): Promise<ConfigurationFile[]> {
const configs = await this.so.find<any>({
type: 'configurations',
search: '*',
searchFields: ['shared_id'],
});
const uniqConfigurationFile = configs.saved_objects
.map<ConfigurationFile>(config => {
if (RuntimeConfigurationFile.decode(config.attributes).isRight()) {
return config.attributes;
} else {
throw new Error(`Invalid ConfigurationFile data. == ${config.attributes}`);
}
})
.reduce((acc, config: ConfigurationFile) => {
if (!acc.has(config.shared_id)) {
acc.set(config.shared_id, config);
}
const prevConfig = acc.get(config.shared_id);
if (prevConfig && prevConfig.version < config.version) {
acc.set(config.shared_id, config);
}

return acc;
}, new Map<string, ConfigurationFile>());

return [...uniqConfigurationFile.values()];
}

public async listVersions(sharedID: string, activeOnly = true): Promise<ConfigurationFile[]> {
const configs = (await this.so.find<any>({
type: 'configurations',
search: sharedID,
searchFields: ['shared_id'],
})).saved_objects;

if (!activeOnly) {
const backupConfigs = await this.so.find<any>({
type: 'backup_configurations',
search: sharedID,
searchFields: ['shared_id'],
});
configs.concat(backupConfigs.saved_objects);
}

return configs.map<ConfigurationFile>(config => {
if (RuntimeConfigurationFile.decode(config.attributes).isRight()) {
return config.attributes;
} else {
throw new Error(`Invalid ConfigurationFile data. == ${config.attributes}`);
}
});
}

public async update(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,24 @@
* you may not use this file except in compliance with the Elastic License.
*/

import { SavedObjectsService, SavedObjectsClient as SavedObjectsClientType } from 'src/core/server';
import {
SavedObjectsService,
SavedObjectsClient as SavedObjectsClientType,
SavedObjectAttributes,
SavedObjectsBulkCreateObject,
SavedObjectsBaseOptions,
SavedObjectsFindOptions,
SavedObjectsFindResponse,
SavedObjectsBulkResponse,
SavedObject,
SavedObjectsUpdateOptions,
} from 'src/core/server';
import { ElasticsearchPlugin } from 'src/legacy/core_plugins/elasticsearch';
import {
SavedObjectsCreateOptions,
SavedObjectsBulkGetObject,
SavedObjectsUpdateResponse,
} from 'target/types/server';

export class SODatabaseAdapter {
private client: SavedObjectsClientType;
Expand All @@ -16,4 +32,103 @@ export class SODatabaseAdapter {

this.client = new SavedObjectsClient(internalRepository);
}

/**
* Persists a SavedObject
*
* @param type
* @param attributes
* @param options
*/
async create<T extends SavedObjectAttributes = any>(
type: string,
data: T,
options?: SavedObjectsCreateOptions
) {
return await this.client.create(type, data, options);
}

/**
* Persists multiple documents batched together as a single request
*
* @param objects
* @param options
*/
async bulkCreate<T extends SavedObjectAttributes = any>(
objects: Array<SavedObjectsBulkCreateObject<T>>,
options?: SavedObjectsCreateOptions
) {
return await this.client.bulkCreate(objects, options);
}

/**
* Deletes a SavedObject
*
* @param type
* @param id
* @param options
*/
async delete(type: string, id: string, options: SavedObjectsBaseOptions = {}) {
return await this.client.delete(type, id, options);
}

/**
* Find all SavedObjects matching the search query
*
* @param options
*/
async find<T extends SavedObjectAttributes = any>(
options: SavedObjectsFindOptions
): Promise<SavedObjectsFindResponse<T>> {
return await this.client.find(options);
}

/**
* Returns an array of objects by id
*
* @param objects - an array of ids, or an array of objects containing id, type and optionally fields
* @example
*
* bulkGet([
* { id: 'one', type: 'config' },
* { id: 'foo', type: 'index-pattern' }
* ])
*/
async bulkGet<T extends SavedObjectAttributes = any>(
objects: SavedObjectsBulkGetObject[] = [],
options: SavedObjectsBaseOptions = {}
): Promise<SavedObjectsBulkResponse<T>> {
return await this.client.bulkGet(objects, options);
}

/**
* Retrieves a single object
*
* @param type - The type of SavedObject to retrieve
* @param id - The ID of the SavedObject to retrieve
* @param options
*/
async get<T extends SavedObjectAttributes = any>(
type: string,
id: string,
options: SavedObjectsBaseOptions = {}
): Promise<SavedObject<T>> {
return await this.client.get(type, id, options);
}

/**
* Updates an SavedObject
*
* @param type
* @param id
* @param options
*/
async update<T extends SavedObjectAttributes = any>(
type: string,
id: string,
attributes: Partial<T>,
options: SavedObjectsUpdateOptions = {}
): Promise<SavedObjectsUpdateResponse<T>> {
return await this.client.update(type, id, attributes, options);
}
}

0 comments on commit db5fc8f

Please sign in to comment.