n8n-nodes-variable 1.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +259 -0
- package/dist/Variable/Variable.node.json +17 -0
- package/dist/Variable/variable.svg +14 -0
- package/dist/nodes/Variable/Variable.node.d.ts +5 -0
- package/dist/nodes/Variable/Variable.node.js +744 -0
- package/dist/nodes/Variable/helpers/storage.d.ts +14 -0
- package/dist/nodes/Variable/helpers/storage.js +138 -0
- package/dist/nodes/Variable/helpers/types.d.ts +30 -0
- package/dist/nodes/Variable/helpers/types.js +2 -0
- package/dist/nodes/Variable/helpers/valueParser.d.ts +20 -0
- package/dist/nodes/Variable/helpers/valueParser.js +124 -0
- package/package.json +71 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,259 @@
|
|
|
1
|
+
# n8n-nodes-variable
|
|
2
|
+
|
|
3
|
+
A community node package for [n8n](https://n8n.io) that provides local and global variable management for workflow automation — without needing Code nodes.
|
|
4
|
+
|
|
5
|
+
---
|
|
6
|
+
|
|
7
|
+
## What this node does
|
|
8
|
+
|
|
9
|
+
The **Variable** node lets you store, retrieve, update, and delete named variables anywhere in a workflow. Variables can be scoped to:
|
|
10
|
+
|
|
11
|
+
- **Local (This Execution)** — lives only for the current run, stored on the item's JSON data
|
|
12
|
+
- **Workflow Global** — persists across executions using n8n's workflow static data
|
|
13
|
+
- **Node Local** — persists for the specific node instance
|
|
14
|
+
- **Custom Namespace** — workflow global storage with a fully dynamic namespace string (great for per-user / per-guild data)
|
|
15
|
+
|
|
16
|
+
---
|
|
17
|
+
|
|
18
|
+
## Installation
|
|
19
|
+
|
|
20
|
+
### Community node (recommended)
|
|
21
|
+
|
|
22
|
+
1. In n8n, go to **Settings → Community Nodes**
|
|
23
|
+
2. Click **Install**
|
|
24
|
+
3. Enter `n8n-nodes-variable` and confirm
|
|
25
|
+
|
|
26
|
+
### Manual install (self-hosted)
|
|
27
|
+
|
|
28
|
+
```bash
|
|
29
|
+
cd ~/.n8n/custom # or your N8N_CUSTOM_EXTENSIONS path
|
|
30
|
+
npm install n8n-nodes-variable
|
|
31
|
+
```
|
|
32
|
+
|
|
33
|
+
Then restart n8n.
|
|
34
|
+
|
|
35
|
+
---
|
|
36
|
+
|
|
37
|
+
## Operations
|
|
38
|
+
|
|
39
|
+
| Operation | Description |
|
|
40
|
+
|---|---|
|
|
41
|
+
| **Set Variable** | Create or update a variable |
|
|
42
|
+
| **Get Variable** | Retrieve a variable value |
|
|
43
|
+
| **Delete Variable** | Remove a variable |
|
|
44
|
+
| **Has Variable** | Check whether a variable exists (returns boolean) |
|
|
45
|
+
| **List Variables** | List all keys (and optionally values) in a namespace |
|
|
46
|
+
| **Clear Variables** | Delete all variables in a namespace (requires confirmation) |
|
|
47
|
+
| **Increment Variable** | Add a number to a numeric variable |
|
|
48
|
+
| **Decrement Variable** | Subtract a number from a numeric variable |
|
|
49
|
+
| **Append to Array** | Push a value onto an array variable |
|
|
50
|
+
| **Merge Object** | Merge a JSON object into an object variable |
|
|
51
|
+
| **Toggle Boolean** | Flip a boolean variable between `true` and `false` |
|
|
52
|
+
|
|
53
|
+
---
|
|
54
|
+
|
|
55
|
+
## Scopes explained
|
|
56
|
+
|
|
57
|
+
### Local (This Execution)
|
|
58
|
+
|
|
59
|
+
Variables are stored directly on the item's JSON under `_variables` (configurable). They pass forward through subsequent nodes in the same execution and are gone when the execution ends.
|
|
60
|
+
|
|
61
|
+
```json
|
|
62
|
+
{
|
|
63
|
+
"name": "Alice",
|
|
64
|
+
"_variables": {
|
|
65
|
+
"default": {
|
|
66
|
+
"step": 3,
|
|
67
|
+
"seen": true
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
### Workflow Global
|
|
74
|
+
|
|
75
|
+
Variables persist across executions using n8n's [workflow static data](https://docs.n8n.io/code/cookbook/builtin/get-workflow-static-data/). Stored under:
|
|
76
|
+
|
|
77
|
+
```
|
|
78
|
+
staticData.n8nNodesVariable.namespaces[namespace].variables[key]
|
|
79
|
+
```
|
|
80
|
+
|
|
81
|
+
This is suitable for light-to-moderate state such as counters, flags, and small data objects.
|
|
82
|
+
|
|
83
|
+
### Node Local
|
|
84
|
+
|
|
85
|
+
Like Workflow Global but scoped to the specific node instance. Useful for node-level state that should not conflict with other Variable nodes in the same workflow.
|
|
86
|
+
|
|
87
|
+
### Custom Namespace
|
|
88
|
+
|
|
89
|
+
Uses Workflow Global storage with a fully expression-capable namespace string. Ideal for per-entity state in Discord bots and similar automation:
|
|
90
|
+
|
|
91
|
+
```
|
|
92
|
+
economy → balance per user
|
|
93
|
+
cooldowns → command cooldown per guild+user
|
|
94
|
+
guild_{{$json.guild.id}} → per-guild settings
|
|
95
|
+
```
|
|
96
|
+
|
|
97
|
+
---
|
|
98
|
+
|
|
99
|
+
## Examples
|
|
100
|
+
|
|
101
|
+
### 1. Set a local variable and get it later
|
|
102
|
+
|
|
103
|
+
**Node 1 — Set (Local Execution)**
|
|
104
|
+
- Operation: `Set Variable`
|
|
105
|
+
- Scope: `Local (This Execution)`
|
|
106
|
+
- Key: `stepCount`
|
|
107
|
+
- Value Type: `Number`
|
|
108
|
+
- Value: `1`
|
|
109
|
+
|
|
110
|
+
**Node 2 — Get (Local Execution)**
|
|
111
|
+
- Operation: `Get Variable`
|
|
112
|
+
- Scope: `Local (This Execution)`
|
|
113
|
+
- Key: `stepCount`
|
|
114
|
+
- Output Field Name: `currentStep`
|
|
115
|
+
|
|
116
|
+
The output item will have `currentStep: 1`.
|
|
117
|
+
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
### 2. Set a workflow global counter
|
|
121
|
+
|
|
122
|
+
**Increment on every execution:**
|
|
123
|
+
- Operation: `Increment Variable`
|
|
124
|
+
- Scope: `Workflow Global`
|
|
125
|
+
- Namespace: `stats`
|
|
126
|
+
- Key: `runCount`
|
|
127
|
+
- Amount: `1`
|
|
128
|
+
- Initialize If Missing: `true`
|
|
129
|
+
- Initial Value: `0`
|
|
130
|
+
|
|
131
|
+
The variable `stats.runCount` persists between executions and increments each time the workflow runs.
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
### 3. Discord bot: user balance (economy system)
|
|
136
|
+
|
|
137
|
+
**Set balance when a user earns coins:**
|
|
138
|
+
- Operation: `Set Variable`
|
|
139
|
+
- Scope: `Custom Namespace`
|
|
140
|
+
- Custom Namespace: `economy`
|
|
141
|
+
- Key: `balance_{{$json.user.id}}`
|
|
142
|
+
- Value Type: `Number`
|
|
143
|
+
- Value: `{{$json.amount}}`
|
|
144
|
+
|
|
145
|
+
**Get balance when displaying it:**
|
|
146
|
+
- Operation: `Get Variable`
|
|
147
|
+
- Scope: `Custom Namespace`
|
|
148
|
+
- Custom Namespace: `economy`
|
|
149
|
+
- Key: `balance_{{$json.user.id}}`
|
|
150
|
+
- Use Default Value: `true`
|
|
151
|
+
- Default Value: `0`
|
|
152
|
+
|
|
153
|
+
**Increment after earning:**
|
|
154
|
+
- Operation: `Increment Variable`
|
|
155
|
+
- Scope: `Custom Namespace`
|
|
156
|
+
- Custom Namespace: `economy`
|
|
157
|
+
- Key: `balance_{{$json.user.id}}`
|
|
158
|
+
- Amount: `{{$json.coinsEarned}}`
|
|
159
|
+
|
|
160
|
+
---
|
|
161
|
+
|
|
162
|
+
### 4. Discord bot: cooldown tracking
|
|
163
|
+
|
|
164
|
+
**Set a cooldown flag:**
|
|
165
|
+
- Operation: `Set Variable`
|
|
166
|
+
- Scope: `Custom Namespace`
|
|
167
|
+
- Custom Namespace: `cooldowns`
|
|
168
|
+
- Key: `cmd_{{$json.guild.id}}_{{$json.user.id}}`
|
|
169
|
+
- Value Type: `Number`
|
|
170
|
+
- Value: `{{Date.now()}}`
|
|
171
|
+
- Overwrite If Exists: `true`
|
|
172
|
+
|
|
173
|
+
**Check if cooldown is active:**
|
|
174
|
+
- Operation: `Has Variable`
|
|
175
|
+
- Scope: `Custom Namespace`
|
|
176
|
+
- Custom Namespace: `cooldowns`
|
|
177
|
+
- Key: `cmd_{{$json.guild.id}}_{{$json.user.id}}`
|
|
178
|
+
|
|
179
|
+
---
|
|
180
|
+
|
|
181
|
+
## Output modes
|
|
182
|
+
|
|
183
|
+
| Mode | Description |
|
|
184
|
+
|---|---|
|
|
185
|
+
| **Preserve Input + Add Result** _(default)_ | Keep all input fields and add a result object at `variable` (or your chosen field name) |
|
|
186
|
+
| **Result Only** | Output only the operation result object |
|
|
187
|
+
| **Add / Update Field** | Set a single field on the input item with just the variable's value |
|
|
188
|
+
|
|
189
|
+
### Example result object
|
|
190
|
+
|
|
191
|
+
```json
|
|
192
|
+
{
|
|
193
|
+
"variable": {
|
|
194
|
+
"operation": "get",
|
|
195
|
+
"scope": "customNamespace",
|
|
196
|
+
"namespace": "economy",
|
|
197
|
+
"key": "balance_123456789",
|
|
198
|
+
"value": 500,
|
|
199
|
+
"exists": true
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
```
|
|
203
|
+
|
|
204
|
+
---
|
|
205
|
+
|
|
206
|
+
## Limitations and concurrency warning
|
|
207
|
+
|
|
208
|
+
Workflow Global and Node Local variables use n8n's static data, which is:
|
|
209
|
+
|
|
210
|
+
- **Suitable for:** counters, feature flags, small state objects, per-user values in low-traffic bots
|
|
211
|
+
- **Not suitable for:** high-concurrency write operations (e.g., simultaneously updating the same counter from hundreds of parallel executions)
|
|
212
|
+
- **Not cross-workflow:** variables are scoped to the workflow they belong to, not shared globally across all workflows
|
|
213
|
+
|
|
214
|
+
For high-volume or high-concurrency state, consider using a database node (Redis, Postgres, MongoDB) instead.
|
|
215
|
+
|
|
216
|
+
Future versions of this node may add built-in Redis, Postgres, or n8n Data Tables backends.
|
|
217
|
+
|
|
218
|
+
---
|
|
219
|
+
|
|
220
|
+
## Development
|
|
221
|
+
|
|
222
|
+
```bash
|
|
223
|
+
git clone <your-repo>
|
|
224
|
+
cd n8n-nodes-variable
|
|
225
|
+
npm install
|
|
226
|
+
npm run build
|
|
227
|
+
npm test
|
|
228
|
+
```
|
|
229
|
+
|
|
230
|
+
### Linking to a local n8n instance
|
|
231
|
+
|
|
232
|
+
```bash
|
|
233
|
+
# In the package directory
|
|
234
|
+
npm run build
|
|
235
|
+
npm link
|
|
236
|
+
|
|
237
|
+
# In your n8n custom nodes directory (~/.n8n/custom)
|
|
238
|
+
npm link n8n-nodes-variable
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
Then restart n8n and search for **Variable** in the nodes panel.
|
|
242
|
+
|
|
243
|
+
### Manual validation workflow
|
|
244
|
+
|
|
245
|
+
After linking:
|
|
246
|
+
|
|
247
|
+
1. Create a new workflow
|
|
248
|
+
2. Add **Manual Trigger**
|
|
249
|
+
3. Add **Variable** → Set Variable, Local Execution, Key: `myVar`, Value: `hello`
|
|
250
|
+
4. Add **Variable** → Get Variable, Local Execution, Key: `myVar`
|
|
251
|
+
5. Add **Variable** → Increment Variable, Workflow Global, Namespace: `stats`, Key: `runCount`
|
|
252
|
+
6. Add **Variable** → List Variables, Workflow Global, Namespace: `stats`
|
|
253
|
+
7. Execute and verify the output of each step
|
|
254
|
+
|
|
255
|
+
---
|
|
256
|
+
|
|
257
|
+
## License
|
|
258
|
+
|
|
259
|
+
MIT
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
{
|
|
2
|
+
"node": "n8n-nodes-variable.variable",
|
|
3
|
+
"nodeVersion": "1.0",
|
|
4
|
+
"codexVersion": "1.0",
|
|
5
|
+
"categories": [
|
|
6
|
+
"Core Nodes",
|
|
7
|
+
"Helpers"
|
|
8
|
+
],
|
|
9
|
+
"resources": {
|
|
10
|
+
"primaryDocumentation": [
|
|
11
|
+
{
|
|
12
|
+
"url": "https://github.com/dagostino-esi/n8n-nodes-variable"
|
|
13
|
+
}
|
|
14
|
+
]
|
|
15
|
+
},
|
|
16
|
+
"alias": ["variable", "state", "store", "counter", "flag"]
|
|
17
|
+
}
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 60 60" width="60" height="60">
|
|
2
|
+
<!-- Background rounded square -->
|
|
3
|
+
<rect width="60" height="60" rx="10" ry="10" fill="#6366f1"/>
|
|
4
|
+
<!-- Outer brackets: { } -->
|
|
5
|
+
<text
|
|
6
|
+
x="30" y="43"
|
|
7
|
+
text-anchor="middle"
|
|
8
|
+
font-family="'Courier New', Courier, monospace"
|
|
9
|
+
font-size="28"
|
|
10
|
+
font-weight="700"
|
|
11
|
+
fill="white"
|
|
12
|
+
letter-spacing="-1"
|
|
13
|
+
>{x}</text>
|
|
14
|
+
</svg>
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import { type IExecuteFunctions, type INodeExecutionData, type INodeType, type INodeTypeDescription } from 'n8n-workflow';
|
|
2
|
+
export declare class Variable implements INodeType {
|
|
3
|
+
description: INodeTypeDescription;
|
|
4
|
+
execute(this: IExecuteFunctions): Promise<INodeExecutionData[][]>;
|
|
5
|
+
}
|