Appolo Context
nodejs async hooks context storage can be used to create context per request
Attention! This package is using the new experimental async hooks API.
Installation:
npm install appolo-context --save
Usage
; let context = namespace;context; { let context = namespace context return Promise; } { let context = namespace let id = context; return Promise; } contextscopeasync await ; let id = await ; console // someId
Express Usage
;; context; let app = ;app app
Now you can retrieve the context in any file or function.
; { return context;}
Namespace
create(name:string|Symbol):Context
Create new context by given name
; let context = namespace;context;
get(name:string|Symbol):Context
get context by given name
; let context = namespace;context
delete(name:string|Symbol)
delete context by given name and destroy it
; let context = namespace;
get context():Context
get the default context
; let context = namespacecontext; context
Context
initialize()
initialize the async hooks must be called before any get or set methods this will create the async hooks and enable them.
Context
scope(fn:Function)
create new context scope and triggers the given function
;; context; let app = ;app
get(key:any):any
get context value by key
set(key:any,value:any)
set context value by key
enable()
enable the async hooks
destroy()
destroy the context and clean all resources.
License
MIT