11
11
import 'core-js/stable' ;
12
12
import 'regenerator-runtime/runtime' ;
13
13
import path from 'path' ;
14
- import { app , BrowserWindow , shell , ipcMain , screen } from 'electron' ;
14
+ import {
15
+ app ,
16
+ BrowserWindow ,
17
+ shell ,
18
+ ipcMain ,
19
+ screen ,
20
+ dialog ,
21
+ IpcMainEvent ,
22
+ } from 'electron' ;
15
23
import { autoUpdater } from 'electron-updater' ;
16
24
import log from 'electron-log' ;
17
- import { connectDB , sqlExecute } from './electron/DBHandler' ;
18
-
19
- import MenuBuilder from './menu' ;
20
- import { IpcMainEvent } from 'electron/main' ;
21
- import { dialog } from 'electron' ;
22
25
import fs from 'fs' ;
26
+ import MenuBuilder from './menu' ;
27
+ import { connectDB , disconnectDB , sqlExecute } from './electron/DBHandler' ;
28
+
23
29
const { format } = require ( '@fast-csv/format' ) ;
24
30
25
31
export default class AppUpdater {
@@ -160,7 +166,7 @@ app.on('activate', () => {
160
166
if ( mainWindow === null ) createWindow ( ) ;
161
167
} ) ;
162
168
163
- ipcMain . on ( 'ExportCSV' , ( _ : IpcMainEvent , params : any ) => {
169
+ ipcMain . on ( 'ExportCSV' , ( _ : IpcMainEvent , params ) => {
164
170
const { fileName, data } = params ;
165
171
166
172
dialog
@@ -172,27 +178,29 @@ ipcMain.on('ExportCSV', (_: IpcMainEvent, params: any) => {
172
178
filters : [ { name : 'CSV files' , extensions : [ 'csv' ] } ] ,
173
179
properties : [ ] ,
174
180
} )
175
- . then ( ( file : any ) => {
181
+ . then ( ( file : Electron . SaveDialogReturnValue ) => {
176
182
// Stating whether dialog operation was cancelled or not.
177
183
console . log ( file . canceled ) ;
178
184
if ( ! file . canceled ) {
179
- let items : unknown [ ] = [ ] ;
185
+ const items : unknown [ ] = [ ] ;
180
186
181
187
const stream = format ( { headers : true } ) ;
182
- const fileName = file . filePath . toString ( ) ;
183
- const csvFile = fs . createWriteStream ( fileName ) ;
188
+ const filePath = file ? .filePath ? .toString ( ) ?? '/Downloads' ;
189
+ const csvFile = fs . createWriteStream ( filePath ) ;
184
190
stream . pipe ( csvFile ) ;
185
- data . forEach ( ( el : Record < string , any > , i : number ) => {
186
- const parsed : Record < string , any > = { } ;
187
- Object . entries ( el ) . forEach ( ( [ key , value ] : [ any , any ] ) => {
191
+ data . forEach ( ( el : Record < string , unknown > , i : number ) => {
192
+ const parsed : Record < string , unknown > = { } ;
193
+ Object . entries ( el ) . forEach ( ( [ key , value ] : [ string , unknown ] ) => {
188
194
parsed [ key ] =
189
195
typeof value === 'object' ? JSON . stringify ( value ) : value ;
190
196
} ) ;
191
197
items . push ( parsed ) ;
192
198
stream . write ( items [ i ] ) ;
193
199
} ) ;
194
200
stream . end ( ) ;
201
+ return 1 ;
195
202
}
203
+ return 0 ;
196
204
} )
197
205
. catch ( ( err ) => {
198
206
console . log ( err ) ;
@@ -209,6 +217,13 @@ ipcMain.handle('connect', async (_, params) => {
209
217
return res ;
210
218
} ) ;
211
219
220
+ ipcMain . handle ( 'disconnect' , async ( ) => {
221
+ console . log ( 'disconnect' ) ;
222
+ // console.log(JSON.stringify({ params }, null, 2));
223
+ const res = await disconnectDB ( ) ;
224
+ return res ;
225
+ } ) ;
226
+
212
227
ipcMain . handle ( 'SQL_EXECUTE' , sqlExecute ) ;
213
228
214
229
// Define custom protocol handler.
0 commit comments