-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtulip_tables.html
514 lines (481 loc) · 19.2 KB
/
tulip_tables.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
<script src="node-red-tulip-edge/js/tulip_tables_common.js"></script>
<script type="text/javascript">
(() => {
const QUERY_TYPES = this.tulip_tables_common.TABLE_QUERY_TYPES;
RED.nodes.registerType('tulip-tables', {
category: 'Tulip',
inputs: 1,
outputs: 1,
color: '#65CCB8',
icon: 'tulip.svg',
defaults: {
name: { value: 'tulip-tables' },
apiAuth: {
value: '',
type: 'tulip-api-auth',
required: true,
},
keepAlive: { value: true },
keepAliveMsecs: { value: 10000 },
retainMsgProps: { value: true },
// Which API call to use
queryType: { value: 0 },
// String parameters
tableId: { value: '' },
queryId: { value: '' },
aggregationId: { value: '' },
recordId: { value: '' },
fieldId: { value: '' },
linkId: { value: '' },
sortBy: { value: '_sequenceNumber' },
sortByFieldId: { value: '' },
sortDir: { value: 'asc' },
function: { value: 'sum' },
filterAggregator: { value: 'all' },
// Ints
limit: { value: 10 },
offset: { value: 0 },
// Bool parameters
includeDeleted: { value: false },
includeTotalCount: { value: false },
allowRecordsInUse: { value: false },
// Arrays
field: { value: [] },
filters: { value: [] },
sortOptions: { value: [] },
body: { value: '{}' }, // JSON body
},
paletteLabel: 'tables',
label: function () {
// TODO: better naming based on what the api call actually is
return this.name || 'tulip-tables';
},
oneditprepare: function () {
const node = this;
// Add retainMsgProps as a property to transition legacy nodes. Set to false since
// previously msg props were not retained.
if (!('retainMsgProps' in node)) {
node.retainMsgProps = false;
}
// Whether or not to show keep-alive msecs option
const updateKeepAlive = () => {
const keepAlive = $('#node-input-keepAlive').is(':checked');
if (keepAlive) {
$('#div-node-input-keepAliveMsecs').show();
} else {
$('#div-node-input-keepAliveMsecs').hide();
}
};
updateKeepAlive();
$('#node-input-keepAlive').change(() => {
updateKeepAlive();
});
// Create dropdown menu of table query types
const nQueryTypes = QUERY_TYPES.length;
const querySelect = document.getElementById('node-input-queryType');
for (let i = 0; i < nQueryTypes; i++) {
const option = document.createElement('option');
option.value = i;
option.text = QUERY_TYPES[i].text;
querySelect.add(option);
}
// Select the current query type
querySelect.selectedIndex = node.queryType;
// Show all relevant parameters to this query
const queryTypeInfo = QUERY_TYPES[node.queryType];
const params = queryTypeInfo.pathParams.concat(queryTypeInfo.queryParams);
params.forEach((param) => {
const divToShow = `#div-node-input-${param}`;
$(divToShow).show();
});
// Set the document to hide/show elements for different query selections
$(querySelect).data('prevQueryType', node.queryType);
$(querySelect).change(function (data) {
const newQueryType = $(this).val();
const oldQueryType = $(this).data('prevQueryType');
// Hide all parameters for old query type
const oldInfo = QUERY_TYPES[oldQueryType];
const oldParams = oldInfo.pathParams.concat(oldInfo.queryParams);
oldParams.forEach((param) => {
const divToHide = `#div-node-input-${param}`;
$(divToHide).hide();
});
// Show all parameters for new query type
const newInfo = QUERY_TYPES[newQueryType];
const newParams = newInfo.pathParams.concat(newInfo.queryParams);
newParams.forEach((param) => {
const divToShow = `#div-node-input-${param}`;
$(divToShow).show();
});
// If POST or PUT request, also show request body
if (newInfo.method == 'POST' || newInfo.method == 'PUT') {
$('#div-node-input-body').show();
} else {
$('#div-node-input-body').hide();
}
// Save the current query type
$(this).data('prevQueryType', newQueryType);
});
// Show custom sortBy field
const sortBySelect = document.getElementById('node-input-sortBy');
$(sortBySelect).change(function (data) {
if ($(this).val() == 'other') {
$(`#div-node-input-sortByOther`).show();
} else {
$(`#div-node-input-sortByOther`).hide();
}
});
// Convert JSON inputs to typedInput
$('#node-input-body').typedInput({
type: 'json',
types: ['json'],
});
// Adds an editableList widget for the given parameter `p`
// of the specified type `t`
function addEditableTypedList(param, paramType) {
// Creates a function to be called when adding a list item
// for this parameter & type
const getAddItem = function () {
return function (container, i, data) {
container.css({
overflow: 'hidden',
whiteSpace: 'nowrap',
});
const row = $('<div/>').appendTo(container);
// Create the TypedInput element
const typedInputElem = $('<input/>', {
class: `node-input-${param}-value`,
type: 'text',
width: '100%',
})
.appendTo(row)
.typedInput({
default: paramType,
types: [paramType],
type: paramType,
})
.typedInput('show');
// Set the TypedInput element value
if (data.value != undefined) {
typedInputElem.typedInput('value', data.value);
} else {
// Use default value based on type
switch (paramType) {
case 'str':
typedInputElem.typedInput('value', '');
break;
case 'json':
typedInputElem.typedInput('value', '{}');
break;
}
}
};
};
// Create the editable list
$(`#node-input-${param}-container`).editableList({
addButton: true,
height: 'auto',
scrollOnAdd: true,
sortable: true,
removable: true,
addItem: getAddItem(),
});
// Populate list with current values
node[param].forEach((v) => {
const value = paramType == 'json' ? JSON.stringify(v) : v;
$(`#node-input-${param}-container`).editableList('addItem', { value });
});
}
// Add editableList widget for list parameters
addEditableTypedList('field', 'str');
addEditableTypedList('filters', 'json');
addEditableTypedList('sortOptions', 'json');
},
oneditsave: function () {
const node = this;
// Saves values in EditableList to the node config
function saveListValues(param, paramType) {
const entries = $(`#node-input-${param}-container`).editableList('items');
node[param] = [];
entries.each(function (e) {
// Iterate through list items in UI
const entry = $(this);
const entryValue = entry.find(`.node-input-${param}-value`).val();
switch (paramType) {
// Push non-empty values
case 'str':
if (entryValue != '') {
node[param].push(entryValue);
}
break;
case 'json':
if (entryValue != '{}') {
node[param].push(JSON.parse(entryValue));
}
break;
}
});
// Overwrite the node's config
this[param] = node[param];
}
// Save list parameters
saveListValues('field', 'str');
saveListValues('filters', 'json');
saveListValues('sortOptions', 'json');
},
});
})();
</script>
<script type="text/html" data-template-name="tulip-tables">
<div class="form-row">
<label for="node-input-name"><i class="fa fa-tag"></i> Name</label>
<input type="text" id="node-input-name" placeholder="Name" />
</div>
<div class="form-row">
<label for="node-input-apiAuth"
><i class="fa fa-user-circle"></i> Tulip API Authentication</label
>
<input id="node-input-apiAuth" />
</div>
<div class="form-row">
<label for="node-input-keepAlive"> HTTP Options</label>
<input
type="checkbox"
id="node-input-keepAlive"
autocomplete="off"
style="margin-left:10px; vertical-align:top; width:auto"
/>
<label for="node-input-keepAlive" style="width:auto">Enable Connection Keep-Alive</label>
</div>
<div class="form-row" id="div-node-input-keepAliveMsecs" hidden>
<label for="node-input-keepAliveMsecs"> </label>
<span style="width:auto">Keep-Alive Initial Delay (ms)</span>
<input
type="number"
min="0"
step="1"
id="node-input-keepAliveMsecs"
value="10000"
style="width:auto"
/>
</div>
<br>
<div class="form-row">
<label for="node-input-retainMsgProps"> Output</label>
<input
type="checkbox"
id="node-input-retainMsgProps"
autocomplete="off"
style="margin-left:10px; vertical-align:top; width:auto"
/>
<label for="node-input-retainMsgProps" style="width:auto">Retain Input Message Properties</label>
</div>
<br>
<div class="form-row">
<label for="node-input-queryType"> Query Type</label>
<select id="node-input-queryType" style="width:70%"></select>
</div>
<hr />
<label style="width:100%;">
<h4>Query Options</h4>
</label>
<div class="form-row" id="div-node-input-includeDeleted" hidden>
<input
type="checkbox"
id="node-input-includeDeleted"
autocomplete="off"
style="margin-left:10px; vertical-align:top; width:auto"
/>
<label for="node-input-includeDeleted" style="width:auto">Include deleted tables</label>
</div>
<div class="form-row" id="div-node-input-includeTotalCount" hidden>
<input
type="checkbox"
id="node-input-includeTotalCount"
autocomplete="off"
style="margin-left:10px; vertical-align:top; width:auto"
/>
<label for="node-input-includeTotalCount" style="width:auto">Include total count</label>
</div>
<div class="form-row" id="div-node-input-allowRecordsInUse" hidden>
<input
type="checkbox"
id="node-input-allowRecordsInUse"
autocomplete="off"
style="margin-left:10px; vertical-align:top; width:auto"
/>
<label for="node-input-allowRecordsInUse" style="width:auto">Allow records in use</label>
</div>
<div class="form-row" id="div-node-input-tableId" hidden>
<label for="node-input-tableId"> Table ID</label>
<input type="text" id="node-input-tableId" placeholder="tableId" />
</div>
<div class="form-row" id="div-node-input-queryId" hidden>
<label for="node-input-queryId"> Query ID</label>
<input type="text" id="node-input-queryId" placeholder="queryId" />
</div>
<div class="form-row" id="div-node-input-recordId" hidden>
<label for="node-input-recordId"> Record ID</label>
<input type="text" id="node-input-recordId" placeholder="recordId" />
</div>
<div class="form-row" id="div-node-input-aggregationId" hidden>
<label for="node-input-aggregationId"> Aggregation ID</label>
<input type="text" id="node-input-aggregationId" placeholder="aggregationId" />
</div>
<div class="form-row" id="div-node-input-fieldId" hidden>
<label for="node-input-fieldId"> Field ID</label>
<input type="text" id="node-input-fieldId" placeholder="fieldId" />
</div>
<div class="form-row" id="div-node-input-function" hidden>
<label for="node-input-function"> Aggregation Function</label>
<select id="node-input-function" style="width:70%;">
<option value="sum" selected>Sum</option>
<option value="count">Count</option>
<option value="avg">Average</option>
<option value="min">Minimum</option>
<option value="max">Maximum</option>
<option value="mode">Mode</option>
</select>
</div>
<div id="div-node-input-sortBy" hidden>
<div class="form-row">
<label for="node-input-sortBy"> Sort By</label>
<select id="node-input-sortBy" style="width:70%;">
<option value="_sequenceNumber" selected>Sequence Number</option>
<option value="_updatedAt">Updated At</option>
<option value="_createdAt">Created At</option>
<option value="other">(choose table field)</option>
</select>
</div>
<div class="form-row" id="div-node-input-sortByOther" hidden>
<label for="node-input-sortByOther"> </label>
<span style="width:70%">
<span style="padding-right:2px; width:auto;">Table Field:</span>
<input type="text" id="node-input-sortByOther" style="width:auto;" />
</span>
</div>
</div>
<div class="form-row" id="div-node-input-sortDir" hidden>
<label for="node-input-sortDir"> Sort Direction</label>
<select id="node-input-sortDir" style="width:70%;">
<option value="asc" selected>Ascending</option>
<option value="desc">Descending</option>
</select>
</div>
<div class="form-row" id="div-node-input-sortOptions" hidden>
<label for="node-input-sortOptions">Sort Options</label>
<ol id="node-input-sortOptions-container"></ol>
</div>
<div class="form-row" id="div-node-input-filters" hidden>
<label for="node-input-filters">Filters</label>
<ol id="node-input-filters-container"></ol>
</div>
<div class="form-row" id="div-node-input-filterAggregator" hidden>
<label for="node-input-filterAggregator">Filter Aggregator</label>
<select id="node-input-filterAggregator" style="width:70%;">
<option value="all" selected>all</option>
<option value="any">any</option>
</select>
</div>
<div class="form-row" id="div-node-input-limit" hidden>
<label for="node-input-limit"> Limit</label>
<input type="number" min="0" step="1" id="node-input-limit" value="10" />
</div>
<div class="form-row" id="div-node-input-offset" hidden>
<label for="node-input-offset"> Offset</label>
<input type="number" min="0" step="1" id="node-input-offset" value="0" />
</div>
<div class="form-row" id="div-node-input-field" hidden>
<label for="node-input-field">Fields</label>
<ol id="node-input-field-container"></ol>
</div>
<div id="div-node-input-body">
<hr />
<label style="width:100%;">
<h4>Request Body Options</h4>
</label>
<div class="form-row">
<label for="node-input-body"> Request Body</label>
<input type="text" id="node-input-body" />
</div>
</div>
</script>
<script type="text/html" data-help-name="tulip-tables">
<p>Reads & manipulates data in Tulip Tables using the Tulip Tables API.</p>
<h3>Inputs</h3>
<dl class="message-properties">
<dt class="optional">
body
<span class="property-type">JSON</span>
</dt>
<dd>
If the query type is a POST or PUT request, the body of the request. Overrides the data
in the "Request Body" field.
</dd>
<dt class="optional">
headers
<span class="property-type">JSON</span>
</dt>
<dd>
HTTP headers to include in the request. If a POST or PUT request, the header "Content-Type"
will be set to "application/json" and will override <code>msg.headers["Content-Type"]</code>.
</dd>
<dt class="optional">
[parameter]
</dt>
<dd>
Any parameter of a Tulip Tables API request can be set by the input <code>msg.[parameter]</code>.
The message value overrides the parameter value set by the node configuration.
</dd>
</dl>
<h3>Outputs</h3>
<ol class="node-ports">
<dl class="message-properties">
<dt>response <span class="property-type">JSON</span></dt>
<dd>The HTTP response object from the API request.</dd>
<dt>payload</dt>
<dd>The parsed response body.</dd>
</dl>
</ol>
<h3>Overview</h3>
<p>Each <code>tulip-tables</code> node is configured to send data to a Tulip Tables API endpoint. On an input message, the
node will send the configured request and output the HTTP response along with any returned data. The "Query Type" field
determines the type of request, relevant parameters, and the response data type. See the Tulip Tables API documentation
for more information on the different types of requests.
</p>
<p>
To send data to a given factory instance, you must configure a <code>tulip-api-auth</code> node with authentication details.
The API token used for authentication must have the appropriate permissions: either <code>tables:read</code>, <code>tables:write</code>,
<code>table-queries:read</code>, or <code>table-queries:write</code>, depending on the type of request. This authentication node
can be shared between multiple nodes that require Tulip API authentication.</p>
<h3>Advanced Operation</h3>
<p>
The Tulip Tables API request parameters are specified in the node configuration. However, all parameters can also be specified by the
input <code>msg</code>, and this value will dynamically override the static parameter value in the node configuration. For example,
if you the "Table ID" field set to <code>id1</code> but send an input with <code>msg.tableId=id2</code>, the request will set parameter
<code>tableId</code> to <code>id2</code>. The parameters <code>sortOptions</code> and <code>filters</code> can be set by the <code>msg</code>,
and the request body (if a POST or PUT request) can be set by <code>msg.body</code>.
</p>
<p>
Select "Retain Input Message Properties" to keep all properties of the input message in the output message, otherwise they will be cleared.
If an input message property conflicts with a property set by this node (ex: <code>msg.response</code>) then the input message property
will be overwritten.
</p>
<p>
You can configure the HTTP agent to use keep-alive by checking "Enable Connection Keep-Alive" (default=<code>true</code>),
and set the agent keepAliveMsecs (default=<code>10000ms</code>). Note that this sets the keep-alive behaviour of the HTTP agent,
which is different than setting the "Connection": "Keep-Alive" HTTP header (which can be set through <code>msg.headers</code>).
</p>
<h3>References</h3>
<ul>
<li><a href=https://support.tulip.co/en/articles/2835889-an-overview-of-tables>
<b>Overview of Tulip Tables</b>
</a></li>
<li><a href=https://support.tulip.co/en/articles/3983173-how-to-use-the-table-api>
<b>How to Use the Table API</b>
</a></li>
<li>
<b>Tulip API Documentation:</b> available at <a>https://[your-factory-url]/apiDocs</a>, see Tulip Tables endpoints.
</li>
</ul>
</script>