Skip to content

Davide-Gheri/nestjs-algoliasearch

Repository files navigation

nestjs-algoliasearch

Algolia search module for NestJS, using the official algoliasearch package

Install

npm install nestjs-algoliasearch algoliasearch

or

yarn add nestjs-algoliasearch algoliasearch

Getting started

Register the module

// 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 {}

Register the indexes

// 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

Using the registered Index

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,
  )
}