UNPKG

n8n-nodes-base

Version:

Base nodes of n8n

89 lines 4.17 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.prepareNames = prepareNames; exports.pgTriggerFunction = pgTriggerFunction; exports.initDB = initDB; exports.searchSchema = searchSchema; exports.searchTables = searchTables; const errors_1 = require("@n8n/errors"); const transport_1 = require("./transport"); function prepareNames(id, mode, additionalFields) { let suffix = id.replace(/-/g, '_'); if (mode === 'manual') { suffix = `${suffix}_manual`; } let functionName = additionalFields.functionName || `n8n_trigger_function_${suffix}()`; if (!(functionName.includes('(') && functionName.includes(')'))) { functionName = `${functionName}()`; } const triggerName = additionalFields.triggerName || `n8n_trigger_${suffix}`; const channelName = additionalFields.channelName || `n8n_channel_${suffix}`; if (channelName.includes('-')) { throw new errors_1.ApplicationError('Channel name cannot contain hyphens (-)', { level: 'warning' }); } return { functionName, triggerName, channelName }; } async function pgTriggerFunction(db, additionalFields, functionName, triggerName, channelName) { const schema = this.getNodeParameter('schema', 'public', { extractValue: true }); const tableName = this.getNodeParameter('tableName', undefined, { extractValue: true, }); const target = `${schema}."${tableName}"`; const firesOn = this.getNodeParameter('firesOn', 0); const functionReplace = "CREATE OR REPLACE FUNCTION $1:raw RETURNS trigger LANGUAGE 'plpgsql' COST 100 VOLATILE NOT LEAKPROOF AS $BODY$ begin perform pg_notify('$2:raw', row_to_json($3:raw)::text); return null; end; $BODY$;"; const dropIfExist = 'DROP TRIGGER IF EXISTS $1:raw ON $2:raw'; const functionExists = "CREATE FUNCTION $1:raw RETURNS trigger LANGUAGE 'plpgsql' COST 100 VOLATILE NOT LEAKPROOF AS $BODY$ begin perform pg_notify('$2:raw', row_to_json($3:raw)::text); return null; end; $BODY$"; const trigger = 'CREATE TRIGGER $4:raw AFTER $3:raw ON $1:raw FOR EACH ROW EXECUTE FUNCTION $2:raw'; const whichData = firesOn === 'DELETE' ? 'old' : 'new'; if (channelName.includes('-')) { throw new errors_1.ApplicationError('Channel name cannot contain hyphens (-)', { level: 'warning' }); } const replaceIfExists = additionalFields.replaceIfExists ?? false; try { if (replaceIfExists || !(additionalFields.triggerName ?? additionalFields.functionName)) { await db.any(functionReplace, [functionName, channelName, whichData]); await db.any(dropIfExist, [triggerName, target, whichData]); } else { await db.any(functionExists, [functionName, channelName, whichData]); } await db.any(trigger, [target, functionName, firesOn, triggerName]); } catch (error) { if (error.message.includes('near "-"')) { throw new errors_1.ApplicationError('Names cannot contain hyphens (-)', { level: 'warning' }); } throw error; } } async function initDB() { const credentials = await this.getCredentials('postgres'); const options = this.getNodeParameter('options', {}); return await transport_1.configurePostgres.call(this, credentials, options); } async function searchSchema() { const { db } = await initDB.call(this); const schemaList = await db.any('SELECT schema_name FROM information_schema.schemata'); const results = schemaList.map((s) => ({ name: s.schema_name, value: s.schema_name, })); return { results }; } async function searchTables() { const schema = this.getNodeParameter('schema', 0); const { db } = await initDB.call(this); let tableList = []; try { tableList = await db.any('SELECT table_name FROM information_schema.tables WHERE table_schema = $1', [schema.value]); } catch (error) { throw new errors_1.ApplicationError(error); } const results = tableList.map((s) => ({ name: s.table_name, value: s.table_name, })); return { results }; } //# sourceMappingURL=PostgresTrigger.functions.js.map