#Where to store run-time data (state)? Are services stateless?

13 messages · Page 1 of 1 (latest)

summer cave
#

I wonder what is the normal way to store data (that are not intended for the DB) so that other parties (services, controllers an modules) could use them?

And one more question. Is my understanding correct that service is a stateless thing? I personally usually think of them this way. Then service classes is not a good place to keep data, right? But what is? The module?

trim flax
#

Can I ask what kind of data you intent to store in run-time? Just out of curiosity, I don't have an answer but I would like to know the motivation for doing this.

clear gale
#

You can pass data around your services using events or by calling them directly, if you need some external state storage then look into redis. But I am suspicious on why you'd need it just for service communication!

summer cave
summer cave
#

What about sevices part of my question? Are your services statefull or stateless?

#

Redis sounds like an overkill to me

green marsh
#

I mean, if you just need it in memory I would make some sort of MEMORY_STORAGE provider that's essentially just an object that you can inject to any provider you need

summer cave
green marsh
# summer cave Under provider you mean just a service then, right?

Doesn't even need to be a full class (depending on your needs of course) it could be as simple as a plain object

{
  provide: 'MEMORY_STORAGE',
  useValue: {},
}

Then inject it with @Inject('MEMORY_STORAGE') private readonly storage: Record<string, unknown> and boom

summer cave
green marsh
#

Check out the page on custom providers

summer cave
#

Thanks @green marsh