Context Object
The Context Object (ctx
) is a crucial component in Nanoservice-TS that facilitates data sharing and state management across different Nodes within a single Workflow execution instance. It acts as a shared memory space, allowing Nodes to pass information to subsequent Nodes, access initial trigger data, and maintain state throughout the lifecycle of a workflow run.
What is the Context Object?
When a Trigger initiates a Workflow, Nanoservice-TS creates a unique Context Object for that specific workflow instance. This object persists throughout the execution of that instance and is accessible to every Node within it.
Key characteristics of the Context Object:
- Instance-Specific: Each run of a workflow gets its own isolated Context Object. Data in one workflow instance does not interfere with another.
- Shared State: Nodes can read from and write to the Context Object, allowing them to share data and state dynamically.
- Data Carrier: It carries initial data from the trigger (e.g., HTTP request body, query parameters, message payload) and makes it available to all Nodes.
- Configuration & Services: It can also be used to provide access to configuration values, shared services, or utility functions that Nodes might need.
Structure and Usage
In nanoservice-ts
, the Context Object (ctx
) has a specific structure that enables nodes to exchange data and maintain state throughout execution:
Breakdown of the Context Object
-
ctx.request
: Contains all HTTP request details:body
: The request payload (e.g., for POST, PUT requests)method
: HTTP method (e.g., GET, POST, etc.)headers
: Request headersquery
: Query parameters (e.g.,?key=value
)params
: Path parameters (e.g.,/:id
)
-
ctx.response
: Holds workflow outputs:data
: The latest processed data from nodes
-
ctx.vars
: Temporary workflow variables:- These are not persisted across requests.
- Nodes can store variables dynamically for later use.
Conceptual Example of Context Usage in a Node
Data Flow and Immutability
While the Context Object allows for shared mutable state, it’s essential to be mindful of how data is modified. Some Nanoservice-TS implementations or best practices might encourage treating parts of the context (like initial trigger data) as immutable to prevent unintended side effects.
Nodes typically read the data they need from the context (or directly from their input
which is often populated from the context by the workflow engine), perform their operations, and then write their results back to the context if those results need to be accessed by subsequent nodes.
Context in Workflows
Context is essential for passing data between nodes and making dynamic decisions in workflows.
Example: Using ctx.request.params
In this workflow, we extract id
from the request URL and fetch details from MongoDB:
How it Works:
path: "/movies/:id"
defines a dynamic route.- The MongoDB node retrieves the movie using:
This extracts
id
from the URL (e.g.,/movies/123
→ctx.request.params.id = 123
).
Example: Using ctx.vars
This workflow stores an API response in a variable and uses it in a later step:
How it Works:
- The API call node (
fetch-data
) fetches API data and stores it inctx.vars['fetch-data']
. - The mapper node (
process-data
) extracts and processes values fromctx.vars['fetch-data']
.
Best Practices for Using the Context Object
- Clear Naming Conventions: Use consistent and descriptive keys when setting data in the context to avoid collisions and improve readability (e.g.,
nodeName.outputKey
,shared.config.apiKey
). - Minimize Global State: While the context is shared, avoid overusing it as a global dumping ground. Prefer passing data explicitly through Node inputs and outputs when the data flow is linear and simple.
- Schema Awareness: Be aware of the data types you are storing and retrieving. While the context itself might be flexible, Nodes often expect specific data types as per their
inputSchema
. - Error Handling Data: The context can also be used to store error information if a Node fails, which can then be picked up by error handling Nodes or branches in the workflow.
- Security Considerations: Be cautious about storing sensitive information in the context, especially if logs or full context dumps are generated for debugging. Sanitize or avoid storing raw sensitive data if possible.
Key Takeaways
- ✅
ctx
allows passing request data to nodes. - ✅ Nodes modify and store values in
ctx.vars
. - ✅ Nodes can dynamically read/write to
ctx.response.data
. - ✅
ctx.request.params
,ctx.request.body
, andctx.response
drive workflow execution.
The Context Object is a powerful mechanism that enables dynamic and flexible workflows in Nanoservice-TS. Understanding how to use it effectively is key to building robust and maintainable nanoservice applications.
Next, learn about what initiates workflows: Triggers.