feat: ipc event channel to send logs to renderer

This commit is contained in:
Fabio Di Stasio 2022-07-15 09:53:48 +02:00
parent abf829867e
commit f12a04b052
7 changed files with 17 additions and 5 deletions

View File

@ -25,6 +25,7 @@ export interface IpcResponse<T = any> {
*/
export interface ClientParams {
client: ClientCode;
uid: string;
params:
mysql.ConnectionOptions & {schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean}
| pg.ClientConfig & {schema: string; ssl?: mysql.SslOptions; ssh?: SSHConfig; readonly: boolean}

View File

@ -55,6 +55,7 @@ export default (connections: {[key: string]: antares.Client}) => {
try {
const connection = await ClientsFactory.getClient({
uid: conn.uid,
client: conn.client,
params
});
@ -128,6 +129,7 @@ export default (connections: {[key: string]: antares.Client}) => {
try {
const connection = ClientsFactory.getClient({
uid: conn.uid,
client: conn.client,
params,
poolSize: 5

View File

@ -1,11 +1,14 @@
import * as antares from 'common/interfaces/antares';
import { webContents } from 'electron';
import mysql from 'mysql2/promise';
import * as pg from 'pg';
import SSH2Promise from 'ssh2-promise';
const queryLogger = (sql: string) => {
const queryLogger = ({ sql, cUid }: {sql: string; cUid: string}) => {
// Remove comments, newlines and multiple spaces
const escapedSql = sql.replace(/(\/\*(.|[\r\n])*?\*\/)|(--(.*|[\r\n]))/gm, '').replace(/\s\s+/g, ' ');
const mainWindow = webContents.fromId(1);
mainWindow.send('query-log', { cUid, sql: escapedSql });
console.log(escapedSql);
};
@ -14,15 +17,17 @@ const queryLogger = (sql: string) => {
*/
export class AntaresCore {
_client: antares.ClientCode;
protected _cUid: string
protected _params: mysql.ConnectionOptions | pg.ClientConfig | { databasePath: string; readonly: boolean};
protected _poolSize: number;
protected _ssh?: SSH2Promise;
protected _logger: (sql: string) => void;
protected _logger: (args: {sql: string; cUid: string}) => void;
protected _queryDefaults: antares.QueryBuilderObject;
protected _query: antares.QueryBuilderObject;
constructor (args: antares.ClientParams) {
this._client = args.client;
this._cUid = args.uid;
this._params = args.params;
this._poolSize = args.poolSize || undefined;
this._logger = args.logger || queryLogger;

View File

@ -1536,7 +1536,7 @@ export class MySQLClient extends AntaresCore {
}
async raw<T = antares.QueryResult> (sql: string, args?: antares.QueryParams) {
if (process.env.NODE_ENV === 'development') this._logger(sql);
if (process.env.NODE_ENV === 'development') this._logger({ cUid: this._cUid, sql });
args = {
nest: false,

View File

@ -1314,7 +1314,7 @@ export class PostgreSQLClient extends AntaresCore {
}
async raw<T = antares.QueryResult> (sql: string, args?: antares.QueryParams) {
if (process.env.NODE_ENV === 'development') this._logger(sql);
if (process.env.NODE_ENV === 'development') this._logger({ cUid: this._cUid, sql });
args = {
nest: false,

View File

@ -586,7 +586,7 @@ export class SQLiteClient extends AntaresCore {
}
async raw<T = antares.QueryResult> (sql: string, args?: antares.QueryParams) {
if (process.env.NODE_ENV === 'development') this._logger(sql);// TODO: replace BLOB content with a placeholder
if (process.env.NODE_ENV === 'development') this._logger({ cUid: this._cUid, sql });// TODO: replace BLOB content with a placeholder
args = {
nest: false,

View File

@ -71,6 +71,10 @@ ipcRenderer.on('open-connections-modal', () => {
isAllConnectionsModal.value = true;
});
ipcRenderer.on('query-log', (e, sql: string) => {
console.log(sql);
});
document.addEventListener('DOMContentLoaded', () => {
setTimeout(() => {
changeApplicationTheme(applicationTheme.value);// Forces persistentStore to save on file and mail process