denwa-nest-shared 1.0.83 → 1.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,99 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.OpenSearchCore = void 0;
4
+ class OpenSearchCore {
5
+ client;
6
+ enabled;
7
+ constructor(client, enabled) {
8
+ this.client = client;
9
+ this.enabled = enabled;
10
+ }
11
+ async ensureIndex(index) {
12
+ if (!this.client)
13
+ return;
14
+ try {
15
+ const exists = await this.client.indices.exists({ index });
16
+ if (!exists.body) {
17
+ await this.client.indices.create({
18
+ index,
19
+ body: {
20
+ settings: { number_of_shards: 1, number_of_replicas: 0 },
21
+ mappings: {
22
+ properties: {
23
+ id: { type: 'keyword' },
24
+ nameRU: { type: 'text', analyzer: 'russian' },
25
+ nameEN: { type: 'text', analyzer: 'english' },
26
+ descriptionRU: { type: 'text', analyzer: 'russian' },
27
+ descriptionEN: { type: 'text', analyzer: 'english' },
28
+ createdAt: { type: 'date' },
29
+ updatedAt: { type: 'date' },
30
+ },
31
+ },
32
+ },
33
+ });
34
+ }
35
+ }
36
+ catch (error) {
37
+ console.error(`Failed to ensure index '${index}': ${error.message}`);
38
+ }
39
+ }
40
+ async indexDocument(index, id, body) {
41
+ if (!this.enabled || !this.client)
42
+ return;
43
+ try {
44
+ await this.client.index({ index, id, body, refresh: 'true' });
45
+ }
46
+ catch (error) {
47
+ console.error(`Failed to index document '${id}' in '${index}': ${error.message}`);
48
+ }
49
+ }
50
+ async removeDocument(index, id) {
51
+ if (!this.enabled || !this.client)
52
+ return;
53
+ try {
54
+ await this.client.delete({ index, id, refresh: 'true' });
55
+ }
56
+ catch (error) {
57
+ console.error(`Failed to remove document '${id}' from '${index}': ${error.message}`);
58
+ }
59
+ }
60
+ async search(index, query, fields, page = 1, limit = 20) {
61
+ if (!this.enabled || !this.client) {
62
+ return { data: [], total: 0, page, limit };
63
+ }
64
+ try {
65
+ const from = (page - 1) * limit;
66
+ const result = await this.client.search({
67
+ index,
68
+ from,
69
+ size: limit,
70
+ body: {
71
+ query: {
72
+ multi_match: {
73
+ query,
74
+ fields,
75
+ type: 'best_fields',
76
+ fuzziness: 'AUTO',
77
+ },
78
+ },
79
+ },
80
+ });
81
+ const totalHits = typeof result.body.hits.total === 'number'
82
+ ? result.body.hits.total
83
+ : (result.body.hits.total?.value ?? 0);
84
+ const hits = result.body.hits.hits ?? [];
85
+ return {
86
+ data: hits.map((h) => h._source ?? {}),
87
+ total: totalHits,
88
+ page,
89
+ limit,
90
+ };
91
+ }
92
+ catch (error) {
93
+ console.error(`Search failed on '${index}': ${error.message}`);
94
+ return { data: [], total: 0, page, limit };
95
+ }
96
+ }
97
+ }
98
+ exports.OpenSearchCore = OpenSearchCore;
99
+ //# sourceMappingURL=opensearch-core.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"opensearch-core.js","sourceRoot":"","sources":["../src/opensearch-core.ts"],"names":[],"mappings":";;;AAEA,MAAa,cAAc;IAEN;IACA;IAFnB,YACmB,MAAqB,EACrB,OAAgB;QADhB,WAAM,GAAN,MAAM,CAAe;QACrB,YAAO,GAAP,OAAO,CAAS;IAChC,CAAC;IAEJ,KAAK,CAAC,WAAW,CAAC,KAAa;QAC7B,IAAI,CAAC,IAAI,CAAC,MAAM;YAAE,OAAO;QACzB,IAAI,CAAC;YACH,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC,EAAE,KAAK,EAAE,CAAC,CAAC;YAC3D,IAAI,CAAC,MAAM,CAAC,IAAI,EAAE,CAAC;gBACjB,MAAM,IAAI,CAAC,MAAM,CAAC,OAAO,CAAC,MAAM,CAAC;oBAC/B,KAAK;oBACL,IAAI,EAAE;wBACJ,QAAQ,EAAE,EAAE,gBAAgB,EAAE,CAAC,EAAE,kBAAkB,EAAE,CAAC,EAAE;wBACxD,QAAQ,EAAE;4BACR,U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M,CAAC,IAAI,CAAC,IAAI,CAAC,IAAI,IAAI,EAAE,CAAC;YACzC,OAAO;gBACL,IAAI,EAAE,IAAI,CAAC,GAAG,CAAC,CAAC,CAAwC,EAAE,EAAE,CAAC,CAAC,CAAC,OAAO,IAAI,EAAE,CAAC;gBAC7E,KAAK,EAAE,SAAS;gBAChB,IAAI;gBACJ,KAAK;aACN,CAAC;QACJ,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACf,OAAO,CAAC,KAAK,CAAC,qBAAqB,KAAK,MAAO,KAAe,CAAC,OAAO,EAAE,CAAC,CAAC;YAC1E,OAAO,EAAE,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,CAAC,EAAE,IAAI,EAAE,KAAK,EAAE,CAAC;QAC7C,CAAC;IACH,CAAC;CACF;AApGD,wCAoGC","sourcesContent":["import type { Client } from '@opensearch-project/opensearch';\n\nexport class OpenSearchCore {\n constructor(\n private readonly client: Client | null,\n private readonly enabled: boolean,\n ) {}\n\n async ensureIndex(index: string): Promise<void> {\n if (!this.client) return;\n try {\n const exists = await this.client.indices.exists({ index });\n if (!exists.body) {\n await this.client.indices.create({\n index,\n body: {\n settings: { number_of_shards: 1, number_of_replicas: 0 },\n mappings: {\n properties: {\n id: { type: 'keyword' },\n nameRU: { type: 'text', analyzer: 'russian' },\n nameEN: { type: 'text', analyzer: 'english' },\n descriptionRU: { type: 'text', analyzer: 'russian' },\n descriptionEN: { type: 'text', analyzer: 'english' },\n createdAt: { type: 'date' },\n updatedAt: { type: 'date' },\n },\n },\n },\n });\n }\n } catch (error) {\n console.error(`Failed to ensure index '${index}': ${(error as Error).message}`);\n }\n }\n\n async indexDocument(index: string, id: string, body: Record<string, unknown>): Promise<void> {\n if (!this.enabled || !this.client) return;\n try {\n await this.client.index({ index, id, body, refresh: 'true' });\n } catch (error) {\n console.error(\n `Failed to index document '${id}' in '${index}': ${(error as Error).message}`,\n );\n }\n }\n\n async removeDocument(index: string, id: string): Promise<void> {\n if (!this.enabled || !this.client) return;\n try {\n await this.client.delete({ index, id, refresh: 'true' });\n } catch (error) {\n console.error(\n `Failed to remove document '${id}' from '${index}': ${(error as Error).message}`,\n );\n }\n }\n\n async search(\n index: string,\n query: string,\n fields: string[],\n page = 1,\n limit = 20,\n ): Promise<{ data: Record<string, unknown>[]; total: number; page: number; limit: number }> {\n if (!this.enabled || !this.client) {\n return { data: [], total: 0, page, limit };\n }\n try {\n const from = (page - 1) * limit;\n const result = await this.client.search({\n index,\n from,\n size: limit,\n body: {\n query: {\n multi_match: {\n query,\n fields,\n type: 'best_fields',\n fuzziness: 'AUTO',\n },\n },\n },\n });\n\n const totalHits =\n typeof result.body.hits.total === 'number'\n ? result.body.hits.total\n : (result.body.hits.total?.value ?? 0);\n const hits = result.body.hits.hits ?? [];\n return {\n data: hits.map((h: { _source?: Record<string, unknown> }) => h._source ?? {}),\n total: totalHits,\n page,\n limit,\n };\n } catch (error) {\n console.error(`Search failed on '${index}': ${(error as Error).message}`);\n return { data: [], total: 0, page, limit };\n }\n }\n}\n"]}