Skip to content

Commit

Permalink
Datasource: Elasticsearch
Browse files Browse the repository at this point in the history
  • Loading branch information
Navaneeth-pk committed Jul 15, 2021
1 parent 42be2f6 commit c5868b9
Show file tree
Hide file tree
Showing 6 changed files with 129 additions and 24 deletions.
28 changes: 28 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"webpack-cli": "^3.3.0"
},
"dependencies": {
"@elastic/elasticsearch": "^7.13.0",
"eslint": "^7.25.0"
},
"scripts": {
Expand Down
57 changes: 57 additions & 0 deletions server/plugins/datasources/elasticsearch/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { Injectable } from '@nestjs/common';
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> {

const scheme = sourceOptions.scheme;
const host = sourceOptions.host;
const port = sourceOptions.port;
const username = sourceOptions.username;
const password = sourceOptions.password;

let url = '';

if(username === '' || password === '') {
url = `${scheme}://${username}:${password}@${host}:${port}`;
} else {
url = `${scheme}://${host}:${port}`;
}

const client = new Client({ node: url });

let result = { };
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);
break;
case 'get':
result = await getDocument(client, queryOptions.index, queryOptions.id);
break;
case 'update':
result = await updateDocument(client, queryOptions.index, queryOptions.id, queryOptions.body);
break;
}
} catch (err) {
console.log(err);
}

return {
status: 'ok',
data: result
}
}
}
40 changes: 40 additions & 0 deletions server/plugins/datasources/elasticsearch/operations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
export async function search(client, index: string, query: string): Promise<object> {

const result = await client.search({
index,
body: JSON.parse(query)
})

return result;
}


export async function indexDocument(client, index: string, body: string): Promise<object> {

const result = await client.index({
index,
body: JSON.parse(body)
})

return result;
}


export async function getDocument(client, index: string, id: string): Promise<object> {

const result = await client.get({ index, id });

return result;
}


export async function updateDocument(client, index: string, id: string, body: string): Promise<object> {

const result = await client.update({
index,
id,
body: JSON.parse(body)
})

return result;
}
23 changes: 0 additions & 23 deletions server/plugins/datasources/mysql/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,4 @@ export default class MysqlQueryService implements QueryService {
data: result[0]
}
}

async buildBulkUpdateQuery(queryOptions: any): Promise<string> {
let queryText = '';

const tableName = queryOptions['table'];
const primaryKey = queryOptions['primary_key_column'];
const records = queryOptions['records'];

for (const record of records) {
queryText = `${queryText} UPDATE ${tableName} SET`;

for (const key of Object.keys(record)) {
if (key !== primaryKey) {
queryText = ` ${queryText} ${key} = '${record[key]}', `;
}
}

queryText = `${queryText} WHERE ${primaryKey} = ${record[primaryKey]};`;
}

return queryText.trim();

}
}
4 changes: 3 additions & 1 deletion server/src/services/data_queries.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@ import { CredentialsService } from './credentials.service';
import FirestoreQueryService from '../../plugins/datasources/firestore';
import PostgresqlQueryService from '../../plugins/datasources/postgresql';
import MysqlQueryService from '../../plugins/datasources/mysql';
import ElasticsearchService from '../../plugins/datasources/elasticsearch';

@Injectable()
export class DataQueriesService {

private plugins = {
postgresql: PostgresqlQueryService,
firestore: FirestoreQueryService,
mysql: MysqlQueryService
mysql: MysqlQueryService,
elasticsearch: ElasticsearchService
};

constructor(
Expand Down

0 comments on commit c5868b9

Please sign in to comment.