nestjs-algolia-module
Version:
The super fast Algolia NestJS module
127 lines (96 loc) • 2.95 kB
Markdown
# nestjs-algolia-module
The super fast Algolia module for NestJS!
## Install
```bash
yarn add nestjs-algolia-module
```
## Getting started
### Register the module
```typescript
// app.module.ts
import { AlgoliaModule } from 'nestjs-algolia-module';
export class AppModule {}
```
### Optional: Create the config if you choose to register using a config service
```typescript
// algolia.config.ts
import { Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { AlgoliaModuleOptions } from 'nestjs-algolia-module';
export class AlgoliaConfigService {
constructor(private configService: ConfigService) {}
createAlgoliaOptions(): AlgoliaModuleOptions {
return {
applicationId: this.configService.get<string>('algolia.appKey'),
apiKey: this.configService.get<string>('algolia.apiKey'),
// You can also set AlgoliaSearchOptions
// options:
};
}
}
```
### Register the indexes
```typescript
// example.module.ts
import { AlgoliaModule } from 'nestjs-algolia-module';
import { Entity1, Entity2 } from '...';
export class ExampleModule {}
```
Check [Algolia docs](https://www.algolia.com/doc/api-reference/settings-api-parameters/) for a list of all available Index options
### Use the registered Index
The Index is injectable using the ` ` decorator
```typescript
// example.service.ts
import { InjectIndex } from 'nestjs-algolia-module';
import { SearchIndex } from 'algoliasearch';
import { Entity1 } from '...';
export class ExampleService {
constructor(
private algoliaEntity1Index: SearchIndex
// Or you can inject using a string
private algoliaCustomNameIndex: SearchIndex
)
async createEntity(...) {
// ...
this.algoliaEntity1Index.saveObject({
id: ...,
objectID: ...,
// otherParameter: ...
});
// ...
}
}
```
## Support
If you use and like this library, buy me a coffee!
[](https://www.paypal.com/donate/?business=B3F78MJ4PP7XC&no_recurring=0¤cy_code=USD)
Code contributions are encouraged and welcomed!