State Management (Pinia)
Relevant source files
The following files were used as context for generating this wiki page:
- packages/frontend/editor-ui/src/stores/workflows.store.ts
- packages/frontend/editor-ui/src/stores/canvas.store.ts
- packages/frontend/editor-ui/src/stores/builder.store.ts
- packages/frontend/@n8n/stores/src/useRootStore.ts
- packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts
- packages/frontend/editor-ui/src/components/VirtualSchema.test.ts
- packages/frontend/editor-ui/src/Interface.ts
- packages/frontend/editor-ui/src/constants.ts
- packages/frontend/editor-ui/src/components/RunData.test.ts
- packages/frontend/editor-ui/src/components/CommunityPackageManageConfirmModal.test.ts
The n8n frontend utilizes Pinia as its primary state management library. This approach provides a centralized and organized way to handle application-wide state, particularly within the complex workflow editor UI. Pinia's modular structure allows for the separation of concerns, where each store is responsible for a specific domain of the application's state, such as workflows, UI elements, node types, or user settings.
This system is crucial for maintaining a reactive and consistent user experience. State changes are managed through well-defined actions and getters, ensuring that data flow is predictable and easy to debug. The stores are extensively used across Vue components to access and manipulate shared data, and they are designed to be highly testable, with established patterns for mocking and isolation in unit tests.
Architecture and Core Concepts
The state management architecture is built around a collection of independent Pinia stores. Each store is defined in its own file and encapsulates a piece of the global state, along with the logic to modify and retrieve it. Components access these stores via composable functions (e.g., useWorkflowsStore()).
Key Stores
The application is divided into several key state management stores:
| Store | Responsibility | Source Files - |
|---|---|---|
workflowsStore | Manages the state of the current workflow, including its nodes, connections, settings, and execution data. | packages/frontend/editor-ui/src/stores/workflows.store.ts - |
uiStore | Handles general UI state, such as the visibility and data of modals. | packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:21 - |
nodeTypesStore | Caches and provides information about all available node types, including their properties and descriptions. | packages/frontend/editor-ui/src/components/VirtualSchema.test.ts:133, packages/frontend/editor-ui/src/components/CommunityPackageManageConfirmModal.test.ts:1 - |
ndvStore | Manages the state of the Node Detail View (NDV), including the active node and panel display modes. | packages/frontend/editor-ui/src/components/VirtualSchema.test.ts:129 - |
settingsStore | Holds user-specific and application-wide settings, such as feature flags and editor preferences. | packages/frontend/editor-ui/src/components/VirtualSchema.test.ts:140 - |
This diagram illustrates the dependencies and interactions between the major Pinia stores in the n8n frontend.
Key State Interfaces
The structure of the state within each store is defined by a set of core TypeScript interfaces.
WorkflowsState
This interface defines the shape of the workflowsStore. It is central to the editor's operation.
| Property | Type | Description |
|---|---|---|
workflow | IWorkflowDb | The complete data object for the currently loaded workflow. |
nodeMetadata | NodeMetadataMap | Metadata for each node, such as pristine status and update timestamps. |
workflowExecutionData | `IExecutionResponse | null` |
workflowExecutionPairedItemMappings | {[itemId: string]: Set<string>} | Mappings for paired items during execution. |
usedCredentials | Record<string, IUsedCredential> | A record of credentials used in the current workflow. |
Sources: packages/frontend/editor-ui/src/Interface.ts:600-613
NDVState (Node Detail View)
This interface governs the state of the Node Detail View (NDV) panel, which is used to configure nodes and view their I/O data.
| Property | Type | Description |
|---|---|---|
activeNodeName | `string | null` |
input / output | object | State objects for the input and output panels, including displayMode ('table', 'json', 'schema', etc.). |
mappingTelemetry | object | Telemetry data related to data mapping actions. |
isMappingOnboarded | boolean | Flag to track if the user has completed the data mapping onboarding. |
draggable | object | State related to dragging and dropping items for data mapping. |
Sources: packages/frontend/editor-ui/src/Interface.ts:660-686
Data Flow and Interactions
The Pinia stores work in concert to create a reactive UI. A common pattern involves a user action in a component triggering an action in a store, which updates the state, causing other components to reactively update.
Example: Pinning Node Data
The following sequence diagram shows what happens when a user pins the output data of a node.
This flow demonstrates how a component (RunData.vue) dispatches an action to update shared state, and another component (VirtualSchema.vue) automatically reflects that change because it's subscribed to the same store.
Sources: packages/frontend/editor-ui/src/components/RunData.test.ts:133-138, packages/frontend/editor-ui/src/components/VirtualSchema.test.ts:192-200
State Management in Testing
The n8n frontend codebase has a robust strategy for testing components that rely on Pinia stores. The @pinia/testing library is used to facilitate this.
Test Setup
In test files, createTestingPinia() is called to provide a sandboxed Pinia instance for the component under test. This prevents state from leaking between tests.
// Example from WhatsNewModal.test.ts
import { createTestingPinia } from '@pinia/testing';
import { useUIStore } from '@/stores/ui.store';
// ...
describe('WhatsNewModal', () => {
beforeEach(() => {
createTestingPinia();
uiStore = mockedStore(useUIStore);
uiStore.modalsById = {
[WHATS_NEW_MODAL_KEY]: {
open: true,
},
};
// ...
});
});
Sources: packages/frontend/editor-ui/src/components/WhatsNewModal.test.ts:16-17, 60-65
Mocking Stores
A mockedStore utility is often used to get a typed reference to a store. This allows test suites to easily set initial state, mock return values of getters, and spy on actions.
In VirtualSchema.test.ts, the workflowsStore is manipulated to simulate different data scenarios, such as a node having pinned data or no data at all. This allows the VirtualSchema component to be tested under various conditions without needing a full execution environment.
// Example from VirtualSchema.test.ts
async function setupStore() {
// ...
const pinia = createTestingPinia({ stubActions: false });
setActivePinia(pinia);
const workflowsStore = useWorkflowsStore();
// ...
workflowsStore.workflow = workflow as IWorkflowDb;
// ...
}
// ... in a test case
useWorkflowsStore().pinData({
node: mockNode1,
data: [
{ json: { name: 'John', age: 22, hobbies: ['surfing', 'traveling'] } },
{ json: { name: 'Joe', age: 33, hobbies: ['skateboarding', 'gaming'] } },
],
});
Sources: packages/frontend/editor-ui/src/components/VirtualSchema.test.ts:124-138, 203-210
This testing approach ensures that components behave correctly in response to different states managed by Pinia, leading to more reliable and maintainable UI code.