forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
42be2f6
commit c5868b9
Showing
6 changed files
with
129 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters