{"id":26579833,"url":"https://github.com/mondaycom/node-execution-context","last_synced_at":"2025-03-23T06:29:12.364Z","repository":{"id":65652074,"uuid":"239987989","full_name":"mondaycom/node-execution-context","owner":"mondaycom","description":null,"archived":false,"fork":false,"pushed_at":"2023-04-17T13:22:05.000Z","size":64,"stargazers_count":37,"open_issues_count":2,"forks_count":5,"subscribers_count":14,"default_branch":"master","last_synced_at":"2025-03-13T20:38:48.433Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/mondaycom.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2020-02-12T10:42:15.000Z","updated_at":"2023-05-26T23:46:34.000Z","dependencies_parsed_at":"2024-06-21T17:50:44.102Z","dependency_job_id":"c66f13ed-39c7-4730-8f8b-128d087493b6","html_url":"https://github.com/mondaycom/node-execution-context","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"7909ba2ca52d6d823a7391510928bed0bd4ab6b2"},"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondaycom%2Fnode-execution-context","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondaycom%2Fnode-execution-context/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondaycom%2Fnode-execution-context/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mondaycom%2Fnode-execution-context/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mondaycom","download_url":"https://codeload.github.com/mondaycom/node-execution-context/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245066072,"owners_count":20555392,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-03-23T06:29:11.364Z","updated_at":"2025-03-23T06:29:12.345Z","avatar_url":"https://github.com/mondaycom.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"\n# node-execution-context\nA simple, straightforward library that allows you to create persistent request-level execution context using the async_hooks module that will be accessible anywhere in the code scoped to the current request you're handling at any given moment.\n\n## Table of Contents\n- [Install](#install)\n- [Getting Started](#getting-started)\n- [Available Functions](#available-functions)\n\n## Install\n```\n$ npm install @mondaydotcomorg/node-execution-context\n```\n\nor with yarn:\n\n```\n$ yarn add @mondaydotcomorg/node-execution-context\n```\n\n## Getting Started\n\nLet's create a service that will use our library in order to create and get context.\n```javascript\nconst contextProvider = require('@mondaydotcomorg/node-execution-context');\n\nfunction createExecutionContext(contextData) {\n    contextProvider.createExecutionContext(contextData);\n};\n\nfunction getExecutionContext() {\n  const context = contextProvider.getExecutionContext();\n  return context;\n};\n\nmodule.exports = {\n  getExecutionContext,\n  createExecutionContext\n};\n```\n\nNow wherever we want in our code we can pass an object to createExecutionContext and it will be saved and accesible for any async resources descendant from that place forward.\n\nFor example let's do this in a middleware that is the first thing that runs on a new request. \n```javascript\nconst executionContextService = require('services/execution-context-service');\n\nasync function authenticationMiddleware(req, res, next) {\n    const { accountPermissions } = req.body\n    \n    executionContextService.createExecutionContext({\n      accountPermissions,\n      method: req.method,\n    });\n\n    next();\n}\n```\n\nNow we can use this context later and be certain that the request being handled is the same one for which we are getting our context.\n\n```javascript\nconst executionContextService = require('services/execution-context-service');\nconst eventModelService =  require('services/event-model-service');\n\nasync function createNewEvent(eventData) {\n    const { accountPermissions } = executionContextService.getExecutionContext();\n    eventModelService.createEvent(accountPermissions, eventData);\n}\n```\n\n## Available Functions\n### createExecutionContext(contextObject, traceOptions = { enabled: false, initialData: {} })\nCreates an execution context identified by the asyncId of the current asyncResource. This will be available anywhere in the execution that is inside the async chain of this resource. \nContext passed must be an object. \nYou cannot create an execution context twice within the same async resource. If you want to update after creation use set ot update. This check will fail only in non-prod environments for performance purposes.\n\nOptional Params: traceOptions can be passed if you want to set some initial trace data into the trace and have the context add a trace detailing async Id and resource type each time context is updated. If you do not pass this object the trace is never created. This can be used for debugging or for enriching logs, however should not be passed if not needed as this will be added fro every async resource created.\n\n### getExecutionContext()\nReturns only the context object given as the first param to createExecutionContext.\n  \n### getExecutionTrace()\nReturns only the trace array collected if enabled in traceOptions.\n\n### getExecutionData()\nReturns entire context data including both context object and trace array.\n   \n### setExecutionContext(newContext)\nAllows you to completly override context saved for current async resource. \n\n### updateExecutionContext(contextUpdates)\nAllows you update specific keys in the context saved for current async resource. \n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmondaycom%2Fnode-execution-context","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmondaycom%2Fnode-execution-context","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmondaycom%2Fnode-execution-context/lists"}