@remembered/decorator
v0.1.2
Published
This project is just a template for creation of new projects
Downloads
15
Readme
Decorator for easy remembered appliance.
How to Install
npm i @remembered/decorator remembered
How to use it
Apply the decorator UseRemembered to every method you want:
class MyClass {
@UseRemembered((a: SomeType) => a.id)
async myMethod(a: SomeType) {
return processResult(a);
}
}
Notice that the decorator needs a callback to define how the remembering key will be generated. this callback will be called with the exactly same parameters myMethod is called. The decorator itself will not make any difference but, in some initialization part of your code, after all your classes are already loaded (which is trivial if you're using the default class declaration and import pattern), just call setupRemembered:
setupRemembered((class) => injector.get(Remembered));
Look that setupRemembered receives a callback that must return a Remembered instance. This callback receives a reference to the Class object where Remembered will be applied. This way, you have control on how you want to instantiate it, or if you want to use some extension like @remembered/redis. There is also a helper function that guarantees to you that only one instance per decorated class will be returned. To use it, you can do like this:
setupRemembered(getRememberedByClassFactory((class) => injector.get(Remembered)));
License
Licensed under MIT.