Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/fvilers/nestjs-algolia
The algolia NestJS module based on the official algolia package
https://github.com/fvilers/nestjs-algolia
algolia algolia-search nest nestjs nodejs typescript
Last synced: 6 days ago
JSON representation
The algolia NestJS module based on the official algolia package
- Host: GitHub
- URL: https://github.com/fvilers/nestjs-algolia
- Owner: fvilers
- License: mit
- Created: 2019-02-25T08:04:09.000Z (over 5 years ago)
- Default Branch: master
- Last Pushed: 2024-06-18T06:59:05.000Z (5 months ago)
- Last Synced: 2024-10-31T18:51:38.547Z (13 days ago)
- Topics: algolia, algolia-search, nest, nestjs, nodejs, typescript
- Language: TypeScript
- Homepage:
- Size: 471 KB
- Stars: 17
- Watchers: 4
- Forks: 9
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# nestjs-algolia
The algolia NestJS module based on the official algolia package
## Support
If you use and like this library, feel free to support my Open Source projects.
[![donate](https://www.paypalobjects.com/en_US/BE/i/btn/btn_donateCC_LG.gif)](https://www.paypal.com/cgi-bin/webscr?cmd=_donations&business=JZ26X897M9V9L¤cy_code=EUR)
## How to install
```
npm install nestjs-algolia
npm install --save-dev @types/algoliasearch
```or
```
yarn add nestjs-algolia
yarn add -D @types/algoliasearch
```## How to use
**Register the module**
```
import { AlgoliaModule } from 'nestjs-algolia';@Module({
imports: [
AlgoliaModule.register({
applicationId: 'YOUR_APPLICATION_ID',
apiKey: 'YOUR_API_KEY',
}),
],
})
export class AppModule {}
```**Inject the service**
```
import { AlgoliaService } from 'nestjs-algolia';@Injectable()
export class AppService {
constructor(private readonly algoliaService: AlgoliaService) {}addRecordToIndex(
indexName: string,
record: any,
): Promise {
const index = this.algoliaService.initIndex(indexName);return index.addObject(record);
}
}
```## Async options
Quite often you might want to asynchronously pass your module options instead of passing them beforehand. In such case, use `registerAsync()` method, that provides a couple of various ways to deal with async data.
### Use factory
```
AlgoliaModule.registerAsync({
useFactory: () => ({
applicationId: 'YOUR_APPLICATION_ID',
apiKey: 'YOUR_API_KEY',
}),
});
```Obviously, our factory behaves like every other one (might be `async` and is able to inject dependencies through `inject`).
```
AlgoliaModule.registerAsync({
imports: [ConfigModule],
useFactory: async (configService: ConfigService) => ({
applicationId: configService.getString('ALGOLIA_APPLICATION_ID'),
apiKey: configService.getString('ALGOLIA_API_KEY'),
}),
inject: [ConfigService],
}),
```### Use class
```
AlgoliaModule.registerAsync({
useClass: AlgoliaConfigService,
});
```Above construction will instantiate `AlgoliaConfigService` inside `AlgoliaModule` and will leverage it to create options object.
```
class AlgoliaConfigService implements AlgoliaOptionsFactory {
createAlgoliaOptions(): AlgoliaModuleOptions {
return {
applicationId: 'YOUR_APPLICATION_ID',
apiKey: 'YOUR_API_KEY',
};
}
}
```### Use existing
```
AlgoliaModule.registerAsync({
imports: [ConfigModule],
useExisting: ConfigService,
}),
```It works the same as `useClass` with one critical difference - `AlgoliaModule` will lookup imported modules to reuse already created `ConfigService`, instead of instantiating it on its own.
## Versions
Use the following table to match this module with the NestJS version
| nestjs-algolia | nestjs |
| -------------- | ------ |
| 1.x | 5.x |
| 2.x | 6.x |
| 3.x | 7.x |
| 4.x | 8.x |