@oino-ts/db 0.16.2 → 0.17.1
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 +131 -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 +131 -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 +46 -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 +37 -37
- package/src/OINODb.ts +321 -321
- package/src/OINODbApi.test.ts +492 -499
- package/src/OINODbApi.ts +602 -517
- package/src/OINODbConfig.ts +98 -104
- package/src/OINODbDataField.ts +403 -403
- package/src/OINODbDataModel.ts +291 -291
- package/src/OINODbFactory.ts +68 -137
- package/src/OINODbModelSet.ts +351 -351
- package/src/OINODbParser.ts +447 -443
- package/src/OINODbSqlParams.ts +592 -592
- package/src/OINODbSwagger.ts +208 -208
- package/src/index.ts +120 -136
package/dist/cjs/OINODbApi.js
CHANGED
|
@@ -5,11 +5,67 @@
|
|
|
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
|
+
}
|
|
68
|
+
exports.OINODbApiRequest = OINODbApiRequest;
|
|
13
69
|
/**
|
|
14
70
|
* OINO API request result object with returned data and/or http status code/message and
|
|
15
71
|
* error / warning messages.
|
|
@@ -17,19 +73,19 @@ const API_EMPTY_PARAMS = { sqlParams: {} };
|
|
|
17
73
|
*/
|
|
18
74
|
class OINODbApiResult extends common_1.OINOResult {
|
|
19
75
|
/** DbApi request params */
|
|
20
|
-
|
|
76
|
+
request;
|
|
21
77
|
/** Returned data if any */
|
|
22
78
|
data;
|
|
23
79
|
/**
|
|
24
80
|
* Constructor of OINODbApiResult.
|
|
25
81
|
*
|
|
26
|
-
* @param
|
|
82
|
+
* @param request DbApi request parameters
|
|
27
83
|
* @param data result data
|
|
28
84
|
*
|
|
29
85
|
*/
|
|
30
|
-
constructor(
|
|
86
|
+
constructor(request, data) {
|
|
31
87
|
super();
|
|
32
|
-
this.
|
|
88
|
+
this.request = request;
|
|
33
89
|
this.data = data;
|
|
34
90
|
}
|
|
35
91
|
/**
|
|
@@ -41,11 +97,11 @@ class OINODbApiResult extends common_1.OINOResult {
|
|
|
41
97
|
async writeApiResponse(headers = {}) {
|
|
42
98
|
let response = null;
|
|
43
99
|
if (this.success && this.data) {
|
|
44
|
-
const body = await this.data.writeString(this.
|
|
45
|
-
response = new Response(body, { status: this.
|
|
100
|
+
const body = await this.data.writeString(this.request.responseType);
|
|
101
|
+
response = new Response(body, { status: this.status, statusText: this.statusText, headers: headers });
|
|
46
102
|
}
|
|
47
103
|
else {
|
|
48
|
-
response = new Response(JSON.stringify(this, null, 3), { status: this.
|
|
104
|
+
response = new Response(JSON.stringify(this, null, 3), { status: this.status, statusText: this.statusText, headers: headers });
|
|
49
105
|
}
|
|
50
106
|
for (let i = 0; i < this.messages.length; i++) {
|
|
51
107
|
response.headers.set('X-OINO-MESSAGE-' + i, this.messages[i]);
|
|
@@ -220,14 +276,14 @@ class OINODbApi {
|
|
|
220
276
|
}
|
|
221
277
|
//logDebug("OINODbApi.validateHttpValues", {result:result})
|
|
222
278
|
}
|
|
223
|
-
_parseData(httpResult,
|
|
279
|
+
_parseData(httpResult, request) {
|
|
224
280
|
let rows = [];
|
|
225
281
|
try {
|
|
226
|
-
if (Array.isArray(
|
|
227
|
-
rows =
|
|
282
|
+
if (Array.isArray(request.data)) {
|
|
283
|
+
rows = request.data;
|
|
228
284
|
}
|
|
229
|
-
else {
|
|
230
|
-
rows = index_js_1.OINODbParser.createRows(this.datamodel,
|
|
285
|
+
else if (request.data != null) {
|
|
286
|
+
rows = index_js_1.OINODbParser.createRows(this.datamodel, request.data, request);
|
|
231
287
|
}
|
|
232
288
|
}
|
|
233
289
|
catch (e) {
|
|
@@ -235,10 +291,10 @@ class OINODbApi {
|
|
|
235
291
|
}
|
|
236
292
|
return rows;
|
|
237
293
|
}
|
|
238
|
-
async _doGet(result,
|
|
294
|
+
async _doGet(result, rowId, request) {
|
|
239
295
|
let sql = "";
|
|
240
296
|
try {
|
|
241
|
-
sql = this.datamodel.printSqlSelect(
|
|
297
|
+
sql = this.datamodel.printSqlSelect(rowId, request.sqlParams || {});
|
|
242
298
|
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "_doGet", "Print SQL", { sql: sql });
|
|
243
299
|
const sql_res = await this.db.sqlSelect(sql);
|
|
244
300
|
if (sql_res.hasErrors()) {
|
|
@@ -248,7 +304,7 @@ class OINODbApi {
|
|
|
248
304
|
}
|
|
249
305
|
}
|
|
250
306
|
else {
|
|
251
|
-
result.data = new index_js_1.OINODbModelSet(this.datamodel, sql_res,
|
|
307
|
+
result.data = new index_js_1.OINODbModelSet(this.datamodel, sql_res, request.sqlParams);
|
|
252
308
|
}
|
|
253
309
|
}
|
|
254
310
|
catch (e) {
|
|
@@ -380,28 +436,38 @@ class OINODbApi {
|
|
|
380
436
|
this._debugOnError = debugOnError;
|
|
381
437
|
}
|
|
382
438
|
/**
|
|
383
|
-
* Method for
|
|
439
|
+
* Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
384
440
|
* SQL select, insert, update and delete.
|
|
385
441
|
*
|
|
386
|
-
* @param method HTTP
|
|
387
|
-
* @param
|
|
388
|
-
* @param data HTTP body data as either serialized string or unserialized JS object
|
|
389
|
-
* @param
|
|
442
|
+
* @param method HTTP method of the REST request
|
|
443
|
+
* @param rowId URL id of the REST request
|
|
444
|
+
* @param data HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
445
|
+
* @param sqlParams SQL parameters for the REST request
|
|
390
446
|
*
|
|
391
447
|
*/
|
|
392
|
-
async doRequest(method,
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
448
|
+
async doRequest(method, rowId, data, sqlParams) {
|
|
449
|
+
return this.runRequest(new OINODbApiRequest({ method: method, rowId: rowId, data: data, sqlParams: sqlParams }));
|
|
450
|
+
}
|
|
451
|
+
/**
|
|
452
|
+
* Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
453
|
+
* SQL select, insert, update and delete.
|
|
454
|
+
*
|
|
455
|
+
* @param request OINO DB API request
|
|
456
|
+
*
|
|
457
|
+
*/
|
|
458
|
+
async runRequest(request) {
|
|
459
|
+
index_js_1.OINOBenchmark.startMetric("OINODbApi", "doRequest." + request.method);
|
|
460
|
+
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doRequest", "Request", { method: request.method, id: request.rowId, data: request.data });
|
|
461
|
+
let result = new OINODbApiResult(request);
|
|
396
462
|
let rows = [];
|
|
397
|
-
if ((method == "POST") || (method == "PUT")) {
|
|
398
|
-
rows = this._parseData(result,
|
|
463
|
+
if ((request.method == "POST") || (request.method == "PUT")) {
|
|
464
|
+
rows = this._parseData(result, request);
|
|
399
465
|
}
|
|
400
|
-
if (method == "GET") {
|
|
401
|
-
await this._doGet(result,
|
|
466
|
+
if (request.method == "GET") {
|
|
467
|
+
await this._doGet(result, request.rowId, request);
|
|
402
468
|
}
|
|
403
|
-
else if (method == "PUT") {
|
|
404
|
-
if (!
|
|
469
|
+
else if (request.method == "PUT") {
|
|
470
|
+
if (!request.rowId) {
|
|
405
471
|
result.setError(400, "HTTP PUT method requires an URL ID for the row that is updated!", "DoRequest");
|
|
406
472
|
}
|
|
407
473
|
else if (rows.length != 1) {
|
|
@@ -409,15 +475,15 @@ class OINODbApi {
|
|
|
409
475
|
}
|
|
410
476
|
else {
|
|
411
477
|
try {
|
|
412
|
-
await this._doPut(result,
|
|
478
|
+
await this._doPut(result, request.rowId, rows);
|
|
413
479
|
}
|
|
414
480
|
catch (e) {
|
|
415
481
|
result.setError(500, "Unhandled exception in HTTP PUT doRequest: " + e.message, "DoRequest");
|
|
416
482
|
}
|
|
417
483
|
}
|
|
418
484
|
}
|
|
419
|
-
else if (method == "POST") {
|
|
420
|
-
if (
|
|
485
|
+
else if (request.method == "POST") {
|
|
486
|
+
if (request.rowId) {
|
|
421
487
|
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
488
|
}
|
|
423
489
|
else if (rows.length == 0) {
|
|
@@ -432,13 +498,13 @@ class OINODbApi {
|
|
|
432
498
|
}
|
|
433
499
|
}
|
|
434
500
|
}
|
|
435
|
-
else if (method == "DELETE") {
|
|
436
|
-
if (!
|
|
501
|
+
else if (request.method == "DELETE") {
|
|
502
|
+
if (!request.rowId) {
|
|
437
503
|
result.setError(400, "HTTP DELETE method requires an id!", "DoRequest");
|
|
438
504
|
}
|
|
439
505
|
else {
|
|
440
506
|
try {
|
|
441
|
-
await this._doDelete(result,
|
|
507
|
+
await this._doDelete(result, request.rowId, null);
|
|
442
508
|
}
|
|
443
509
|
catch (e) {
|
|
444
510
|
result.setError(500, "Unhandled exception in HTTP DELETE doRequest: " + e.message, "DoRequest");
|
|
@@ -446,29 +512,38 @@ class OINODbApi {
|
|
|
446
512
|
}
|
|
447
513
|
}
|
|
448
514
|
else {
|
|
449
|
-
result.setError(405, "Unsupported HTTP method '" + method + "' for REST request", "DoRequest");
|
|
515
|
+
result.setError(405, "Unsupported HTTP method '" + request.method + "' for REST request", "DoRequest");
|
|
450
516
|
}
|
|
451
|
-
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doRequest." + method);
|
|
517
|
+
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doRequest." + request.method);
|
|
452
518
|
return Promise.resolve(result);
|
|
453
519
|
}
|
|
454
520
|
/**
|
|
455
|
-
* Method for
|
|
456
|
-
* SQL select, insert, update and delete.
|
|
521
|
+
* Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
|
|
457
522
|
*
|
|
458
|
-
* @param method HTTP
|
|
459
|
-
* @param
|
|
460
|
-
* @param
|
|
523
|
+
* @param method HTTP method of the REST request
|
|
524
|
+
* @param rowId URL id of the REST request
|
|
525
|
+
* @param data HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
461
526
|
*
|
|
462
527
|
*/
|
|
463
|
-
async doBatchUpdate(method, data,
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
528
|
+
async doBatchUpdate(method, rowId, data, sqlParams) {
|
|
529
|
+
return this.runRequest(new OINODbApiRequest({ method: method, rowId: rowId, data: data, sqlParams: sqlParams }));
|
|
530
|
+
}
|
|
531
|
+
/**
|
|
532
|
+
* Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
|
|
533
|
+
*
|
|
534
|
+
* @param request HTTP URL parameters as key-value-pairs
|
|
535
|
+
*
|
|
536
|
+
*/
|
|
537
|
+
async runBatchUpdate(request) {
|
|
538
|
+
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doBatchUpdate", "Request", { request: request, data: request.data });
|
|
539
|
+
let result = new OINODbApiResult(request);
|
|
540
|
+
if ((request.method != "PUT") && (request.method != "DELETE")) {
|
|
541
|
+
result.setError(500, "Batch update only supports PUT and DELETE methods!", "DoBatchUpdate");
|
|
542
|
+
return Promise.resolve(result);
|
|
543
|
+
}
|
|
544
|
+
index_js_1.OINOBenchmark.startMetric("OINODbApi", "doBatchUpdate." + request.method);
|
|
545
|
+
const rows = [] = this._parseData(result, request);
|
|
546
|
+
if (request.method == "PUT") {
|
|
472
547
|
try {
|
|
473
548
|
await this._doPut(result, null, rows);
|
|
474
549
|
}
|
|
@@ -476,7 +551,7 @@ class OINODbApi {
|
|
|
476
551
|
result.setError(500, "Unhandled exception in HTTP PUT doRequest: " + e.message, "DoBatchUpdate");
|
|
477
552
|
}
|
|
478
553
|
}
|
|
479
|
-
else if (method == "DELETE") {
|
|
554
|
+
else if (request.method == "DELETE") {
|
|
480
555
|
try {
|
|
481
556
|
await this._doDelete(result, null, rows);
|
|
482
557
|
}
|
|
@@ -484,10 +559,7 @@ class OINODbApi {
|
|
|
484
559
|
result.setError(500, "Unhandled exception in HTTP DELETE doRequest: " + e.message, "DoBatchUpdate");
|
|
485
560
|
}
|
|
486
561
|
}
|
|
487
|
-
|
|
488
|
-
result.setError(405, "Unsupported HTTP method '" + method + "' for batch update", "DoBatchUpdate");
|
|
489
|
-
}
|
|
490
|
-
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doBatchUpdate." + method);
|
|
562
|
+
index_js_1.OINOBenchmark.endMetric("OINODbApi", "doBatchUpdate." + request.method);
|
|
491
563
|
return Promise.resolve(result);
|
|
492
564
|
}
|
|
493
565
|
/**
|
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");
|