Algolia search module for NestJS, using the official algoliasearch package
npm install nestjs-algoliasearch algoliasearch
or
yarn add nestjs-algoliasearch algoliasearch
// app.module.ts
import { AlgoliaModule } from 'nestjs-algoliasearch';
@Module({
imports: [
//...
AlgoliaModule.forRoot({
applicationId: 'ALGOLIA_APPLICATION_ID',
apiKey: 'ALGOLIA_API_KEY',
}),
// Or async:
AlgoliaModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => config.get('algolia'),
}),
//...
],
})
export class AppModule {}
// todo.module.ts
import { AlgoliaModule } from 'nestjs-algoliasearch';
@Module({
imports: [
TypeOrmModule.forFeature([
TodoEntity,
]),
AlgoliaModule.forFeature([{
name: TodoEntity, // Or a string, this will be the index name
options: {/* custom Algolia client index settings */},
}])
],
})
export class TodoModule {}
See the official Algolia client documentatin for a list of all available Index options
The Index is injectable using the @InjectIndex
decorator, the returned value is an algoliasearch.SearchIndex
.
See the official Algolia client documentation for a list of available methods
// todo.service.ts
import { InjectIndex } from 'nestjs-algoliasearch';
import { SearchIndex } from 'algoliasearch';
@Injectable()
export class TodoService {
constructor(
@InjectIndex(TodoEntity /* or the string used as "name" prop while registering the index */)
private readonly todoIndex: SearchIndex,
)
}