#Indexing Different Types to ElasticSearch Based on Single Webhook Endpoint

2 messages · Page 1 of 1 (latest)

worldly wraith
#

Hello,

I'm coming from a Ruby on Rails MVC background to Nest.js and the introduction of Dependency Injection and Services is making it difficult for me to architect a pretty simple solution where a webhook endpoint takes a type and id , then looks up the corresponding document from a CMS API, and then formats and saves the document to an ElasticSearch Index corresponding with the type.

Normally in Rails I would have the Model be responsible for the logic of persisting itself to the datastore, and use static aka class properties to represent concepts like overall indexName and how to indexAll documents. But with Nest.js it seems like I need to move almost everything into like SearchDocumentTypeAService and SeachDocumentTypeBService (and overcome duplication with mixins or something) and tell them about SearchIndexTypeAService and SearchIndexTypeBService. I think I could live with that but I'm not sure how to Inject the proper Service based on the type paramater from my requests?? This all feels way harder than it should be. Does anyone have examples/patterns/suggestions/advice for how to do this in Nest?? Much appreciated 🙏

distant grotto
#

Could you perhaps take in a type to your endpoint, then run it through a switch to call the appropriate service that is responsible for indexing that document type?

// Where type is either a string literal or an enum
switch(type) {
  case 'DocumentTypeA':
     searchIndexTypeAService.reindexAll();
     break;
  case 'DocumentTypeB':
     searchIndexTypeBService.reindexAll();
     break;
  default:
     // return an error for an invalid type
 }