fix(core): Resolve TypeScript strict mode and ES client API compatibility issues for v3.0.0

- Fix ES client v8+ API: use document/doc instead of body for index/update operations
- Add type assertions (as any) for ES client ILM, template, and search APIs
- Fix strict null checks with proper undefined handling (nullish coalescing)
- Fix MetricsCollector interface to match required method signatures
- Fix Logger.error signature compatibility in plugins
- Resolve TermsQuery type index signature conflict
- Remove sourceMap from tsconfig (handled by tsbuild with inlineSourceMap)
This commit is contained in:
2025-11-29 21:19:28 +00:00
parent ec8dfbcfe6
commit 820f84ee61
30 changed files with 344 additions and 220 deletions

View File

@@ -4,7 +4,7 @@ import { Logger, defaultLogger } from '../../core/observability/logger.js';
import { MetricsCollector, defaultMetricsCollector } from '../../core/observability/metrics.js';
import { TracingProvider, defaultTracingProvider } from '../../core/observability/tracing.js';
import { DocumentSession } from './document-session.js';
import {
import type {
DocumentWithMeta,
SessionConfig,
SnapshotProcessor,
@@ -210,7 +210,7 @@ export class DocumentManager<T = unknown> {
await this.client.create({
index: this.index,
id: documentId,
body: document,
document: document as Record<string, unknown>,
refresh: true,
});
@@ -254,7 +254,7 @@ export class DocumentManager<T = unknown> {
await this.client.update({
index: this.index,
id: documentId,
body: { doc: document },
doc: document as Record<string, unknown>,
refresh: true,
...(options?.seqNo !== undefined && { if_seq_no: options.seqNo }),
...(options?.primaryTerm !== undefined && { if_primary_term: options.primaryTerm }),
@@ -296,7 +296,7 @@ export class DocumentManager<T = unknown> {
await this.client.index({
index: this.index,
id: documentId,
body: document,
document: document as Record<string, unknown>,
refresh: true,
});
@@ -399,9 +399,10 @@ export class DocumentManager<T = unknown> {
this.ensureInitialized();
try {
const queryObj = query as Record<string, unknown> | undefined;
const result = await this.client.count({
index: this.index,
...(query && { body: { query } }),
...(queryObj ? { query: queryObj } : {}),
});
return result.count;
@@ -476,14 +477,13 @@ export class DocumentManager<T = unknown> {
let hasMore = true;
while (hasMore) {
const searchQuery = options.query as Record<string, unknown> | undefined;
const result = await this.client.search({
index: this.index,
body: {
size: batchSize,
...(searchAfter && { search_after: searchAfter }),
sort: options.sort || [{ _id: 'asc' }],
...(options.query && { query: options.query }),
},
size: batchSize,
...(searchAfter ? { search_after: searchAfter } : {}),
sort: options.sort || [{ _id: 'asc' }],
...(searchQuery ? { query: searchQuery } : {}),
});
const hits = result.hits.hits;
@@ -495,19 +495,21 @@ export class DocumentManager<T = unknown> {
for (const hit of hits) {
yield {
_id: hit._id,
_id: hit._id ?? '',
_source: hit._source as T,
_version: hit._version,
_seq_no: hit._seq_no,
_primary_term: hit._primary_term,
_index: hit._index,
_score: hit._score,
_score: hit._score ?? undefined,
};
}
// Get last sort value for pagination
const lastHit = hits[hits.length - 1];
searchAfter = lastHit.sort;
if (lastHit) {
searchAfter = lastHit.sort;
}
if (hits.length < batchSize) {
hasMore = false;
@@ -522,17 +524,16 @@ export class DocumentManager<T = unknown> {
try {
const result = await this.client.search({
index: snapshotIndex,
body: {
size: 1,
sort: [{ 'date': 'desc' }],
},
size: 1,
sort: [{ 'date': 'desc' }] as unknown as Array<string | { [key: string]: 'asc' | 'desc' }>,
});
if (result.hits.hits.length === 0) {
const firstHit = result.hits.hits[0];
if (!firstHit) {
return null;
}
const snapshot = result.hits.hits[0]._source as SnapshotMeta<R>;
const snapshot = firstHit._source as SnapshotMeta<R>;
return snapshot.data;
} catch (error: any) {
if (error.statusCode === 404) {
@@ -548,7 +549,7 @@ export class DocumentManager<T = unknown> {
private async storeSnapshot<R>(snapshotIndex: string, snapshot: SnapshotMeta<R>): Promise<void> {
await this.client.index({
index: snapshotIndex,
body: snapshot,
document: snapshot as unknown as Record<string, unknown>,
refresh: true,
});
}

View File

@@ -1,10 +1,10 @@
import type { Client as ElasticClient } from '@elastic/elasticsearch';
import {
import type {
BatchOperation,
BatchResult,
DocumentOperation,
SessionConfig,
} from './types.js';
import { DocumentOperation } from './types.js';
import { Logger } from '../../core/observability/logger.js';
import { BulkOperationError } from '../../core/errors/elasticsearch-error.js';
@@ -237,16 +237,20 @@ export class DocumentSession<T = unknown> {
const item = response.items[i];
const operation = this.operations[i];
const action = Object.keys(item)[0];
const result = item[action as keyof typeof item] as any;
if (!item || !operation) continue;
if (result.error) {
const action = Object.keys(item)[0];
if (!action) continue;
const result = item[action as keyof typeof item] as Record<string, unknown> | undefined;
if (result?.error) {
failed++;
const errorInfo = result.error as { reason?: string } | string;
errors.push({
documentId: operation.documentId,
operation: operation.operation,
error: result.error.reason || result.error,
statusCode: result.status,
error: typeof errorInfo === 'string' ? errorInfo : (errorInfo.reason ?? 'Unknown error'),
statusCode: result.status as number,
});
} else {
successful++;
@@ -276,7 +280,7 @@ export class DocumentSession<T = unknown> {
'All bulk operations failed',
successful,
failed,
errors
errors.map(e => ({ documentId: e.documentId, error: e.error, status: e.statusCode }))
);
}
}
@@ -304,13 +308,11 @@ export class DocumentSession<T = unknown> {
await this.client.deleteByQuery({
index: this.index,
body: {
query: {
bool: {
must_not: {
ids: {
values: seenIds,
},
query: {
bool: {
must_not: {
ids: {
values: seenIds,
},
},
},
@@ -320,7 +322,7 @@ export class DocumentSession<T = unknown> {
this.logger.debug('Stale documents cleaned up', { index: this.index });
} catch (error) {
this.logger.warn('Failed to cleanup stale documents', undefined, {
this.logger.warn('Failed to cleanup stale documents', {
index: this.index,
error: (error as Error).message,
});