From 0eb0903667b51b9999eef3b60e8a0e04b4fc3089 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Thu, 17 Aug 2023 20:53:28 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/els.classes.fastpush.ts | 35 ++++++++++++++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index 2d06544..06f227f 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.53', + version: '1.0.54', description: 'log to elasticsearch in a kibana compatible format' } diff --git a/ts/els.classes.fastpush.ts b/ts/els.classes.fastpush.ts index 6dabbb8..badea88 100644 --- a/ts/els.classes.fastpush.ts +++ b/ts/els.classes.fastpush.ts @@ -1,5 +1,10 @@ import { Client as ElasticClient } from '@elastic/elasticsearch'; +interface FastPushOptions { + deleteOldData?: boolean; // Clear the index + deleteIndex?: boolean; // Delete the entire index +} + export class FastPush { private client: ElasticClient; @@ -10,22 +15,34 @@ export class FastPush { }); } - async pushToIndex(indexName: string, docArray: any[]) { + async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) { if (docArray.length === 0) return; - // Check if index exists const { body: indexExists } = await this.client.indices.exists({ index: indexName }); - if (!indexExists) { + if (indexExists) { + if (options?.deleteIndex) { + await this.client.indices.delete({ index: indexName }); + } else if (options?.deleteOldData) { + await this.client.deleteByQuery({ + index: indexName, + body: { + query: { + match_all: {} + } + } + }); + } + } + + if (!indexExists || options?.deleteIndex) { // Create index with mappings (for simplicity, we use dynamic mapping) await this.client.indices.create({ index: indexName, body: { mappings: { - dynamic: "true", - properties: { - // If there's a need for specific mappings, they can be added here - }, + dynamic: "true" + // ... other specific mappings }, }, }); @@ -45,3 +62,7 @@ export class FastPush { await this.client.bulk({ body: bulkBody }); } } + +// Usage example: +// const fastPush = new FastPush('http://localhost:9200', { username: 'elastic', password: 'password' }); +// fastPush.pushToIndex('my_index', [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }], { deleteOldData: true });