UNPKG

prisme-flow

Version:

prisme platform flow engine

64 lines (57 loc) 1.86 kB
/** * Created by prisme.io on 09/06/2017. */ module.exports = function (RED) { 'use strict' var reconnect = RED.settings.sqliteReconnectTime || 20000 var sqlite3 = require('sqlite3') function SqliteNodeDB (n) { RED.nodes.createNode(this, n) this.dbname = n.db var node = this node.doConnect = function () { node.db = new sqlite3.Database(node.dbname) node.db.on('open', function () { if (node.tick) { clearTimeout(node.tick) } node.log('opened ' + node.dbname + ' ok') }) node.db.on('error', function (err) { node.error('failed to open ' + node.dbname, err) node.tick = setTimeout(function () { node.doConnect() }, reconnect) }) } node.on('close', function () { if (node.tick) { clearTimeout(node.tick) } if (node.db) { node.db.close() } }) } RED.nodes.registerType('sqlitedb', SqliteNodeDB) function SqliteNodeIn (n) { RED.nodes.createNode(this, n) this.mydb = n.mydb this.mydbConfig = RED.nodes.getNode(this.mydb) if (this.mydbConfig) { this.mydbConfig.doConnect() var node = this node.on('input', function (msg) { if (typeof msg.topic === 'string') { // console.log("query:",msg.topic); var bind = Array.isArray(msg.payload) ? msg.payload : [] node.mydbConfig.db.all(msg.topic, bind, function (err, row) { if (err) { node.error(err, msg) } else { msg.payload = row node.send(msg) } }) } else { if (typeof msg.topic !== 'string') { node.error('msg.topic : the query is not defined as a string', msg) } } }) } else { this.error('Sqlite database not configured') } } RED.nodes.registerType('sqlite', SqliteNodeIn) }