Skip to content

A continuation-local storage (async context) module compatible with NestJS's dependency injection.

License

Notifications You must be signed in to change notification settings

Papooch/nestjs-cls

Folders and files

NameName
Last commit message
Last commit date

Latest commit

author
github-action[bot]
Mar 19, 2025
79e7068 · Mar 19, 2025
Feb 8, 2025
Apr 22, 2024
Jan 21, 2025
Feb 28, 2025
Mar 19, 2025
Feb 18, 2023
Aug 14, 2023
Jan 22, 2024
Oct 23, 2024
Mar 23, 2023
Jan 22, 2024
Jan 21, 2025
Jan 29, 2024
Aug 11, 2023
Feb 16, 2025
Jan 21, 2025
Aug 29, 2023
Feb 8, 2025
Sep 12, 2021
Jan 21, 2025
Mar 19, 2025

Repository files navigation

NestJS CLS (Async Context)

A continuation-local storage module compatible with NestJS' dependency injection based on AsyncLocalStorage.

Notice: The documentation has been moved to a dedicated website.

Continuation-local storage allows to store state and propagate it throughout callbacks and promise chains. It allows storing data throughout the lifetime of a web request or any other asynchronous duration. It is similar to thread-local storage in other languages.

Some common use cases that this library enables include:

  • Tracking the Request ID and other metadata for logging purposes
  • Keeping track of the user throughout the whole request
  • Making the dynamic Tenant database connection available everywhere in multi-tenant apps
  • Propagating the authentication level or role to restrict access to resources
  • Seamlessly propagating database transaction across services without breaking encapsulation and isolation by explicitly passing it around (Now available with the Transactional plugin)
  • Using "request" context in cases where actual REQUEST-scoped providers are not supported (passport strategies, cron controllers, websocket gateways, queue consumers, ...)

Most of these are to some extent solvable using REQUEST-scoped providers or passing the context as a parameter, but these solutions are often clunky and come with a whole lot of other issues.


Documentation


Contributing

Contributing to a community project is always welcome, please see the Contributing guide :)