From 8f9f2fdf05199bf1ccb873ac8da8c9245122d6e2 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Mon, 14 Aug 2023 10:58:28 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/elasticsearch.classes.elasticindex.ts | 2 +- ts/els.classes.kvstore.ts | 111 ++++++++++++++++++ ...lasticsearch.plugins.ts => els.plugins.ts} | 0 4 files changed, 113 insertions(+), 2 deletions(-) create mode 100644 ts/els.classes.kvstore.ts rename ts/{elasticsearch.plugins.ts => els.plugins.ts} (100%) 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