diff --git a/data/index.js b/data/index.js new file mode 100644 index 0000000..b105582 --- /dev/null +++ b/data/index.js @@ -0,0 +1,11 @@ +module.exports = function(config) { + + var knex = require('knex')(config); + + return { + getEntity: function (table, where) { + + return knex(table).where(where).select('*'); + } + } +} diff --git a/index.js b/index.js index 5833af0..ba85262 100644 --- a/index.js +++ b/index.js @@ -1,30 +1,33 @@ const express = require('express'); const app = express(); + const WebSocketClient = require('ws'); const config = require('./config/config.json'); +const data = require('./data/index')(config.bo); app.listen(config.port); +app.use(express.json({limit: '50mb'})); const client = new WebSocketClient(config.urlHO); client.on('open', function open() { console.log('conection to socket ho is open'); -}); -app.get('/getSocket', function (req, res) { + client.send(JSON.stringify({ gln: config.gln, action: 'gnl'})); - var promesa = new Promise(function (resolve, reject) { + client.on('message', function incoming(message) { - client.send('isAlive?'); - - client.on('message', function incoming(message) { - console.log('recibe bo: %s', message); - resolve(message); - }); - }); + message = message.data.toString('utf8'); - promesa.then(function (data) { - res.send(data); + switch (message) { + case message.action == 'getEntity': + data.getEntity(message.tableName, message.where || {}).then(function(data) { + client.send(data); + }); + break; + default: + break; + } }); }); diff --git a/package.json b/package.json index 48874ed..e0789e2 100644 --- a/package.json +++ b/package.json @@ -10,6 +10,8 @@ "license": "ISC", "dependencies": { "express": "^4.16.4", + "knex": "^0.16.5", + "mssql": "^5.1.0", "ws": "^7.0.0" } }