node-rls
- Request Local Storage for nodejs
node-rls
Is a library for attaching metadata to a nodejs call stack.
It provides high-level key-value store style functions that are concurrency safe.
This can be extremely useful for logging where you want to attach metadata like request ids which you want to implicitly propagate to your logging function.
To accomplish this node-rls
uses nodejs async hooks.
For more information see node-continuation-local-storage and cls-hooked.
Installation
$ npm install node-rls
Usage
const RLS ={await RLSreturn RLS}RLS
For more examples see the tests
Using as an express middleware
In this example we attach a request id to incoming requests
const RLSMiddleware = const RLS = const Express = const UUID = const app = // Initialize the contextapp // Attach the requestid to the requestapp // Create an endpoint returning the request idapp
API Reference
- node-rls
- .NotInitializedError :
Error
- .run(callback) ⇒
Object
- .get(key) ⇒
Object
- .copy() ⇒
Object
- .set(key, value) ⇒
undefined
- .delete(key) ⇒
undefined
- .update(obj) ⇒
undefined
- .incr(key, count) ⇒
Number
- .decr(key, count) ⇒
Number
- .NotInitializedError :
Error
node-rls.NotInitializedError : Kind: static class of node-rls
new NotInitializedError(message)
Thrown when context has not yet been initialized
Param | Type | Description |
---|---|---|
message | string |
Error message |
Object
node-rls.run(callback) ⇒ Create a new context and run callback.
Kind: static method of node-rls
Returns: Object
- Result from callback
Param | Type | Description |
---|---|---|
callback | function |
Async callback |
Example
// Any called function will have access to conext// even async ones { return RLS} RLS
Object
node-rls.get(key) ⇒ Get object from storage.
Kind: static method of node-rls
Returns: Object
- Stored object
Throws:
NotInitializedError
Param | Type | Description |
---|---|---|
key | String |
Storage key |
Object
node-rls.copy() ⇒ Get KV store as a javascript object Mutating this object will NOT mutate the KV store
Kind: static method of node-rls
Returns: Object
- Shallow copy of KV store
Throws:
NotInitializedError
undefined
node-rls.set(key, value) ⇒ Set storage object
Kind: static method of node-rls
Returns: undefined
- Returns nothing
Throws:
NotInitializedError
Param | Type | Description |
---|---|---|
key | String |
Storage key |
value | Object |
Storage key |
undefined
node-rls.delete(key) ⇒ Delete storage object
Kind: static method of node-rls
Returns: undefined
- Returns nothing
Throws:
NotInitializedError
Param | Type | Description |
---|---|---|
key | String |
Storage key |
undefined
node-rls.update(obj) ⇒ Update storage from a map
Kind: static method of node-rls
Returns: undefined
- No return value
Throws:
NotInitializedError
Param | Type | Description |
---|---|---|
obj | Object |
KV mapping object |
Example
// Update storage with all values from objectconst obj = foo: 'bar' someKey: 'someValue'await console // Prints bar
Number
node-rls.incr(key, count) ⇒ Atomically increment a counter
Kind: static method of node-rls
Returns: Number
- Counter after increment
Throws:
NotInitializedError
Param | Type | Description |
---|---|---|
key | Object |
Storage key |
count | Object |
Increment by count |
Number
node-rls.decr(key, count) ⇒ Atomically decrement a counter
Kind: static method of node-rls
Returns: Number
- Counter after decrement
Throws:
NotInitializedError
Param | Type | Description |
---|---|---|
key | Object |
Storage key |
count | Object |
Decrement by count |