n8n-nodes-base
Version:
Base nodes of n8n
110 lines • 4.04 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.description = void 0;
exports.execute = execute;
const n8n_workflow_1 = require("n8n-workflow");
const utilities_1 = require("../../../../../utils/utilities");
const utils_1 = require("../../helpers/utils");
const common_descriptions_1 = require("../common.descriptions");
const properties = [
{
displayName: 'Command',
name: 'deleteCommand',
type: 'options',
default: 'truncate',
options: [
{
name: 'Truncate',
value: 'truncate',
description: "Only removes the table's data and preserves the table's structure",
},
{
name: 'Delete',
value: 'delete',
description: "Delete the rows that match the 'Select Rows' conditions below. If no selection is made, all rows in the table are deleted.",
},
{
name: 'Drop',
value: 'drop',
description: "Deletes the table's data and also the table's structure permanently",
},
],
},
{
displayName: 'Restart Sequences',
name: 'restartSequences',
type: 'boolean',
default: false,
description: 'Whether to reset identity (auto-increment) columns to their initial values',
displayOptions: {
show: {
deleteCommand: ['truncate'],
},
},
},
{
...common_descriptions_1.whereFixedCollection,
displayOptions: {
show: {
deleteCommand: ['delete'],
},
},
},
{
...common_descriptions_1.combineConditionsCollection,
displayOptions: {
show: {
deleteCommand: ['delete'],
},
},
},
common_descriptions_1.optionsCollection,
];
const displayOptions = {
show: {
resource: ['database'],
operation: ['deleteTable'],
},
hide: {
table: [''],
},
};
exports.description = (0, utilities_1.updateDisplayOptions)(displayOptions, properties);
async function execute(runQueries, items, nodeOptions, _db) {
const queries = [];
for (let i = 0; i < items.length; i++) {
const options = this.getNodeParameter('options', i, {});
const schema = this.getNodeParameter('schema', i, undefined, {
extractValue: true,
});
const table = this.getNodeParameter('table', i, undefined, {
extractValue: true,
});
const deleteCommand = this.getNodeParameter('deleteCommand', i);
let query = '';
let values = [schema, table];
if (deleteCommand === 'drop') {
const cascade = options.cascade ? ' CASCADE' : '';
query = `DROP TABLE IF EXISTS $1:name.$2:name${cascade}`;
}
if (deleteCommand === 'truncate') {
const identity = this.getNodeParameter('restartSequences', i, false)
? ' RESTART IDENTITY'
: '';
const cascade = options.cascade ? ' CASCADE' : '';
query = `TRUNCATE TABLE $1:name.$2:name${identity}${cascade}`;
}
if (deleteCommand === 'delete') {
const whereClauses = (0, utils_1.getWhereClauses)(this, i);
const combineConditions = this.getNodeParameter('combineConditions', i, 'AND');
[query, values] = (0, utils_1.addWhereClauses)(this.getNode(), i, 'DELETE FROM $1:name.$2:name', whereClauses, values, combineConditions);
}
if (query === '') {
throw new n8n_workflow_1.NodeOperationError(this.getNode(), 'Invalid delete command, only drop, delete and truncate are supported ', { itemIndex: i });
}
const queryWithValues = { query, values };
queries.push(queryWithValues);
}
return await runQueries(queries, nodeOptions);
}
//# sourceMappingURL=deleteTable.operation.js.map