ToolJet/plugins/packages/mysql/lib/index.ts
Maurits Lourens 60f515d19c
Feature/2395 - add eslint to plugins (#2402)
* merge develop

* Add eslint dependencies, configs and scripts to plugins project

* run lint with Github action

* ignore tests and dist folders

* fun eslint with --fix and manual fixes, renamed __tests_ to __tests__

* add plugins packages folder to lint-staged config

* fix lint issue
2022-03-10 12:29:48 +05:30

129 lines
3.4 KiB
TypeScript

import { Knex, knex } from 'knex';
import {
cacheConnection,
getCachedConnection,
ConnectionTestResult,
QueryService,
QueryResult,
} from '@tooljet-plugins/common';
import { SourceOptions, QueryOptions } from './types';
export default class MysqlQueryService implements QueryService {
private static _instance: MysqlQueryService;
constructor() {
if (MysqlQueryService._instance) {
return MysqlQueryService._instance;
}
MysqlQueryService._instance = this;
return MysqlQueryService._instance;
}
async run(
sourceOptions: SourceOptions,
queryOptions: QueryOptions,
dataSourceId: string,
dataSourceUpdatedAt: string
): Promise<QueryResult> {
let result = {
rows: [],
};
let query = '';
if (queryOptions.mode === 'gui') {
if (queryOptions.operation === 'bulk_update_pkey') {
query = await this.buildBulkUpdateQuery(queryOptions);
}
} else {
query = queryOptions.query;
}
const knexInstance = await this.getConnection(sourceOptions, {}, true, dataSourceId, dataSourceUpdatedAt);
try {
result = await knexInstance.raw(query);
} catch (err) {
console.log(err);
}
return {
status: 'ok',
data: result[0],
};
}
async testConnection(sourceOptions: SourceOptions): Promise<ConnectionTestResult> {
const knexInstance = await this.getConnection(sourceOptions, {}, false);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const result = await knexInstance.raw('select @@version;');
return {
status: 'ok',
};
}
async buildConnection(sourceOptions: SourceOptions) {
const config: Knex.Config = {
client: 'mysql',
connection: {
host: sourceOptions.host,
user: sourceOptions.username,
password: sourceOptions.password,
database: sourceOptions.database,
port: +sourceOptions.port,
multipleStatements: true,
ssl: sourceOptions.ssl_enabled ?? false, // Disabling by default for backward compatibility
},
};
return knex(config);
}
async getConnection(
sourceOptions: SourceOptions,
options: any,
checkCache: boolean,
dataSourceId?: string,
dataSourceUpdatedAt?: string
): Promise<any> {
if (checkCache) {
let connection = await getCachedConnection(dataSourceId, dataSourceUpdatedAt);
if (connection) {
return connection;
} else {
connection = await this.buildConnection(sourceOptions);
dataSourceId && cacheConnection(dataSourceId, connection);
return connection;
}
} else {
return await this.buildConnection(sourceOptions);
}
}
async buildBulkUpdateQuery(queryOptions: any): Promise<string> {
let queryText = '';
const tableName = queryOptions['table'];
const primaryKey = queryOptions['primary_key_column'];
const records = queryOptions['records'];
for (const record of records) {
const primaryKeyValue = typeof record[primaryKey] === 'string' ? `'${record[primaryKey]}'` : record[primaryKey];
queryText = `${queryText} UPDATE ${tableName} SET`;
for (const key of Object.keys(record)) {
if (key !== primaryKey) {
queryText = ` ${queryText} ${key} = '${record[key]}',`;
}
}
queryText = queryText.slice(0, -1);
queryText = `${queryText} WHERE ${primaryKey} = ${primaryKeyValue};`;
}
return queryText.trim();
}
}