-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlinkedboxdraw.js
496 lines (443 loc) · 20.4 KB
/
linkedboxdraw.js
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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
const fs = require('fs');
const path = require('path');
const express = require('express')
const bodyParser = require('body-parser')
const {Client} = require('pg')
const cors = require('cors')
const http = require('http');
const https = require('https');
const certificate = fs.readFileSync('/etc/letsencrypt/live/diskloud.fr/fullchain.pem');
const privateKey = fs.readFileSync('/etc/letsencrypt/live/diskloud.fr/privkey.pem');
var credentials = {key: privateKey, cert: certificate};
const app = express()
var httpServer = http.createServer(app);
var httpsServer = https.createServer(credentials, app);
httpServer.listen(8080, () => {
console.log('http linkedboxdraw server listening on port 8080!')
});
httpsServer.listen(8443, () => {
console.log('https linkedboxdraw server listening on port 8443!')
});
app.use(cors())
app.options('*', cors());
app.use(bodyParser.json({ limit: '10mb' }))
app.use(bodyParser.urlencoded({ limit: '10mb', extended: true }))
// CORS middleware
const allowCrossDomain = (req, res, next) => {
console.log("entering allowCrossDomain ...");
res.header("Access-Control-Allow-Origin", "https://ludoaubert.github.io");
res.header("Access-Control-Allow-Credentials", true);
res.header("Access-Control-Allow-Methods", "GET,POST");
res.header("Access-Control-Allow-Headers", "Origin, Content-Type, Accept");
next();
};
app.use(allowCrossDomain);
app.post('/linkedboxdraw/post', async (req, res) => {
const data = req.body;
console.log(JSON.stringify(data));
console.log(data.diagram);
const uuid_diagram = data.diagram[0].uuid_diagram;
console.log(uuid_diagram);
const client = new Client({
host:'localhost',
port:5433,
user:'postgres',
password:'7472',
database:'linkedboxdraw'
});
await client.connect();
const ret1 = await client.query(`
WITH cte (table_name, columns) AS (
SELECT table_name, STRING_AGG(FORMAT('%s %s', column_name, data_type),', ' ORDER BY ordinal_position)
FROM INFORMATION_SCHEMA.COLUMNS
WHERE table_schema='public'
GROUP BY table_name
)
SELECT json_object_agg(table_name, columns)
FROM cte
`);
const column_list = ret1.rows[0].json_object_agg;
const result1 = await client.query(`
MERGE INTO diagram d
USING json_to_recordset('${JSON.stringify(data.diagram)}') AS rd(${column_list.diagram})
ON d.uuid_diagram=rd.uuid_diagram
WHEN NOT MATCHED BY TARGET THEN
INSERT (uuid_diagram, title) VALUES(rd.uuid_diagram, rd.title)
WHEN MATCHED AND d.title != rd.title THEN
UPDATE SET title = rd.title
RETURNING merge_action(), d.*;
`);
console.log("MERGE INTO diagram done...");
const result2 = await client.query(`
MERGE INTO box b
USING (
SELECT rb.uuid_box, rb.title, d.iddiagram
FROM json_to_recordset('${JSON.stringify(data.box)}') AS rb(${column_list.box})
JOIN diagram d ON d.uuid_diagram='${uuid_diagram}'
) rb
ON b.uuid_box=rb.uuid_box
WHEN NOT MATCHED BY TARGET THEN
INSERT (uuid_box, title, iddiagram) VALUES(rb.uuid_box, rb.title, rb.iddiagram)
WHEN MATCHED AND b.title != rb.title THEN
UPDATE SET title = rb.title
WHEN NOT MATCHED BY SOURCE
AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram=b.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), b.*;
`);
console.log(result2.rows);
console.log("MERGE INTO box done...");
const result3 = await client.query(`
MERGE INTO field f
USING (
SELECT d.iddiagram, rf.uuid_field, rf.name, b.idbox
FROM json_to_recordset('${JSON.stringify(data.field)}') AS rf(${column_list.field})
JOIN json_to_recordset('${JSON.stringify(data.box)}') AS rb(${column_list.box}) ON rf.idbox=rb.idbox
JOIN box b ON b.uuid_box=rb.uuid_box
JOIN diagram d ON d.uuid_diagram='${uuid_diagram}'
) rf
ON f.uuid_field = rf.uuid_field
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_field, name, idbox) VALUES(rf.iddiagram, rf.uuid_field, rf.name, rf.idbox)
WHEN MATCHED AND f.name != rf.name THEN
UPDATE SET name = rf.name
WHEN NOT MATCHED BY SOURCE AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram=f.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), f.*;
`);
console.log(result3.rows);
console.log("MERGE INTO field done...");
const result4 = await client.query(`
MERGE INTO value v
USING (
SELECT d.iddiagram, rv.uuid_value, rv.data, f.idfield
FROM json_to_recordset('${JSON.stringify(data.value)}') AS rv(${column_list.value})
JOIN json_to_recordset('${JSON.stringify(data.field)}') AS rf(${column_list.field}) ON rv.idfield=rf.idfield
JOIN field f ON f.uuid_field = rf.uuid_field
JOIN diagram d ON d.uuid_diagram='${uuid_diagram}'
) rv
ON v.uuid_value = rv.uuid_value
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_value, data, idfield) VALUES(rv.iddiagram, rv.uuid_value, rv.data, rv.idfield)
WHEN MATCHED AND (v.data, v.idfield) != (rv.data, rv.idfield) THEN
UPDATE SET data = rv.data, idfield = rv.idfield
WHEN NOT MATCHED BY SOURCE AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram=v.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), v.*;
`);
console.log(result4.rows);
console.log("MERGE INTO value done...");
const result5 = await client.query(`
MERGE INTO link l
USING (
SELECT d.iddiagram, rl.uuid_link, b_from.idbox AS idbox_from, f_from.idfield AS idfield_from, b_to.idbox AS idbox_to, f_to.idfield AS idfield_to
FROM json_to_recordset('${JSON.stringify(data.link)}') AS rl(${column_list.link})
JOIN json_to_recordset('${JSON.stringify(data.box)}') AS rb_from(${column_list.box}) ON rl.idbox_from=rb_from.idbox
JOIN json_to_recordset('${JSON.stringify(data.box)}') AS rb_to(${column_list.box}) ON rl.idbox_to=rb_to.idbox
LEFT JOIN json_to_recordset('${JSON.stringify(data.field)}') AS rf_from(${column_list.field}) ON rl.idfield_from=rf_from.idfield
LEFT JOIN json_to_recordset('${JSON.stringify(data.field)}') AS rf_to(${column_list.field}) ON rl.idfield_to=rf_to.idfield
JOIN box b_from ON b_from.uuid_box=rb_from.uuid_box
JOIN box b_to ON b_to.uuid_box=rb_to.uuid_box
LEFT JOIN field f_from ON f_from.uuid_field=rf_from.uuid_field
LEFT JOIN field f_to ON f_to.uuid_field=rf_to.uuid_field
JOIN diagram d ON d.uuid_diagram = '${uuid_diagram}'
) rl
ON l.uuid_link = rl.uuid_link
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_link, idbox_from, idfield_from, idbox_to, idfield_to)
VALUES(rl.iddiagram, rl.uuid_link, rl.idbox_from, rl.idfield_from, rl.idbox_to, rl.idfield_to)
WHEN NOT MATCHED BY SOURCE
AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram=l.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), l.*;
`);
console.log(result5.rows);
console.log("MERGE INTO link done...");
const result6 = await client.query(`
MERGE INTO tag t
USING (
SELECT d.iddiagram, rt.uuid_tag, rt.type_code, rt.code
FROM json_to_recordset('${JSON.stringify(data.tag)}') AS rt(${column_list.tag})
JOIN diagram d ON d.uuid_diagram = '${uuid_diagram}'
) rt
ON t.uuid_tag = rt.uuid_tag
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_tag, type_code, code) VALUES(rt.iddiagram, rt.uuid_tag, rt.type_code, rt.code)
WHEN MATCHED AND (t.type_code, t.code) != (rt.type_code, rt.code) THEN
UPDATE SET type_code = rt.type_code, code = rt.code
WHEN NOT MATCHED BY SOURCE
AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram = t.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), t.*;
`);
console.log(result6.rows);
console.log("MERGE INTO tag done...");
const result7 = await client.query(`
MERGE INTO message_tag m
USING (
SELECT rm.uuid_message, rm.message, d.iddiagram
FROM json_to_recordset('${JSON.stringify(data.message_tag)}') AS rm(${column_list.message_tag})
JOIN diagram d ON d.uuid_diagram = '${uuid_diagram}'
) rm
ON m.uuid_message = rm.uuid_message
WHEN NOT MATCHED BY TARGET THEN
INSERT (uuid_message, message, iddiagram) VALUES(rm.uuid_message, rm.message, rm.iddiagram)
WHEN MATCHED AND m.message != rm.message THEN
UPDATE SET message = rm.message
WHEN NOT MATCHED BY SOURCE
AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram = m.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), m.*;
`);
console.log(result7.rows);
console.log("MERGE INTO message_tag done...");
const result8 = await client.query(`
MERGE INTO graph g
USING (
SELECT rg.uuid_graph, from_table,
CASE from_table
WHEN 'tag' THEN t.idtag
WHEN 'message_tag' THEN m.idmessage
END AS from_key,
to_table,
CASE to_table
WHEN 'box' THEN b.idbox
WHEN 'field' THEN f.idfield
WHEN 'value' THEN v.idvalue
WHEN 'link' THEN l.idlink
END AS to_key,
d.iddiagram
FROM json_to_recordset('${JSON.stringify(data.graph)}') AS rg("idgraph" int, "uuid_graph" uuid, "from_table" source_table, "from_key" int, "to_table" target_table, "to_key" int)
LEFT JOIN json_to_recordset('${JSON.stringify(data.tag)}') AS rt(${column_list.tag}) ON rt.idtag=rg.from_key
LEFT JOIN tag t ON t.uuid_tag = rt.uuid_tag
LEFT JOIN json_to_recordset('${JSON.stringify(data.message_tag)}') AS rm(${column_list.message_tag}) ON rm.idmessage=rg.from_key
LEFT JOIN message_tag m ON m.uuid_message = rm.uuid_message
LEFT JOIN json_to_recordset('${JSON.stringify(data.box)}') AS rb(${column_list.box}) ON rb.idbox=rg.to_key
LEFT JOIN box b ON b.uuid_box = rb.uuid_box
LEFT JOIN json_to_recordset('${JSON.stringify(data.field)}') AS rf(${column_list.field}) ON rf.idfield=rg.to_key
LEFT JOIN field f ON f.uuid_field = rf.uuid_field
LEFT JOIN json_to_recordset('${JSON.stringify(data.value)}') AS rv(${column_list.value}) ON rv.idvalue=rg.to_key
LEFT JOIN value v ON v.uuid_value = rv.uuid_value
LEFT JOIN json_to_recordset('${JSON.stringify(data.link)}') AS rl(${column_list.link}) ON rl.idlink=rg.to_key
LEFT JOIN link l ON rl.uuid_link=l.uuid_link
JOIN diagram d ON d.uuid_diagram = '${uuid_diagram}'
) rg
ON g.uuid_graph = rg.uuid_graph
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_graph, from_table, from_key, to_table, to_key)
VALUES(rg.iddiagram, rg.uuid_graph, rg.from_table, rg.from_key, rg.to_table, rg.to_key)
WHEN MATCHED AND (g.from_table, g.from_key, g.to_table, g.to_key, g.iddiagram) !=
(rg.from_table,rg.from_key,rg.to_table,rg.to_key,rg.iddiagram) THEN
UPDATE SET from_table = rg.from_table, from_key = rg.from_key, to_table = rg.to_table, to_key = rg.to_key,
iddiagram = rg.iddiagram
WHEN NOT MATCHED BY SOURCE AND EXISTS(
SELECT * FROM diagram d WHERE g.iddiagram=d.iddiagram AND d.uuid_diagram='${uuid_diagram}'
)
THEN DELETE
RETURNING merge_action(), g.*;
`);
console.log(result8.rows);
console.log("MERGE INTO graph done...");
const result9 = await client.query(`
MERGE INTO rectangle r
USING (
SELECT d.iddiagram, rr.uuid_rectangle, rr.width, rr.height, b.idbox
FROM json_to_recordset('${JSON.stringify(data.rectangle)}') AS rr(${column_list.rectangle})
JOIN json_to_recordset('${JSON.stringify(data.box)}') AS rb(${column_list.box}) ON rr.idbox=rb.idbox
JOIN box b ON b.uuid_box=rb.uuid_box
JOIN diagram d ON d.uuid_diagram='${uuid_diagram}'
) rr
ON r.uuid_rectangle = rr.uuid_rectangle
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_rectangle, width, height, idbox)
VALUES(rr.iddiagram, rr.uuid_rectangle, rr.width, rr.height, rr.idbox)
WHEN MATCHED AND (r.width, r.height, r.idbox) != (rr.width, rr.height, rr.idbox) THEN
UPDATE SET width = rr.width, height = rr.height, idbox = rr.idbox
WHEN NOT MATCHED BY SOURCE
AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram=r.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), r.*;
`);
console.log(result9.rows);
console.log("MERGE INTO rectangle done...");
const result10 = await client.query(`
MERGE INTO translation t
USING (
SELECT d.iddiagram, rt.uuid_translation, rt.context, r.idrectangle, rt.x, rt.y, rt.z
FROM json_to_recordset('${JSON.stringify(data.translation)}') AS rt(${column_list.translation})
JOIN json_to_recordset('${JSON.stringify(data.rectangle)}') AS rr(${column_list.rectangle}) ON rt.idrectangle=rr.idrectangle
JOIN rectangle r ON r.uuid_rectangle=rr.uuid_rectangle
JOIN diagram d ON d.uuid_diagram='${uuid_diagram}'
) rt
ON t.uuid_translation = rt.uuid_translation
WHEN NOT MATCHED BY TARGET THEN
INSERT (iddiagram, uuid_translation, context, idrectangle, x, y, z)
VALUES(rt.iddiagram, rt.uuid_translation, rt.context, rt.idrectangle, rt.x, rt.y, rt.z)
WHEN MATCHED AND (t.context, t.x, t.y, t.z) != (rt.context, rt.x, rt.y, rt.z) THEN
UPDATE SET context = rt.context, x = rt.x, y = rt.y, z = rt.z
WHEN NOT MATCHED BY SOURCE
AND EXISTS(SELECT * FROM diagram d WHERE d.iddiagram=t.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), t.*;
`);
console.log(result10.rows);
console.log("MERGE INTO translation done...");
/*
const result11 = await client.query(`
MERGE INTO polyline p
USING json_to_recordset('${JSON.stringify(data.polyline)}') AS rp(${column_list.polyline})
ON p.uuid_polyline = rp.uuid_polyline
WHEN NOT MATCHED BY TARGET THEN
INSERT (uuid_polyline, context, points) VALUES(rp.uuid_polyline, rp.context, rp.points)
WHEN MATCHED AND (p.context, p.points) != (rp.context, rp.points) THEN
UPDATE SET context = rp.context, points = rp.points
WHEN NOT MATCHED BY SOURCE
AND EXISTS (SELECT * FROM diagram d WHERE d.iddiagram=p.iddiagram AND d.uuid_diagram='${uuid_diagram}')
THEN DELETE
RETURNING merge_action(), p.*;
`);
console.log(result11.rows);
console.log("MERGE INTO polyline done...");
*/
const result12 = await client.query(`
MERGE INTO user_diagram ud
USING (
SELECT rud.uuid_ud, u.iduser, d.iddiagram
FROM json_to_recordset('${JSON.stringify(data.user_diagram)}') AS rud(${column_list.user_diagram})
JOIN json_to_recordset('${JSON.stringify(data.diagram)}') AS rd(${column_list.diagram}) ON rd.iddiagram=rud.iddiagram
JOIN json_to_recordset('${JSON.stringify(data.utilisateur)}') AS ru(${column_list.utilisateur}) ON rud.iduser=ru.iduser
JOIN diagram d ON d.uuid_diagram = rd.uuid_diagram
JOIN utilisateur u ON u.uuid_user = ru.uuid_user
) rud
ON ud.uuid_ud=rud.uuid_ud
WHEN NOT MATCHED BY TARGET THEN
INSERT(uuid_ud, iduser, iddiagram) VALUES(rud.uuid_ud, rud.iduser, rud.iddiagram)
WHEN MATCHED AND (ud.iduser, ud.iddiagram) != (rud.iduser, rud.iddiagram) THEN
UPDATE SET iduser=rud.iduser, iddiagram=rud.iddiagram
RETURNING merge_action(), ud.*;
`);
console.log(result12.rows);
console.log("MERGE INTO user_diagram done...");
})
app.post('/linkedboxdraw/create_account', async(req, res) => {
const data = req.body;
console.log(data);
const email = data.email;
const password = data.password;
console.log(email);
console.log(password);
const client = new Client({
host:'localhost',
port:5433,
user:'postgres',
password:'7472',
database:'linkedboxdraw'
});
await client.connect();
const result = await client.query(`
INSERT INTO utilisateur(email, password_hash)
SELECT '${email}', md5('${password}')::uuid
RETURNING *
`);
const uuid_user = result.rows[0].uuid_user;
const json_uuid_user = JSON.stringify(uuid_user);
console.log(json_uuid_user);
res.json(json_uuid_user);
})
app.post('/linkedboxdraw/login', async(req, res) => {
const data = req.body;
console.log(data);
const email = data.email;
const password = data.password;
console.log(email);
console.log(password);
const client = new Client({
host:'localhost',
port:5433,
user:'postgres',
password:'7472',
database:'linkedboxdraw'
});
await client.connect();
const result = await client.query(`
SELECT uuid_user
FROM utilisateur
WHERE email='${email}' AND password_hash = md5('${password}')::uuid
`);
const uuid_user = result.rows[0].uuid_user;
console.log(uuid_user);
const result2 = await client.query(`
SELECT json_build_object(
'utilisateur', (SELECT json_agg(row_to_json(u))::json FROM utilisateur u WHERE u.uuid_user='${uuid_user}')
)
`);
const doc = result2.rows[0].json_build_object;
const json_doc = JSON.stringify(doc);
console.log(json_doc);
res.json(json_doc);
})
app.get('/linkedboxdraw/list', async (req, res) => {
console.log(req.query);
const uuid_user = req.query.uuid_user;
console.log(uuid_user);
const client = new Client({
host:'localhost',
port:5433,
user:'postgres',
password:'7472',
database:'linkedboxdraw'
});
await client.connect();
const result = await client.query(`
SELECT coalesce(json_agg(json_build_object('uuid_diagram', d.uuid_diagram, 'title', d.title) ORDER BY d.title), '[]'::json)
FROM diagram d
JOIN user_diagram ud ON ud.iddiagram=d.iddiagram
JOIN utilisateur u ON ud.iduser=u.iduser
WHERE u.uuid_user='${uuid_user}'
`);
const doc = result.rows[0].coalesce;
const json_doc = JSON.stringify(doc);
console.log(json_doc);
res.json(json_doc);
});
app.get('/linkedboxdraw/get', async (req, res) => {
console.log(req.query);
const uuid_diagram = req.query.uuid_diagram;
console.log(uuid_diagram);
const client = new Client({
host:'localhost',
port:5433,
user:'postgres',
password:'7472',
database:'linkedboxdraw'
});
await client.connect();
const result = await client.query(`
SELECT json_build_object(
'diagram', (SELECT json_agg(row_to_json(d))::json FROM diagram d WHERE d.uuid_diagram='${uuid_diagram}'),
'user_diagram', (SELECT coalesce(json_agg(row_to_json(ud))::json, '[]'::json) FROM user_diagram ud JOIN diagram d ON ud.iddiagram=d.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'box', (SELECT json_agg(row_to_json(b))::json FROM box b JOIN diagram d ON d.iddiagram=b.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'field', (SELECT coalesce(json_agg(row_to_json(f))::json, '[]'::json) FROM field f JOIN diagram d ON d.iddiagram=f.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'value', (SELECT coalesce(json_agg(row_to_json(v))::json, '[]'::json) FROM value v JOIN diagram d ON d.iddiagram=v.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'link', (SELECT coalesce(json_agg(row_to_json(l))::json, '[]'::json) FROM link l JOIN diagram d ON d.iddiagram=l.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'tag', (SELECT json_agg(row_to_json(t))::json FROM tag t JOIN diagram d ON d.iddiagram=t.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'message_tag', (SELECT coalesce(json_agg(row_to_json(m))::json, '[]'::json) FROM message_tag m JOIN diagram d ON d.iddiagram=m.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'graph', (SELECT coalesce(json_agg(row_to_json(g))::json, '[]'::json) FROM graph g JOIN diagram d ON d.iddiagram=g.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'rectangle', (SELECT json_agg(row_to_json(r))::json FROM rectangle r JOIN diagram d ON d.iddiagram=r.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'translation', (SELECT json_agg(row_to_json(t))::json FROM translation t JOIN diagram d ON d.iddiagram=t.iddiagram AND d.uuid_diagram='${uuid_diagram}'),
'polyline', (SELECT coalesce(json_agg(row_to_json(p))::json, '[]'::json) FROM polyline p JOIN diagram d ON d.iddiagram=p.iddiagram AND d.uuid_diagram='${uuid_diagram}')
)
`);
const doc = result.rows[0].json_build_object;
const json_doc = JSON.stringify(doc);
console.log(json_doc);
res.json(json_doc);
})
/*
to run as service using a node.js module called pm2 which allows to do the same thing as systemd:
>pm2 start linkedboxdraw.js
to start node linkedboxdraw.js as a service
>pm2 monit
to look at the log
>pm2 stop linkedboxdraw.js
*/