budibase/packages/server/src/integrations/elasticsearch.js

45 lines
895 B
JavaScript
Raw Normal View History

2020-11-26 18:03:18 +01:00
const { Client } = require("@elastic/elasticsearch")
const ELASTICSEARCH_OPTIONS = {
url: {
type: "string",
required: true,
default: "localhost",
},
index: {
type: "string",
required: true,
},
query: {
type: "json",
2020-11-26 18:03:18 +01:00
required: true,
},
}
class ElasticSearchIntegration {
constructor(config) {
this.config = config
this.client = new Client({ node: config.url })
}
async query() {
try {
const result = await this.client.search({
index: this.config.index,
body: JSON.parse(this.config.query),
})
return result.body.hits.hits.map(({ _source }) => _source)
2020-11-26 18:34:15 +01:00
} catch (err) {
console.error("Error querying elasticsearch", err)
throw err
2020-11-26 18:03:18 +01:00
} finally {
await this.client.close()
}
}
}
module.exports = {
schema: ELASTICSEARCH_OPTIONS,
integration: ElasticSearchIntegration,
}