-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson2mysql.js
More file actions
executable file
·354 lines (312 loc) · 7.27 KB
/
Copy pathjson2mysql.js
File metadata and controls
executable file
·354 lines (312 loc) · 7.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
const SimpleSqlBuilder = require('@caitken-com/simple-sql-builder');
/**
* @description Converts JSON to MYSQL
* @package json2mysql
* @param {Object} RED
* @author Christopher Aitken
* @version 1.0
*/
module.exports = function(RED)
{
function Json2mysqlNode(config)
{
RED.nodes.createNode(this, config);
const node = this;
node.query = null;
node.server = RED.nodes.getNode(config.server);
node.template = config.template;
/**
* @description Handle incoming messages
* @memberof Json2mysqlNode
* @param {json} msg [msg.topic => (string), msg.payload => (json)]
* @return {void}
*/
node.on('input', function(msg)
{
let topic = msg.topic || null;
// Apply template to payload
if (node.template !== "")
{
try
{
let incoming = JSON.parse(JSON.stringify(msg.payload));
msg.payload = JSON.parse(node.template);
// Override template with incoming values
if (typeof incoming == 'object')
{
for (let key in incoming)
{
msg.payload[key] = JSON.parse(JSON.stringify(incoming[key]));
}
}
}
catch (error)
{
return node.warn(error);
}
}
// Payload validation
if (!('payload' in msg)) return node.send(null);
if (typeof msg.payload != 'object') return node.send(null);
if (!('return' in msg.payload)) msg.payload.return = 'string';
// Populate query generator
try
{
node.query = SimpleSqlBuilder.fromJson(msg.payload);
}
catch (err)
{
return node.warn('err1: ' + err);
}
// Generate query and either execute or return query string
switch (msg.payload.return)
{
// Return query string (good for debugging)
case 'string':
try
{
node.send({'payload': node.query, 'topic': topic});
}
catch (err)
{
node.warn(err);
}
break;
// An array of rows, where each row is an associative array
case 'array':
node.server.query(node.query)
.then((result) =>
{
node.send({
'payload': {...result.payload},
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
// An array of rows, where each row is a numeric array
case 'array-num':
node.server.query(node.query)
.then((result) =>
{
let rows = [];
for (let i in result.payload)
{
let cols = [];
for (let j in result.payload[i])
{
cols.push(result.payload[i][j]);
}
rows.push(cols);
}
node.send({
'payload': rows,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
// A single row, as an associative array
case 'row':
node.server.query(node.query)
.then((result) =>
{
for (let i in result.payload)
{
node.send({
'payload': {...result.payload[i]},
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
break;
}
})
.catch((err) =>
{
node.warn(err);
});
break;
// A single row, as a numeric array
case 'row-num':
node.server.query(node.query)
.then((result) =>
{
let row = [];
for (let i in result.payload)
{
for (let j in result.payload[i])
{
row.push(result.payload[i][j]);
}
break;
}
node.send({
'payload': row,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
// Returns _array_ of `{identifier: value}` _object_ pairs where the identifier is the first column in the result set, and the value is the second
case 'map':
node.server.query(node.query)
.then((result) =>
{
let rows = [];
for (let i in result.payload)
{
let cols = [];
let count = 0;
for (let j in result.payload[i])
{
cols.push(result.payload[i][j]);
if (++count == 2) break;
}
rows.push({[cols[0]]: cols[1]});
}
node.send({
'payload': rows,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
// Returns _object_ of `{identifier: {col: vaal, ...}}`, where the identifier is the first column in the result set, and the value an _object_ of `{name:value}` pairs
case 'map-array':
node.server.query(node.query)
.then((result) =>
{
let rows = {};
for (let i in result.payload)
{
let id = null;
let cols = {};
let count = 0;
for (let j in result.payload[i])
{
if (count++ == 0)
{
id = result.payload[i][j];
continue;
}
cols[j] = result.payload[i][j];
}
rows[id] = cols;
}
node.send({
'payload': rows,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
// A single value, of the first column of the first row
case 'val':
node.server.query(node.query)
.then((result) =>
{
let val = null;
for (let i in result.payload)
{
for (let j in result.payload[i])
{
val = result.payload[i][j];
break;
}
break;
}
node.send({
'payload': val,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
// Returns an _array_ from the first column of each row
case 'col':
node.server.query(node.query)
.then((result) =>
{
let rows = [];
for (let i in result.payload)
{
let count = 0;
for (let j in result.payload[i])
{
if (count++ > 0) continue;
rows.push(result.payload[i][j]);
}
}
node.send({
'payload': rows,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
case 'count':
node.server.query(node.query)
.then((result) =>
{
node.send({
'payload': result.payload.length,
'insertId': result.insertId,
'affectedRows': result.affectedRows,
'changedRows': result.changedRows,
'topic': topic,
});
})
.catch((err) =>
{
node.warn(err);
});
break;
}
});
}
RED.nodes.registerType('json2mysql', Json2mysqlNode);
}