Compare commits

..

4 Commits

Author SHA1 Message Date
f32403961d 1.0.55 2023-08-18 07:58:10 +02:00
9734949241 fix(core): update 2023-08-18 07:58:09 +02:00
b70444824b 1.0.54 2023-08-17 20:53:28 +02:00
0eb0903667 fix(core): update 2023-08-17 20:53:28 +02:00
3 changed files with 30 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{ {
"name": "@apiclient.xyz/elasticsearch", "name": "@apiclient.xyz/elasticsearch",
"version": "1.0.53", "version": "1.0.55",
"private": false, "private": false,
"description": "log to elasticsearch in a kibana compatible format", "description": "log to elasticsearch in a kibana compatible format",
"main": "dist_ts/index.js", "main": "dist_ts/index.js",

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.55',
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 });