-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
496 lines (461 loc) · 15.4 KB
/
Copy pathindex.js
File metadata and controls
496 lines (461 loc) · 15.4 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
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
'use strict'
const redis_url = process.env.REDIS_URL || 'localhost';
const redis_password = process.env.REDIS_PASSWORD;
const express = require('express');
const app = express();
const bodyParser = require('body-parser');
const redis = require("redis");
const genericPool = require("generic-pool");
const factory = {
create: () => {
return redis.createClient(6379, redis_url, {
password: redis_password
});
},
destroy: (client) => {
client.quit();
}
};
const opts = {
max: 1000,
min: 5,
};
const pool = genericPool.createPool(factory, opts);
async function prepareDatabase() {
let client = await pool.acquire();
createDatabase(client);
createTables(client);
createStatements(client);
pool.release(client);
}
function createDatabase(client) {
client.send_command(
"REDISQL.CREATE_DB",
["DB"],
(err, _) => {
if (err) {
console.log("Error in creating the database: ", err.message);
} else {
console.log("Database created");
}
});
}
function createTables(client) {
let statement = `CREATE TABLE IF NOT EXISTS namespace (
namespace TEXT PRIMARY KEY
);
CREATE TABLE IF NOT EXISTS json_data (
namespace STRING,
object_name STRING,
data JSON,
PRIMARY KEY (namespace, object_name),
FOREIGN KEY(namespace) REFERENCES namespace(namespace) ON UPDATE CASCADE ON DELETE CASCADE
);`;
client.send_command(
"REDISQL.EXEC",
["DB", statement],
(err, _) => {
if (err) {
console.log("Error in creating the tables: ", err.message);
} else {
console.log("Table created");
}
}
);
}
function createStatements(client) {
let statements = {
"create_namespace": `INSERT INTO namespace VALUES(?1);`,
"upsert_object": `INSERT OR REPLACE
INTO json_data (namespace, object_name, data)
VALUES (?1, ?2, json(?3))`,
"get_object": `SELECT data
FROM json_data
WHERE namespace = ?1 AND
object_name = ?2;`,
"extract": `SELECT json_extract(data, ?3)
FROM json_data
WHERE namespace = ?1 AND
object_name = ?2;`,
"insert": `UPDATE json_data
SET data = json_insert(json(data), ?3, json(?4))
WHERE namespace = ?1 AND
object_name = ?2 AND
data != json_insert(json(data), ?3, json(?4));`,
"patch": `UPDATE json_data
SET data = json_patch(data, json(?3))
WHERE namespace = ?1 AND
object_name = ?2 AND
data != json_patch(data, json(?3));`,
"remove": `UPDATE json_data
SET data = json_remove(data, ?3)
WHERE namespace = ?1 AND
object_name = ?2 AND
data != json_remove(data, ?3);`,
"replace":`UPDATE json_data
SET data = json_replace(json(data), ?3, json(?4))
WHERE namespace = ?1 AND
object_name = ?2 AND
data != json_replace(json(data), ?3, json(?4));`,
"set": `UPDATE json_data
SET data = json_set(data, ?3, json(?4))
WHERE namespace = ?1 AND
object_name = ?2 AND
data != json_set(data, ?3, json(?4));`
};
for (var key in statements) {
let stmt = key;
let query = statements[key];
client.send_command(
"REDISQL.CREATE_STATEMENT",
["DB", stmt, query],
(err, res) => {
if (err) {
console.log(err);
client.send_command(
"REDISQL.UPDATE_STATEMENT",
["DB", stmt, query],
(err,res) => {
if (err) {
console.log("Impossible to create or update the statement: ", stmt);
console.log(err.message);
} else {
console.log("Update statement: ", stmt);
}
}
);
} else {
console.log(res);
console.log("Create statement: ", stmt);
}
});
}
}
function formatCallBackResult(statusCode, body) {
return {
statusCode: statusCode,
headers: {
"Content-Type": "application/json"
},
body: JSON.stringify(body)
};
}
function returnError(err, res) {
console.log(err);
console.log(err.message);
const error = {
"status": "error",
"message": err.message
};
res.status(400).json(error);
}
async function createNewNamespace(namespace, res) {
console.log("Creating new namespace");
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "create_namespace", namespace],
function(err, result) {
pool.release(client);
if (err) {
returnError(err, res);
} else {
const success = {
"status": "success",
"message": "namespace correctly created"
};
res.status(201).json(success);
}
});
}
async function getObject(namespace, obj, res) {
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "get_object", namespace, obj],
function(err, result){
pool.release(client);
if (err) {
returnError(err, res);
} else {
console.log(result);
const result_obj = JSON.parse(result);
res.status(200).json(result_obj);
}
}
);
}
async function createObject(namespace, obj, body, res) {
let json = body;
let client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "upsert_object", namespace, obj, JSON.stringify(json)],
function(err, result) {
pool.release(client);
if (err) {
returnError(err, res);
} else {
console.log("Pre result");
const result_obj = {
"status": "success",
"message": "created new object",
"namespace": namespace,
"obj": obj,
"object": body
};
console.log(result_obj);
res.status(201).json(result_obj);
}
}
);
return;
}
function missingKeyError(missingKey, res) {
const error_message = "Necessary the \"" + missingKey + "\" key in the JSON body of the request";
returnError(error_message, res);
}
async function extract(namespace, obj, path, res){
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "extract", namespace, obj, path],
function(err, result) {
pool.release(client);
if (err) {
returnError(err, res);
return;
}
if (result[0][0] == null) {
const error_message = "The path provide does not match inside the JSON object";
returnError(error_message, res);
return;
}
const string_result = result[0][0];
const result_obj = {
status: 'success',
result: string_result
};
res.status(200).json(result_obj);
return;
}
);
return;
}
async function insert(namespace, obj, path, value, res) {
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "insert", namespace, obj, path, JSON.stringifY(value)],
function(err, result) {
pool.release(client);
console.log("Error: ", err);
console.log("Result: ", result);
if (err) {
returnError(err, res);
return;
}
if (result[1] == 0) {
const error_message = "Value not modified, either the path is wrong or the couple namespace/obj"
returnError(error_message, res);
return;
}
const result_obj = {message: "Insert executed, and value modified."};
res.status(200).json(result_obj);
return;
}
);
return;
}
async function patch(namespace, obj, value, res){
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "patch", namespace, obj, JSON.stringify(value)],
function(err, result) {
pool.release(client);
if (err) {
returnError(err, res);
return;
}
if (result[1] == 0) {
const error_message = "Value not modified, either the path is wrong or the couple namespace/obj"
returnError(error_message, res);
return;
}
const result_obj = {message: "Path executed, and value modified."};
res.status(200).json(result_obj);
return;
}
);
return;
}
async function remove(namespace, obj, path, res){
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "remove", namespace, obj, path],
function(err, result) {
pool.release(client);
if (err) {
returnError(err, res);
return;
}
if (result[1] == 0) {
const error_message = "Value not modified, either the path is wrong or the couple namespace/obj"
returnError(error_message, res);
return;
}
const result_obj = {message: "Remove executed, and value modified."};
res.status(200).json(result_obj);
return;
}
);
return;
}
async function replace(namespace, obj, path, value, res) {
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "replace". namespace, obj, path, JSON.stringify(value)],
function(err, result) {
pool.release(client);
console.log("Error: ", err);
console.log("Result: ", result);
if (err) {
returnError(err, res);
return;
}
if (result[1] == 0) {
const error_message = "Value not modified, either the path is wrong or the couple namespace/obj"
returnError(error_message, res);
return;
}
const result_obj = {message: "Replace executed, and value modified."};
res.status(200).json(result_obj);
return;
}
);
return;
}
async function set(namespace, obj, path, value, res) {
const client = await pool.acquire();
client.send_command(
"REDISQL.EXEC_STATEMENT",
["DB", "set", namespace, obj, path, JSON.stringify(value)],
function(err, result) {
pool.release(client);
console.log("Error: ", err);
console.log("Result: ", result);
if (err) {
returnError(err, res);
return;
}
if (result[1] == 0) {
const error_message = "Value not modified, either the path is wrong or the couple namespace/obj"
returnError(error_message, res);
return;
}
const result_obj = {message: "Set executed, and value modified."};
res.status(200).json(result_obj);
return;
}
);
return;
}
function patchObject(namespace, obj, action, res) {
let jsonAction = action;
if (!("action" in jsonAction)) {
missingKeyError("action", res);
return;
}
switch (jsonAction.action) {
case "extract":
if (!("path" in jsonAction)) {
missingKeyError("path", res);
return;
}
extract(namespace, obj, jsonAction.path, res);
break;
case "insert":
if (!("path" in jsonAction)) {
missingKeyError("path", res);
return;
}
if (!("value" in jsonAction)) {
missingKeyError("value", res);
return;
}
insert(namespace, obj, jsonAction.path, jsonAction.value, res);
break;
case "patch":
if (!("value" in jsonAction)) {
missingKeyError("value", res);
return;
}
patch(namespace, obj, jsonAction.value, res);
break;
case "remove":
if (!("path" in jsonAction)) {
missingKeyError("path", res);
return;
}
remove(namespace, obj, jsonAction.path, res);
break;
case "replace":
if (!("path" in jsonAction)) {
missingKeyError("path", res);
return;
}
if (!("value" in jsonAction)) {
missingKeyError("value", res);
return;
}
replace(namespace, obj, jsonAction.path, jsonAction.value, res);
break;
case "set":
if (!("path" in jsonAction)) {
missingKeyError("path", res);
return;
}
if (!("value" in jsonAction)) {
missingKeyError("value", res);
return;
}
set(namespace, obj, jsonAction.path, jsonAction.value, res);
break;
default:
const error_message = "The \"action\" key in the JSON body \
of the request must be one of the following: \"extract\" \
\"insert\", \"patch\", \"remove\", \"replace\" or \"set\"";
returnError(error_message, res);
break;
}
return;
}
app.use(bodyParser.json());
app.put('/:namespace', (req, res) => {
let namespace = req.params['namespace'];
createNewNamespace(namespace, res);
});
app.get('/:namespace/:obj', (req, res) => {
let namespace = req.params['namespace'];
let obj = req.params['obj'];
getObject(namespace, obj, res);
});
app.put('/:namespace/:obj', (req, res) => {
console.log(req.body);
let namespace = req.params['namespace'];
let obj = req.params['obj'];
createObject(namespace, obj, req.body, res);
});
app.patch('/:namespace/:obj', (req, res) => {
console.log(req.body);
let namespace = req.params['namespace'];
let obj = req.params['obj'];
patchObject(namespace, obj, req.body, res);
})
app.listen(3000, () => {
prepareDatabase();
console.log("Start listening on port 3000");
});