Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: use node async local storage #2

Open
theodorDiaconu opened this issue Dec 25, 2024 · 0 comments
Open

feat: use node async local storage #2

theodorDiaconu opened this issue Dec 25, 2024 · 0 comments
Assignees

Comments

@theodorDiaconu
Copy link
Collaborator

theodorDiaconu commented Dec 25, 2024

Ability to create a generic applicationContext which can be used in the app.
This is typically created when you're handling some backend routes (rest, graphql) and you have the graphql context, or express 'request' object but we need to move things outside this layer into application logic.

// in resource for routing

const { AsyncLocalStorage } = require('node:async_hooks');
const asyncLocalStorage = new AsyncLocalStorage();

// can be exported by the package and extended.
export type IApplicationContext = {
   user: User
   roles: string[],
   isLoggedIn: boolean,
}

// you can also add it as a middleware in express
async function createContext(req, res, security: SecurityService): IApplicationContext { 
  // define the response like user, tenant, roles, etc
}

{
  async init(_, { doSomething, security, validator }) {
     app.get("/account", async (req, res) => {
          await validator.validate(AccountFiltersModel, req.params.filters)
          await withContext({}, doSomething, params); // params should be typesafe. 
     };
  }
  
// and in do something
const context = useContext();
@theodorDiaconu theodorDiaconu changed the title feat: use node runincontext feat: use node async local storage Dec 25, 2024
@theodorDiaconu theodorDiaconu self-assigned this Feb 10, 2025
theodorDiaconu added a commit that referenced this issue Feb 10, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant