fix(core): update

This commit is contained in:
Philipp Kunz 2023-08-17 20:53:28 +02:00
parent 4d11dca22c
commit 0eb0903667
2 changed files with 29 additions and 8 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@apiclient.xyz/elasticsearch', name: '@apiclient.xyz/elasticsearch',
version: '1.0.53', version: '1.0.54',
description: 'log to elasticsearch in a kibana compatible format' description: 'log to elasticsearch in a kibana compatible format'
} }

View File

@ -1,5 +1,10 @@
import { Client as ElasticClient } from '@elastic/elasticsearch'; import { Client as ElasticClient } from '@elastic/elasticsearch';
interface FastPushOptions {
deleteOldData?: boolean; // Clear the index
deleteIndex?: boolean; // Delete the entire index
}
export class FastPush { export class FastPush {
private client: ElasticClient; 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; if (docArray.length === 0) return;
// Check if index exists
const { body: indexExists } = await this.client.indices.exists({ index: indexName }); 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) // Create index with mappings (for simplicity, we use dynamic mapping)
await this.client.indices.create({ await this.client.indices.create({
index: indexName, index: indexName,
body: { body: {
mappings: { mappings: {
dynamic: "true", dynamic: "true"
properties: { // ... other specific mappings
// If there's a need for specific mappings, they can be added here
},
}, },
}, },
}); });
@ -45,3 +62,7 @@ export class FastPush {
await this.client.bulk({ body: bulkBody }); 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 });