UNPKG

n8n-nodes-azuresql

Version:

n8n node to connect and interact with Azure SQL databases with dynamic database selection

179 lines (178 loc) 7.03 kB
"use strict"; var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) { if (k2 === undefined) k2 = k; var desc = Object.getOwnPropertyDescriptor(m, k); if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) { desc = { enumerable: true, get: function() { return m[k]; } }; } Object.defineProperty(o, k2, desc); }) : (function(o, m, k, k2) { if (k2 === undefined) k2 = k; o[k2] = m[k]; })); var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) { Object.defineProperty(o, "default", { enumerable: true, value: v }); }) : function(o, v) { o["default"] = v; }); var __importStar = (this && this.__importStar) || (function () { var ownKeys = function(o) { ownKeys = Object.getOwnPropertyNames || function (o) { var ar = []; for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k; return ar; }; return ownKeys(o); }; return function (mod) { if (mod && mod.__esModule) return mod; var result = {}; if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]); __setModuleDefault(result, mod); return result; }; })(); Object.defineProperty(exports, "__esModule", { value: true }); exports.AzureSql = void 0; const mssql = __importStar(require("mssql")); class AzureSql { constructor() { this.description = { displayName: 'Azure SQL', name: 'azureSql', icon: 'file:azuresql.svg', group: ['input'], version: 1, description: 'Get, add and update data in Azure SQL', defaults: { name: 'Azure SQL', }, // @ts-ignore inputs: ['main'], // @ts-ignore outputs: ['main'], credentials: [ { name: 'azureSqlApi', required: true, }, ], properties: [ { displayName: 'Operation', name: 'operation', type: 'options', noDataExpression: true, options: [ { name: 'Execute Query', value: 'executeQuery', description: 'Execute an SQL query', action: 'Execute a SQL query', }, { name: 'Insert', value: 'insert', description: 'Insert rows in database', action: 'Insert rows in database', }, { name: 'Update', value: 'update', description: 'Update rows in database', action: 'Update rows in database', }, { name: 'Delete', value: 'delete', description: 'Delete rows in database', action: 'Delete rows in database', }, ], default: 'executeQuery', }, { displayName: 'Database Name', name: 'database', type: 'string', default: '', description: 'Name of the database to use. If not specified, the default database from credentials will be used.', placeholder: 'Leave blank to use default from credentials', }, { displayName: 'Query', name: 'query', type: 'string', displayOptions: { show: { operation: ['executeQuery'], }, }, default: '', placeholder: 'SELECT id, name FROM product WHERE id < 40', required: true, description: 'The SQL query to execute', }, ], }; } async execute() { const credentials = await this.getCredentials('azureSql'); // Get input data (not used in this simplified version) this.getInputData(); const operation = this.getNodeParameter('operation', 0); const databaseOverride = this.getNodeParameter('database', 0, ''); // Configure connection with optional database override const config = { server: credentials.server, port: credentials.port, database: databaseOverride || credentials.database, user: credentials.user, password: credentials.password, domain: credentials.domain ? credentials.domain : undefined, connectionTimeout: credentials.connectTimeout, requestTimeout: credentials.requestTimeout, options: { encrypt: credentials.tls, enableArithAbort: false, tdsVersion: credentials.tdsVersion, trustServerCertificate: credentials.allowUnauthorizedCerts, }, }; const pool = new mssql.ConnectionPool(config); let returnItems = []; try { await pool.connect(); if (operation === 'executeQuery') { const query = this.getNodeParameter('query', 0); const result = await pool.request().query(query); if (result.recordset) { returnItems = this.helpers.returnJsonArray(result.recordset); } else { returnItems = this.helpers.returnJsonArray([{ message: 'Query executed successfully' }]); } } else { // For other operations, we'll just return a success message for now // In a real implementation, you would handle insert, update, delete operations returnItems = this.helpers.returnJsonArray([ { message: `${operation} operation would be executed here with database: ${config.database}` }, ]); } } catch (error) { if (this.continueOnFail()) { returnItems = this.helpers.returnJsonArray({ error: error.message }); } else { throw error; } } finally { await pool.close(); } return [returnItems]; } } exports.AzureSql = AzureSql;