-
Notifications
You must be signed in to change notification settings - Fork 5.3k
/
utils.ts
572 lines (489 loc) · 15.5 KB
/
utils.ts
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
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
import type {
IDataObject,
IExecuteFunctions,
INode,
INodeExecutionData,
INodePropertyOptions,
} from 'n8n-workflow';
import { NodeOperationError, jsonParse } from 'n8n-workflow';
import { generatePairedItemData } from '../../../../utils/utilities';
import type {
ColumnInfo,
EnumInfo,
PgpClient,
PgpDatabase,
QueryMode,
QueryValues,
QueryWithValues,
SortRule,
WhereClause,
} from './interfaces';
export function wrapData(data: IDataObject | IDataObject[]): INodeExecutionData[] {
if (!Array.isArray(data)) {
return [{ json: data }];
}
return data.map((item) => ({
json: item,
}));
}
export function prepareErrorItem(
items: INodeExecutionData[],
error: IDataObject | NodeOperationError | Error,
index: number,
) {
return {
json: { message: error.message, item: { ...items[index].json }, error: { ...error } },
pairedItem: { item: index },
} as INodeExecutionData;
}
export function parsePostgresError(
node: INode,
error: any,
queries: QueryWithValues[],
itemIndex?: number,
) {
if (error.message.includes('syntax error at or near') && queries.length) {
try {
const snippet = error.message.match(/syntax error at or near "(.*)"/)[1] as string;
const failedQureryIndex = queries.findIndex((query) => query.query.includes(snippet));
if (failedQureryIndex !== -1) {
if (!itemIndex) {
itemIndex = failedQureryIndex;
}
const failedQuery = queries[failedQureryIndex].query;
const lines = failedQuery.split('\n');
const lineIndex = lines.findIndex((line) => line.includes(snippet));
const errorMessage = `Syntax error at line ${lineIndex + 1} near "${snippet}"`;
error.message = errorMessage;
}
} catch {}
}
let message = error.message;
const errorDescription = error.description ? error.description : error.detail || error.hint;
let description = errorDescription;
if (!description && queries[itemIndex || 0]?.query) {
description = `Failed query: ${queries[itemIndex || 0].query}`;
}
if (error.message.includes('ECONNREFUSED')) {
message = 'Connection refused';
try {
description = error.message.split('ECONNREFUSED ')[1].trim();
} catch (e) {}
}
if (error.message.includes('ENOTFOUND')) {
message = 'Host not found';
try {
description = error.message.split('ENOTFOUND ')[1].trim();
} catch (e) {}
}
if (error.message.includes('ETIMEDOUT')) {
message = 'Connection timed out';
try {
description = error.message.split('ETIMEDOUT ')[1].trim();
} catch (e) {}
}
return new NodeOperationError(node, error as Error, {
message,
description,
itemIndex,
});
}
export function addWhereClauses(
node: INode,
itemIndex: number,
query: string,
clauses: WhereClause[],
replacements: QueryValues,
combineConditions: string,
): [string, QueryValues] {
if (clauses.length === 0) return [query, replacements];
let combineWith = 'AND';
if (combineConditions === 'OR') {
combineWith = 'OR';
}
let replacementIndex = replacements.length + 1;
let whereQuery = ' WHERE';
const values: QueryValues = [];
clauses.forEach((clause, index) => {
if (clause.condition === 'equal') {
clause.condition = '=';
}
if (['>', '<', '>=', '<='].includes(clause.condition)) {
const value = Number(clause.value);
if (Number.isNaN(value)) {
throw new NodeOperationError(
node,
`Operator in entry ${index + 1} of 'Select Rows' works with numbers, but value ${
clause.value
} is not a number`,
{
itemIndex,
},
);
}
clause.value = value;
}
const columnReplacement = `$${replacementIndex}:name`;
values.push(clause.column);
replacementIndex = replacementIndex + 1;
let valueReplacement = '';
if (clause.condition !== 'IS NULL') {
valueReplacement = ` $${replacementIndex}`;
values.push(clause.value);
replacementIndex = replacementIndex + 1;
}
const operator = index === clauses.length - 1 ? '' : ` ${combineWith}`;
whereQuery += ` ${columnReplacement} ${clause.condition}${valueReplacement}${operator}`;
});
return [`${query}${whereQuery}`, replacements.concat(...values)];
}
export function addSortRules(
query: string,
rules: SortRule[],
replacements: QueryValues,
): [string, QueryValues] {
if (rules.length === 0) return [query, replacements];
let replacementIndex = replacements.length + 1;
let orderByQuery = ' ORDER BY';
const values: string[] = [];
rules.forEach((rule, index) => {
const columnReplacement = `$${replacementIndex}:name`;
values.push(rule.column);
replacementIndex = replacementIndex + 1;
const endWith = index === rules.length - 1 ? '' : ',';
const sortDirection = rule.direction === 'DESC' ? 'DESC' : 'ASC';
orderByQuery += ` ${columnReplacement} ${sortDirection}${endWith}`;
});
return [`${query}${orderByQuery}`, replacements.concat(...values)];
}
export function addReturning(
query: string,
outputColumns: string[],
replacements: QueryValues,
): [string, QueryValues] {
if (outputColumns.includes('*')) return [`${query} RETURNING *`, replacements];
const replacementIndex = replacements.length + 1;
return [`${query} RETURNING $${replacementIndex}:name`, [...replacements, outputColumns]];
}
const isSelectQuery = (query: string) => {
return query
.replace(/\/\*.*?\*\//g, '') // remove multiline comments
.replace(/\n/g, '')
.split(';')
.filter((statement) => statement && !statement.startsWith('--')) // remove comments and empty statements
.every((statement) => statement.trim().toLowerCase().startsWith('select'));
};
export function configureQueryRunner(
this: IExecuteFunctions,
node: INode,
continueOnFail: boolean,
pgp: PgpClient,
db: PgpDatabase,
) {
return async (queries: QueryWithValues[], items: INodeExecutionData[], options: IDataObject) => {
let returnData: INodeExecutionData[] = [];
const emptyReturnData: INodeExecutionData[] =
options.operation === 'select' ? [] : [{ json: { success: true } }];
const queryBatching = (options.queryBatching as QueryMode) || 'single';
if (queryBatching === 'single') {
try {
returnData = (await db.multi(pgp.helpers.concat(queries)))
.map((result, i) => {
return this.helpers.constructExecutionMetaData(wrapData(result as IDataObject[]), {
itemData: { item: i },
});
})
.flat();
if (!returnData.length) {
const pairedItem = generatePairedItemData(queries.length);
if ((options?.nodeVersion as number) < 2.3) {
if (emptyReturnData.length) {
emptyReturnData[0].pairedItem = pairedItem;
}
returnData = emptyReturnData;
} else {
returnData = queries.every((query) => isSelectQuery(query.query))
? []
: [{ json: { success: true }, pairedItem }];
}
}
} catch (err) {
const error = parsePostgresError(node, err, queries);
if (!continueOnFail) throw error;
return [
{
json: {
message: error.message,
error: { ...error },
},
},
];
}
}
if (queryBatching === 'transaction') {
returnData = await db.tx(async (transaction) => {
const result: INodeExecutionData[] = [];
for (let i = 0; i < queries.length; i++) {
try {
const query = queries[i].query;
const values = queries[i].values;
let transactionResults;
if ((options?.nodeVersion as number) < 2.3) {
transactionResults = await transaction.any(query, values);
} else {
transactionResults = (await transaction.multi(query, values)).flat();
}
if (!transactionResults.length) {
if ((options?.nodeVersion as number) < 2.3) {
transactionResults = emptyReturnData;
} else {
transactionResults = isSelectQuery(query) ? [] : [{ success: true }];
}
}
const executionData = this.helpers.constructExecutionMetaData(
wrapData(transactionResults),
{ itemData: { item: i } },
);
result.push(...executionData);
} catch (err) {
const error = parsePostgresError(node, err, queries, i);
if (!continueOnFail) throw error;
result.push(prepareErrorItem(items, error, i));
return result;
}
}
return result;
});
}
if (queryBatching === 'independently') {
returnData = await db.task(async (task) => {
const result: INodeExecutionData[] = [];
for (let i = 0; i < queries.length; i++) {
try {
const query = queries[i].query;
const values = queries[i].values;
let transactionResults;
if ((options?.nodeVersion as number) < 2.3) {
transactionResults = await task.any(query, values);
} else {
transactionResults = (await task.multi(query, values)).flat();
}
if (!transactionResults.length) {
if ((options?.nodeVersion as number) < 2.3) {
transactionResults = emptyReturnData;
} else {
transactionResults = isSelectQuery(query) ? [] : [{ success: true }];
}
}
const executionData = this.helpers.constructExecutionMetaData(
wrapData(transactionResults),
{ itemData: { item: i } },
);
result.push(...executionData);
} catch (err) {
const error = parsePostgresError(node, err, queries, i);
if (!continueOnFail) throw error;
result.push(prepareErrorItem(items, error, i));
}
}
return result;
});
}
return returnData;
};
}
export function replaceEmptyStringsByNulls(
items: INodeExecutionData[],
replace?: boolean,
): INodeExecutionData[] {
if (!replace) return items;
const returnData: INodeExecutionData[] = items.map((item) => {
const newItem = { ...item };
const keys = Object.keys(newItem.json);
for (const key of keys) {
if (newItem.json[key] === '') {
newItem.json[key] = null;
}
}
return newItem;
});
return returnData;
}
export function prepareItem(values: IDataObject[]) {
const item = values.reduce((acc, { column, value }) => {
acc[column as string] = value;
return acc;
}, {} as IDataObject);
return item;
}
export async function columnFeatureSupport(
db: PgpDatabase,
): Promise<{ identity_generation: boolean; is_generated: boolean }> {
const result = await db.any(
`SELECT EXISTS (
SELECT 1 FROM information_schema.columns WHERE table_name = 'columns' AND table_schema = 'information_schema' AND column_name = 'is_generated'
) as is_generated,
EXISTS (
SELECT 1 FROM information_schema.columns WHERE table_name = 'columns' AND table_schema = 'information_schema' AND column_name = 'identity_generation'
) as identity_generation;`,
);
return result[0];
}
export async function getTableSchema(
db: PgpDatabase,
schema: string,
table: string,
options?: { getColumnsForResourceMapper?: boolean },
): Promise<ColumnInfo[]> {
const select = ['column_name', 'data_type', 'is_nullable', 'udt_name', 'column_default'];
if (options?.getColumnsForResourceMapper) {
// Check if columns exist before querying (identity_generation was added in v10, is_generated in v12)
const supported = await columnFeatureSupport(db);
if (supported.identity_generation) {
select.push('identity_generation');
}
if (supported.is_generated) {
select.push('is_generated');
}
}
const selectString = select.join(', ');
const columns = await db.any(
`SELECT ${selectString} FROM information_schema.columns WHERE table_schema = $1 AND table_name = $2`,
[schema, table],
);
return columns;
}
export async function uniqueColumns(db: PgpDatabase, table: string, schema = 'public') {
// Using the modified query from https://wiki.postgresql.org/wiki/Retrieve_primary_key_columns
// `quote_ident` - properly quote and escape an identifier
// `::regclass` - cast a string to a regclass (internal type for object names)
const unique = await db.any(
`
SELECT DISTINCT a.attname
FROM pg_index i JOIN pg_attribute a ON a.attrelid = i.indrelid AND a.attnum = ANY(i.indkey)
WHERE i.indrelid = (quote_ident($1) || '.' || quote_ident($2))::regclass
AND (i.indisprimary OR i.indisunique);
`,
[schema, table],
);
return unique as IDataObject[];
}
export async function getEnums(db: PgpDatabase): Promise<EnumInfo[]> {
const enumsData = await db.any(
'SELECT pg_type.typname, pg_enum.enumlabel FROM pg_type JOIN pg_enum ON pg_enum.enumtypid = pg_type.oid;',
);
return enumsData as EnumInfo[];
}
export function getEnumValues(enumInfo: EnumInfo[], enumName: string): INodePropertyOptions[] {
return enumInfo.reduce((acc, current) => {
if (current.typname === enumName) {
acc.push({ name: current.enumlabel, value: current.enumlabel });
}
return acc;
}, [] as INodePropertyOptions[]);
}
export async function doesRowExist(
db: PgpDatabase,
schema: string,
table: string,
values: string[],
): Promise<boolean> {
const where = [];
for (let i = 3; i < 3 + values.length; i += 2) {
where.push(`$${i}:name=$${i + 1}`);
}
const exists = await db.any(
`SELECT EXISTS(SELECT 1 FROM $1:name.$2:name WHERE ${where.join(' AND ')})`,
[schema, table, ...values],
);
return exists[0].exists;
}
export function checkItemAgainstSchema(
node: INode,
item: IDataObject,
columnsInfo: ColumnInfo[],
index: number,
) {
if (columnsInfo.length === 0) return item;
const schema = columnsInfo.reduce((acc, { column_name, data_type, is_nullable }) => {
acc[column_name] = { type: data_type.toUpperCase(), nullable: is_nullable === 'YES' };
return acc;
}, {} as IDataObject);
for (const key of Object.keys(item)) {
if (schema[key] === undefined) {
throw new NodeOperationError(node, `Column '${key}' does not exist in selected table`, {
itemIndex: index,
});
}
if (item[key] === null && !(schema[key] as IDataObject)?.nullable) {
throw new NodeOperationError(node, `Column '${key}' is not nullable`, {
itemIndex: index,
});
}
}
return item;
}
export const configureTableSchemaUpdater = (initialSchema: string, initialTable: string) => {
let currentSchema = initialSchema;
let currentTable = initialTable;
return async (db: PgpDatabase, tableSchema: ColumnInfo[], schema: string, table: string) => {
if (currentSchema !== schema || currentTable !== table) {
currentSchema = schema;
currentTable = table;
tableSchema = await getTableSchema(db, schema, table);
}
return tableSchema;
};
};
/**
* If postgress column type is array we need to convert it to fornmat that postgres understands, original object data would be modified
* @param data the object with keys representing column names and values
* @param schema table schema
* @param node INode
* @param itemIndex the index of the current item
*/
export const convertArraysToPostgresFormat = (
data: IDataObject,
schema: ColumnInfo[],
node: INode,
itemIndex = 0,
) => {
for (const columnInfo of schema) {
//in case column type is array we need to convert it to fornmat that postgres understands
if (columnInfo.data_type.toUpperCase() === 'ARRAY') {
let columnValue = data[columnInfo.column_name];
if (typeof columnValue === 'string') {
columnValue = jsonParse(columnValue);
}
if (Array.isArray(columnValue)) {
const arrayEntries = columnValue.map((entry) => {
if (typeof entry === 'number') {
return entry;
}
if (typeof entry === 'boolean') {
entry = String(entry);
}
if (typeof entry === 'object') {
entry = JSON.stringify(entry);
}
if (typeof entry === 'string') {
return `"${entry.replace(/"/g, '\\"')}"`; //escape double quotes
}
return entry;
});
//wrap in {} instead of [] as postgres does and join with ,
data[columnInfo.column_name] = `{${arrayEntries.join(',')}}`;
} else {
if (columnInfo.is_nullable === 'NO') {
throw new NodeOperationError(
node,
`Column '${columnInfo.column_name}' has to be an array`,
{
itemIndex,
},
);
}
}
}
}
};