Giter Site home page Giter Site logo

fvilers / nestjs-algolia Goto Github PK

View Code? Open in Web Editor NEW
15.0 4.0 8.0 353 KB

The algolia NestJS module based on the official algolia package

License: MIT License

JavaScript 1.86% TypeScript 98.14%
nestjs nest nodejs typescript algolia algolia-search

nestjs-algolia's Introduction

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

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<algoliasearch.Task> {
    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

nestjs-algolia's People

Contributors

dependabot[bot] avatar fvilers avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

nestjs-algolia's Issues

supporting root registration

Hi
Do you have any plan to add forRootmodule registration method?
By register we need to re-register individually for each module.

Nest can't resolve dependencies of the AlgoliaService (?). Please make sure that the argument at index [0] is available in the context.

Error message

2019-09-24T18:02:00.115Z [ERROR] [ExceptionHandler]     Nest can't resolve dependencies of the AlgoliaService (?). Please make sure that the argument at index [0] is available in the SearchIndexModule context.Error: Nest can't resolve dependencies of the AlgoliaService (?). Please make sure that the argument at index [0] is available in the SearchIndexModule context.
    at Injector.lookupComponentInExports (/Users/paulsalmon/workspace/plato/plato-monorepo-research/node_modules/@nestjs/core/injector/injector.js:183:19)
    at process._tickCallback (internal/process/next_tick.js:68:7)
    at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
    at Object.<anonymous> (/Users/paulsalmon/workspace/plato/plato-monorepo-research/node_modules/ts-node/src/bin.ts:158:12)
    at Module._compile (internal/modules/cjs/loader.js:701:30)
    at Object.Module._extensions..js (internal/modules/cjs/loader.js:712:10)
    at Module.load (internal/modules/cjs/loader.js:600:32)
    at tryModuleLoad (internal/modules/cjs/loader.js:539:12)
    at Function.Module._load (internal/modules/cjs/loader.js:531:3)
    at Function.Module.runMain (internal/modules/cjs/loader.js:754:12)
error Command failed with exit code 1.
// search-index.module.ts
import { Module } from '@nestjs/common';
import { AlgoliaModule, AlgoliaService } from 'nestjs-algolia';
import { SearchIndexService } from './search-index.service';

@Module({
  imports: [
    AlgoliaModule.register({
      applicationId: 'APPLICATION_ID',
      apiKey: 'API_KEY',
    }),
  ],
  providers: [AlgoliaService, SearchIndexService],
})
export class SearchIndexModule {}
// search-index.service.ts
import { Injectable } from '@nestjs/common';
import { AlgoliaService } from 'nestjs-algolia';

@Injectable()
export class SearchIndexService {
  constructor(private readonly algoliaService: AlgoliaService) {}
}

Versions:
@nestjs/common - 6.7.1
@nestjs/core - 6.7.1
nestjs-algolia - 1.1.0

Hello! Thank you for the project. So I am having this error that I would guess is related to the last update of Nest. Any idea?

Nest can't resolve dependencies of the AlgoliaService (?). Please make sure that the argument ALGOLIA_CLIENT at index [0] is available in the AlgoliaService context.

HI I have an issue with Algolia:

import { AlgoliaModule, AlgoliaService } from 'nestjs-algolia';
import { Module } from '@nestjs/common';
import { algoliaAppId, algoliaWriteApiKey } from '../../utils/utils';
import { Algolia } from '@business/algolia/algolia.service';

@Module({
  imports: [
    AlgoliaModule.register({
      applicationId: algoliaAppId,
      apiKey: algoliaWriteApiKey,
    }),
    AlgoliaService,
  ],
  providers: [Algolia, AlgoliaService],
  exports: [Algolia, AlgoliaService],
})
export class AlgoModule {}
import { AlgoliaService } from 'nestjs-algolia';
import { Injectable } from '@nestjs/common';

@Injectable()
export class Algolia {
  constructor(private readonly algoliaService: AlgoliaService) {}

  addRecordToIndex(indexName: string, record: any): Promise<any> {
    const index = this.algoliaService.initIndex(indexName);
    return index.addObject(record);
  }
}
Error: Nest can't resolve dependencies of the AlgoliaService (?). Please make sure that the argument ALGOLIA_CLIENT at index [0] is available in the AlgoliaService context.

Potential solutions:
- If ALGOLIA_CLIENT is a provider, is it part of the current AlgoliaService?
- If ALGOLIA_CLIENT is exported from a separate @Module, is that module imported within AlgoliaService?
  @Module({
    imports: [ /* the Module containing ALGOLIA_CLIENT */ ]
  })

Are all algolia node methods supported?

I am running nest v4, and using the v4 of this library, but am unsure if this uses the v3 or v4 version of algolia's node client.

I just tried using the findObject method and it is saying that the method is not available on the index object. Other methods are working fine for me.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.