MySQL and PostgreSQL integration in Electron application.
You can use https://github.com/Alex1998100/VbNetSupport/blob/main/Func/mysqlpool.js and https://github.com/Alex1998100/VbNetSupport/blob/main/Func/postgrespool.js functions (which use connection pools) within your Electron application. Node.js libraries like mysql and pg work perfectly fine in Electron's main process. You'll just need to handle a few things specific to the Electron environment:
- 1. Context Isolation (Recommended): For security and better separation of concerns, it's highly recommended to enable context isolation in your Electron app. This prevents direct access to Node.js APIs from your renderer processes (where your UI runs). Communicate between the main and renderer processes using Electron's IPC (inter-process communication) mechanisms.
- 2. IPC Communication: If you're using context isolation (and you should be), you'll need to use IPC to communicate between your renderer process (where your UI likely interacts with the database) and the main process (where you'll run your database functions).
- 3. Module Structure: Organize your code to keep database interactions within the main process. Here’s a general structure:
So, main.js (Electron Main Process):
1: const { app, BrowserWindow, ipcMain } = require('electron');
2: import { callMySQLProcedure, executeMySQLQuery, pool as mysqlPool } from "./Func/mysqlpool.js";
3: import { pgPoolProcedure, pgPoolQuery, pool as pgPool } from "./Func/postgrespool.js"; // Import pgPool
4:
5: // ... (Electron window setup code) ...
6:
7: // Listen for asynchronous messages from the renderer process
8: ipcMain.handle('get-mysql-data', async (event, query, values) => { // Example: get-mysql-data channel
9: try {
10: return await executeMySQLQuery(query, values); // Call your MySQL function
11: } catch (error) {
12: console.error("Error in get-mysql-data:", error);
13: throw error; // Re-throw to propagate the error to the renderer process
14: }
15: });
16:
17: ipcMain.handle('call-pg-procedure', async (event, procedureName, parameters) => { // Example
18: try {
19: return await pgPoolProcedure(procedureName, parameters);
20: } catch (error) {
21: console.error("Error in call-pg-procedure:", error);
22: throw error;
23: }
24: });
25:
26: // ... (Other IPC handlers for database operations)
27:
28: // Close pools when the application exits:
29: app.on('window-all-closed', async () => {
30: try {
31: await mysqlPool.end();
32: console.log("MySQL pool closed");
33: await pgPool.end();
34: console.log("PostgreSQL pool closed");
35: } catch (err) {
36: console.error("Error closing pools:", err);
37: }
38: if (process.platform !== 'darwin') app.quit();
39: });
renderer.js (Electron Renderer Process):
1: // ... other imports ...
2:
3: // Send an asynchronous message to the main process
4: async function getMySQLData(query, values) {
5: try {
6: const result = await window.electronAPI.getMySQLData(query, values); // Use the same channel name
7: // ... process the result ...
8: console.log(result)
9: return result
10: } catch(err) {
11: console.error("Error in renderer getMySQLData:", err)
12: }
13: }
14:
15: async function callPostgreSQLProcedure(procedureName, parameters) {
16: try {
17: const result = await window.electronAPI.callPostgreSQLProcedure(procedureName, parameters);
18: return result; // ... process the result
19: } catch(err) {
20: console.error("Error in renderer process:", err);
21: }
22: }
23:
24: // ... use the functions in your UI logic ...
25: getMySQLData('SELECT * from vbnet.entrance', []).then((r)=>{
26: console.log(r)
27: })
28:
29: callPostgreSQLProcedure('your_procedure', ['your_parameter']).then((r)=>{
30: console.log(r)
31: })
32: preload.js (Bridge between Main and Renderer):
33: // Expose ipcRenderer to the renderer process
34: const { contextBridge, ipcRenderer } = require('electron');
35:
36: contextBridge.exposeInMainWorld('electronAPI', {
37: getMySQLData: (query, values) => ipcRenderer.invoke('get-mysql-data', query, values),
38: callPostgreSQLProcedure: (procedureName, parameters) => ipcRenderer.invoke('call-pg-procedure', procedureName, parameters),
39: // ... other exposed methods for database operations
40: });
Electron context:
AngularElectron context:
Comments (
)
)
Link to this page:
http://www.vb-net.com/AngularElectron/MySQLPostgreSQL.htm
|
|