Use normal es6 classes with NestJS and access any service - nestjs

When I have a nestjs app, everything is seperated into modules, controllers and services. But what I am actually looking for, is a way to instantiate as many classes of a certain type which then have access to a modules service for example.
I want to "spawn" classes on the go to act like a worker, executing tasks on repeat. Is this even possible with NestJS or cant I use normal classes to interact with NestJS services?
For example ModuleA has ServiceA,
serviceA created a bunch of objects of SampleClass which have access to ServiceA methods.

Related

Are Entities between two sub-apps in NestJs sharable?

I am very new to NestJS and I am trying to create a project with multiple subapps. The query that I have is, if one entity is present in one sub-app, can we use that same entity in another sub-app of the same project?
This is my project strucutre:
apps
subApp1
entities
- entityone.ts
controller, modules and services
subApp2
entities
- entityTWo.ts
controller, modules and services
I want to use the entity one in subApp2 service. Is it possible? If so, how we can acheive this? Is there any better way to approach this situation where I need to use Entity of one sub app into another.
I tried exporting the Entity from one sub-app and import the Module in another sub app. But I am getting this error:
Nest cannot export a provider/module that is not a part of the currently processed module (StoreModule). Please verify whether the exported Store is available in this particular context.
I am not sure, if it was the correct way for this situation, but this solution came into my mind and i tried it.
you don't need to specify entities for every module.
keep them all together apart, so you can use them wherever you want.

Nestjs TypeOrm - Entities importing 3rd party modules and async functions

I want to build an app where I have an Entity of Type A. This Entity class should have an update method which then uses 3rd party modules for example to fetch data from a 3rd party api and so on.
I am kind of lost on how to do that, if its even possible to. Basically I want the Entities to be able to perform function calls just like normal es6 classes.
Until now I always used a service that updates all entities, I prefer all Entities to handle their own updates tho.
Basically can the enities I define which also get stored to database have async functions I could then just call on the object after retrieving it from the database? Is there a constructor call once these entities get fetched via find() methods of the repositories?
I pretty much want to avoid having an Entity of type A and also a class implementation for type A just to perform stuff and use 3rd party modules. Until now I used to create the Model, fetch the Models from the database and than instantiate Objects (Classes) that represent these models which is kind of verbose to me.

Reflective injector that allows to inject dependencies outside of constructor parameters in nestjs

I need to get access to a service inside my model class. That service has a bunch of injection of its own, so it would be great if I could get an instance of that service inside that class model.
Angular, for example, has a reflective injector that allows to do that, I remember seeing somewhere an example of instantiating a service with the help of the nestjs reflector but I couldn't find that example, it was pretty long ago.
I don't know if I had to add any code to the question. I just have a model class and I need to use a service there. I need to inject it not through the class constructor, that's it, I believe.
How do I do that?
EDIT:
Seems like nest has a reflector but I also have to inject it...my goodness...it doesn't have a reflective injector akin to that in Angular.
In Angluar you don't have to inject the reflector itself...apparently it's not always suitable to do that...

Creating a node module for the SailsJS App

I have a sails project which has many functionalities .
ex: Address creation, event management, notification, payment and many
others.
Now I want to make different services for each one of these. As far as my knowledge, I have two ways to do so:
Create a micro service for every service. (Which is not possible to maintain for now)
or
Create respective modules.
So, I'm able to create the module for Notifications and Payment.
But able to understand how can I create a module for address which can have:
Module (want to bind with the sails module)
Controllers
Routes
I just need a plug and play module for the functionalities which can access the database same way as sails module does.
Few more questions to add on further :
Is there any other better method by which this can be achieved ?
Can I bind or control the sails controller and the module before sails lift by the node_module?
Maybe you should consider making a hook instead
http://sailsjs.org/documentation/concepts/extending-sails/hooks

Separating models, logic and DAOs in express/node.js

What's the best way to separate the different tiers of an express application, so that my app.js file doesn't get crammed full of functions? I'm coming from a Java world, so I typically have my models, business logic and DAO code in separate tiers.
The other question, that has been bothering me: how do I open a connection to a DB in app.js and then share that among the various pieces of code that need access to it? Not the routed functions, but the business logic modules.
See this project as separate files as follows:
https://github.com/lethus/popbroker
routes.js - Here we put the routes, usually referenced to controllers
models.js - Model here you put the functions of MongoDB
forms.js - You work the validation of objects
controllers / users.js - That would be something like java UI, here we call the models.js and do the insert, update, list, Finds

Resources