Skip to content

Instantly share code, notes, and snippets.

@mdimai666
Created April 18, 2018 07:39
Show Gist options
  • Save mdimai666/d3daea9edac80629c26737fbc3b7a7de to your computer and use it in GitHub Desktop.
Save mdimai666/d3daea9edac80629c26737fbc3b7a7de to your computer and use it in GitHub Desktop.
[
{
"id": "18c14b4b.131de5",
"type": "function",
"z": "4e51036c.b5a04c",
"name": "gen Query",
"func": "var jj = global.get('jj'); //JSON safe stringify\n\ntry {\n var sql = \"INSERT INTO logs (code,source,title,body,json,screenshot,uid)\";\n sql += \" VALUES \";\n var v = msg.payload;\n \n if(typeof msg === 'string') return;\n \n var a = [v.code,\n escapeQuote(escapeObj(v.source)||'node-red').slice(0,100),\n escapeQuote(v.title).slice(0,200),\n escapeQuote(escapeObj(v.body||'node body')),\n escapeQuote(jj(v.json)),\n '',v.uid];\n \n // node.warn(a);\n a = a.map(s=>`'${s}'`).join(',');\n sql += `(${a})`;\n msg.topic = sql;\n msg.payload = '';\n return msg;\n} catch (err){\n node.error(err);\n}\n\n\nfunction escapeObj(va){\n if(typeof va === 'string')\n return va;\n else return jj(va)\n}\n\nfunction escapeSP(v) {\n // return v\n // .replace(/[\\\"]/g, '\\\\\"')\n // .replace(/[\\\\]/g, '\\\\\\\\')\n // .replace(/[\\/]/g, '\\\\/')\n // .replace(/[\\b]/g, '\\\\b')\n // .replace(/[\\f]/g, '\\\\f')\n // .replace(/[\\n]/g, '\\\\n')\n // .replace(/[\\r]/g, '\\\\r')\n // .replace(/[\\t]/g, '\\\\t')\n // return encodeURI(this)\n \n return v\n .replace(/\\\\n/g, \"\\\\n\")\n .replace(/\\\\'/g, \"\\\\'\")\n .replace(/\\\\\"/g, '\\\\\"')\n .replace(/\\\\&/g, \"\\\\&\")\n .replace(/\\\\r/g, \"\\\\r\")\n .replace(/\\\\t/g, \"\\\\t\")\n .replace(/\\\\b/g, \"\\\\b\")\n .replace(/\\\\f/g, \"\\\\f\")\n};\n\nfunction escapeQuote(v){\n return v.toString().replace(/'/g, '´');\n}",
"outputs": 1,
"noerr": 0,
"x": 343,
"y": 184,
"wires": [
[
"e1ac938f.e8755"
]
]
},
{
"id": "6de44728.663738",
"type": "function",
"z": "4e51036c.b5a04c",
"name": "structurize Log message",
"func": "var jj = global.get('jj'); //JSON safe stringify\nvar log = {};\nvar uid = global.get('uuid')();\n\n\nif (typeof msg === 'object') {\n try {\n //Если это стандартная ошибка\n if (msg.error) {\n\n var e = msg.error;\n log = {\n title: escapeObj(e.message) || 'No title',\n code: 910,\n source: 'node-red: ' + escapeObj(e.source).slice(0, 100),\n body: (e)?jj(e):escapeObj(msg),\n json: jj(msg),\n\n }\n }\n else {\n\n //если это какой то другой объект\n let title;\n if (msg.title) title = escapeObj(msg.title);\n if (!title) title = msg.topic;\n if (!title) title = 'no title';\n // node.warn(title);\n // if(title.trim().length<2)\n // title = jj(msg)).slice(0,100);\n log = {\n code: parseInt(msg.code) | 911,\n source: escapeObj(msg.source) || 'node-red',\n title: title,\n body: (msg.payload)?msg.payload:escapeObj(msg),\n json: jj(msg),\n }\n }\n } catch (err) {\n //если не смогли разобрать объект\n log.code = 914;\n log.source = \"node-red\";\n log.title = \"cant resolved error\";\n log.json = jj(msg);\n log.body = escapeObj(err);\n }\n} else {\n\n log = {\n code: 913,\n source: 'node-red',\n title: msg.slice(0, 100),\n body: escapeObj(msg),\n json: jj({}),\n\n }\n}\n\nlog.uid = uid;\nmsg.payload = log;\n// node.warn(log);\nreturn msg;\n\nfunction escapeObj(va) {\n if (typeof va === 'string')\n return va;\n else return jj(va)\n}\n\nfunction escapeQuote(v){\n return v.replace(/'/g, '´');\n}",
"outputs": 1,
"noerr": 0,
"x": 311,
"y": 102,
"wires": [
[
"18c14b4b.131de5"
]
]
},
{
"id": "e1ac938f.e8755",
"type": "sqldbs",
"z": "4e51036c.b5a04c",
"mydb": "de9a6238.6de55",
"querytype": "Insert",
"name": "",
"x": 500.5,
"y": 184,
"wires": [
[]
]
},
{
"id": "de9a6238.6de55",
"type": "sqldbsdatabase",
"z": "",
"host": "127.0.0.1",
"port": "5432",
"db": "sendy",
"dialect": "postgres"
}
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment