Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,30 @@ import { ElasticsearchModule } from '@codemask-labs/nestjs-elasticsearch'
class AppModule {}
```

The `register()` method supports all the configuration properties available in `ClientOptions` from the [@elastic/elasticsearch](https://www.npmjs.com/package/@elastic/elasticsearch) package.
or

```typescript
import { ElasticsearchModule } from '@codemask-labs/nestjs-elasticsearch'

@Module({
imports: [
ElasticsearchModule.registerAsync({
imports: [ConfigModule],
useFactory: (configService: ConfigService) => ({
node: configService.get('ELASTICSEARCH_NODE'),
auth: {
username: configService.get('ELASTICSEARCH_USERNAME'),
password: configService.get('ELASTICSEARCH_PASSWORD'),
},
}),
inject: [ConfigService],
})
]
})
class AppModule {}
```

The `register()` and `registerAsync()` methods support all the configuration properties available in `ClientOptions` from the [@elastic/elasticsearch](https://www.npmjs.com/package/@elastic/elasticsearch) package.

### Registering the index

Expand Down
11 changes: 11 additions & 0 deletions src/module/elasticsearch.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { ClassConstructor } from 'lib/common'
import { Index } from './injectables'
import { ElasticsearchService } from './elasticsearch.service'
import { getIndexInjectionToken } from './utils'
import { AsyncModuleOptions } from './types'

@Module({})
export class ElasticsearchModule {
Expand All @@ -18,6 +19,16 @@ export class ElasticsearchModule {
}
}

static registerAsync(options: AsyncModuleOptions): DynamicModule {
return {
global: options.global ?? true,
module: ElasticsearchModule,
imports: [BaseElasticsearchModule.registerAsync(options)],
providers: [ElasticsearchService],
exports: [ElasticsearchService],
}
}

// eslint-disable-next-line @typescript-eslint/no-explicit-any
static forFeature(documents: Array<ClassConstructor<any>>): DynamicModule {
const providers: Array<Provider> = documents.map(document => ({
Expand Down
11 changes: 11 additions & 0 deletions src/module/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { DynamicModule, InjectionToken, Type } from '@nestjs/common'
import { ClientOptions } from '@elastic/elasticsearch'

export type AsyncModuleOptions = {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
imports?: Array<Type<any> | DynamicModule | Promise<DynamicModule>>
global?: boolean
inject?: Array<InjectionToken>
// eslint-disable-next-line @typescript-eslint/no-explicit-any
useFactory: (...args: Array<any>) => Promise<ClientOptions> | ClientOptions
}
6 changes: 4 additions & 2 deletions src/test/features/1-making-a-search.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,10 @@ import { getQueries } from 'lib/utils'
describe('Making a search', () => {
const { app } = setupNestApplication({
imports: [
ElasticsearchModule.register({
node: TEST_ELASTICSEARCH_NODE,
ElasticsearchModule.registerAsync({
useFactory: () => ({
node: TEST_ELASTICSEARCH_NODE,
}),
}),
],
})
Expand Down