diff --git a/api/src/controllers/connections.js b/api/src/controllers/connections.js index ee0a03890..4fef6a91e 100644 --- a/api/src/controllers/connections.js +++ b/api/src/controllers/connections.js @@ -26,8 +26,8 @@ module.exports = { }, test(req, res) { const subprocess = fork(`${__dirname}/../proc/connectProcess.js`); - subprocess.send(req.body); subprocess.on('message', resp => res.json(resp)); + subprocess.send(req.body); }, save_meta: 'post', @@ -52,6 +52,6 @@ module.exports = { get_meta: 'get', async get({ id }) { const res = await this.datastore.find({ _id: id }); - return res; + return res[0]; }, }; diff --git a/api/src/controllers/serverConnections.js b/api/src/controllers/serverConnections.js index 6047b211c..1c5d731ef 100644 --- a/api/src/controllers/serverConnections.js +++ b/api/src/controllers/serverConnections.js @@ -1,17 +1,38 @@ const connections = require('./connections'); const socket = require('../utility/socket'); +const { fork } = require('child_process'); module.exports = { opened: [], + handle_databases(id, { databases }) { + const existing = this.opened.find(x => x.connection.id == id); + if (!existing) return; + existing.databases = databases; + socket.emit(`database-list-changed-${id}`); + }, + async ensureOpened(id) { const existing = this.opened.find(x => x.connection.id == id); if (existing) return existing; - + const connection = await connections.get(id); + const subprocess = fork(`${__dirname}/../proc/serverConnectionProcess.js`); + const newOpened = { + id, + subprocess, + databases: [], + }; + this.opened.push(newOpened); + subprocess.on('message', ({ msgtype, ...message }) => { + this[`handle_${msgtype}`](id, message); + }); + subprocess.send({ msgtype: 'connect', ...connection }); + return newOpened; }, listDatabases_meta: 'get', async listDatabases({ id }) { - const opened = this.ensureOpened(id); + const opened = await this.ensureOpened(id); + return opened.databases; }, }; diff --git a/api/src/proc/connectProcess.js b/api/src/proc/connectProcess.js index f5bbf4de3..c46799a78 100644 --- a/api/src/proc/connectProcess.js +++ b/api/src/proc/connectProcess.js @@ -6,6 +6,6 @@ process.on('message', async connection => { process.send(res); } catch (e) { console.log(e); - process.send({ error: e.message }); + process.send({ msgtype: 'error', error: e.message }); } }); diff --git a/api/src/proc/serverConnectionProcess.js b/api/src/proc/serverConnectionProcess.js index 0c05fdf8c..567d078b0 100644 --- a/api/src/proc/serverConnectionProcess.js +++ b/api/src/proc/serverConnectionProcess.js @@ -1,20 +1,32 @@ -function handleConnect() {} +let systemConnection; +let storedConnection; + +async function handleRefreshDatabases() { + const driver = require(`../engines/${storedConnection.engine}/index`); + const databases = await driver.listDatabases(systemConnection); + process.send({ msgtype: 'databases', databases }); +} + +async function handleConnect(connection) { + storedConnection = connection; + const driver = require(`../engines/${storedConnection.engine}/index`); + systemConnection = await driver.connect(storedConnection); + setInterval(handleRefreshDatabases, 30 * 1000); +} const messageHandlers = { connect: handleConnect, }; -function handleMessage({ type, ...other }) { - const handler = messageHandlers[type]; - handler(other); +async function handleMessage({ msgtype, ...other }) { + const handler = messageHandlers[msgtype]; + await handler(other); } -process.on('message', async connection => { +process.on('message', async message => { try { - const connectFunc = require(`../engines/${connection.engine}/connect`); - const res = await connectFunc(connection); - process.send(res); + await handleMessage(message); } catch (e) { - process.send({ error: e.message }); + process.send({ msgtype: 'error', error: e.message }); } });