UNPKG

@telepilotco/n8n-nodes-kv-storage

Version:

Key-Value Storage Node for n8n supporting different scopes

229 lines 9.11 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.KvStorage = void 0; require("reflect-metadata"); const typedi_1 = require("typedi"); const KvStorageService_1 = require("./KvStorageService"); class KvStorage { constructor() { this.description = { displayName: 'Key-Value Storage', name: 'kvStorage', icon: 'file:KvStorage.svg', group: ['storage'], version: 1, description: 'Key-Value Storage Getter and Setter', defaults: { name: 'KVStorage', }, credentials: [], inputs: ['main'], outputs: ['main'], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', options: [ { name: 'Get Value by Key in Scope', value: 'getValue', action: 'Get value by key in scope', }, { name: 'Increment Value by Key in Scope. Create Key if It Does Not Exist', value: 'incrementValue', action: 'Increment value by key in scope create key if it does not exist', }, { name: 'List All Keys in Scope', value: 'listAllScopeKeys', action: 'List all keys in scope', }, { name: 'List All KeyValues in ALL Scopes', value: 'listAllKeyValuesInAllScopes', action: 'Get all values and keys in all scopes debug', }, { name: 'List All KeyValues in Scope', value: 'listAllKeyValues', action: 'List all values and keys in scope', }, { name: 'Set Value for Key in Scope', value: 'setValue', action: 'Set value for key in scope', }, ], default: 'getValue', noDataExpression: true, }, { displayName: 'Scope', name: 'scope', type: 'options', required: true, displayOptions: { show: { operation: [ 'listAllKeyValues', 'listAllScopeKeys', 'getValue', 'setValue', 'incrementValue', ], }, }, options: [ { name: 'ALL Scopes', value: KvStorageService_1.Scope.ALL, }, { name: 'Execution Scope', value: KvStorageService_1.Scope.EXECUTION, }, { name: 'Workflow Scope', value: KvStorageService_1.Scope.WORKFLOW, }, { name: 'Instance Scope', value: KvStorageService_1.Scope.INSTANCE, }, ], default: 'WORKFLOW', }, { displayName: 'Key', name: 'key', type: 'string', required: true, displayOptions: { show: { operation: ['getValue', 'setValue', 'incrementValue'], }, }, default: '', placeholder: 'my-example-key', }, { displayName: 'Value', name: 'val', type: 'string', required: true, displayOptions: { show: { operation: ['setValue'], }, }, default: '', placeholder: 'my-example-value', }, { displayName: 'ExecutionId', name: 'executionId', type: 'string', required: true, displayOptions: { show: { operation: ['listAllKeyValues', 'listAllScopeKeys', 'getValue', 'setValue'], scope: [KvStorageService_1.Scope.EXECUTION], }, }, default: '={{ $execution.id }}', placeholder: '={{ $execution.ID }}', description: 'Do not change this - this is unique identifier of Execution', }, { displayName: 'Expire', name: 'expire', type: 'boolean', displayOptions: { show: { operation: ['setValue', 'incrementValue'], }, }, default: true, description: 'Whether to set a timeout on key', }, { displayName: 'TTL', name: 'ttl', type: 'number', typeOptions: { minValue: 1, }, displayOptions: { show: { operation: ['setValue', 'incrementValue'], expire: [true], }, }, default: 60, description: 'Number of seconds before key expiration', }, ], }; } async execute() { const returnData = []; const operation = this.getNodeParameter('operation', 0); let specifier = ''; let scope = KvStorageService_1.Scope.ALL; try { const scopeVar = this.getNodeParameter('scope', 0); scope = KvStorageService_1.Scope[scopeVar]; switch (scope) { case KvStorageService_1.Scope.EXECUTION: specifier = this.getNodeParameter('executionId', 0); break; case KvStorageService_1.Scope.WORKFLOW: specifier = this.getWorkflow().id; break; case KvStorageService_1.Scope.INSTANCE: specifier = 'N8N'; break; default: break; } } catch (e) { } const service = typedi_1.Container.get(KvStorageService_1.KvStorageService); if (operation === 'listAllKeyValuesInAllScopes') { const result = service.listAllKeyValuesInAllScopes(); returnData.push(result); } else if (operation === 'listAllScopeKeys') { const result = service.listAllKeysInScope(scope, specifier); returnData.push(result); } else if (operation === 'listAllKeyValues') { const result = service.listAllKeyValuesInScope(scope, specifier); returnData.push(result); } else if (operation === 'getValue') { const key = this.getNodeParameter('key', 0); const result = service.getValue(key, scope, specifier); returnData.push(result); } else if (operation === 'setValue') { const key = this.getNodeParameter('key', 0); const val = this.getNodeParameter('val', 0); const ttl = this.getNodeParameter('ttl', 0, -1); const result = service.setValue(key, val, scope, specifier, ttl); returnData.push(result); } else if (operation === 'incrementValue') { const key = this.getNodeParameter('key', 0); const ttl = this.getNodeParameter('ttl', 0, -1); const result = service.incrementValue(key, scope, specifier, ttl); returnData.push(result); } return [this.helpers.returnJsonArray(returnData)]; } } exports.KvStorage = KvStorage; //# sourceMappingURL=KvStorage.node.js.map