nestjs-opensearch
Advanced tools
Comparing version 0.3.2 to 0.4.0
@@ -1,2 +0,2 @@ | ||
import type { ModuleMetadata } from '@nestjs/common'; | ||
import type { ModuleMetadata, Type } from '@nestjs/common'; | ||
import type { ClientOptions } from '@opensearch-project/opensearch'; | ||
@@ -6,6 +6,14 @@ export interface OpensearchClientOptions extends ClientOptions { | ||
} | ||
export interface OpensearchAsyncClientOptions extends Pick<ModuleMetadata, 'imports'> { | ||
clientName?: string | symbol; | ||
declare type OpensearchAsyncClientOptionsBase = OpensearchClientOptions & Pick<ModuleMetadata, 'imports'>; | ||
interface OpensearchAsyncClientOptionsUseFactory extends OpensearchAsyncClientOptionsBase { | ||
inject?: any[]; | ||
useFactory: (...args: any[]) => ClientOptions | Promise<ClientOptions>; | ||
inject?: any[]; | ||
} | ||
export interface OpensearchClientOptionsFactory { | ||
createOpensearchClientOptions: () => ClientOptions | Promise<ClientOptions>; | ||
} | ||
interface OpensearchAsyncClientOptionsUseClass extends OpensearchAsyncClientOptionsBase { | ||
useClass: Type<OpensearchClientOptionsFactory>; | ||
} | ||
export declare type OpensearchAsyncClientOptions = OpensearchAsyncClientOptionsUseFactory | OpensearchAsyncClientOptionsUseClass; | ||
export {}; |
@@ -34,8 +34,8 @@ "use strict"; | ||
static forRootAsync(options) { | ||
const providers = OpensearchModule_1.buildAsyncProviders(options); | ||
const { internalProviders, externalProviders } = OpensearchModule_1.buildAsyncProviders(options); | ||
return { | ||
module: OpensearchModule_1, | ||
imports: Array.isArray(options) ? undefined : options.imports, | ||
exports: providers, | ||
providers, | ||
exports: externalProviders, | ||
providers: internalProviders.concat(externalProviders), | ||
}; | ||
@@ -61,12 +61,34 @@ } | ||
} | ||
return options.map((option) => ({ | ||
provide: option.clientName ? (0, helpers_1.buildInjectionToken)(option.clientName) : opensearch_client_1.OpensearchClient, | ||
inject: [symbols_1.clientMapSym, ...(option.inject || [])], | ||
useFactory: async (clientMap, ...args) => { | ||
const clientOptions = await option.useFactory(...args); | ||
const client = new opensearch_client_1.OpensearchClient(Object.assign(Object.assign({}, clientOptions), { clientName: option.clientName })); | ||
clientMap.set(option.clientName, client); | ||
return client; | ||
}, | ||
})); | ||
const internalProviders = []; | ||
const externalProviders = []; | ||
options.forEach((option) => { | ||
const inject = [symbols_1.clientMapSym]; | ||
const isUseClass = 'useClass' in option; | ||
if (isUseClass) { | ||
internalProviders.push({ | ||
provide: option.useClass, | ||
useClass: option.useClass, | ||
}); | ||
inject.push(option.useClass); | ||
} | ||
else if (Array.isArray(option.inject)) { | ||
inject.push(...option.inject); | ||
} | ||
externalProviders.push({ | ||
provide: option.clientName ? (0, helpers_1.buildInjectionToken)(option.clientName) : opensearch_client_1.OpensearchClient, | ||
inject, | ||
useFactory: async (clientMap, ...args) => { | ||
const clientOptions = await (isUseClass | ||
? args[0].createOpensearchClientOptions() | ||
: option.useFactory(...args)); | ||
const client = new opensearch_client_1.OpensearchClient(Object.assign(Object.assign({}, clientOptions), { clientName: option.clientName })); | ||
clientMap.set(option.clientName, client); | ||
return client; | ||
}, | ||
}); | ||
}); | ||
return { | ||
internalProviders, | ||
externalProviders, | ||
}; | ||
} | ||
@@ -73,0 +95,0 @@ async onApplicationShutdown() { |
{ | ||
"name": "nestjs-opensearch", | ||
"version": "0.3.2", | ||
"version": "0.4.0", | ||
"author": "neoatlan", | ||
@@ -15,3 +15,3 @@ "license": "MIT", | ||
"clean": "rimraf dist", | ||
"test": "yarn build && yarn workspaces foreach -v --include 'test-*' run test" | ||
"test": "yarn build && node scripts/sync-tests && yarn workspaces foreach -v --include 'test-*' run test" | ||
}, | ||
@@ -18,0 +18,0 @@ "keywords": [ |
@@ -30,3 +30,3 @@ <p align="center"> | ||
## Usage | ||
## Module configuration | ||
Module for single connection: | ||
@@ -67,3 +67,3 @@ ```typescript | ||
Module for async configuration: | ||
Module for async configuration using useFactory: | ||
```typescript | ||
@@ -74,6 +74,6 @@ import { OpensearchModule } from 'nestjs-opensearch'; | ||
imports: [ | ||
// See also: https://docs.nestjs.com/techniques/configuration | ||
ConfigModule, | ||
OpensearchModule.forRootAsync({ | ||
clientName: 'baz', | ||
// See also: https://docs.nestjs.com/techniques/configuration | ||
imports: [ ConfigModule ], | ||
inject: [ ConfigService ], | ||
@@ -90,4 +90,31 @@ useFactory: (configService) => ({ | ||
Client injection: | ||
Module for async configuration using useClass: | ||
```typescript | ||
import type { ClientOptions } from '@opensearch-project/opensearch'; | ||
import { OpensearchModule, OpensearchClientOptionsFactory } from 'nestjs-opensearch'; | ||
@Injectable() | ||
export class OpensearchConfigService implements OpensearchClientOptionsFactory { | ||
public async createOpensearchClientOptions(): Promise<ClientOptions> { | ||
const configs = await fetch(...); | ||
return { | ||
node: configs.node, | ||
}; | ||
} | ||
} | ||
@Module({ | ||
imports: [ | ||
OpensearchModule.forRootAsync({ | ||
clientName: 'qux', | ||
useClass: OpensearchConfigService, | ||
}), | ||
], | ||
providers: (...), | ||
}) | ||
export class SearchModule { } | ||
``` | ||
## Client usage | ||
```typescript | ||
import { InjectOpensearchClient, OpensearchClient } from 'nestjs-opensearch'; | ||
@@ -94,0 +121,0 @@ |
18524
281
134