2021-07-15 14:59:11 +00:00
|
|
|
import { Injectable } from '@nestjs/common';
|
2021-07-18 07:43:19 +00:00
|
|
|
import { ConnectionTestResult } from 'src/modules/data_sources/connection_test_result.type';
|
2021-07-15 14:59:11 +00:00
|
|
|
import { QueryResult } from 'src/modules/data_sources/query_result.type';
|
|
|
|
|
import { QueryService } from 'src/modules/data_sources/query_service.interface';
|
|
|
|
|
import { getDocument, updateDocument } from './operations';
|
|
|
|
|
import { indexDocument, search } from './operations';
|
|
|
|
|
const { Client } = require('@elastic/elasticsearch');
|
|
|
|
|
|
|
|
|
|
@Injectable()
|
|
|
|
|
export default class ElasticsearchService implements QueryService {
|
|
|
|
|
async run(sourceOptions: any, queryOptions: any, dataSourceId: string): Promise<QueryResult> {
|
2021-07-17 07:59:28 +00:00
|
|
|
const client = await this.getConnection(sourceOptions);
|
2021-09-21 13:48:28 +00:00
|
|
|
let result = {};
|
2021-07-15 14:59:11 +00:00
|
|
|
const operation = queryOptions.operation;
|
|
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
switch (operation) {
|
|
|
|
|
case 'search':
|
|
|
|
|
result = await search(client, queryOptions.index, queryOptions.query);
|
|
|
|
|
break;
|
|
|
|
|
case 'index_document':
|
|
|
|
|
result = await indexDocument(client, queryOptions.index, queryOptions.body);
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
2021-07-15 14:59:11 +00:00
|
|
|
case 'get':
|
|
|
|
|
result = await getDocument(client, queryOptions.index, queryOptions.id);
|
|
|
|
|
break;
|
|
|
|
|
case 'update':
|
|
|
|
|
result = await updateDocument(client, queryOptions.index, queryOptions.id, queryOptions.body);
|
2021-09-21 13:48:28 +00:00
|
|
|
break;
|
2021-07-15 14:59:11 +00:00
|
|
|
}
|
|
|
|
|
} catch (err) {
|
|
|
|
|
console.log(err);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
status: 'ok',
|
2021-09-21 13:48:28 +00:00
|
|
|
data: result,
|
|
|
|
|
};
|
2021-07-15 14:59:11 +00:00
|
|
|
}
|
2021-07-17 07:59:28 +00:00
|
|
|
|
2021-07-18 07:43:19 +00:00
|
|
|
async testConnection(sourceOptions: object): Promise<ConnectionTestResult> {
|
|
|
|
|
const client = await this.getConnection(sourceOptions);
|
|
|
|
|
await client.info();
|
|
|
|
|
|
|
|
|
|
return {
|
2021-09-21 13:48:28 +00:00
|
|
|
status: 'ok',
|
|
|
|
|
};
|
2021-07-18 07:43:19 +00:00
|
|
|
}
|
|
|
|
|
|
2021-07-17 07:59:28 +00:00
|
|
|
async getConnection(sourceOptions: any): Promise<any> {
|
|
|
|
|
const scheme = sourceOptions.scheme;
|
|
|
|
|
const host = sourceOptions.host;
|
|
|
|
|
const port = sourceOptions.port;
|
|
|
|
|
const username = sourceOptions.username;
|
|
|
|
|
const password = sourceOptions.password;
|
|
|
|
|
|
|
|
|
|
let url = '';
|
|
|
|
|
|
2021-09-21 13:48:28 +00:00
|
|
|
if (username === '' || password === '') {
|
2021-07-17 07:59:28 +00:00
|
|
|
url = `${scheme}://${username}:${password}@${host}:${port}`;
|
|
|
|
|
} else {
|
|
|
|
|
url = `${scheme}://${host}:${port}`;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return new Client({ node: url });
|
|
|
|
|
}
|
2021-07-15 14:59:11 +00:00
|
|
|
}
|