My project is a medium sized Electron app, and I use promise-mysql to reach the back end. While there are several good examples covering how to connect to MySQL in Electron, none I have found are using a connection pool.
The database server I am using is very small (a NUC), and so I wish to use a pool of connections to keep a lid on things. It seems to me that the natural place to instantiate the connection pool would be in Electron's main.js file just before the app launches the window. This is so that all the renderers can get to it. To get and release connections I wrote ipc handlers like this:
// MySQL setup
const ipc = require('electron').ipcMain;
var mysql = require('promise-mysql');
var mysqlParms = require("./mysqlConfig.json");
let cfMySQLPool = mysql.createPool({
host: mysqlParms.host,
user: mysqlParms.user,
password: mysqlParms.password,
database: mysqlParms.database,
multipleStatements: true,
connectionLimit: 4 // NUC is a small box
});
ipc.on('getCFConnection', function (event, arg) {
event.sender.send('cfConnection', cfMySQLPool.getConnection()); // send a connection Promise
})
ipc.on('releaseCFConnection', function (event, cfConnection) {
cfMySQLPool.releaseConnection(cfConnection); // release the connection from the pool
})
However, this fails. The renderers do not receive the Promise from getConnection(), and I think I found out why. When event.sender.send (which is webcontents.send) passes the argument, the docs state:
"Arguments will be serialized in JSON internally and hence no functions or prototype chain will be included."
So it appears the ability to .then() the getConnection promise in the renderer is stripped away by JSON serialization.
Does anyone have any solutions to offer on implementing the promise-mysql connection pool in Electron so that any renderer can call getConnection / releaseConnection on it? Thank you.
via Geek Stocks
No comments:
Post a Comment