diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 1ade192..e77acba 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/elasticsearch', - version: '1.0.49', + version: '1.0.50', description: 'log to elasticsearch in a kibana compatible format' } diff --git a/ts/elasticsearch.classes.elasticindex.ts b/ts/elasticsearch.classes.elasticindex.ts index cc06a88..2067cd8 100644 --- a/ts/elasticsearch.classes.elasticindex.ts +++ b/ts/elasticsearch.classes.elasticindex.ts @@ -1,4 +1,4 @@ -import * as plugins from './elasticsearch.plugins.js'; +import * as plugins from './els.plugins.js'; import { ElsSmartlogDestination } from './els.classes.smartlogdestination.js'; import { type ILogPackage } from '@pushrocks/smartlog-interfaces'; import { Stringmap } from '@pushrocks/lik'; diff --git a/ts/els.classes.kvstore.ts b/ts/els.classes.kvstore.ts new file mode 100644 index 0000000..8f62939 --- /dev/null +++ b/ts/els.classes.kvstore.ts @@ -0,0 +1,111 @@ +import * as plugins from './els.plugins.js'; +import { Client as ElasticClient } from '@elastic/elasticsearch'; + +export interface IElasticKVStoreConstructorOptions { + index: string; + node: string; + auth?: { + username: string; + password: string; + }; +} + +export class ElasticKVStore { + public client: ElasticClient; + public index: string; + private readyDeferred: any; + + constructor(options: IElasticKVStoreConstructorOptions) { + this.client = new ElasticClient({ + node: options.node, + ...(options.auth && { auth: options.auth }), + }); + this.index = options.index; + this.readyDeferred = plugins.smartpromise.defer(); + this.setupIndex(); + } + + private async setupIndex() { + try { + const { body: indexExists } = await this.client.indices.exists({ index: this.index }); + + if (!indexExists) { + await this.client.indices.create({ + index: this.index, + body: { + mappings: { + properties: { + key: { + type: 'keyword' + }, + value: { + type: 'text' + } + } + } + } + }); + } + this.readyDeferred.resolve(); + } catch (err) { + this.readyDeferred.reject(err); + } + } + + async set(key: string, value: string) { + await this.readyDeferred.promise; + await this.client.index({ + index: this.index, + id: key, + body: { + key, + value + } + }); + } + + async get(key: string): Promise { + await this.readyDeferred.promise; + + try { + const response = await this.client.get({ + index: this.index, + id: key + }); + return response.body._source.value; + } catch (error) { + if (error.meta && error.meta.statusCode === 404) { + return null; + } + throw error; + } + } + + async delete(key: string) { + await this.readyDeferred.promise; + + try { + await this.client.delete({ + index: this.index, + id: key + }); + } catch (error) { + if (error.meta && error.meta.statusCode !== 404) { + throw error; + } + } + } + + async clear() { + await this.readyDeferred.promise; + + await this.client.deleteByQuery({ + index: this.index, + body: { + query: { + match_all: {} + } + } + }); + } +} diff --git a/ts/elasticsearch.plugins.ts b/ts/els.plugins.ts similarity index 100% rename from ts/elasticsearch.plugins.ts rename to ts/els.plugins.ts