{"library":"nestjs-opensearch","title":"NestJS OpenSearch Module","description":"The `nestjs-opensearch` package provides a dedicated module for integrating OpenSearch with the NestJS framework. It simplifies connecting to OpenSearch clusters, offering configurations for single, multiple, and asynchronously provisioned clients. The current stable version is 1.4.1, which includes support for NestJS v11 and OpenSearch client v3. Releases tend to align with major NestJS and `@opensearch-project/opensearch` peer dependency updates to maintain compatibility. Its key differentiators include leveraging NestJS's declarative module structure, providing robust dependency injection for OpenSearch clients (both default and named), and supporting both synchronous and asynchronous configuration patterns, including `useFactory` and `useClass` options. This design allows for flexible integration within various NestJS application architectures, adhering to the framework's modularity and inversion of control principles.","language":"javascript","status":"active","last_verified":"Sun Apr 19","install":{"commands":["npm install nestjs-opensearch"],"cli":null},"imports":["import { OpensearchModule } from 'nestjs-opensearch';","import { InjectOpensearchClient } from 'nestjs-opensearch';","import { OpensearchClient } from 'nestjs-opensearch';","import { OpensearchClientOptionsFactory } from 'nestjs-opensearch';"],"auth":{"required":false,"env_vars":[]},"quickstart":{"code":"import { Module, Injectable } from '@nestjs/common';\nimport { ConfigModule, ConfigService } from '@nestjs/config';\nimport { OpensearchModule, InjectOpensearchClient, OpensearchClient } from 'nestjs-opensearch';\n\n// A basic configuration factory for NestJS ConfigModule\nfunction configuration() {\n  return {\n    opensearch: {\n      node: process.env.OPENSEARCH_NODE || 'http://localhost:9200',\n      auth: {\n        username: process.env.OPENSEARCH_USERNAME || 'admin',\n        password: process.env.OPENSEARCH_PASSWORD || 'admin'\n      }\n    }\n  };\n}\n\n@Injectable()\nexport class SearchService {\n  constructor(\n    // Inject the default client (no clientName specified)\n    @InjectOpensearchClient() private readonly defaultClient: OpensearchClient,\n    // Inject a named client\n    @InjectOpensearchClient('myNamedClient') private readonly namedClient: OpensearchClient,\n  ) {}\n\n  async indexDocument(index: string, id: string, document: any): Promise<void> {\n    await this.defaultClient.index({\n      index,\n      id,\n      body: document,\n      refresh: true,\n    });\n    console.log(`Document '${id}' indexed successfully using the default client.`);\n  }\n\n  async searchDocuments(index: string, query: any): Promise<any[]> {\n    const { body } = await this.namedClient.search({\n      index,\n      body: { query },\n    });\n    console.log(`Search performed on '${index}' using 'myNamedClient'.`);\n    return body.hits.hits;\n  }\n}\n\n@Module({\n  imports: [\n    // Load configuration for the application\n    ConfigModule.forRoot({\n      load: [configuration],\n      isGlobal: true,\n    }),\n    // Configure the default OpenSearch client asynchronously\n    OpensearchModule.forRootAsync({\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory: (configService: ConfigService) => ({\n        node: configService.get<string>('opensearch.node'),\n        auth: configService.get<{ username: string; password: string }>('opensearch.auth'),\n      }),\n    }),\n    // Configure a named OpenSearch client asynchronously\n    OpensearchModule.forRootAsync({\n      clientName: 'myNamedClient',\n      imports: [ConfigModule],\n      inject: [ConfigService],\n      useFactory: (configService: ConfigService) => ({\n        node: configService.get<string>('opensearch.node'),\n        auth: configService.get<{ username: string; password: string }>('opensearch.auth'),\n      }),\n    }),\n  ],\n  providers: [SearchService],\n  exports: [SearchService],\n})\nexport class AppModule {}\n\n// To bootstrap and run this application:\n// import { NestFactory } from '@nestjs/core';\n// async function bootstrap() {\n//   const app = await NestFactory.create(AppModule);\n//   await app.listen(3000);\n//   const searchService = app.get(SearchService);\n//   await searchService.indexDocument('my-test-index', 'doc1', { title: 'Hello World' });\n//   const results = await searchService.searchDocuments('my-test-index', { match_all: {} });\n//   console.log('Search results:', results);\n// }\n// bootstrap();","lang":"typescript","description":"This quickstart demonstrates how to configure default and named OpenSearch clients asynchronously using `ConfigModule` and `useFactory`. It then shows how to inject these clients into a service (`SearchService`) to perform basic indexing and searching operations, illustrating a typical NestJS integration pattern.","tag":null,"tag_description":null,"last_tested":null,"results":[]},"compatibility":null}