2021-06-24 19:17:26 +02:00
import {
Integration ,
2022-08-11 14:50:05 +02:00
DatasourceFieldType ,
QueryType ,
2022-08-11 12:48:58 +02:00
IntegrationBase ,
2023-05-05 16:47:55 +02:00
DatasourceFeature ,
2023-05-17 09:38:37 +02:00
ConnectionInfo ,
2022-08-11 12:48:58 +02:00
} from "@budibase/types"
2021-06-24 19:16:48 +02:00
2022-11-01 02:08:17 +01:00
import { Client , ClientOptions } from "@elastic/elasticsearch"
2024-03-28 13:14:56 +01:00
import { HOST_ADDRESS } from "./utils"
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
interface ElasticsearchConfig {
url : string
2022-10-25 05:59:34 +02:00
ssl? : boolean
ca? : string
rejectUnauthorized? : boolean
2022-08-12 18:03:06 +02:00
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
const SCHEMA : Integration = {
docs : "https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/index.html" ,
description :
"Elasticsearch is a search engine based on the Lucene library. It provides a distributed, multitenant-capable full-text search engine with an HTTP web interface and schema-free JSON documents." ,
friendlyName : "ElasticSearch" ,
type : "Non-relational" ,
2023-05-24 10:50:51 +02:00
features : {
[ DatasourceFeature . CONNECTION_CHECKING ] : true ,
} ,
2022-08-12 18:03:06 +02:00
datasource : {
url : {
type : DatasourceFieldType . STRING ,
required : true ,
2024-03-28 13:14:56 +01:00
default : ` http:// ${ HOST_ADDRESS } :9200 ` ,
2021-06-24 19:16:48 +02:00
} ,
2022-10-25 05:59:34 +02:00
ssl : {
type : DatasourceFieldType . BOOLEAN ,
default : false ,
required : false ,
} ,
rejectUnauthorized : {
type : DatasourceFieldType . BOOLEAN ,
default : true ,
required : false ,
} ,
ca : {
type : DatasourceFieldType . LONGFORM ,
default : false ,
required : false ,
} ,
2022-08-12 18:03:06 +02:00
} ,
query : {
create : {
type : QueryType . FIELDS ,
customisable : true ,
fields : {
index : {
type : DatasourceFieldType . STRING ,
required : true ,
2021-06-24 19:16:48 +02:00
} ,
} ,
2022-08-12 18:03:06 +02:00
} ,
read : {
type : QueryType . FIELDS ,
customisable : true ,
fields : {
index : {
type : DatasourceFieldType . STRING ,
required : true ,
2021-06-24 19:16:48 +02:00
} ,
} ,
2022-08-12 18:03:06 +02:00
} ,
update : {
type : QueryType . FIELDS ,
customisable : true ,
fields : {
id : {
type : DatasourceFieldType . STRING ,
required : true ,
} ,
index : {
type : DatasourceFieldType . STRING ,
required : true ,
2021-06-24 19:16:48 +02:00
} ,
} ,
2022-08-12 18:03:06 +02:00
} ,
delete : {
type : QueryType . FIELDS ,
fields : {
2023-02-01 13:47:38 +01:00
id : {
2022-08-12 18:03:06 +02:00
type : DatasourceFieldType . STRING ,
required : true ,
} ,
2023-02-01 13:47:38 +01:00
index : {
2022-08-12 18:03:06 +02:00
type : DatasourceFieldType . STRING ,
required : true ,
2021-06-24 19:16:48 +02:00
} ,
} ,
} ,
2022-08-12 18:03:06 +02:00
} ,
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
class ElasticSearchIntegration implements IntegrationBase {
private config : ElasticsearchConfig
2023-05-17 09:38:37 +02:00
private client
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
constructor ( config : ElasticsearchConfig ) {
this . config = config
2022-10-25 05:59:34 +02:00
2022-11-01 02:08:17 +01:00
const clientConfig : ClientOptions = {
2022-10-25 05:59:34 +02:00
node : this.config.url ,
}
2022-11-01 02:08:17 +01:00
if ( this . config . ssl ) {
clientConfig . ssl = {
rejectUnauthorized : this.config.rejectUnauthorized ,
ca : this.config.ca || undefined ,
}
2022-10-25 05:59:34 +02:00
}
2022-11-01 02:08:17 +01:00
this . client = new Client ( clientConfig )
2022-08-12 18:03:06 +02:00
}
2021-06-24 19:16:48 +02:00
2023-05-17 09:38:37 +02:00
async testConnection ( ) : Promise < ConnectionInfo > {
try {
await this . client . info ( )
return { connected : true }
} catch ( e : any ) {
return {
connected : false ,
error : e.message as string ,
}
}
}
2022-08-12 18:03:06 +02:00
async create ( query : { index : string ; json : object } ) {
const { index , json } = query
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
try {
const result = await this . client . index ( {
index ,
body : json ,
} )
return result . body
} catch ( err ) {
console . error ( "Error writing to elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
2021-06-24 19:16:48 +02:00
}
2022-08-12 18:03:06 +02:00
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
async read ( query : { index : string ; json : object } ) {
const { index , json } = query
try {
const result = await this . client . search ( {
index : index ,
body : json ,
} )
return result . body . hits . hits . map ( ( { _source } : any ) = > _source )
} catch ( err ) {
console . error ( "Error querying elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
2021-06-24 19:16:48 +02:00
}
2022-08-12 18:03:06 +02:00
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
async update ( query : { id : string ; index : string ; json : object } ) {
const { id , index , json } = query
try {
const result = await this . client . update ( {
id ,
index ,
body : json ,
} )
return result . body
} catch ( err ) {
console . error ( "Error querying elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
2021-06-24 19:16:48 +02:00
}
2022-08-12 18:03:06 +02:00
}
2021-06-24 19:16:48 +02:00
2023-02-01 13:47:38 +01:00
async delete ( query : { id : string ; index : string } ) {
const { id , index } = query
2022-08-12 18:03:06 +02:00
try {
2023-02-01 13:47:38 +01:00
const result = await this . client . delete ( {
id ,
index ,
} )
2022-08-12 18:03:06 +02:00
return result . body
} catch ( err ) {
console . error ( "Error deleting from elasticsearch" , err )
throw err
} finally {
await this . client . close ( )
2021-06-24 19:16:48 +02:00
}
}
2022-08-12 18:03:06 +02:00
}
2021-06-24 19:16:48 +02:00
2022-08-12 18:03:06 +02:00
export default {
schema : SCHEMA ,
integration : ElasticSearchIntegration ,
2021-06-24 19:16:48 +02:00
}