@oino-ts/common 0.21.2 → 1.0.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.
Files changed (69) hide show
  1. package/README.md +183 -0
  2. package/dist/cjs/OINOApi.js +322 -0
  3. package/dist/cjs/OINOConfig.js +104 -0
  4. package/dist/cjs/OINOConstants.js +42 -0
  5. package/dist/cjs/OINODataField.js +346 -0
  6. package/dist/cjs/OINODataModel.js +182 -0
  7. package/dist/cjs/OINODataSource.js +165 -0
  8. package/dist/cjs/OINOFormatter.js +6 -5
  9. package/dist/cjs/OINOHtmlTemplate.js +21 -18
  10. package/dist/cjs/OINOModelSet.js +333 -0
  11. package/dist/cjs/OINOParser.js +448 -0
  12. package/dist/cjs/OINOQueryParams.js +434 -0
  13. package/dist/cjs/OINORequest.js +21 -13
  14. package/dist/cjs/OINOResult.js +13 -12
  15. package/dist/cjs/OINOStr.js +11 -11
  16. package/dist/cjs/OINOSwagger.js +205 -0
  17. package/dist/cjs/index.js +57 -39
  18. package/dist/esm/OINOApi.js +315 -0
  19. package/dist/esm/OINOConfig.js +100 -0
  20. package/dist/esm/OINOConstants.js +39 -0
  21. package/dist/esm/OINODataField.js +337 -0
  22. package/dist/esm/OINODataModel.js +178 -0
  23. package/dist/esm/OINODataSource.js +159 -0
  24. package/dist/esm/OINOFormatter.js +2 -1
  25. package/dist/esm/OINOHtmlTemplate.js +4 -1
  26. package/dist/esm/OINOModelSet.js +329 -0
  27. package/dist/esm/OINOParser.js +444 -0
  28. package/dist/esm/OINOQueryParams.js +426 -0
  29. package/dist/esm/OINORequest.js +9 -1
  30. package/dist/esm/OINOResult.js +2 -1
  31. package/dist/esm/OINOStr.js +1 -1
  32. package/dist/esm/OINOSwagger.js +201 -0
  33. package/dist/esm/index.js +14 -32
  34. package/dist/types/OINOApi.d.ts +191 -0
  35. package/dist/types/OINOConfig.d.ts +63 -0
  36. package/dist/types/OINOConstants.d.ts +51 -0
  37. package/dist/types/OINODataField.d.ts +209 -0
  38. package/dist/types/OINODataModel.d.ts +78 -0
  39. package/dist/types/OINODataSource.d.ts +184 -0
  40. package/dist/types/OINOHtmlTemplate.d.ts +1 -1
  41. package/dist/types/OINOModelSet.d.ts +64 -0
  42. package/dist/types/OINOParser.d.ts +42 -0
  43. package/dist/types/OINOQueryParams.d.ts +270 -0
  44. package/dist/types/OINORequest.d.ts +4 -1
  45. package/dist/types/OINOResult.d.ts +1 -1
  46. package/dist/types/OINOStr.d.ts +1 -1
  47. package/dist/types/OINOSwagger.d.ts +25 -0
  48. package/dist/types/index.d.ts +14 -31
  49. package/package.json +32 -32
  50. package/src/OINOApi.ts +429 -0
  51. package/src/OINOBenchmark.ts +323 -323
  52. package/src/OINOConfig.ts +113 -0
  53. package/src/OINOConstants.ts +59 -0
  54. package/src/OINODataField.ts +371 -0
  55. package/src/OINODataModel.ts +187 -0
  56. package/src/OINODataSource.ts +280 -0
  57. package/src/OINOFormatter.ts +166 -165
  58. package/src/OINOHeaders.ts +51 -51
  59. package/src/OINOHtmlTemplate.test.ts +114 -114
  60. package/src/OINOHtmlTemplate.ts +225 -222
  61. package/src/OINOLog.ts +292 -292
  62. package/src/OINOModelSet.ts +359 -0
  63. package/src/OINOParser.ts +441 -0
  64. package/src/OINOQueryParams.ts +449 -0
  65. package/src/OINORequest.ts +204 -196
  66. package/src/OINOResult.ts +331 -330
  67. package/src/OINOStr.ts +254 -254
  68. package/src/OINOSwagger.ts +213 -0
  69. package/src/index.ts +18 -38
@@ -6,7 +6,8 @@
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINO_EMPTY_FORMATTER = exports.OINOFormatter = void 0;
9
- const index_js_1 = require("./index.js");
9
+ const OINOConstants_js_1 = require("./OINOConstants.js");
10
+ const OINOLog_js_1 = require("./OINOLog.js");
10
11
  /**
11
12
  * Class for formatting strings and values.
12
13
  *
@@ -53,8 +54,8 @@ class OINOFormatter {
53
54
  for (let i = 0; i < formatters.length; i++) {
54
55
  let match = formatters[i]?.match(this.OINO_FORMATTER_REGEXP);
55
56
  if (!match) {
56
- index_js_1.OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Invalid formatter string", { formatter: formatters[i] });
57
- throw new Error(index_js_1.OINO_ERROR_PREFIX + "Invalid formatter: " + formatters[i]);
57
+ OINOLog_js_1.OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Invalid formatter string", { formatter: formatters[i] });
58
+ throw new Error(OINOConstants_js_1.OINO_ERROR_PREFIX + "Invalid formatter: " + formatters[i]);
58
59
  }
59
60
  else {
60
61
  const formatter_type = match[1].toLowerCase().substring(0, match[1].indexOf('('));
@@ -81,8 +82,8 @@ class OINOFormatter {
81
82
  formatter_params.push(decodeURIComponent(match[15]), decodeURIComponent(match[16]));
82
83
  }
83
84
  else {
84
- index_js_1.OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Unknown formatter type", { formatter: formatters[i] });
85
- throw new Error(index_js_1.OINO_ERROR_PREFIX + "Unsupported formatter: " + formatters[i]);
85
+ OINOLog_js_1.OINOLog.error("@oino-ts/common", "OINOFormatter", "parse", "Unknown formatter type", { formatter: formatters[i] });
86
+ throw new Error(OINOConstants_js_1.OINO_ERROR_PREFIX + "Unsupported formatter: " + formatters[i]);
86
87
  }
87
88
  types.push(formatter_type);
88
89
  params.push(formatter_params);
@@ -1,7 +1,10 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
3
  exports.OINOHtmlTemplate = void 0;
4
- const _1 = require(".");
4
+ const OINOConstants_js_1 = require("./OINOConstants.js");
5
+ const OINOStr_js_1 = require("./OINOStr.js");
6
+ const OINOResult_js_1 = require("./OINOResult.js");
7
+ const OINOBenchmark_js_1 = require("./OINOBenchmark.js");
5
8
  const OINOFormatter_1 = require("./OINOFormatter");
6
9
  /**
7
10
  * Class for rendering HTML from data.
@@ -66,7 +69,7 @@ class OINOHtmlTemplate {
66
69
  }
67
70
  }
68
71
  _createHttpResult(html) {
69
- const result = new _1.OINOHttpResult({ body: html });
72
+ const result = new OINOResult_js_1.OINOHttpResult({ body: html });
70
73
  if (this.expires >= 1) {
71
74
  result.expires = Math.round(this.expires);
72
75
  }
@@ -111,7 +114,7 @@ class OINOHtmlTemplate {
111
114
  */
112
115
  setVariableFromValue(variable, value, escapeValue = true) {
113
116
  if (escapeValue) {
114
- value = _1.OINOStr.encode(value, _1.OINOContentType.html);
117
+ value = OINOStr_js_1.OINOStr.encode(value, OINOConstants_js_1.OINOContentType.html);
115
118
  }
116
119
  this._variables[variable] = value;
117
120
  }
@@ -126,7 +129,7 @@ class OINOHtmlTemplate {
126
129
  if (object) {
127
130
  for (let key in object) {
128
131
  if (escapeValue) {
129
- this._variables[key] = _1.OINOStr.encode(object[key], _1.OINOContentType.html);
132
+ this._variables[key] = OINOStr_js_1.OINOStr.encode(object[key], OINOConstants_js_1.OINOContentType.html);
130
133
  }
131
134
  else {
132
135
  this._variables[key] = object[key];
@@ -151,10 +154,10 @@ class OINOHtmlTemplate {
151
154
  *
152
155
  */
153
156
  renderFromKeyValue(key, value) {
154
- _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue");
157
+ OINOBenchmark_js_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromKeyValue");
155
158
  this.setVariableFromValue(key, value);
156
159
  const result = this.render();
157
- _1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue");
160
+ OINOBenchmark_js_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromKeyValue");
158
161
  return result;
159
162
  }
160
163
  /**
@@ -164,10 +167,10 @@ class OINOHtmlTemplate {
164
167
  *
165
168
  */
166
169
  renderFromObject(object = true) {
167
- _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject");
170
+ OINOBenchmark_js_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromObject");
168
171
  this.setVariableFromProperties(object);
169
172
  const result = this.render();
170
- _1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject");
173
+ OINOBenchmark_js_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromObject");
171
174
  return result;
172
175
  }
173
176
  /**
@@ -182,29 +185,29 @@ class OINOHtmlTemplate {
182
185
  *
183
186
  */
184
187
  renderFromResult(result, messageSeparator = "", includeErrorMessages = false, includeWarningMessages = false, includeInfoMessages = false, includeDebugMessages = false) {
185
- _1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
188
+ OINOBenchmark_js_1.OINOBenchmark.startMetric("OINOHtmlTemplate", "renderFromResult");
186
189
  this.setVariableFromValue("status", result.status.toString());
187
190
  this.setVariableFromValue("statusText", result.statusText.toString());
188
191
  let messages = [];
189
192
  for (let i = 0; i < result.messages.length; i++) {
190
- if (includeErrorMessages && result.messages[i].startsWith(_1.OINO_ERROR_PREFIX)) {
191
- messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
193
+ if (includeErrorMessages && result.messages[i].startsWith(OINOConstants_js_1.OINO_ERROR_PREFIX)) {
194
+ messages.push(OINOStr_js_1.OINOStr.encode(result.messages[i], OINOConstants_js_1.OINOContentType.html));
192
195
  }
193
- if (includeWarningMessages && result.messages[i].startsWith(_1.OINO_WARNING_PREFIX)) {
194
- messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
196
+ if (includeWarningMessages && result.messages[i].startsWith(OINOConstants_js_1.OINO_WARNING_PREFIX)) {
197
+ messages.push(OINOStr_js_1.OINOStr.encode(result.messages[i], OINOConstants_js_1.OINOContentType.html));
195
198
  }
196
- if (includeInfoMessages && result.messages[i].startsWith(_1.OINO_INFO_PREFIX)) {
197
- messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
199
+ if (includeInfoMessages && result.messages[i].startsWith(OINOConstants_js_1.OINO_INFO_PREFIX)) {
200
+ messages.push(OINOStr_js_1.OINOStr.encode(result.messages[i], OINOConstants_js_1.OINOContentType.html));
198
201
  }
199
- if (includeDebugMessages && result.messages[i].startsWith(_1.OINO_DEBUG_PREFIX)) {
200
- messages.push(_1.OINOStr.encode(result.messages[i], _1.OINOContentType.html));
202
+ if (includeDebugMessages && result.messages[i].startsWith(OINOConstants_js_1.OINO_DEBUG_PREFIX)) {
203
+ messages.push(OINOStr_js_1.OINOStr.encode(result.messages[i], OINOConstants_js_1.OINOContentType.html));
201
204
  }
202
205
  }
203
206
  if (messageSeparator && (messages.length > 0)) {
204
207
  this.setVariableFromValue("messages", messages.join(messageSeparator), false); // messages have been escaped already
205
208
  }
206
209
  const http_result = this.render();
207
- _1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult");
210
+ OINOBenchmark_js_1.OINOBenchmark.endMetric("OINOHtmlTemplate", "renderFromResult");
208
211
  return http_result;
209
212
  }
210
213
  }
@@ -0,0 +1,333 @@
1
+ "use strict";
2
+ /*
3
+ * This Source Code Form is subject to the terms of the Mozilla Public
4
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
5
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
6
+ */
7
+ Object.defineProperty(exports, "__esModule", { value: true });
8
+ exports.OINOModelSet = void 0;
9
+ const OINOConstants_js_1 = require("./OINOConstants.js");
10
+ const OINOConfig_js_1 = require("./OINOConfig.js");
11
+ const OINOStr_js_1 = require("./OINOStr.js");
12
+ const OINOLog_js_1 = require("./OINOLog.js");
13
+ const OINODataField_js_1 = require("./OINODataField.js");
14
+ /**
15
+ * Class for dataset based on a data model that can be serialized to
16
+ * a supported format:
17
+ * - JSON (application/json)
18
+ * - CSV (text/csv)
19
+ *
20
+ */
21
+ class OINOModelSet {
22
+ /** Reference to datamodel */
23
+ datamodel;
24
+ /** Reference to data set */
25
+ dataset;
26
+ /** SQL parameters */
27
+ queryParams;
28
+ /** Collection of errors */
29
+ errors;
30
+ /**
31
+ * Constructor for `OINOModelSet`.
32
+ *
33
+ * @param datamodel data model
34
+ * @param dataset data set
35
+ * @param queryParams SQL parameters
36
+ */
37
+ constructor(datamodel, dataset, queryParams) {
38
+ this.datamodel = datamodel;
39
+ this.dataset = dataset;
40
+ this.queryParams = queryParams;
41
+ this.errors = this.dataset.messages;
42
+ }
43
+ _encodeAndHashFieldValue(field, value, contentType, primaryKeyValues, rowIdSeed) {
44
+ let result;
45
+ if (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) {
46
+ if (value && (field instanceof OINODataField_js_1.OINONumberDataField) && (this.datamodel.api.hashid) && ((this.queryParams?.aggregate === undefined) || (this.queryParams.aggregate.isAggregated(field.name) == false))) {
47
+ value = this.datamodel.api.hashid.encode(value, rowIdSeed);
48
+ }
49
+ if (field.fieldParams.isPrimaryKey) {
50
+ primaryKeyValues.push(value || "");
51
+ }
52
+ }
53
+ result = OINOStr_js_1.OINOStr.encode(value, contentType);
54
+ return result;
55
+ }
56
+ _writeRowJson(row) {
57
+ // console.log("OINOModelSet._writeRowJson: row=" + row)
58
+ const model = this.datamodel;
59
+ const fields = model.fields;
60
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
61
+ let primary_key_values = [];
62
+ let json_row = "";
63
+ for (let i = 0; i < fields.length; i++) {
64
+ const f = fields[i];
65
+ if ((this.queryParams?.select?.isSelected(f.name) === false) && (f.fieldParams.isPrimaryKey == false)) {
66
+ continue;
67
+ }
68
+ let value = f.serializeCell(row[i]);
69
+ if (value === undefined) {
70
+ // skip undefined values
71
+ }
72
+ else if (value === null) {
73
+ json_row += "," + OINOStr_js_1.OINOStr.encode(f.name, OINOConstants_js_1.OINOContentType.json) + ":null";
74
+ }
75
+ else {
76
+ let is_hashed = (f.fieldParams.isPrimaryKey || f.fieldParams.isForeignKey) && (f instanceof OINODataField_js_1.OINONumberDataField) && (this.datamodel.api.hashid != null);
77
+ let is_value = (f instanceof OINODataField_js_1.OINOBooleanDataField) || ((f instanceof OINODataField_js_1.OINONumberDataField) && !is_hashed);
78
+ value = this._encodeAndHashFieldValue(f, value, OINOConstants_js_1.OINOContentType.json, primary_key_values, f.name + " " + row_id_seed);
79
+ if (is_value) {
80
+ value = value.substring(1, value.length - 1);
81
+ }
82
+ json_row += "," + OINOStr_js_1.OINOStr.encode(f.name, OINOConstants_js_1.OINOContentType.json) + ":" + value;
83
+ }
84
+ }
85
+ json_row = OINOStr_js_1.OINOStr.encode(OINOConfig_js_1.OINOConfig.OINO_ID_FIELD, OINOConstants_js_1.OINOContentType.json) + ":" + OINOStr_js_1.OINOStr.encode(OINOConfig_js_1.OINOConfig.printOINOId(primary_key_values), OINOConstants_js_1.OINOContentType.json) + json_row;
86
+ return "{" + json_row + "}";
87
+ }
88
+ async _writeStringJson() {
89
+ let result = "";
90
+ while (!this.dataset.isEof()) {
91
+ if (result != "") {
92
+ result += ",\r\n";
93
+ }
94
+ const row = this.dataset.getRow();
95
+ result += this._writeRowJson(row);
96
+ await this.dataset.next();
97
+ }
98
+ result = "[\r\n" + result + "\r\n]";
99
+ return result;
100
+ }
101
+ _writeHeaderCsv() {
102
+ const model = this.datamodel;
103
+ const fields = model.fields;
104
+ let csv_header = "\"" + OINOConfig_js_1.OINOConfig.OINO_ID_FIELD + "\"";
105
+ for (let i = 0; i < fields.length; i++) {
106
+ if ((this.queryParams?.select?.isSelected(fields[i].name) === false) && (fields[i].fieldParams.isPrimaryKey == false)) {
107
+ continue;
108
+ }
109
+ csv_header += ",\"" + fields[i].name + "\"";
110
+ }
111
+ return csv_header;
112
+ }
113
+ _writeRowCsv(row) {
114
+ const model = this.datamodel;
115
+ const fields = model.fields;
116
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
117
+ let primary_key_values = [];
118
+ let csv_row = "";
119
+ for (let i = 0; i < fields.length; i++) {
120
+ const f = fields[i];
121
+ if ((this.queryParams?.select?.isSelected(f.name) === false) && (f.fieldParams.isPrimaryKey == false)) {
122
+ continue;
123
+ }
124
+ let value = f.serializeCell(row[i]);
125
+ if (value == null) {
126
+ csv_row += "," + OINOStr_js_1.OINOStr.encode(value, OINOConstants_js_1.OINOContentType.csv); // either null or undefined
127
+ }
128
+ else {
129
+ value = this._encodeAndHashFieldValue(f, value, OINOConstants_js_1.OINOContentType.csv, primary_key_values, f.name + " " + row_id_seed);
130
+ csv_row += "," + value;
131
+ }
132
+ }
133
+ csv_row = OINOStr_js_1.OINOStr.encode(OINOConfig_js_1.OINOConfig.printOINOId(primary_key_values), OINOConstants_js_1.OINOContentType.csv) + csv_row;
134
+ return csv_row;
135
+ }
136
+ async _writeStringCsv() {
137
+ let result = this._writeHeaderCsv();
138
+ while (!this.dataset.isEof()) {
139
+ if (result != "") {
140
+ result += "\r\n";
141
+ }
142
+ const row = this.dataset.getRow();
143
+ result += this._writeRowCsv(row);
144
+ await this.dataset.next();
145
+ }
146
+ return result;
147
+ }
148
+ _writeRowFormdataParameterBlock(blockName, blockValue, multipartBoundary) {
149
+ if (blockValue === null) {
150
+ return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"\r\n\r\n";
151
+ }
152
+ else {
153
+ return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"\r\n\r\n" + blockValue + "\r\n";
154
+ }
155
+ }
156
+ _writeRowFormdataFileBlock(blockName, blockValue, multipartBoundary) {
157
+ return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"; filename=" + blockName + "\"\r\nContent-Type: application/octet-stream\r\nContent-Transfer-Encoding: BASE64\r\n\r\n" + blockValue + "\r\n";
158
+ }
159
+ _writeRowFormdata(row) {
160
+ const multipart_boundary = "---------OINOMultipartBoundary35424568"; // this method is just used for test data generation and we want it to be static
161
+ const model = this.datamodel;
162
+ const fields = model.fields;
163
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
164
+ let primary_key_values = [];
165
+ let result = "";
166
+ for (let i = 0; i < fields.length; i++) {
167
+ const f = fields[i];
168
+ if ((this.queryParams?.select?.isSelected(f.name) === false) && (f.fieldParams.isPrimaryKey == false)) {
169
+ continue;
170
+ }
171
+ let value = f.serializeCell(row[i]);
172
+ let formdata_block = "";
173
+ let is_file = (f instanceof OINODataField_js_1.OINOBlobDataField);
174
+ if (value === undefined) {
175
+ OINOLog_js_1.OINOLog.info("@oino-ts/db", "OINOModelSet", "_writeRowFormdata", "Undefined value skipped", { field_name: f.name });
176
+ }
177
+ else if (value === null) {
178
+ formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, null, multipart_boundary);
179
+ }
180
+ else {
181
+ value = this._encodeAndHashFieldValue(f, value, OINOConstants_js_1.OINOContentType.formdata, primary_key_values, f.name + " " + row_id_seed);
182
+ if (is_file) {
183
+ formdata_block = this._writeRowFormdataFileBlock(f.name, value, multipart_boundary);
184
+ }
185
+ else {
186
+ formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, value, multipart_boundary);
187
+ }
188
+ }
189
+ result += formdata_block;
190
+ }
191
+ result = this._writeRowFormdataParameterBlock(OINOConfig_js_1.OINOConfig.OINO_ID_FIELD, OINOConfig_js_1.OINOConfig.printOINOId(primary_key_values), multipart_boundary) + result;
192
+ return result;
193
+ }
194
+ _writeStringFormdata() {
195
+ const row = this.dataset.getRow();
196
+ let result = this._writeRowFormdata(row);
197
+ return result;
198
+ }
199
+ _writeRowUrlencode(row) {
200
+ const model = this.datamodel;
201
+ const fields = model.fields;
202
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
203
+ let primary_key_values = [];
204
+ let urlencode_row = "";
205
+ for (let i = 0; i < fields.length; i++) {
206
+ const f = fields[i];
207
+ if ((this.queryParams?.select?.isSelected(f.name) === false) && (f.fieldParams.isPrimaryKey == false)) {
208
+ continue;
209
+ }
210
+ let value = f.serializeCell(row[i]);
211
+ if ((value === undefined)) { // || (value === null)) {
212
+ // console.log("OINOModelSet._writeRowUrlencode undefined field value:" + fields[i].name)
213
+ }
214
+ else {
215
+ value = this._encodeAndHashFieldValue(f, value, OINOConstants_js_1.OINOContentType.urlencode, primary_key_values, f.name + " " + row_id_seed);
216
+ if (urlencode_row != "") {
217
+ urlencode_row += "&";
218
+ }
219
+ urlencode_row += OINOStr_js_1.OINOStr.encode(f.name, OINOConstants_js_1.OINOContentType.urlencode) + "=" + value;
220
+ }
221
+ }
222
+ urlencode_row = OINOStr_js_1.OINOStr.encode(OINOConfig_js_1.OINOConfig.OINO_ID_FIELD, OINOConstants_js_1.OINOContentType.urlencode) + "=" + OINOStr_js_1.OINOStr.encode(OINOConfig_js_1.OINOConfig.printOINOId(primary_key_values), OINOConstants_js_1.OINOContentType.urlencode) + "&" + urlencode_row;
223
+ return urlencode_row;
224
+ }
225
+ async _writeStringUrlencode() {
226
+ let result = "";
227
+ let line_count = 0;
228
+ while (!this.dataset.isEof()) {
229
+ const row = this.dataset.getRow();
230
+ result += this._writeRowUrlencode(row) + "\r\n";
231
+ await this.dataset.next();
232
+ line_count += 1;
233
+ }
234
+ if (line_count > 1) {
235
+ OINOLog_js_1.OINOLog.warning("@oino-ts/db", "OINOModelSet", "_writeStringUrlencode", "Content type " + OINOConstants_js_1.OINOContentType.urlencode + " does not officially support multiline content!", {});
236
+ }
237
+ return result;
238
+ }
239
+ _exportRow(row) {
240
+ // console.log("OINOModelSet._exportRow: row=" + row)
241
+ const model = this.datamodel;
242
+ const fields = model.fields;
243
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
244
+ let primary_key_values = [];
245
+ let result = {};
246
+ for (let i = 0; i < fields.length; i++) {
247
+ const f = fields[i];
248
+ if (f.fieldParams.isPrimaryKey) {
249
+ primary_key_values.push(f.serializeCell(row[i]) || "");
250
+ }
251
+ if ((this.queryParams?.select?.isSelected(f.name) === false) && (f.fieldParams.isPrimaryKey == false)) {
252
+ continue;
253
+ }
254
+ let value = f.datasource.parseValueAsCell(row[i], f.nativeType); // retain original value without serialization
255
+ if (value === undefined) {
256
+ // skip undefined values
257
+ }
258
+ else if (value === null) { // differentiate null and undefined
259
+ result[f.name] = null;
260
+ }
261
+ else {
262
+ result[f.name] = value;
263
+ }
264
+ }
265
+ result[OINOConfig_js_1.OINOConfig.OINO_ID_FIELD] = OINOConfig_js_1.OINOConfig.printOINOId(primary_key_values);
266
+ return result;
267
+ }
268
+ /**
269
+ * Serialize model set in the given format.
270
+ *
271
+ * @param [contentType=OINOContentType.json] serialization content type
272
+ *
273
+ */
274
+ async writeString(contentType = OINOConstants_js_1.OINOContentType.json) {
275
+ let result = "";
276
+ if (contentType == OINOConstants_js_1.OINOContentType.csv) {
277
+ result += await this._writeStringCsv();
278
+ }
279
+ else if (contentType == OINOConstants_js_1.OINOContentType.json) {
280
+ result += await this._writeStringJson();
281
+ }
282
+ else if (contentType == OINOConstants_js_1.OINOContentType.formdata) {
283
+ result += await this._writeStringFormdata();
284
+ }
285
+ else if (contentType == OINOConstants_js_1.OINOContentType.urlencode) {
286
+ result += await this._writeStringUrlencode();
287
+ }
288
+ else {
289
+ OINOLog_js_1.OINOLog.error("@oino-ts/db", "OINOModelSet", "writeString", "Content type is only for input!", { contentType: contentType });
290
+ }
291
+ return result;
292
+ }
293
+ /**
294
+ * Get value of given field in the current row. Undefined if no rows,
295
+ * field not found or value does not exist.
296
+ *
297
+ * @param fieldName name of the field
298
+ * @param serialize serialize the value
299
+ *
300
+ */
301
+ getValueByFieldName(fieldName, serialize = false) {
302
+ let result = undefined;
303
+ if (!this.dataset.isEof()) {
304
+ const current_row = this.dataset.getRow();
305
+ const field_index = this.datamodel.findFieldIndexByName(fieldName);
306
+ if (field_index >= 0) {
307
+ result = current_row[field_index];
308
+ if (serialize) {
309
+ result = this.datamodel.fields[field_index].serializeCell(result);
310
+ }
311
+ }
312
+ }
313
+ return result;
314
+ }
315
+ /**
316
+ * Export all rows as a record with OINOId as key and object with row cells as values.
317
+ *
318
+ * @param idFieldName optional field name to use as key instead of OINOId
319
+ */
320
+ async exportAsRecord(idFieldName) {
321
+ const result = {};
322
+ const row_id_field = idFieldName || OINOConfig_js_1.OINOConfig.OINO_ID_FIELD;
323
+ while (!this.dataset.isEof()) {
324
+ const row_data = this.dataset.getRow();
325
+ const row_export = this._exportRow(row_data);
326
+ const row_id = row_export[row_id_field];
327
+ result[row_id] = row_export;
328
+ await this.dataset.next();
329
+ }
330
+ return result;
331
+ }
332
+ }
333
+ exports.OINOModelSet = OINOModelSet;