mirror of
https://github.com/n8n-io/n8n.git
synced 2025-02-21 02:56:40 -08:00
feat(node): remove knex dependency from MySQL node
This commit is contained in:
parent
c34f4841c2
commit
a78da100f8
|
@ -5,7 +5,8 @@ import {
|
||||||
INodeType,
|
INodeType,
|
||||||
INodeTypeDescription,
|
INodeTypeDescription,
|
||||||
} from 'n8n-workflow';
|
} from 'n8n-workflow';
|
||||||
import * as knex from 'knex';
|
// @ts-ignore
|
||||||
|
import * as mysql2 from 'mysql2/promise';
|
||||||
|
|
||||||
import { copyInputItems } from './GenericFunctions';
|
import { copyInputItems } from './GenericFunctions';
|
||||||
|
|
||||||
|
@ -174,7 +175,7 @@ export class MySQL implements INodeType {
|
||||||
throw new Error('No credentials got returned!');
|
throw new Error('No credentials got returned!');
|
||||||
}
|
}
|
||||||
|
|
||||||
const client = knex({ client: 'mysql2', connection: credentials });
|
const connection = await mysql2.createConnection(credentials);
|
||||||
const items = this.getInputData();
|
const items = this.getInputData();
|
||||||
const operation = this.getNodeParameter('operation', 0) as string;
|
const operation = this.getNodeParameter('operation', 0) as string;
|
||||||
let returnItems = [];
|
let returnItems = [];
|
||||||
|
@ -187,18 +188,20 @@ export class MySQL implements INodeType {
|
||||||
const queryQueue = items.map((item, index) => {
|
const queryQueue = items.map((item, index) => {
|
||||||
const rawQuery = this.getNodeParameter('query', index) as string;
|
const rawQuery = this.getNodeParameter('query', index) as string;
|
||||||
|
|
||||||
return client.raw(rawQuery);
|
return connection.query(rawQuery);
|
||||||
});
|
});
|
||||||
let queryResult = await Promise.all(queryQueue);
|
let queryResult = await Promise.all(queryQueue);
|
||||||
|
|
||||||
queryResult = queryResult.reduce((result, current) => {
|
queryResult = queryResult.reduce((collection, result) => {
|
||||||
if (Array.isArray(current[0])) {
|
const [rows, fields] = result;
|
||||||
return result.concat(current[0]);
|
|
||||||
|
if (Array.isArray(rows)) {
|
||||||
|
return collection.concat(rows);
|
||||||
}
|
}
|
||||||
|
|
||||||
result.push(current[0]);
|
collection.push(rows);
|
||||||
|
|
||||||
return result;
|
return collection;
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
|
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
|
||||||
|
@ -212,9 +215,12 @@ export class MySQL implements INodeType {
|
||||||
const columnString = this.getNodeParameter('columns', 0) as string;
|
const columnString = this.getNodeParameter('columns', 0) as string;
|
||||||
const columns = columnString.split(',').map(column => column.trim());
|
const columns = columnString.split(',').map(column => column.trim());
|
||||||
const insertItems = copyInputItems(items, columns);
|
const insertItems = copyInputItems(items, columns);
|
||||||
const insertData = await client.insert(insertItems).into(table);
|
const insertPlaceholder = `(${columns.map(column => '?').join(',')})`;
|
||||||
|
const insertSQL = `INSERT INTO ${table}(${columnString}) VALUES ${items.map(item => insertPlaceholder).join(',')};`;
|
||||||
|
const queryItems = insertItems.reduce((collection, item) => collection.concat(Object.values(item as any)), []);
|
||||||
|
const queryResult = await connection.query(insertSQL, queryItems);
|
||||||
|
|
||||||
returnItems = [{ json: { row_count: insertData[0] }}];
|
returnItems = this.helpers.returnJsonArray(queryResult[0] as IDataObject);
|
||||||
|
|
||||||
} else if (operation === 'update') {
|
} else if (operation === 'update') {
|
||||||
// ----------------------------------
|
// ----------------------------------
|
||||||
|
@ -231,15 +237,12 @@ export class MySQL implements INodeType {
|
||||||
}
|
}
|
||||||
|
|
||||||
const updateItems = copyInputItems(items, columns);
|
const updateItems = copyInputItems(items, columns);
|
||||||
const queryQueue = updateItems.map((item) => {
|
const updateSQL = `UPDATE ${table} SET ${columns.map(column => `${column} = ?`).join(',')} WHERE ${updateKey} = ?;`;
|
||||||
return client(table)
|
const queryQueue = updateItems.map((item) => connection.query(updateSQL, Object.values(item).concat(item[updateKey])));
|
||||||
.where(updateKey, '=', item[updateKey])
|
let queryResult = await Promise.all(queryQueue);
|
||||||
.update(item);
|
|
||||||
});
|
|
||||||
|
|
||||||
await Promise.all(queryQueue);
|
queryResult = queryResult.map(result => result[0]);
|
||||||
|
returnItems = this.helpers.returnJsonArray(queryResult as IDataObject[]);
|
||||||
returnItems = this.helpers.returnJsonArray(updateItems as IDataObject[]);
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
throw new Error(`The operation "${operation}" is not supported!`);
|
throw new Error(`The operation "${operation}" is not supported!`);
|
||||||
|
|
|
@ -187,7 +187,6 @@
|
||||||
"gm": "^1.23.1",
|
"gm": "^1.23.1",
|
||||||
"googleapis": "^42.0.0",
|
"googleapis": "^42.0.0",
|
||||||
"imap-simple": "^4.3.0",
|
"imap-simple": "^4.3.0",
|
||||||
"knex": "^0.20.1",
|
|
||||||
"lodash.get": "^4.4.2",
|
"lodash.get": "^4.4.2",
|
||||||
"lodash.set": "^4.3.2",
|
"lodash.set": "^4.3.2",
|
||||||
"lodash.unset": "^4.5.2",
|
"lodash.unset": "^4.5.2",
|
||||||
|
|
Loading…
Reference in a new issue