@oino-ts/db 0.17.5 → 0.19.0
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/OINODb.js +4 -3
- package/dist/cjs/OINODbApi.js +83 -34
- package/dist/cjs/OINODbDataField.js +8 -7
- package/dist/cjs/OINODbDataModel.js +3 -2
- package/dist/cjs/OINODbModelSet.js +21 -20
- package/dist/cjs/OINODbParser.js +46 -44
- package/dist/cjs/OINODbSqlParams.js +16 -15
- package/dist/cjs/index.js +1 -17
- package/dist/esm/OINODb.js +2 -1
- package/dist/esm/OINODbApi.js +76 -27
- package/dist/esm/OINODbDataField.js +2 -1
- package/dist/esm/OINODbDataModel.js +2 -1
- package/dist/esm/OINODbModelSet.js +2 -1
- package/dist/esm/OINODbParser.js +11 -9
- package/dist/esm/OINODbSqlParams.js +2 -1
- package/dist/esm/index.js +0 -3
- package/dist/types/OINODb.d.ts +2 -1
- package/dist/types/OINODbApi.d.ts +28 -19
- package/dist/types/OINODbModelSet.d.ts +2 -1
- package/dist/types/OINODbParser.d.ts +7 -4
- package/dist/types/index.d.ts +4 -4
- package/package.json +4 -4
- package/src/OINODb.ts +2 -1
- package/src/OINODbApi.test.ts +54 -53
- package/src/OINODbApi.ts +83 -35
- package/src/OINODbDataField.ts +3 -1
- package/src/OINODbDataModel.ts +2 -1
- package/src/OINODbModelSet.ts +2 -1
- package/src/OINODbParser.ts +11 -10
- package/src/OINODbSqlParams.ts +2 -1
- package/src/index.ts +2 -6
package/dist/cjs/OINODb.js
CHANGED
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbMemoryDataSet = exports.OINODbDataSet = exports.OINODb = void 0;
|
|
9
|
+
const common_1 = require("@oino-ts/common");
|
|
9
10
|
const index_js_1 = require("./index.js");
|
|
10
11
|
/**
|
|
11
12
|
* Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
|
|
@@ -85,7 +86,7 @@ class OINODbDataSet {
|
|
|
85
86
|
*/
|
|
86
87
|
hasErrors() {
|
|
87
88
|
for (let i = 0; i < this.messages.length; i++) {
|
|
88
|
-
if (this.messages[i].startsWith(
|
|
89
|
+
if (this.messages[i].startsWith(common_1.OINO_ERROR_PREFIX)) {
|
|
89
90
|
return true;
|
|
90
91
|
}
|
|
91
92
|
}
|
|
@@ -97,7 +98,7 @@ class OINODbDataSet {
|
|
|
97
98
|
*/
|
|
98
99
|
getFirstError() {
|
|
99
100
|
for (let i = 0; i < this.messages.length; i++) {
|
|
100
|
-
if (this.messages[i].startsWith(
|
|
101
|
+
if (this.messages[i].startsWith(common_1.OINO_ERROR_PREFIX)) {
|
|
101
102
|
return this.messages[i];
|
|
102
103
|
}
|
|
103
104
|
}
|
|
@@ -124,7 +125,7 @@ class OINODbMemoryDataSet extends OINODbDataSet {
|
|
|
124
125
|
constructor(data, errors = []) {
|
|
125
126
|
super(data, errors);
|
|
126
127
|
if ((data == null) || !(Array.isArray(data))) {
|
|
127
|
-
throw new Error(
|
|
128
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
|
|
128
129
|
}
|
|
129
130
|
this._rows = data;
|
|
130
131
|
if (this.isEmpty()) {
|
package/dist/cjs/OINODbApi.js
CHANGED
|
@@ -7,20 +7,25 @@
|
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = exports.OINODbApiRequest = void 0;
|
|
9
9
|
const node_buffer_1 = require("node:buffer");
|
|
10
|
-
const index_js_1 = require("./index.js");
|
|
11
10
|
const common_1 = require("@oino-ts/common");
|
|
11
|
+
const index_js_1 = require("./index.js");
|
|
12
12
|
const hashid_1 = require("@oino-ts/hashid");
|
|
13
13
|
class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
14
14
|
rowId;
|
|
15
|
-
|
|
15
|
+
rowData;
|
|
16
16
|
sqlParams;
|
|
17
17
|
constructor(init) {
|
|
18
18
|
super(init);
|
|
19
19
|
this.rowId = init?.rowId || "";
|
|
20
|
-
this.
|
|
20
|
+
this.rowData = init?.rowData || null;
|
|
21
21
|
this.sqlParams = init?.sqlParams || {};
|
|
22
22
|
if (init?.filter) {
|
|
23
|
-
|
|
23
|
+
if (init.filter instanceof index_js_1.OINODbSqlFilter) {
|
|
24
|
+
this.sqlParams.filter = init.filter;
|
|
25
|
+
}
|
|
26
|
+
else {
|
|
27
|
+
this.sqlParams.filter = index_js_1.OINODbSqlFilter.parse(init.filter);
|
|
28
|
+
}
|
|
24
29
|
}
|
|
25
30
|
if (!this.sqlParams.filter) {
|
|
26
31
|
const filter_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_FILTER_PARAM);
|
|
@@ -29,7 +34,12 @@ class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
|
29
34
|
}
|
|
30
35
|
}
|
|
31
36
|
if (init?.order) {
|
|
32
|
-
|
|
37
|
+
if (init.order instanceof index_js_1.OINODbSqlOrder) {
|
|
38
|
+
this.sqlParams.order = init.order;
|
|
39
|
+
}
|
|
40
|
+
else {
|
|
41
|
+
this.sqlParams.order = index_js_1.OINODbSqlOrder.parse(init.order);
|
|
42
|
+
}
|
|
33
43
|
}
|
|
34
44
|
if (!this.sqlParams.order) {
|
|
35
45
|
const order_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_ORDER_PARAM);
|
|
@@ -38,7 +48,12 @@ class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
|
38
48
|
}
|
|
39
49
|
}
|
|
40
50
|
if (init?.limit) {
|
|
41
|
-
|
|
51
|
+
if (init.limit instanceof index_js_1.OINODbSqlLimit) {
|
|
52
|
+
this.sqlParams.limit = init.limit;
|
|
53
|
+
}
|
|
54
|
+
else {
|
|
55
|
+
this.sqlParams.limit = index_js_1.OINODbSqlLimit.parse(init.limit);
|
|
56
|
+
}
|
|
42
57
|
}
|
|
43
58
|
if (!this.sqlParams.limit) {
|
|
44
59
|
const limit_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_LIMIT_PARAM);
|
|
@@ -47,7 +62,12 @@ class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
|
47
62
|
}
|
|
48
63
|
}
|
|
49
64
|
if (init?.aggregate) {
|
|
50
|
-
|
|
65
|
+
if (init.aggregate instanceof index_js_1.OINODbSqlAggregate) {
|
|
66
|
+
this.sqlParams.aggregate = init.aggregate;
|
|
67
|
+
}
|
|
68
|
+
else {
|
|
69
|
+
this.sqlParams.aggregate = index_js_1.OINODbSqlAggregate.parse(init.aggregate);
|
|
70
|
+
}
|
|
51
71
|
}
|
|
52
72
|
if (!this.sqlParams.aggregate) {
|
|
53
73
|
const aggregate_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_AGGREGATE_PARAM);
|
|
@@ -56,7 +76,12 @@ class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
|
56
76
|
}
|
|
57
77
|
}
|
|
58
78
|
if (init?.select) {
|
|
59
|
-
|
|
79
|
+
if (init.select instanceof index_js_1.OINODbSqlSelect) {
|
|
80
|
+
this.sqlParams.select = init.select;
|
|
81
|
+
}
|
|
82
|
+
else {
|
|
83
|
+
this.sqlParams.select = index_js_1.OINODbSqlSelect.parse(init.select);
|
|
84
|
+
}
|
|
60
85
|
}
|
|
61
86
|
if (!this.sqlParams.select) {
|
|
62
87
|
const select_param = this.url?.searchParams.get(index_js_1.OINODbConfig.OINODB_SQL_SELECT_PARAM);
|
|
@@ -65,13 +90,28 @@ class OINODbApiRequest extends common_1.OINOHttpRequest {
|
|
|
65
90
|
}
|
|
66
91
|
}
|
|
67
92
|
}
|
|
68
|
-
static async fromFetchRequest(request) {
|
|
69
|
-
const body = await request.arrayBuffer();
|
|
93
|
+
static async fromFetchRequest(request, rowId, rowData, sqlParams) {
|
|
70
94
|
return new OINODbApiRequest({
|
|
71
95
|
url: new URL(request.url),
|
|
72
96
|
method: request.method,
|
|
73
97
|
headers: Object.fromEntries(request.headers),
|
|
74
|
-
|
|
98
|
+
rowId: rowId,
|
|
99
|
+
rowData: rowData ?? node_buffer_1.Buffer.from(await request.arrayBuffer()),
|
|
100
|
+
sqlParams: sqlParams
|
|
101
|
+
});
|
|
102
|
+
}
|
|
103
|
+
static fromHttpRequest(request, rowId, rowData, sqlParams) {
|
|
104
|
+
return new OINODbApiRequest({
|
|
105
|
+
url: typeof request.url === "string" ? new URL(request.url) : request.url,
|
|
106
|
+
method: request.method,
|
|
107
|
+
headers: Object.fromEntries(request.headers),
|
|
108
|
+
rowId: rowId,
|
|
109
|
+
rowData: rowData ?? request.bodyAsBuffer(),
|
|
110
|
+
requestType: request.requestType,
|
|
111
|
+
responseType: request.responseType,
|
|
112
|
+
multipartBoundary: request.multipartBoundary,
|
|
113
|
+
lastModified: request.lastModified,
|
|
114
|
+
sqlParams: sqlParams
|
|
75
115
|
});
|
|
76
116
|
}
|
|
77
117
|
}
|
|
@@ -124,7 +164,7 @@ exports.OINODbApiResult = OINODbApiResult;
|
|
|
124
164
|
* Specialized HTML template that can render ´OINODbApiResult´.
|
|
125
165
|
*
|
|
126
166
|
*/
|
|
127
|
-
class OINODbHtmlTemplate extends
|
|
167
|
+
class OINODbHtmlTemplate extends common_1.OINOHtmlTemplate {
|
|
128
168
|
/** Locale validation regex */
|
|
129
169
|
static LOCALE_REGEX = /^(\w\w)(\-\w\w)?$/;
|
|
130
170
|
/** Locale formatter */
|
|
@@ -168,7 +208,7 @@ class OINODbHtmlTemplate extends index_js_1.OINOHtmlTemplate {
|
|
|
168
208
|
*
|
|
169
209
|
*/
|
|
170
210
|
async renderFromDbData(modelset, overrideValues) {
|
|
171
|
-
|
|
211
|
+
common_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromDbData");
|
|
172
212
|
let html = "";
|
|
173
213
|
const dataset = modelset.dataset;
|
|
174
214
|
const datamodel = modelset.datamodel;
|
|
@@ -214,7 +254,7 @@ class OINODbHtmlTemplate extends index_js_1.OINOHtmlTemplate {
|
|
|
214
254
|
}
|
|
215
255
|
this.modified = last_modified;
|
|
216
256
|
const result = this._createHttpResult(html);
|
|
217
|
-
|
|
257
|
+
common_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromDbData");
|
|
218
258
|
return result;
|
|
219
259
|
}
|
|
220
260
|
}
|
|
@@ -244,7 +284,7 @@ class OINODbApi {
|
|
|
244
284
|
*/
|
|
245
285
|
constructor(db, params) {
|
|
246
286
|
if (!params.tableName) {
|
|
247
|
-
throw new Error(
|
|
287
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": OINODbApiParams needs to define a table name!");
|
|
248
288
|
}
|
|
249
289
|
this.db = db;
|
|
250
290
|
this.params = params;
|
|
@@ -288,12 +328,13 @@ class OINODbApi {
|
|
|
288
328
|
}
|
|
289
329
|
_parseData(httpResult, request) {
|
|
290
330
|
let rows = [];
|
|
331
|
+
const data = request.rowData ?? request.body;
|
|
291
332
|
try {
|
|
292
|
-
if (Array.isArray(
|
|
293
|
-
rows =
|
|
333
|
+
if (Array.isArray(data)) {
|
|
334
|
+
rows = data;
|
|
294
335
|
}
|
|
295
|
-
else if (
|
|
296
|
-
rows = index_js_1.OINODbParser.createRows(this.datamodel,
|
|
336
|
+
else if (data != null) {
|
|
337
|
+
rows = index_js_1.OINODbParser.createRows(this.datamodel, data, request.requestType, request.multipartBoundary);
|
|
297
338
|
}
|
|
298
339
|
}
|
|
299
340
|
catch (e) {
|
|
@@ -449,25 +490,33 @@ class OINODbApi {
|
|
|
449
490
|
* Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
450
491
|
* SQL select, insert, update and delete.
|
|
451
492
|
*
|
|
452
|
-
* @param
|
|
493
|
+
* @param request OINO HTTP request object containing all parameters of the REST request
|
|
453
494
|
* @param rowId URL id of the REST request
|
|
454
|
-
* @param
|
|
495
|
+
* @param rowData HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
455
496
|
* @param sqlParams SQL parameters for the REST request
|
|
456
497
|
*
|
|
457
498
|
*/
|
|
458
|
-
async
|
|
459
|
-
|
|
499
|
+
async doHttpRequest(request, rowId, rowData, sqlParams) {
|
|
500
|
+
const api_request = OINODbApiRequest.fromHttpRequest(request, rowId, rowData, sqlParams);
|
|
501
|
+
return this.doApiRequest(api_request);
|
|
460
502
|
}
|
|
461
503
|
/**
|
|
462
504
|
* Method for handling a HTTP REST request with GET, POST, PUT, DELETE corresponding to
|
|
463
505
|
* SQL select, insert, update and delete.
|
|
464
506
|
*
|
|
465
|
-
* @param
|
|
507
|
+
* @param method HTTP method of the REST request
|
|
508
|
+
* @param rowId URL id of the REST request
|
|
509
|
+
* @param rowData HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
510
|
+
* @param sqlParams SQL parameters for the REST request
|
|
511
|
+
* @param contentType content type of the HTTP body data, default is JSON
|
|
466
512
|
*
|
|
467
513
|
*/
|
|
468
|
-
async
|
|
469
|
-
|
|
470
|
-
|
|
514
|
+
async doRequest(method, rowId, rowData, sqlParams, contentType = common_1.OINOContentType.json) {
|
|
515
|
+
return this.doApiRequest(new OINODbApiRequest({ method: method, rowId: rowId, rowData: rowData, sqlParams: sqlParams, requestType: contentType }));
|
|
516
|
+
}
|
|
517
|
+
async doApiRequest(request) {
|
|
518
|
+
common_1.OINOBenchmark.startMetric("OINODbApi", "doRequest." + request.method);
|
|
519
|
+
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doRequest", "Request", { method: request.method, id: request.rowId, data: request.rowData });
|
|
471
520
|
let result = new OINODbApiResult(request);
|
|
472
521
|
let rows = [];
|
|
473
522
|
if ((request.method == "POST") || (request.method == "PUT")) {
|
|
@@ -524,7 +573,7 @@ class OINODbApi {
|
|
|
524
573
|
else {
|
|
525
574
|
result.setError(405, "Unsupported HTTP method '" + request.method + "' for REST request", "DoRequest");
|
|
526
575
|
}
|
|
527
|
-
|
|
576
|
+
common_1.OINOBenchmark.endMetric("OINODbApi", "doRequest." + request.method);
|
|
528
577
|
return Promise.resolve(result);
|
|
529
578
|
}
|
|
530
579
|
/**
|
|
@@ -532,11 +581,11 @@ class OINODbApi {
|
|
|
532
581
|
*
|
|
533
582
|
* @param method HTTP method of the REST request
|
|
534
583
|
* @param rowId URL id of the REST request
|
|
535
|
-
* @param
|
|
584
|
+
* @param rowData HTTP body data as either serialized string or unserialized JS object or OINODataRow-array or Buffer/Uint8Array binary data
|
|
536
585
|
*
|
|
537
586
|
*/
|
|
538
|
-
async doBatchUpdate(method, rowId,
|
|
539
|
-
return this.
|
|
587
|
+
async doBatchUpdate(method, rowId, rowData, sqlParams) {
|
|
588
|
+
return this.doApiRequest(new OINODbApiRequest({ method: method, rowId: rowId, rowData: rowData, sqlParams: sqlParams }));
|
|
540
589
|
}
|
|
541
590
|
/**
|
|
542
591
|
* Method for handling a HTTP REST request with batch update using PUT or DELETE methods.
|
|
@@ -545,13 +594,13 @@ class OINODbApi {
|
|
|
545
594
|
*
|
|
546
595
|
*/
|
|
547
596
|
async runBatchUpdate(request) {
|
|
548
|
-
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doBatchUpdate", "Request", { request: request, data: request.
|
|
597
|
+
common_1.OINOLog.debug("@oino-ts/db", "OINODbApi", "doBatchUpdate", "Request", { request: request, data: request.rowData });
|
|
549
598
|
let result = new OINODbApiResult(request);
|
|
550
599
|
if ((request.method != "PUT") && (request.method != "DELETE")) {
|
|
551
600
|
result.setError(500, "Batch update only supports PUT and DELETE methods!", "DoBatchUpdate");
|
|
552
601
|
return Promise.resolve(result);
|
|
553
602
|
}
|
|
554
|
-
|
|
603
|
+
common_1.OINOBenchmark.startMetric("OINODbApi", "doBatchUpdate." + request.method);
|
|
555
604
|
const rows = [] = this._parseData(result, request);
|
|
556
605
|
if (request.method == "PUT") {
|
|
557
606
|
try {
|
|
@@ -569,7 +618,7 @@ class OINODbApi {
|
|
|
569
618
|
result.setError(500, "Unhandled exception in HTTP DELETE doRequest: " + e.message, "DoBatchUpdate");
|
|
570
619
|
}
|
|
571
620
|
}
|
|
572
|
-
|
|
621
|
+
common_1.OINOBenchmark.endMetric("OINODbApi", "doBatchUpdate." + request.method);
|
|
573
622
|
return Promise.resolve(result);
|
|
574
623
|
}
|
|
575
624
|
/**
|
|
@@ -6,7 +6,8 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINOStringDataField = exports.OINODbDataField = void 0;
|
|
9
|
-
const
|
|
9
|
+
const buffer_1 = require("buffer");
|
|
10
|
+
const common_1 = require("@oino-ts/common");
|
|
10
11
|
/**
|
|
11
12
|
* Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
|
|
12
13
|
*
|
|
@@ -247,8 +248,8 @@ class OINONumberDataField extends OINODbDataField {
|
|
|
247
248
|
else {
|
|
248
249
|
const result = parseFloat(value);
|
|
249
250
|
if (isNaN(result)) {
|
|
250
|
-
|
|
251
|
-
throw new Error(
|
|
251
|
+
common_1.OINOLog.error("@oino-ts/db", "OINONumberDataField", "toSql", "Invalid value!", { value: value });
|
|
252
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": OINONumberDataField.deserializeCell - Invalid value '" + value + "'"); // incorrectly formatted data could be a security risk, abort processing
|
|
252
253
|
}
|
|
253
254
|
return result;
|
|
254
255
|
}
|
|
@@ -284,11 +285,11 @@ class OINOBlobDataField extends OINODbDataField {
|
|
|
284
285
|
if ((cellVal === null) || (cellVal === undefined)) {
|
|
285
286
|
return cellVal;
|
|
286
287
|
}
|
|
287
|
-
else if (cellVal instanceof Buffer) {
|
|
288
|
+
else if (cellVal instanceof buffer_1.Buffer) {
|
|
288
289
|
return cellVal.toString('base64');
|
|
289
290
|
}
|
|
290
291
|
else if (cellVal instanceof Uint8Array) {
|
|
291
|
-
return Buffer.from(cellVal).toString('base64');
|
|
292
|
+
return buffer_1.Buffer.from(cellVal).toString('base64');
|
|
292
293
|
}
|
|
293
294
|
else {
|
|
294
295
|
return this.db.parseSqlValueAsCell(cellVal, this.sqlType)?.toString();
|
|
@@ -302,10 +303,10 @@ class OINOBlobDataField extends OINODbDataField {
|
|
|
302
303
|
*/
|
|
303
304
|
deserializeCell(value) {
|
|
304
305
|
if (value == null) {
|
|
305
|
-
return Buffer.alloc(0);
|
|
306
|
+
return buffer_1.Buffer.alloc(0);
|
|
306
307
|
}
|
|
307
308
|
else {
|
|
308
|
-
return Buffer.from(value, 'base64'); // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
|
|
309
|
+
return buffer_1.Buffer.from(value, 'base64'); // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
|
|
309
310
|
}
|
|
310
311
|
}
|
|
311
312
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbDataModel = void 0;
|
|
9
|
+
const common_1 = require("@oino-ts/common");
|
|
9
10
|
const index_js_1 = require("./index.js");
|
|
10
11
|
/**
|
|
11
12
|
* OINO Datamodel object for representing one database table and it's columns.
|
|
@@ -97,14 +98,14 @@ class OINODbDataModel {
|
|
|
97
98
|
}
|
|
98
99
|
value = f.printCellAsSqlValue(value);
|
|
99
100
|
if (value == "") { // ids are user input and could be specially crafted to be empty
|
|
100
|
-
throw new Error(
|
|
101
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": empty condition for id '" + id_value + "' for table " + this.api.params.tableName);
|
|
101
102
|
}
|
|
102
103
|
result += f.printSqlColumnName() + "=" + value;
|
|
103
104
|
i = i + 1;
|
|
104
105
|
}
|
|
105
106
|
}
|
|
106
107
|
if (i != id_parts.length) {
|
|
107
|
-
throw new Error(
|
|
108
|
+
throw new Error(common_1.OINO_ERROR_PREFIX + ": id '" + id_value + "' is not a valid key for table " + this.api.params.tableName);
|
|
108
109
|
}
|
|
109
110
|
return "(" + result + ")";
|
|
110
111
|
}
|
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbModelSet = void 0;
|
|
9
|
+
const common_1 = require("@oino-ts/common");
|
|
9
10
|
const index_js_1 = require("./index.js");
|
|
10
11
|
/**
|
|
11
12
|
* Class for dataset based on a data model that can be serialized to
|
|
@@ -46,7 +47,7 @@ class OINODbModelSet {
|
|
|
46
47
|
primaryKeyValues.push(value || "");
|
|
47
48
|
}
|
|
48
49
|
}
|
|
49
|
-
result =
|
|
50
|
+
result = common_1.OINOStr.encode(value, contentType);
|
|
50
51
|
return result;
|
|
51
52
|
}
|
|
52
53
|
_writeRowJson(row) {
|
|
@@ -66,19 +67,19 @@ class OINODbModelSet {
|
|
|
66
67
|
// skip undefined values
|
|
67
68
|
}
|
|
68
69
|
else if (value === null) {
|
|
69
|
-
json_row += "," +
|
|
70
|
+
json_row += "," + common_1.OINOStr.encode(f.name, common_1.OINOContentType.json) + ":null";
|
|
70
71
|
}
|
|
71
72
|
else {
|
|
72
73
|
let is_hashed = (f.fieldParams.isPrimaryKey || f.fieldParams.isForeignKey) && (f instanceof index_js_1.OINONumberDataField) && (this.datamodel.api.hashid != null);
|
|
73
74
|
let is_value = (f instanceof index_js_1.OINOBooleanDataField) || ((f instanceof index_js_1.OINONumberDataField) && !is_hashed);
|
|
74
|
-
value = this._encodeAndHashFieldValue(f, value,
|
|
75
|
+
value = this._encodeAndHashFieldValue(f, value, common_1.OINOContentType.json, primary_key_values, f.name + " " + row_id_seed);
|
|
75
76
|
if (is_value) {
|
|
76
77
|
value = value.substring(1, value.length - 1);
|
|
77
78
|
}
|
|
78
|
-
json_row += "," +
|
|
79
|
+
json_row += "," + common_1.OINOStr.encode(f.name, common_1.OINOContentType.json) + ":" + value;
|
|
79
80
|
}
|
|
80
81
|
}
|
|
81
|
-
json_row =
|
|
82
|
+
json_row = common_1.OINOStr.encode(index_js_1.OINODbConfig.OINODB_ID_FIELD, common_1.OINOContentType.json) + ":" + common_1.OINOStr.encode(index_js_1.OINODbConfig.printOINOId(primary_key_values), common_1.OINOContentType.json) + json_row;
|
|
82
83
|
return "{" + json_row + "}";
|
|
83
84
|
}
|
|
84
85
|
async _writeStringJson() {
|
|
@@ -119,14 +120,14 @@ class OINODbModelSet {
|
|
|
119
120
|
}
|
|
120
121
|
let value = f.serializeCell(row[i]);
|
|
121
122
|
if (value == null) {
|
|
122
|
-
csv_row += "," +
|
|
123
|
+
csv_row += "," + common_1.OINOStr.encode(value, common_1.OINOContentType.csv); // either null or undefined
|
|
123
124
|
}
|
|
124
125
|
else {
|
|
125
|
-
value = this._encodeAndHashFieldValue(f, value,
|
|
126
|
+
value = this._encodeAndHashFieldValue(f, value, common_1.OINOContentType.csv, primary_key_values, f.name + " " + row_id_seed);
|
|
126
127
|
csv_row += "," + value;
|
|
127
128
|
}
|
|
128
129
|
}
|
|
129
|
-
csv_row =
|
|
130
|
+
csv_row = common_1.OINOStr.encode(index_js_1.OINODbConfig.printOINOId(primary_key_values), common_1.OINOContentType.csv) + csv_row;
|
|
130
131
|
return csv_row;
|
|
131
132
|
}
|
|
132
133
|
async _writeStringCsv() {
|
|
@@ -168,13 +169,13 @@ class OINODbModelSet {
|
|
|
168
169
|
let formdata_block = "";
|
|
169
170
|
let is_file = (f instanceof index_js_1.OINOBlobDataField);
|
|
170
171
|
if (value === undefined) {
|
|
171
|
-
|
|
172
|
+
common_1.OINOLog.info("@oino-ts/db", "OINODbModelSet", "_writeRowFormdata", "Undefined value skipped", { field_name: f.name });
|
|
172
173
|
}
|
|
173
174
|
else if (value === null) {
|
|
174
175
|
formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, null, multipart_boundary);
|
|
175
176
|
}
|
|
176
177
|
else {
|
|
177
|
-
value = this._encodeAndHashFieldValue(f, value,
|
|
178
|
+
value = this._encodeAndHashFieldValue(f, value, common_1.OINOContentType.formdata, primary_key_values, f.name + " " + row_id_seed);
|
|
178
179
|
if (is_file) {
|
|
179
180
|
formdata_block = this._writeRowFormdataFileBlock(f.name, value, multipart_boundary);
|
|
180
181
|
}
|
|
@@ -208,14 +209,14 @@ class OINODbModelSet {
|
|
|
208
209
|
// console.log("OINODbModelSet._writeRowUrlencode undefined field value:" + fields[i].name)
|
|
209
210
|
}
|
|
210
211
|
else {
|
|
211
|
-
value = this._encodeAndHashFieldValue(f, value,
|
|
212
|
+
value = this._encodeAndHashFieldValue(f, value, common_1.OINOContentType.urlencode, primary_key_values, f.name + " " + row_id_seed);
|
|
212
213
|
if (urlencode_row != "") {
|
|
213
214
|
urlencode_row += "&";
|
|
214
215
|
}
|
|
215
|
-
urlencode_row +=
|
|
216
|
+
urlencode_row += common_1.OINOStr.encode(f.name, common_1.OINOContentType.urlencode) + "=" + value;
|
|
216
217
|
}
|
|
217
218
|
}
|
|
218
|
-
urlencode_row =
|
|
219
|
+
urlencode_row = common_1.OINOStr.encode(index_js_1.OINODbConfig.OINODB_ID_FIELD, common_1.OINOContentType.urlencode) + "=" + common_1.OINOStr.encode(index_js_1.OINODbConfig.printOINOId(primary_key_values), common_1.OINOContentType.urlencode) + "&" + urlencode_row;
|
|
219
220
|
return urlencode_row;
|
|
220
221
|
}
|
|
221
222
|
async _writeStringUrlencode() {
|
|
@@ -228,7 +229,7 @@ class OINODbModelSet {
|
|
|
228
229
|
line_count += 1;
|
|
229
230
|
}
|
|
230
231
|
if (line_count > 1) {
|
|
231
|
-
|
|
232
|
+
common_1.OINOLog.warning("@oino-ts/db", "OINODbModelSet", "_writeStringUrlencode", "Content type " + common_1.OINOContentType.urlencode + " does not officially support multiline content!", {});
|
|
232
233
|
}
|
|
233
234
|
return result;
|
|
234
235
|
}
|
|
@@ -267,22 +268,22 @@ class OINODbModelSet {
|
|
|
267
268
|
* @param [contentType=OINOContentType.json] serialization content type
|
|
268
269
|
*
|
|
269
270
|
*/
|
|
270
|
-
async writeString(contentType =
|
|
271
|
+
async writeString(contentType = common_1.OINOContentType.json) {
|
|
271
272
|
let result = "";
|
|
272
|
-
if (contentType ==
|
|
273
|
+
if (contentType == common_1.OINOContentType.csv) {
|
|
273
274
|
result += await this._writeStringCsv();
|
|
274
275
|
}
|
|
275
|
-
else if (contentType ==
|
|
276
|
+
else if (contentType == common_1.OINOContentType.json) {
|
|
276
277
|
result += await this._writeStringJson();
|
|
277
278
|
}
|
|
278
|
-
else if (contentType ==
|
|
279
|
+
else if (contentType == common_1.OINOContentType.formdata) {
|
|
279
280
|
result += await this._writeStringFormdata();
|
|
280
281
|
}
|
|
281
|
-
else if (contentType ==
|
|
282
|
+
else if (contentType == common_1.OINOContentType.urlencode) {
|
|
282
283
|
result += await this._writeStringUrlencode();
|
|
283
284
|
}
|
|
284
285
|
else {
|
|
285
|
-
|
|
286
|
+
common_1.OINOLog.error("@oino-ts/db", "OINODbModelSet", "writeString", "Content type is only for input!", { contentType: contentType });
|
|
286
287
|
}
|
|
287
288
|
return result;
|
|
288
289
|
}
|