Creating a node module for the SailsJS App - node.js

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

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.

Use normal es6 classes with NestJS and access any service

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.

how to access a different module in multi target application

I'm new to cloud foundry, so I'm not sure, if my thoughts and plans are right. Maybe someone can explain or discuss it with me.
What I want to do:
Implement a MTA (Multitarget Application) with a a html5-module as frontend and a nodeJS-module as backend. Furthermore there should be a mongodb instance, which will be accessed from the nodejs-module. Later it should also get multitenant.
What I already did:
I implemented a simple nodejs-app and connected it to the db. Persisting and calling data with rest works already fine. I implemented a simple sapui5 app, which consumes data from the db with ajax. For now, the node startscript is in the html5 module, so it works somehow. But now I want to separate the modules.
So I created a mta-project with the two modules in webide and imported the two apps.
What I expect to do for it:
For now, I have an approuter, which is in my nodejs-module, but I can not access the webapp folder in the html5-module from here: file not found error: /home/vcap/app//. Is there a possibility to access the webapp-folder in another module over the path "/home/vcap/app/"? Or can I lookup the app-directory anywhere?
I have read, that an approuter-module (nodejs) can be needed, but I don't know exactly what it does. I think it serves the index.html file when opening the url of the whole app?

Node.js shared object or singleton to ensure a single source of truth

I'm writing a Node app that will generate an array of JMS queue subscriptions based on configuration pulled from a backend db. Upon initialization, I will retrieve the current state from the backend, but I then need to support inbound API calls to the Node server to add and remote subscriptions on the fly (and I'll then update the list stored in the backend).
There must only ever be one instance of this subscription array available in the entire application, and it must be the source of truth.
Example:
On init, I'd get from the API:
{
"amysCar": {
"name": "2016 Lamborghini Gallardo LP 550-2",
"queue": "/queues/amysracingstats"
},
"bobsCar": {
"name": "1967 Ford Mustang Shelby Shelby GT500",
"queue": "/queues/bobsrestorationproject"
}
}
Then let's say Bob decides he no longer wants to publish his restoration project; he'll make an API call to the Node server DELETE /projects/bobsCar. I'll remove the queue listener from the queues array and update the backend db.
In practice, I need to be able to access the JMS queues array from multiple controllers because the controller that handles the API calls is separate from the controller that handles queue subscriptions.
I am aware that singletons are notoriously difficult to write and manage in Node, are frowned upon, and are allegedly completely unnecessary because Node.js module caching supposedly only ever loads a single instance of a module. If that's correct, I could simply store the array of queue subscriptions as a private object in a module, and then require it in the API call controller.
Is this true? Can I use this ability to meet my requirement?
If so, how would I overwrite the initialization of the module so that I can make that initial backend call to get the current state?
Am I overthinking this?
Is this true? Can I use this ability to meet my requirement?
Yes, it is true. Just use a module property. Because of module caching, there will only be one such property.
If so, how would I overwrite the initialization of the module so that I can make that initial backend call to get the current state?
I would assume that the module itself would just make the back-end call to initialize the state. It can do that when first loaded or via the first module constructor call.
Am I overthinking this?
You seem to already know about module caching and how module properties can work as singletons so if you're still trying to avoid using that capability, then that would be overthinking it. But, if you're just confirming how things work before relying on it, no problem with that.

What methods are available in a Sails' generated model/controller?

When you create a model/controller using sails generate user, which models are available? For instance, I know there some like basic CRUD, etc, but how to see all available methods?
PS: Unless I got it all wrong and there are no CRUD methods at all. I'm still learning Sails, so please forgive if its a silly question.
Basically, there are two groups of actions provided by Sails.js blueprints for a newly generated model/controller pair:
REST API: get /:controller/:id?, post /:controller, put /:controller/:id, delete /:controller/:id. These are classic REST set that should be the one being used in production. You can enable/disable these blueprints via rest property in config/controllers.js.
CRUD actions aka shortcuts: /:controller/find/:id?, /:controller/create, /:controller/update/:id, /:controller/destroy/:id. Inspired, by Rails' RESTful conventions, the shortcuts provide a way to call all the REST actions from browser address string, using GET HTTP method only, which can be very handy for developers. These can be enabled/disabled using shortcuts property in config/controllers.js, and it's a good idea to disable them in production (for example, using local environment settings (config/local.js)).

Resources