@oino-ts/db 0.16.2 → 0.17.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/cjs/OINODbApi.js +140 -59
- package/dist/cjs/OINODbConfig.js +0 -4
- package/dist/cjs/OINODbFactory.js +2 -67
- package/dist/cjs/OINODbParser.js +30 -27
- package/dist/cjs/OINODbSwagger.js +4 -4
- package/dist/cjs/index.js +2 -1
- package/dist/esm/OINODbApi.js +140 -60
- package/dist/esm/OINODbConfig.js +0 -4
- package/dist/esm/OINODbFactory.js +3 -68
- package/dist/esm/OINODbParser.js +30 -27
- package/dist/esm/OINODbSwagger.js +4 -4
- package/dist/esm/index.js +1 -1
- package/dist/types/OINODb.d.ts +2 -2
- package/dist/types/OINODbApi.d.ts +47 -16
- package/dist/types/OINODbConfig.d.ts +0 -4
- package/dist/types/OINODbFactory.d.ts +1 -7
- package/dist/types/OINODbParser.d.ts +11 -10
- package/dist/types/index.d.ts +1 -16
- package/package.json +4 -4
- package/src/OINODb.ts +2 -2
- package/src/OINODbApi.test.ts +102 -109
- package/src/OINODbApi.ts +152 -58
- package/src/OINODbConfig.ts +0 -6
- package/src/OINODbFactory.ts +3 -72
- package/src/OINODbParser.ts +32 -28
- package/src/OINODbSwagger.ts +4 -4
- package/src/index.ts +1 -17
package/dist/cjs/OINODbApi.js
CHANGED
|
@@ -5,11 +5,76 @@
|
|
|
5
5
|
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
-
exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = void 0;
|
|
8
|
+
exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINODbApiRequest = void 0;
|
|
9
9
|
const index_js_1 = require("./index.js");
|
|
10
10
|
const common_1 = require("@oino-ts/common");
|
|
11
11
|
const hashid_1 = require("@oino-ts/hashid");
|
|
12
|
-
|
|
12
|
+
class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
13
|
+
rowId;
|
|
14
|
+
data;
|
|
15
|
+
sqlParams;
|
|
16
|
+
constructor(init) {
|
|
17
|
+
super(init);
|
|
18
|
+
this.rowId = init?.rowId || "";
|
|
19
|
+
this.data = init?.data || null;
|
|
20
|
+
this.sqlParams = init?.sqlParams || {};
|
|
21
|
+
if (init?.filter) {
|
|
22
|
+
this.sqlParams.filter = init.filter;
|
|
23
|
+
}
|
|
24
|
+
if (!this.sqlParams.filter) {
|
|
25
|
+
const filter_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_FILTER_PARAM);
|
|
26
|
+
if (filter_param) {
|
|
27
|
+
this.sqlParams.filter = index_js_1.OINODbSqlFilter.parse(filter_param);
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
if (init?.order) {
|
|
31
|
+
this.sqlParams.order = init.order;
|
|
32
|
+
}
|
|
33
|
+
if (!this.sqlParams.order) {
|
|
34
|
+
const order_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_ORDER_PARAM);
|
|
35
|
+
if (order_param) {
|
|
36
|
+
this.sqlParams.order = index_js_1.OINODbSqlOrder.parse(order_param);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
if (init?.limit) {
|
|
40
|
+
this.sqlParams.limit = init.limit;
|
|
41
|
+
}
|
|
42
|
+
if (!this.sqlParams.limit) {
|
|
43
|
+
const limit_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_LIMIT_PARAM);
|
|
44
|
+
if (limit_param) {
|
|
45
|
+
this.sqlParams.limit = index_js_1.OINODbSqlLimit.parse(limit_param);
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (init?.aggregate) {
|
|
49
|
+
this.sqlParams.aggregate = init.aggregate;
|
|
50
|
+
}
|
|
51
|
+
if (!this.sqlParams.aggregate) {
|
|
52
|
+
const aggregate_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_AGGREGATE_PARAM);
|
|
53
|
+
if (aggregate_param) {
|
|
54
|
+
this.sqlParams.aggregate = index_js_1.OINODbSqlAggregate.parse(aggregate_param);
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
if (init?.select) {
|
|
58
|
+
this.sqlParams.select = init.select;
|
|
59
|
+
}
|
|
60
|
+
if (!this.sqlParams.select) {
|
|
61
|
+
const select_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_SELECT_PARAM);
|
|
62
|
+
if (select_param) {
|
|
63
|
+
this.sqlParams.select = index_js_1.OINODbSqlSelect.parse(select_param);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
static async fromRequest(request) {
|
|
68
|
+
const body = await request.arrayBuffer();
|
|
69
|
+
return new OINODbApiRequest({
|
|
70
|
+
url: new URL(request.url),
|
|
71
|
+
method: request.method,
|
|
72
|
+
headers: Object.fromEntries(request.headers),
|
|
73
|
+
data: Buffer.from(body),
|
|
74
|
+
});
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
exports.OINODbApiRequest = OINODbApiRequest;
|
|
13
78
|
/**
|
|
14
79
|
* OINO API request result object with returned data and/or http status code/message and
|
|
15
80
|
* error / warning messages.
|
|
@@ -17,19 +82,19 @@ const API_EMPTY_PARAMS = { sqlParams: {} };
|
|
|
17
82
|
*/
|
|
18
83
|
class OINODbApiResult extends common_1.OINOResult {
|
|
19
84
|
/** DbApi request params */
|
|
20
|
-
|
|
85
|
+
request;
|
|
21
86
|
/** Returned data if any */
|
|
22
87
|
data;
|
|
23
88
|
/**
|
|
24
89
|
* Constructor of OINODbApiResult.
|
|
25
90
|
*
|
|
26
|
-
* @param
|
|
91
|
+
* @param request DbApi request parameters
|
|
27
92
|
* @param data result data
|
|
28
93
|
*
|
|
29
94
|
*/
|
|
30
|
-
constructor(
|
|
95
|
+
constructor(request, data) {
|
|
31
96
|
super();
|
|
32
|
-
this.
|
|
97
|
+
this.request = request;
|
|
33
98
|
this.data = data;
|
|
34
99
|
}
|
|
35
100
|
/**
|
|
@@ -41,11 +106,11 @@ class OINODbApiResult extends common_1.OINOResult {
|
|
|
41
106
|
async writeApiResponse(headers = {}) {
|
|
42
107
|
let response = null;
|
|
43
108
|
if (this.success && this.data) {
|
|
44
|
-
const body = await this.data.writeString(this.
|
|
45
|
-
response = new Response(body, { status: this.
|
|
109
|
+
const body = await this.data.writeString(this.request.responseType);
|
|
110
|
+
response = new Response(body, { status: this.status, statusText: this.statusText, headers: headers });
|
|
46
111
|
}
|
|
47
112
|
else {
|
|
48
|
-
response = new Response(JSON.stringify(this, null, 3), { status: this.
|
|
113
|
+
response = new Response(JSON.stringify(this, null, 3), { status: this.status, statusText: this.statusText, headers: headers });
|
|
49
114
|
}
|
|
50
115
|
for (let i = 0; i < this.messages.length; i++) {
|
|
51
116
|
response.headers.set('X-OINO-MESSAGE-' + i, this.messages[i]);
|
|
@@ -220,14 +285,14 @@ class OINODbApi {
|
|
|
220
285
|
}
|
|
221
286
|
//logDebug("OINODbApi.validateHttpValues", {result:result})
|
|
222
287
|
}
|
|
223
|
-
_parseData(httpResult,
|
|
288
|
+
_parseData(httpResult, request) {
|
|
224
289
|
let rows = [];
|
|
225
290
|
try {
|
|
226
|
-
if (Array.isArray(
|
|
227
|
-
rows =
|
|
291
|
+
if (Array.isArray(request.data)) {
|
|
292
|
+
rows = request.data;
|
|
228
293
|
}
|
|
229
|
-
else {
|
|
230
|
-
rows = index_js_1.OINODbParser.createRows(this.datamodel,
|
|
294
|
+
else if (request.data != null) {
|
|
295
|
+
rows = index_js_1.OINODbParser.createRows(this.datamodel, request.data, request);
|
|
231
296
|
}
|
|
232
297
|
}
|
|
233
298
|
catch (e) {
|
|
@@ -235,10 +300,10 @@ class OINODbApi {
|
|
|
235
300
|
}
|
|
236
301
|
return rows;
|
|
237
302
|
}
|
|
238
|
-
async _doGet(result,
|
|
303
|
+
async _doGet(result, rowId, request) {
|
|
239
304
|
let sql = "";
|
|
240
305
|
try {
|
|
241
|
-
sql = this.datamodel.printSqlSelect(
|
|
306
|
+
sql = this.datamodel.printSqlSelect(rowId, request.sqlParams || {});
|
|
242
307
|
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "_doGet", "Print SQL", { sql: sql });
|
|
243
308
|
const sql_res = await this.db.sqlSelect(sql);
|
|
244
309
|
if (sql_res.hasErrors()) {
|
|
@@ -248,7 +313,7 @@ class OINODbApi {
|
|
|
248
313
|
}
|
|
249
314
|
}
|
|
250
315
|
else {
|
|
251
|
-
result.data = new index_js_1.OINODbModelSet(this.datamodel, sql_res,
|
|
316
|
+
result.data = new index_js_1.OINODbModelSet(this.datamodel, sql_res, request.sqlParams);
|
|
252
317
|
}
|
|
253
318
|
}
|
|
254
319
|
catch (e) {
|
|
@@ -380,28 +445,38 @@ class OINODbApi {
|
|
|
380
445
|
this._debugOnError = debugOnError;
|
|
381
446
|
}
|
|
382
447
|
/**
|
|
383
|
-
* Method for
|
|
448
|
+
* Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
449
|
+
* SQL select, insert, update and delete.
|
|
450
|
+
*
|
|
451
|
+
* @param method HTTP method of the REST request
|
|
452
|
+
* @param rowId URL id of the REST request
|
|
453
|
+
* @param data HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
454
|
+
* @param sqlParams SQL parameters for the REST request
|
|
455
|
+
*
|
|
456
|
+
*/
|
|
457
|
+
async doRequest(method, rowId, data, sqlParams) {
|
|
458
|
+
return this.runRequest(new OINODbApiRequest({ method: method, rowId: rowId, data: data, sqlParams: sqlParams }));
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
384
462
|
* SQL select, insert, update and delete.
|
|
385
463
|
*
|
|
386
|
-
* @param
|
|
387
|
-
* @param id URL id of the REST request
|
|
388
|
-
* @param data HTTP body data as either serialized string or unserialized JS object / OINODataRow-array
|
|
389
|
-
* @param params HTTP URL parameters as key-value-pairs
|
|
464
|
+
* @param request OINO DB API request
|
|
390
465
|
*
|
|
391
466
|
*/
|
|
392
|
-
async
|
|
393
|
-
index_js_1.OINOBenchmark.startMetric("OINODbApi", "doRequest." + method);
|
|
394
|
-
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doRequest", "Request", { method: method, id:
|
|
395
|
-
let result = new OINODbApiResult(
|
|
467
|
+
async runRequest(request) {
|
|
468
|
+
index_js_1.OINOBenchmark.startMetric("OINODbApi", "doRequest." + request.method);
|
|
469
|
+
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doRequest", "Request", { method: request.method, id: request.rowId, data: request.data });
|
|
470
|
+
let result = new OINODbApiResult(request);
|
|
396
471
|
let rows = [];
|
|
397
|
-
if ((method == "POST") || (method == "PUT")) {
|
|
398
|
-
rows = this._parseData(result,
|
|
472
|
+
if ((request.method == "POST") || (request.method == "PUT")) {
|
|
473
|
+
rows = this._parseData(result, request);
|
|
399
474
|
}
|
|
400
|
-
if (method == "GET") {
|
|
401
|
-
await this._doGet(result,
|
|
475
|
+
if (request.method == "GET") {
|
|
476
|
+
await this._doGet(result, request.rowId, request);
|
|
402
477
|
}
|
|
403
|
-
else if (method == "PUT") {
|
|
404
|
-
if (!
|
|
478
|
+
else if (request.method == "PUT") {
|
|
479
|
+
if (!request.rowId) {
|
|
405
480
|
result.setError(400, "HTTP PUT method requires an URL ID for the row that is updated!", "DoRequest");
|
|
406
481
|
}
|
|
407
482
|
else if (rows.length != 1) {
|
|
@@ -409,15 +484,15 @@ class OINODbApi {
|
|
|
409
484
|
}
|
|
410
485
|
else {
|
|
411
486
|
try {
|
|
412
|
-
await this._doPut(result,
|
|
487
|
+
await this._doPut(result, request.rowId, rows);
|
|
413
488
|
}
|
|
414
489
|
catch (e) {
|
|
415
490
|
result.setError(500, "Unhandled exception in HTTP PUT doRequest: " + e.message, "DoRequest");
|
|
416
491
|
}
|
|
417
492
|
}
|
|
418
493
|
}
|
|
419
|
-
else if (method == "POST") {
|
|
420
|
-
if (
|
|
494
|
+
else if (request.method == "POST") {
|
|
495
|
+
if (request.rowId) {
|
|
421
496
|
result.setError(400, "HTTP POST method must not have an URL ID as it does not target an existing row but creates a new one!", "DoRequest");
|
|
422
497
|
}
|
|
423
498
|
else if (rows.length == 0) {
|
|
@@ -432,13 +507,13 @@ class OINODbApi {
|
|
|
432
507
|
}
|
|
433
508
|
}
|
|
434
509
|
}
|
|
435
|
-
else if (method == "DELETE") {
|
|
436
|
-
if (!
|
|
510
|
+
else if (request.method == "DELETE") {
|
|
511
|
+
if (!request.rowId) {
|
|
437
512
|
result.setError(400, "HTTP DELETE method requires an id!", "DoRequest");
|
|
438
513
|
}
|
|
439
514
|
else {
|
|
440
515
|
try {
|
|
441
|
-
await this._doDelete(result,
|
|
516
|
+
await this._doDelete(result, request.rowId, null);
|
|
442
517
|
}
|
|
443
518
|
catch (e) {
|
|
444
519
|
result.setError(500, "Unhandled exception in HTTP DELETE doRequest: " + e.message, "DoRequest");
|
|
@@ -446,29 +521,38 @@ class OINODbApi {
|
|
|
446
521
|
}
|
|
447
522
|
}
|
|
448
523
|
else {
|
|
449
|
-
result.setError(405, "Unsupported HTTP method '" + method + "' for REST request", "DoRequest");
|
|
524
|
+
result.setError(405, "Unsupported HTTP method '" + request.method + "' for REST request", "DoRequest");
|
|
450
525
|
}
|
|
451
|
-
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doRequest." + method);
|
|
526
|
+
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doRequest." + request.method);
|
|
452
527
|
return Promise.resolve(result);
|
|
453
528
|
}
|
|
454
529
|
/**
|
|
455
|
-
* Method for
|
|
456
|
-
* SQL select, insert, update and delete.
|
|
530
|
+
* Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
|
|
457
531
|
*
|
|
458
|
-
* @param method HTTP
|
|
459
|
-
* @param
|
|
460
|
-
* @param
|
|
532
|
+
* @param method HTTP method of the REST request
|
|
533
|
+
* @param rowId URL id of the REST request
|
|
534
|
+
* @param data HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
461
535
|
*
|
|
462
536
|
*/
|
|
463
|
-
async doBatchUpdate(method, data,
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
537
|
+
async doBatchUpdate(method, rowId, data, sqlParams) {
|
|
538
|
+
return this.runRequest(new OINODbApiRequest({ method: method, rowId: rowId, data: data, sqlParams: sqlParams }));
|
|
539
|
+
}
|
|
540
|
+
/**
|
|
541
|
+
* Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
|
|
542
|
+
*
|
|
543
|
+
* @param request HTTP URL parameters as key-value-pairs
|
|
544
|
+
*
|
|
545
|
+
*/
|
|
546
|
+
async runBatchUpdate(request) {
|
|
547
|
+
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doBatchUpdate", "Request", { request: request, data: request.data });
|
|
548
|
+
let result = new OINODbApiResult(request);
|
|
549
|
+
if ((request.method != "PUT") && (request.method != "DELETE")) {
|
|
550
|
+
result.setError(500, "Batch update only supports PUT and DELETE methods!", "DoBatchUpdate");
|
|
551
|
+
return Promise.resolve(result);
|
|
552
|
+
}
|
|
553
|
+
index_js_1.OINOBenchmark.startMetric("OINODbApi", "doBatchUpdate." + request.method);
|
|
554
|
+
const rows = [] = this._parseData(result, request);
|
|
555
|
+
if (request.method == "PUT") {
|
|
472
556
|
try {
|
|
473
557
|
await this._doPut(result, null, rows);
|
|
474
558
|
}
|
|
@@ -476,7 +560,7 @@ class OINODbApi {
|
|
|
476
560
|
result.setError(500, "Unhandled exception in HTTP PUT doRequest: " + e.message, "DoBatchUpdate");
|
|
477
561
|
}
|
|
478
562
|
}
|
|
479
|
-
else if (method == "DELETE") {
|
|
563
|
+
else if (request.method == "DELETE") {
|
|
480
564
|
try {
|
|
481
565
|
await this._doDelete(result, null, rows);
|
|
482
566
|
}
|
|
@@ -484,10 +568,7 @@ class OINODbApi {
|
|
|
484
568
|
result.setError(500, "Unhandled exception in HTTP DELETE doRequest: " + e.message, "DoBatchUpdate");
|
|
485
569
|
}
|
|
486
570
|
}
|
|
487
|
-
|
|
488
|
-
result.setError(405, "Unsupported HTTP method '" + method + "' for batch update", "DoBatchUpdate");
|
|
489
|
-
}
|
|
490
|
-
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doBatchUpdate." + method);
|
|
571
|
+
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doBatchUpdate." + request.method);
|
|
491
572
|
return Promise.resolve(result);
|
|
492
573
|
}
|
|
493
574
|
/**
|
package/dist/cjs/OINODbConfig.js
CHANGED
|
@@ -18,10 +18,6 @@ class OINODbConfig {
|
|
|
18
18
|
static OINODB_SQL_AGGREGATE_PARAM = "oinosqlaggregate";
|
|
19
19
|
/** Name of the OINODbSqlSelect-parameter in request */
|
|
20
20
|
static OINODB_SQL_SELECT_PARAM = "oinosqlselect";
|
|
21
|
-
/** Name of the OINOContentType-parameter request */
|
|
22
|
-
static OINODB_REQUEST_TYPE = "oinorequesttype";
|
|
23
|
-
/** Name of the OINOContentType-parameter request */
|
|
24
|
-
static OINODB_RESPONSE_TYPE = "oinoresponsetype";
|
|
25
21
|
/**
|
|
26
22
|
* Set the name of the OINO ID field
|
|
27
23
|
* @param idField name of the OINO ID field
|
|
@@ -42,13 +42,13 @@ class OINODbFactory {
|
|
|
42
42
|
if (connect) {
|
|
43
43
|
const connect_res = await result.connect();
|
|
44
44
|
if (connect_res.success == false) {
|
|
45
|
-
throw new Error("Database connection failed: " + connect_res.
|
|
45
|
+
throw new Error("Database connection failed: " + connect_res.statusText);
|
|
46
46
|
}
|
|
47
47
|
}
|
|
48
48
|
if (validate) {
|
|
49
49
|
const validate_res = await result.validate();
|
|
50
50
|
if (validate_res.success == false) {
|
|
51
|
-
throw new Error("Database validation failed: " + validate_res.
|
|
51
|
+
throw new Error("Database validation failed: " + validate_res.statusText);
|
|
52
52
|
}
|
|
53
53
|
}
|
|
54
54
|
return result;
|
|
@@ -64,70 +64,5 @@ class OINODbFactory {
|
|
|
64
64
|
await db.initializeApiDatamodel(result);
|
|
65
65
|
return result;
|
|
66
66
|
}
|
|
67
|
-
/**
|
|
68
|
-
* Creates a key-value-collection from Javascript URL parameters.
|
|
69
|
-
*
|
|
70
|
-
* @param request HTTP Request
|
|
71
|
-
*/
|
|
72
|
-
static createParamsFromRequest(request) {
|
|
73
|
-
const url = new URL(request.url);
|
|
74
|
-
let sql_params = {};
|
|
75
|
-
const filter = url.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_FILTER_PARAM);
|
|
76
|
-
if (filter) {
|
|
77
|
-
sql_params.filter = index_js_1.OINODbSqlFilter.parse(filter);
|
|
78
|
-
}
|
|
79
|
-
const order = url.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_ORDER_PARAM);
|
|
80
|
-
if (order) {
|
|
81
|
-
sql_params.order = index_js_1.OINODbSqlOrder.parse(order);
|
|
82
|
-
}
|
|
83
|
-
const limit = url.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_LIMIT_PARAM);
|
|
84
|
-
if (limit) {
|
|
85
|
-
sql_params.limit = index_js_1.OINODbSqlLimit.parse(limit);
|
|
86
|
-
}
|
|
87
|
-
const aggregate = url.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_AGGREGATE_PARAM);
|
|
88
|
-
if (aggregate) {
|
|
89
|
-
sql_params.aggregate = index_js_1.OINODbSqlAggregate.parse(aggregate);
|
|
90
|
-
}
|
|
91
|
-
const select = url.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_SELECT_PARAM);
|
|
92
|
-
if (select) {
|
|
93
|
-
sql_params.select = index_js_1.OINODbSqlSelect.parse(select);
|
|
94
|
-
}
|
|
95
|
-
let result = { sqlParams: sql_params };
|
|
96
|
-
const request_type = url.searchParams.get(index_js_1.OINODbConfig.OINODB_REQUEST_TYPE) || request.headers.get("content-type"); // content-type header can be overridden by query parameter
|
|
97
|
-
if (request_type == index_js_1.OINOContentType.csv) {
|
|
98
|
-
result.requestType = index_js_1.OINOContentType.csv;
|
|
99
|
-
}
|
|
100
|
-
else if (request_type == index_js_1.OINOContentType.urlencode) {
|
|
101
|
-
result.requestType = index_js_1.OINOContentType.urlencode;
|
|
102
|
-
}
|
|
103
|
-
else if (request_type?.startsWith(index_js_1.OINOContentType.formdata)) {
|
|
104
|
-
result.requestType = index_js_1.OINOContentType.formdata;
|
|
105
|
-
result.multipartBoundary = request_type.split('boundary=')[1] || "";
|
|
106
|
-
}
|
|
107
|
-
else {
|
|
108
|
-
result.requestType = index_js_1.OINOContentType.json;
|
|
109
|
-
}
|
|
110
|
-
const response_type = url.searchParams.get(index_js_1.OINODbConfig.OINODB_RESPONSE_TYPE) || request.headers.get("accept"); // accept header can be overridden by query parameter
|
|
111
|
-
const accept_types = response_type?.split(', ') || [];
|
|
112
|
-
for (let i = 0; i < accept_types.length; i++) {
|
|
113
|
-
if (Object.values(index_js_1.OINOContentType).includes(accept_types[i])) {
|
|
114
|
-
result.responseType = accept_types[i];
|
|
115
|
-
break;
|
|
116
|
-
}
|
|
117
|
-
}
|
|
118
|
-
if (result.responseType === undefined) {
|
|
119
|
-
result.responseType = index_js_1.OINOContentType.json;
|
|
120
|
-
}
|
|
121
|
-
const last_modified = request.headers.get("if-modified-since");
|
|
122
|
-
if (last_modified) {
|
|
123
|
-
result.lastModified = new Date(last_modified).getTime();
|
|
124
|
-
}
|
|
125
|
-
const etags = request.headers.get("if-none-match")?.split(',').map(e => e.trim());
|
|
126
|
-
if (etags) {
|
|
127
|
-
result.etags = etags;
|
|
128
|
-
}
|
|
129
|
-
index_js_1.OINOLog.debug("@oino-ts/db", "OINODbFactory", "createParamsFromRequest", "Result", { params: result });
|
|
130
|
-
return result;
|
|
131
|
-
}
|
|
132
67
|
}
|
|
133
68
|
exports.OINODbFactory = OINODbFactory;
|
package/dist/cjs/OINODbParser.js
CHANGED
|
@@ -16,20 +16,20 @@ class OINODbParser {
|
|
|
16
16
|
* Create data rows from request body based on the datamodel.
|
|
17
17
|
*
|
|
18
18
|
* @param datamodel datamodel of the api
|
|
19
|
-
* @param data data as
|
|
20
|
-
* @param
|
|
19
|
+
* @param data data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
20
|
+
* @param request parameters
|
|
21
21
|
*
|
|
22
22
|
*/
|
|
23
|
-
static createRows(datamodel, data,
|
|
23
|
+
static createRows(datamodel, data, request) {
|
|
24
24
|
let result = [];
|
|
25
25
|
if (typeof data == "string") {
|
|
26
|
-
result = this.
|
|
26
|
+
result = this._createRowsFromText(datamodel, data, request);
|
|
27
27
|
}
|
|
28
|
-
else if (data instanceof Buffer) {
|
|
29
|
-
result = this.
|
|
28
|
+
else if ((data instanceof Buffer) || (data instanceof Uint8Array)) {
|
|
29
|
+
result = this._createRowsFromBlob(datamodel, data, request);
|
|
30
30
|
}
|
|
31
31
|
else if (typeof data == "object") {
|
|
32
|
-
result = [this.
|
|
32
|
+
result = [this._createRowFromObject(datamodel, data)];
|
|
33
33
|
}
|
|
34
34
|
return result;
|
|
35
35
|
}
|
|
@@ -38,28 +38,28 @@ class OINODbParser {
|
|
|
38
38
|
*
|
|
39
39
|
* @param datamodel datamodel of the api
|
|
40
40
|
* @param data data as a string
|
|
41
|
-
* @param
|
|
41
|
+
* @param request request parameters
|
|
42
42
|
*
|
|
43
43
|
*/
|
|
44
|
-
static
|
|
45
|
-
if ((
|
|
44
|
+
static _createRowsFromText(datamodel, data, request) {
|
|
45
|
+
if ((request.requestType == index_js_1.OINOContentType.json) || (request.requestType == undefined)) {
|
|
46
46
|
return this._createRowFromJson(datamodel, data);
|
|
47
47
|
}
|
|
48
|
-
else if (
|
|
48
|
+
else if (request.requestType == index_js_1.OINOContentType.csv) {
|
|
49
49
|
return this._createRowFromCsv(datamodel, data);
|
|
50
50
|
}
|
|
51
|
-
else if (
|
|
52
|
-
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"),
|
|
51
|
+
else if (request.requestType == index_js_1.OINOContentType.formdata) {
|
|
52
|
+
return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), request.multipartBoundary || "");
|
|
53
53
|
}
|
|
54
|
-
else if (
|
|
54
|
+
else if (request.requestType == index_js_1.OINOContentType.urlencode) {
|
|
55
55
|
return this._createRowFromUrlencoded(datamodel, data);
|
|
56
56
|
}
|
|
57
|
-
else if (
|
|
57
|
+
else if (request.requestType == index_js_1.OINOContentType.html) {
|
|
58
58
|
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
|
|
59
59
|
return [];
|
|
60
60
|
}
|
|
61
61
|
else {
|
|
62
|
-
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", { contentType:
|
|
62
|
+
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromText", "Unrecognized input content type!", { contentType: request.requestType });
|
|
63
63
|
return [];
|
|
64
64
|
}
|
|
65
65
|
}
|
|
@@ -67,29 +67,32 @@ class OINODbParser {
|
|
|
67
67
|
* Create data rows from request body based on the datamodel.
|
|
68
68
|
*
|
|
69
69
|
* @param datamodel datamodel of the api
|
|
70
|
-
* @param data data as an Buffer
|
|
71
|
-
* @param
|
|
70
|
+
* @param data data as an Buffer or Uint8Array
|
|
71
|
+
* @param request parameters
|
|
72
72
|
*
|
|
73
73
|
*/
|
|
74
|
-
static
|
|
75
|
-
if (
|
|
74
|
+
static _createRowsFromBlob(datamodel, data, request) {
|
|
75
|
+
if (data instanceof Uint8Array && !(data instanceof Buffer)) {
|
|
76
|
+
data = Buffer.from(data);
|
|
77
|
+
}
|
|
78
|
+
if ((request.requestType == index_js_1.OINOContentType.json) || (request.requestType == undefined)) {
|
|
76
79
|
return this._createRowFromJson(datamodel, data.toString()); // JSON is always a string
|
|
77
80
|
}
|
|
78
|
-
else if (
|
|
81
|
+
else if (request.requestType == index_js_1.OINOContentType.csv) {
|
|
79
82
|
return this._createRowFromCsv(datamodel, data.toString()); // binary data has to be base64 encoded so it's a string
|
|
80
83
|
}
|
|
81
|
-
else if (
|
|
82
|
-
return this._createRowFromFormdata(datamodel, data,
|
|
84
|
+
else if (request.requestType == index_js_1.OINOContentType.formdata) {
|
|
85
|
+
return this._createRowFromFormdata(datamodel, data, request.multipartBoundary || "");
|
|
83
86
|
}
|
|
84
|
-
else if (
|
|
87
|
+
else if (request.requestType == index_js_1.OINOContentType.urlencode) {
|
|
85
88
|
return this._createRowFromUrlencoded(datamodel, data.toString()); // data is urlencoded so it's a string
|
|
86
89
|
}
|
|
87
|
-
else if (
|
|
90
|
+
else if (request.requestType == index_js_1.OINOContentType.html) {
|
|
88
91
|
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
|
|
89
92
|
return [];
|
|
90
93
|
}
|
|
91
94
|
else {
|
|
92
|
-
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", { contentType:
|
|
95
|
+
index_js_1.OINOLog.error("@oino-ts/db", "OINODbParser", "createRowsFromBlob", "Unrecognized input content type!", { contentType: request.requestType });
|
|
93
96
|
return [];
|
|
94
97
|
}
|
|
95
98
|
}
|
|
@@ -101,7 +104,7 @@ class OINODbParser {
|
|
|
101
104
|
* @param data data as javascript object
|
|
102
105
|
*
|
|
103
106
|
*/
|
|
104
|
-
static
|
|
107
|
+
static _createRowFromObject(datamodel, data) {
|
|
105
108
|
const fields = datamodel.fields;
|
|
106
109
|
let result = new Array(fields.length);
|
|
107
110
|
for (let i = 0; i < fields.length; i++) {
|
|
@@ -56,10 +56,10 @@ class OINODbSwagger {
|
|
|
56
56
|
"success": {
|
|
57
57
|
"type": "boolean"
|
|
58
58
|
},
|
|
59
|
-
"
|
|
59
|
+
"status": {
|
|
60
60
|
"type": "number"
|
|
61
61
|
},
|
|
62
|
-
"
|
|
62
|
+
"statusText": {
|
|
63
63
|
"type": "string"
|
|
64
64
|
},
|
|
65
65
|
"messages": {
|
|
@@ -71,8 +71,8 @@ class OINODbSwagger {
|
|
|
71
71
|
},
|
|
72
72
|
"required": [
|
|
73
73
|
"success",
|
|
74
|
-
"
|
|
75
|
-
"
|
|
74
|
+
"status",
|
|
75
|
+
"statusText",
|
|
76
76
|
"messages"
|
|
77
77
|
]
|
|
78
78
|
};
|
package/dist/cjs/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.OINODB_UNDEFINED = exports.OINODB_EMPTY_ROWS = exports.OINODB_EMPTY_ROW = exports.OINODbParser = exports.OINODbSwagger = exports.OINODbFactory = exports.OINODbConfig = exports.OINODbSqlNullCheck = exports.OINODbSqlSelect = exports.OINODbSqlAggregateFunctions = exports.OINODbSqlAggregate = exports.OINODbSqlBooleanOperation = exports.OINODbSqlLimit = exports.OINODbSqlComparison = exports.OINODbSqlOrder = exports.OINODbSqlFilter = exports.OINODb = exports.OINODbMemoryDataSet = exports.OINODbDataSet = exports.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINOStringDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINODbDataField = exports.OINODbModelSet = exports.OINODbDataModel = exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINOHtmlTemplate = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = exports.OINOStr = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOContentType = void 0;
|
|
3
|
+
exports.OINODB_UNDEFINED = exports.OINODB_EMPTY_ROWS = exports.OINODB_EMPTY_ROW = exports.OINODbParser = exports.OINODbSwagger = exports.OINODbFactory = exports.OINODbConfig = exports.OINODbSqlNullCheck = exports.OINODbSqlSelect = exports.OINODbSqlAggregateFunctions = exports.OINODbSqlAggregate = exports.OINODbSqlBooleanOperation = exports.OINODbSqlLimit = exports.OINODbSqlComparison = exports.OINODbSqlOrder = exports.OINODbSqlFilter = exports.OINODb = exports.OINODbMemoryDataSet = exports.OINODbDataSet = exports.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINOStringDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINODbDataField = exports.OINODbModelSet = exports.OINODbDataModel = exports.OINODbApiRequest = exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINOHtmlTemplate = exports.OINOHttpResult = exports.OINOResult = exports.OINOConsoleLog = exports.OINOLogLevel = exports.OINOLog = exports.OINOMemoryBenchmark = exports.OINOBenchmark = exports.OINOStr = exports.OINO_DEBUG_PREFIX = exports.OINO_INFO_PREFIX = exports.OINO_WARNING_PREFIX = exports.OINO_ERROR_PREFIX = exports.OINOContentType = void 0;
|
|
4
4
|
const common_1 = require("@oino-ts/common");
|
|
5
5
|
Object.defineProperty(exports, "OINOContentType", { enumerable: true, get: function () { return common_1.OINOContentType; } });
|
|
6
6
|
var common_2 = require("@oino-ts/common");
|
|
@@ -21,6 +21,7 @@ var OINODbApi_js_1 = require("./OINODbApi.js");
|
|
|
21
21
|
Object.defineProperty(exports, "OINODbApiResult", { enumerable: true, get: function () { return OINODbApi_js_1.OINODbApiResult; } });
|
|
22
22
|
Object.defineProperty(exports, "OINODbHtmlTemplate", { enumerable: true, get: function () { return OINODbApi_js_1.OINODbHtmlTemplate; } });
|
|
23
23
|
Object.defineProperty(exports, "OINODbApi", { enumerable: true, get: function () { return OINODbApi_js_1.OINODbApi; } });
|
|
24
|
+
Object.defineProperty(exports, "OINODbApiRequest", { enumerable: true, get: function () { return OINODbApi_js_1.OINODbApiRequest; } });
|
|
24
25
|
var OINODbDataModel_js_1 = require("./OINODbDataModel.js");
|
|
25
26
|
Object.defineProperty(exports, "OINODbDataModel", { enumerable: true, get: function () { return OINODbDataModel_js_1.OINODbDataModel; } });
|
|
26
27
|
var OINODbModelSet_js_1 = require("./OINODbModelSet.js");
|