-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.js
317 lines (283 loc) · 9.2 KB
/
main.js
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
'use strict';
// Import parts of electron to use
const { app, BrowserWindow, ipcMain } = require('electron');
const path = require('path');
const url = require('url');
const os = require('os');
const {
getAllDbs,
getAllTables,
getTableData,
updateTableData,
createTable,
removeTableRow,
deleteTable,
createDatabase,
setUserProvidedDbConnection,
deleteDatabase,
} = require('./src/db/db');
const express = require('express');
const { postgraphile } = require('postgraphile');
// need below for visualizer
const { express: voyagerMiddleware } = require('graphql-voyager/middleware');
const { closeServer } = require('./src/server/util');
const {
LOGIN_FORM_DATA,
GET_DB_NAMES,
GET_DB_NAMES_REPLY,
GET_TABLE_NAMES,
GET_TABLE_NAMES_REPLY,
GET_TABLE_CONTENTS,
GET_TABLE_CONTENTS_REPLY,
CLOSE_SERVER,
UPDATE_TABLE_DATA,
CREATE_TABLE,
CREATE_TABLE_REPLY,
REMOVE_TABLE_ROW,
ADD_TABLE_ROW,
GET_OS_USER,
GET_OS_USER_REPLY,
CREATE_DATABASE,
CREATE_DATABASE_REPLY,
DELETE_TABLE,
DELETE_TABLE_REPLY,
SET_USER_DB_CONNECTION,
DELETE_DATABASE,
DELETE_DATABASE_REPLY,
DATABASE_ERROR,
} = require('./src/constants/ipcNames');
const enableDestroy = require('server-destroy');
// Keep a global reference of the window object, if you don't, the window will
// be closed automatically when the JavaScript object is garbage collected.
let mainWindow;
let LOGGEDIN_USER = '';
let expressApp;
let expressServer;
let PORT_INCREMENT = 0;
/* Setup express server when user clicks a db in the front end */
function setupExpress(databaseName, username = '', password) {
/* Major key is just overwriting existing express app.
This was the solution */
expressApp = express();
// config to connect middleware to database
const schemaName = 'public';
const database = `postgres://${username}:${
password ? `${password}` : ''
}@localhost:5432/${databaseName}`;
const pglConfig = {
watchPg: true,
graphiql: true,
enhanceGraphiql: true,
};
// console.log(database);
// setup middleware for creating our graphql api
expressApp.use(postgraphile(database, schemaName, pglConfig));
// route for visualizer - access via http://localhost:5000/voyager
expressApp.use('/voyager', voyagerMiddleware({ endpointUrl: '/graphql' }));
// assign global var our express server so we can close it later
expressServer = expressApp.listen(5000, function() {
console.log('Listening :)');
// expressServer.close()
});
// enhance with a 'destroy' function
enableDestroy(expressServer);
// expressApp.listen(5000);
}
// Keep a reference for dev mode
let dev = false;
if (
process.defaultApp ||
/[\\/]electron-prebuilt[\\/]/.test(process.execPath) ||
/[\\/]electron[\\/]/.test(process.execPath)
) {
dev = true;
}
// Temporary fix broken high-dpi scale factor on Windows (125% scaling)
// info: https://github.com/electron/electron/issues/9691
if (process.platform === 'win32') {
app.commandLine.appendSwitch('high-dpi-support', 'true');
app.commandLine.appendSwitch('force-device-scale-factor', '1');
}
function createWindow() {
// Create the browser window.
mainWindow = new BrowserWindow({
width: 1024,
height: 768,
show: false,
webPreferences: {
nodeIntegration: true,
},
});
// and load the index.html of the app.
let indexPath;
if (dev && process.argv.indexOf('--noDevServer') === -1) {
indexPath = url.format({
protocol: 'http:',
host: `localhost:${process.env.PORT || 9000}`,
pathname: 'index.html',
slashes: true,
});
} else {
indexPath = url.format({
protocol: 'file:',
pathname: path.join(__dirname, 'dist', 'index.html'),
slashes: true,
});
}
mainWindow.loadURL(indexPath);
// Don't show until we are ready and loaded
mainWindow.once('ready-to-show', () => {
mainWindow.show();
// Open the DevTools automatically if developing
if (dev) {
mainWindow.webContents.openDevTools();
}
});
// Emitted when the window is closed.
mainWindow.on('closed', function() {
// Dereference the window object, usually you would store windows
// in an array if your app supports multi windows, this is the time
// when you should delete the corresponding element.
mainWindow = null;
});
}
/*
* ipcMain
* allows us to safely listen for and send communications to front end
*/
/*
* called from below components to send connection data from connection forms
* ./containers/EditExistingConnection.js
* ./containers/CreateConnection.js
*/
ipcMain.on(LOGIN_FORM_DATA, (_, formData) => {
// console.log('arg in login-form-data', arg); // prints values from form
const { user } = formData; // take value from form
// added to global var so we can use for db connection
LOGGEDIN_USER = user;
});
ipcMain.on(GET_OS_USER, event => {
const { username } = os.userInfo();
event.reply(GET_OS_USER_REPLY, username);
});
/**
* called from ./components/reuse/Header.js
* when user clicks refresh icon, header sends message to trigger
* call to get all the db names and replies with the database names
*/
ipcMain.on(GET_DB_NAMES, async event => {
const dbNames = await getAllDbs();
// reply with database names from query
event.reply(GET_DB_NAMES_REPLY, dbNames);
});
/**
* called from ./components/db/AllDBs.js
* when user clicks database, sends message to trigger creating a db
* and replies with updated array of all db names after dletion
*/
ipcMain.on(CREATE_DATABASE, async (event, databaseName) => {
const existingDatabases = await createDatabase(databaseName);
// reply with database names from query
event.reply(CREATE_DATABASE_REPLY, existingDatabases);
});
/**
* called from ./components/db/AllDBs.js
* when user clicks database, sends message to trigger delete a db
* and replies with updated array of all db names after dletion
*/
ipcMain.on(DELETE_DATABASE, async (event, databaseName) => {
const existingDatabases = await deleteDatabase(databaseName);
// reply with database names from query
event.reply(DELETE_DATABASE_REPLY, existingDatabases);
});
/**
* called from ./components/db/AllDBs.js
* when user clicks database, sends message to trigger getting the table data
* call to get all the table names and replies with the tableNames
*/
ipcMain.on(GET_TABLE_NAMES, async (event, dbname) => {
// when it's not just us testing, we should pass in LOGGEDIN_USER
setupExpress(dbname);
const tableNames = await getAllTables(dbname);
event.reply(GET_TABLE_NAMES_REPLY, tableNames);
});
/**
* called from ./components/db/AllTables.js
* when user clicks specific table, we recieve call
* get all the table data and replies with the table data
*/
ipcMain.on(GET_TABLE_CONTENTS, async (event, args) => {
// args === (table, selectedDb)
const tableData = await getTableData(...args);
event.reply(GET_TABLE_CONTENTS_REPLY, tableData);
});
/**
* called from ./components/reuse/Header.js
* when user qlStico icon, header sends message to trigger stopping the server
* from rec new connections
* call to get all the db names and replies with the database names
*/
ipcMain.on(CLOSE_SERVER, async (event, args) => {
closeServer(expressServer, 'closeserver*****');
});
/**
* called from ./components/db/IndivTable.js
* when the user submits the changes to the table
*/
// args === [selectedTable,selectedDb,tableMatrix]
ipcMain.on(UPDATE_TABLE_DATA, async (event, args) => {
const response = await updateTableData(...args);
typeof response === 'string' && event.reply(DATABASE_ERROR, response);
});
/**
* called from ./components/db/allTables.js
* when the user submits request for a new table
*/
// args === [selectedDb, newTableName]
ipcMain.on(CREATE_TABLE, async (event, args) => {
const newTableAddition = await createTable(...args);
event.reply(CREATE_TABLE_REPLY, newTableAddition);
});
/**
* called from ./components/db/allTables.js
* when the user submits request for deleting table
*/
// args === [selectedDb, selectedTableName]
ipcMain.on(DELETE_TABLE, async (event, args) => {
const tablesAfterDeletion = await deleteTable(...args);
event.reply(DELETE_TABLE_REPLY, tablesAfterDeletion);
});
/**
* called from ./components/db/IndivTable.js
* when the user submits request for deleting a row in the grid
*/
// args === [selectedTable, selectedDb, selectedRowId]
ipcMain.on(REMOVE_TABLE_ROW, async (_, args) => {
await removeTableRow(...args);
});
/**
* called from ./components/db/ConnectPage.js
* when the user submits request for login with a specific connection tile
*/
ipcMain.on(SET_USER_DB_CONNECTION, async (_, userConfig) => {
await setUserProvidedDbConnection(userConfig);
});
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', () => {
// On macOS it is common for applications and their menu bar
// to stay active until the user quits explicitly with Cmd + Q
if (process.platform !== 'darwin') {
app.quit();
}
});
app.on('activate', () => {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});