Я пытаюсь получить доступ к базе данных SQL Server в приложении, используя Vue + Electron. Рекомендуется ли использовать данные ipcMain / ipcRender из Electron в Vue?
Стек: Электрон, Vue, Vuex, Vue Router
Пакет SQL: https://www.npmjs.com/package/mssql
store / modules / sqlmodule.js :
async function resolveQuery(requestHandle, requestParameters) {
let callbackID = new Date().getUTCMilliseconds() + '_mssql';
return new Promise((resolve) => {
//Make a request from the renderer process to the main process.
window.ipcRenderer.send(requestHandle, callbackID, requestParameters);
//Attach a one time listener for the callback
window.ipcRenderer.once(callbackID, (event, result) => {
if (result.hasError) {
resolve({
hasError : true,
error : result.err,
data : null
});
} else {
resolve({
hasError : false,
error : null,
data : result.data.recordset
});
}
});
}).catch ((error) => {
window.console.log(error);
});
}
const actions = {
async getQueryResult({ commit }, id) { // eslint-disable-line no-unused-vars
let queryParams = {
id : id,
};
const queryResult = await resolveQuery('mssql:getQueryResult', queryParams);
if (queryResult.hasError) {
window.console.log('Database Error:', queryResult.error);
} else {
window.console.log('result', queryResult.data);
}
}
};
background.js :
ipcMain.on('mssql:getQueryResult', async function (event, callbackID, queryParams) {
console.log('A request was made to ipcMain.', queryParams);
const commandSyntax = "SELECT * FROM TABLE WHERE id= @id";
await connectionPool.request()
.input('id', mssql.VarChar, queryParams.id)
.query(commandSyntax)
.then(data => {
event.sender.send(callbackID, {
data,
hasError : false
});
})
.catch (err => {
event.sender.send(callbackID, {
err,
hasError : true
});
});