n8n-nodes-get-variables 0.1.2 → 0.1.7

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.
@@ -9,9 +9,9 @@ class GetVariables {
9
9
  icon: 'fa:key',
10
10
  group: ['transform'],
11
11
  version: 1,
12
- description: 'Fetch variables from an n8n DataTable and return them as a key-value object',
12
+ description: 'Fetch variables from an n8n DataTable and merge into workflow data, or create a new Variables table',
13
13
  defaults: { name: 'Get Variables' },
14
- inputs: [],
14
+ inputs: ['main'],
15
15
  outputs: ['main'],
16
16
  credentials: [
17
17
  {
@@ -20,27 +20,101 @@ class GetVariables {
20
20
  },
21
21
  ],
22
22
  properties: [
23
+ {
24
+ displayName: 'Operation',
25
+ name: 'operation',
26
+ type: 'options',
27
+ noDataExpression: true,
28
+ options: [
29
+ {
30
+ name: 'Get Variables',
31
+ value: 'getVariables',
32
+ description: 'Fetch variables from a DataTable and merge into workflow data',
33
+ },
34
+ {
35
+ name: 'Create Variables Table',
36
+ value: 'createTable',
37
+ description: 'Create a new DataTable with the correct column structure',
38
+ },
39
+ ],
40
+ default: 'getVariables',
41
+ },
42
+ // ── Get Variables fields ──────────────────────────────────────────
23
43
  {
24
44
  displayName: 'DataTable',
25
45
  name: 'dataTableId',
26
46
  type: 'options',
27
- typeOptions: {
28
- loadOptionsMethod: 'getDataTables',
29
- },
47
+ typeOptions: { loadOptionsMethod: 'getDataTables' },
30
48
  default: '',
31
49
  required: true,
50
+ displayOptions: { show: { operation: ['getVariables'] } },
32
51
  description: 'The DataTable to fetch variables from',
33
52
  },
53
+ {
54
+ displayName: 'Key Column',
55
+ name: 'keyColumn',
56
+ type: 'string',
57
+ default: 'variable_key',
58
+ required: true,
59
+ displayOptions: { show: { operation: ['getVariables'] } },
60
+ description: 'Column that holds the variable name',
61
+ },
62
+ {
63
+ displayName: 'Value Column',
64
+ name: 'valueColumn',
65
+ type: 'string',
66
+ default: 'variable_value',
67
+ required: true,
68
+ displayOptions: { show: { operation: ['getVariables'] } },
69
+ description: 'Column that holds the variable value',
70
+ },
34
71
  {
35
72
  displayName: 'Variable Keys',
36
73
  name: 'variableKeys',
37
74
  type: 'multiOptions',
38
75
  typeOptions: {
39
76
  loadOptionsMethod: 'getVariableKeys',
40
- loadOptionsDependsOn: ['dataTableId'],
77
+ loadOptionsDependsOn: ['dataTableId', 'keyColumn'],
41
78
  },
42
79
  default: [],
43
- description: 'Which variable_key values to include. Leave empty to return all.',
80
+ displayOptions: { show: { operation: ['getVariables'] } },
81
+ description: 'Which keys to include. Leave empty to return all.',
82
+ },
83
+ // ── Create Table fields ───────────────────────────────────────────
84
+ {
85
+ displayName: 'Table Name',
86
+ name: 'tableName',
87
+ type: 'string',
88
+ default: 'n8n Variables',
89
+ required: true,
90
+ displayOptions: { show: { operation: ['createTable'] } },
91
+ description: 'Name for the new DataTable',
92
+ },
93
+ {
94
+ displayName: 'Key Column Name',
95
+ name: 'keyColumnName',
96
+ type: 'string',
97
+ default: 'variable_key',
98
+ required: true,
99
+ displayOptions: { show: { operation: ['createTable'] } },
100
+ description: 'Name for the key column',
101
+ },
102
+ {
103
+ displayName: 'Value Column Name',
104
+ name: 'valueColumnName',
105
+ type: 'string',
106
+ default: 'variable_value',
107
+ required: true,
108
+ displayOptions: { show: { operation: ['createTable'] } },
109
+ description: 'Name for the value column',
110
+ },
111
+ {
112
+ displayName: 'Continue if Table Already Exists',
113
+ name: 'continueIfExists',
114
+ type: 'boolean',
115
+ default: true,
116
+ displayOptions: { show: { operation: ['createTable'] } },
117
+ description: 'Whether to return the existing table instead of throwing an error',
44
118
  },
45
119
  ],
46
120
  };
@@ -68,6 +142,7 @@ class GetVariables {
68
142
  const baseUrl = credentials.baseUrl.replace(/\/$/, '');
69
143
  const apiKey = credentials.apiKey;
70
144
  const tableId = this.getCurrentNodeParameter('dataTableId');
145
+ const keyColumn = this.getCurrentNodeParameter('keyColumn') || 'variable_key';
71
146
  if (!tableId)
72
147
  return [];
73
148
  const response = await this.helpers.httpRequest({
@@ -77,9 +152,7 @@ class GetVariables {
77
152
  });
78
153
  const rows = (_a = response.data) !== null && _a !== void 0 ? _a : response;
79
154
  const keys = [
80
- ...new Set(rows
81
- .map((r) => r.variable_key)
82
- .filter(Boolean)),
155
+ ...new Set(rows.map((r) => r[keyColumn]).filter(Boolean)),
83
156
  ];
84
157
  return keys.map((key) => ({ name: key, value: key }));
85
158
  },
@@ -87,28 +160,77 @@ class GetVariables {
87
160
  };
88
161
  }
89
162
  async execute() {
90
- var _a;
163
+ var _a, _b;
91
164
  const credentials = await this.getCredentials('n8nApi');
92
165
  const baseUrl = credentials.baseUrl.replace(/\/$/, '');
93
166
  const apiKey = credentials.apiKey;
167
+ const operation = this.getNodeParameter('operation', 0);
168
+ if (operation === 'createTable') {
169
+ const tableName = this.getNodeParameter('tableName', 0);
170
+ const keyColumnName = this.getNodeParameter('keyColumnName', 0);
171
+ const valueColumnName = this.getNodeParameter('valueColumnName', 0);
172
+ const continueIfExists = this.getNodeParameter('continueIfExists', 0);
173
+ try {
174
+ const response = await this.helpers.httpRequest({
175
+ method: 'POST',
176
+ url: `${baseUrl}/api/v1/data-tables`,
177
+ headers: {
178
+ 'X-N8N-API-KEY': apiKey,
179
+ 'Content-Type': 'application/json',
180
+ },
181
+ body: {
182
+ name: tableName,
183
+ columns: [
184
+ { name: keyColumnName, type: 'string' },
185
+ { name: valueColumnName, type: 'string' },
186
+ ],
187
+ },
188
+ });
189
+ return [[{ json: response }]];
190
+ }
191
+ catch (error) {
192
+ if (!continueIfExists)
193
+ throw error;
194
+ // table already exists — find and return it
195
+ const listResponse = await this.helpers.httpRequest({
196
+ method: 'GET',
197
+ url: `${baseUrl}/api/v1/data-tables`,
198
+ headers: { 'X-N8N-API-KEY': apiKey },
199
+ });
200
+ const tables = (_a = listResponse.data) !== null && _a !== void 0 ? _a : listResponse;
201
+ const existing = tables.find((t) => t.name === tableName);
202
+ if (!existing)
203
+ throw error;
204
+ return [[{ json: { ...existing, alreadyExisted: true } }]];
205
+ }
206
+ }
207
+ // ── getVariables ──────────────────────────────────────────────────
94
208
  const tableId = this.getNodeParameter('dataTableId', 0);
209
+ const keyColumn = this.getNodeParameter('keyColumn', 0);
210
+ const valueColumn = this.getNodeParameter('valueColumn', 0);
95
211
  const selectedKeys = this.getNodeParameter('variableKeys', 0);
96
212
  const response = await this.helpers.httpRequest({
97
213
  method: 'GET',
98
214
  url: `${baseUrl}/api/v1/data-tables/${tableId}/rows`,
99
215
  headers: { 'X-N8N-API-KEY': apiKey },
100
216
  });
101
- const rows = (_a = response.data) !== null && _a !== void 0 ? _a : response;
102
- const keyValuePairs = rows.reduce((acc, row) => {
103
- const key = row.variable_key;
217
+ const rows = (_b = response.data) !== null && _b !== void 0 ? _b : response;
218
+ const variables = rows.reduce((acc, row) => {
219
+ const key = row[keyColumn];
104
220
  if (!key)
105
221
  return acc;
106
222
  if (selectedKeys.length === 0 || selectedKeys.includes(key)) {
107
- acc[key] = row.variable_value;
223
+ acc[key] = row[valueColumn];
108
224
  }
109
225
  return acc;
110
226
  }, {});
111
- return [[{ json: keyValuePairs }]];
227
+ const inputItems = this.getInputData();
228
+ if (inputItems.length > 0) {
229
+ return [inputItems.map((item) => ({
230
+ json: { ...item.json, ...variables },
231
+ }))];
232
+ }
233
+ return [[{ json: variables }]];
112
234
  }
113
235
  }
114
236
  exports.GetVariables = GetVariables;
@@ -1 +1 @@
1
- {"version":3,"file":"GetVariables.node.js","sourceRoot":"","sources":["../../../nodes/GetVariables/GetVariables.node.ts"],"names":[],"mappings":";;;AAUA,MAAa,YAAY;IAAzB;QACE,gBAAW,GAAyB;YAClC,WAAW,EAAE,eAAe;YAC5B,IAAI,EAAE,cAAc;YACpB,IAAI,EAAE,QAAQ;YACd,KAAK,EAAE,CAAC,WAAW,CAAC;YACpB,OAAO,EAAE,CAAC;YACV,WAAW,EAAE,6EAA6E;YAC1F,QAAQ,EAAE,EAAE,IAAI,EAAE,eAAe,EAAE;YACnC,MAAM,EAA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,EAAE,EAAE;YACzC,MAAM,GAAG,GAAG,GAAG,CAAC,SAAS,CAAW,CAAC;YACrC,IAAI,CAAC,GAAG;gBAAE,OAAO,GAAG,CAAC;YACrB,IAAI,YAAY,CAAC,MAAM,KAAK,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,GAAG,CAAC,EAAE,CAAC;gBAC5D,GAAG,CAAC,GAAG,CAAC,GAAG,GAAG,CAAC,WAAW,CAAW,CAAC;YACxC,CAAC;YACD,OAAO,GAAG,CAAC;QACb,CAAC,EAAE,EAAiB,CAAC,CAAC;QAEtB,MAAM,UAAU,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAEvC,IAAI,UAAU,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;YAC1B,OAAO,CAAC,UAAU,CAAC,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC;oBAChC,IAAI,EAAE,EAAE,GAAG,IAAI,CAAC,IAAI,EAAE,GAAG,SAAS,EAAE;iBACrC,CAAC,CAAC,CAAC,CAAC;QACP,CAAC;QAED,OAAO,CAAC,CAAC,EAAE,IAAI,EAAE,SAAS,EAAE,CAAC,CAAC,CAAC;IACjC,CAAC;CACF;AAtPD,oCAsPC"}
@@ -15,9 +15,9 @@ export class GetVariables implements INodeType {
15
15
  icon: 'fa:key',
16
16
  group: ['transform'],
17
17
  version: 1,
18
- description: 'Fetch variables from an n8n DataTable and return them as a key-value object',
18
+ description: 'Fetch variables from an n8n DataTable and merge into workflow data, or create a new Variables table',
19
19
  defaults: { name: 'Get Variables' },
20
- inputs: [],
20
+ inputs: ['main'],
21
21
  outputs: ['main'],
22
22
  credentials: [
23
23
  {
@@ -26,27 +26,103 @@ export class GetVariables implements INodeType {
26
26
  },
27
27
  ],
28
28
  properties: [
29
+ {
30
+ displayName: 'Operation',
31
+ name: 'operation',
32
+ type: 'options',
33
+ noDataExpression: true,
34
+ options: [
35
+ {
36
+ name: 'Get Variables',
37
+ value: 'getVariables',
38
+ description: 'Fetch variables from a DataTable and merge into workflow data',
39
+ },
40
+ {
41
+ name: 'Create Variables Table',
42
+ value: 'createTable',
43
+ description: 'Create a new DataTable with the correct column structure',
44
+ },
45
+ ],
46
+ default: 'getVariables',
47
+ },
48
+
49
+ // ── Get Variables fields ──────────────────────────────────────────
29
50
  {
30
51
  displayName: 'DataTable',
31
52
  name: 'dataTableId',
32
53
  type: 'options',
33
- typeOptions: {
34
- loadOptionsMethod: 'getDataTables',
35
- },
54
+ typeOptions: { loadOptionsMethod: 'getDataTables' },
36
55
  default: '',
37
56
  required: true,
57
+ displayOptions: { show: { operation: ['getVariables'] } },
38
58
  description: 'The DataTable to fetch variables from',
39
59
  },
60
+ {
61
+ displayName: 'Key Column',
62
+ name: 'keyColumn',
63
+ type: 'string',
64
+ default: 'variable_key',
65
+ required: true,
66
+ displayOptions: { show: { operation: ['getVariables'] } },
67
+ description: 'Column that holds the variable name',
68
+ },
69
+ {
70
+ displayName: 'Value Column',
71
+ name: 'valueColumn',
72
+ type: 'string',
73
+ default: 'variable_value',
74
+ required: true,
75
+ displayOptions: { show: { operation: ['getVariables'] } },
76
+ description: 'Column that holds the variable value',
77
+ },
40
78
  {
41
79
  displayName: 'Variable Keys',
42
80
  name: 'variableKeys',
43
81
  type: 'multiOptions',
44
82
  typeOptions: {
45
83
  loadOptionsMethod: 'getVariableKeys',
46
- loadOptionsDependsOn: ['dataTableId'],
84
+ loadOptionsDependsOn: ['dataTableId', 'keyColumn'],
47
85
  },
48
86
  default: [],
49
- description: 'Which variable_key values to include. Leave empty to return all.',
87
+ displayOptions: { show: { operation: ['getVariables'] } },
88
+ description: 'Which keys to include. Leave empty to return all.',
89
+ },
90
+
91
+ // ── Create Table fields ───────────────────────────────────────────
92
+ {
93
+ displayName: 'Table Name',
94
+ name: 'tableName',
95
+ type: 'string',
96
+ default: 'n8n Variables',
97
+ required: true,
98
+ displayOptions: { show: { operation: ['createTable'] } },
99
+ description: 'Name for the new DataTable',
100
+ },
101
+ {
102
+ displayName: 'Key Column Name',
103
+ name: 'keyColumnName',
104
+ type: 'string',
105
+ default: 'variable_key',
106
+ required: true,
107
+ displayOptions: { show: { operation: ['createTable'] } },
108
+ description: 'Name for the key column',
109
+ },
110
+ {
111
+ displayName: 'Value Column Name',
112
+ name: 'valueColumnName',
113
+ type: 'string',
114
+ default: 'variable_value',
115
+ required: true,
116
+ displayOptions: { show: { operation: ['createTable'] } },
117
+ description: 'Name for the value column',
118
+ },
119
+ {
120
+ displayName: 'Continue if Table Already Exists',
121
+ name: 'continueIfExists',
122
+ type: 'boolean',
123
+ default: true,
124
+ displayOptions: { show: { operation: ['createTable'] } },
125
+ description: 'Whether to return the existing table instead of throwing an error',
50
126
  },
51
127
  ],
52
128
  };
@@ -76,6 +152,7 @@ export class GetVariables implements INodeType {
76
152
  const baseUrl = (credentials.baseUrl as string).replace(/\/$/, '');
77
153
  const apiKey = credentials.apiKey as string;
78
154
  const tableId = this.getCurrentNodeParameter('dataTableId') as string;
155
+ const keyColumn = (this.getCurrentNodeParameter('keyColumn') as string) || 'variable_key';
79
156
 
80
157
  if (!tableId) return [];
81
158
 
@@ -87,11 +164,7 @@ export class GetVariables implements INodeType {
87
164
 
88
165
  const rows: IDataObject[] = response.data ?? response;
89
166
  const keys = [
90
- ...new Set(
91
- rows
92
- .map((r) => r.variable_key as string)
93
- .filter(Boolean),
94
- ),
167
+ ...new Set(rows.map((r) => r[keyColumn] as string).filter(Boolean)),
95
168
  ];
96
169
 
97
170
  return keys.map((key) => ({ name: key, value: key }));
@@ -103,7 +176,55 @@ export class GetVariables implements INodeType {
103
176
  const credentials = await this.getCredentials('n8nApi');
104
177
  const baseUrl = (credentials.baseUrl as string).replace(/\/$/, '');
105
178
  const apiKey = credentials.apiKey as string;
179
+ const operation = this.getNodeParameter('operation', 0) as string;
180
+
181
+ if (operation === 'createTable') {
182
+ const tableName = this.getNodeParameter('tableName', 0) as string;
183
+ const keyColumnName = this.getNodeParameter('keyColumnName', 0) as string;
184
+ const valueColumnName = this.getNodeParameter('valueColumnName', 0) as string;
185
+ const continueIfExists = this.getNodeParameter('continueIfExists', 0) as boolean;
186
+
187
+ try {
188
+ const response = await this.helpers.httpRequest({
189
+ method: 'POST',
190
+ url: `${baseUrl}/api/v1/data-tables`,
191
+ headers: {
192
+ 'X-N8N-API-KEY': apiKey,
193
+ 'Content-Type': 'application/json',
194
+ },
195
+ body: {
196
+ name: tableName,
197
+ columns: [
198
+ { name: keyColumnName, type: 'string' },
199
+ { name: valueColumnName, type: 'string' },
200
+ ],
201
+ },
202
+ });
203
+
204
+ return [[{ json: response as IDataObject }]];
205
+ } catch (error: unknown) {
206
+ if (!continueIfExists) throw error;
207
+
208
+ // table already exists — find and return it
209
+ const listResponse = await this.helpers.httpRequest({
210
+ method: 'GET',
211
+ url: `${baseUrl}/api/v1/data-tables`,
212
+ headers: { 'X-N8N-API-KEY': apiKey },
213
+ });
214
+
215
+ const tables: IDataObject[] = listResponse.data ?? listResponse;
216
+ const existing = tables.find((t) => t.name === tableName);
217
+
218
+ if (!existing) throw error;
219
+
220
+ return [[{ json: { ...existing, alreadyExisted: true } }]];
221
+ }
222
+ }
223
+
224
+ // ── getVariables ──────────────────────────────────────────────────
106
225
  const tableId = this.getNodeParameter('dataTableId', 0) as string;
226
+ const keyColumn = this.getNodeParameter('keyColumn', 0) as string;
227
+ const valueColumn = this.getNodeParameter('valueColumn', 0) as string;
107
228
  const selectedKeys = this.getNodeParameter('variableKeys', 0) as string[];
108
229
 
109
230
  const response = await this.helpers.httpRequest({
@@ -114,15 +235,23 @@ export class GetVariables implements INodeType {
114
235
 
115
236
  const rows: IDataObject[] = response.data ?? response;
116
237
 
117
- const keyValuePairs = rows.reduce((acc, row) => {
118
- const key = row.variable_key as string;
238
+ const variables = rows.reduce((acc, row) => {
239
+ const key = row[keyColumn] as string;
119
240
  if (!key) return acc;
120
241
  if (selectedKeys.length === 0 || selectedKeys.includes(key)) {
121
- acc[key] = row.variable_value as string;
242
+ acc[key] = row[valueColumn] as string;
122
243
  }
123
244
  return acc;
124
245
  }, {} as IDataObject);
125
246
 
126
- return [[{ json: keyValuePairs }]];
247
+ const inputItems = this.getInputData();
248
+
249
+ if (inputItems.length > 0) {
250
+ return [inputItems.map((item) => ({
251
+ json: { ...item.json, ...variables },
252
+ }))];
253
+ }
254
+
255
+ return [[{ json: variables }]];
127
256
  }
128
257
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "n8n-nodes-get-variables",
3
- "version": "0.1.2",
3
+ "version": "0.1.7",
4
4
  "description": "n8n community node — collapses DataTable rows into a single key-value object",
5
5
  "keywords": [
6
6
  "n8n-community-node-package"