@oino-ts/db 0.0.18 → 0.1.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.
@@ -5,7 +5,8 @@
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.OINODb = void 0;
8
+ exports.OINODbMemoryDataSet = exports.OINODbDataSet = exports.OINODb = void 0;
9
+ const index_js_1 = require("./index.js");
9
10
  /**
10
11
  * Base class for database abstraction, implementing methods for connecting, making queries and parsing/formatting data
11
12
  * between SQL and serialization formats.
@@ -51,3 +52,133 @@ class OINODb {
51
52
  }
52
53
  }
53
54
  exports.OINODb = OINODb;
55
+ /**
56
+ * Base class for SQL results that can be asynchronously iterated (but
57
+ * not necessarity rewinded). Idea is to handle database specific mechanisms
58
+ * for returning and formatting conventions in the database specific
59
+ * implementation. Data might be in memory or streamed in chunks and
60
+ * `OINODbDataSet` will serve it out consistently.
61
+ *
62
+ */
63
+ class OINODbDataSet {
64
+ _data;
65
+ /** Error messages */
66
+ messages;
67
+ /**
68
+ * Constructor for `OINODbDataSet`.
69
+ *
70
+ * @param data internal database specific data type (constructor will throw if invalid)
71
+ * @param messages error messages from SQL-query
72
+ *
73
+ */
74
+ constructor(data, messages = []) {
75
+ this._data = data;
76
+ this.messages = messages;
77
+ }
78
+ /**
79
+ * Checks if the messages contain errors.
80
+ *
81
+ */
82
+ hasErrors() {
83
+ for (let i = 0; i < this.messages.length; i++) {
84
+ if (this.messages[i].startsWith(index_js_1.OINO_ERROR_PREFIX)) {
85
+ return true;
86
+ }
87
+ }
88
+ return false;
89
+ }
90
+ /**
91
+ * Checks if the messages contain errors.
92
+ *
93
+ */
94
+ getFirstError() {
95
+ for (let i = 0; i < this.messages.length; i++) {
96
+ if (this.messages[i].startsWith(index_js_1.OINO_ERROR_PREFIX)) {
97
+ return this.messages[i];
98
+ }
99
+ }
100
+ return "";
101
+ }
102
+ }
103
+ exports.OINODbDataSet = OINODbDataSet;
104
+ /**
105
+ * Generic in memory implementation of a data set where data is an array of rows. Used
106
+ * by BunSqlite and automated testing. Can be rewinded.
107
+ *
108
+ */
109
+ class OINODbMemoryDataSet extends OINODbDataSet {
110
+ _rows;
111
+ _currentRow;
112
+ _eof;
113
+ /**
114
+ * Constructor of `OINODbMemoryDataSet`.
115
+ *
116
+ * @param data data as OINODataRow[] (constructor will throw if invalid)
117
+ * @param errors error messages from SQL-query
118
+ *
119
+ */
120
+ constructor(data, errors = []) {
121
+ super(data, errors);
122
+ if ((data == null) || !(Array.isArray(data))) {
123
+ throw new Error(index_js_1.OINO_ERROR_PREFIX + ": Data needs to be compatible with OINORow[]!"); // TODO: maybe check all rows
124
+ }
125
+ this._rows = data;
126
+ if (this.isEmpty()) {
127
+ this._currentRow = -1;
128
+ this._eof = true;
129
+ }
130
+ else {
131
+ this._currentRow = 0;
132
+ this._eof = false;
133
+ }
134
+ }
135
+ /**
136
+ * Is data set empty.
137
+ *
138
+ */
139
+ isEmpty() {
140
+ return (this._rows.length == 0);
141
+ }
142
+ /**
143
+ * Is there no more content, i.e. either dataset is empty or we have moved beyond last line
144
+ *
145
+ */
146
+ isEof() {
147
+ return (this._eof);
148
+ }
149
+ /**
150
+ * Attempts to moves dataset to the next row, possibly waiting for more data to become available. Returns !isEof().
151
+ *
152
+ */
153
+ async next() {
154
+ if (this._currentRow < this._rows.length - 1) {
155
+ this._currentRow = this._currentRow + 1;
156
+ }
157
+ else {
158
+ this._eof = true;
159
+ }
160
+ return Promise.resolve(!this._eof);
161
+ }
162
+ /**
163
+ * Gets current row of data.
164
+ *
165
+ */
166
+ getRow() {
167
+ if ((this._currentRow >= 0) && (this._currentRow < this._rows.length)) {
168
+ return this._rows[this._currentRow];
169
+ }
170
+ else {
171
+ return index_js_1.OINODB_EMPTY_ROW;
172
+ }
173
+ }
174
+ /**
175
+ * Rewinds data set to the first row, returns !isEof().
176
+ *
177
+ */
178
+ first() {
179
+ this._currentRow = 0;
180
+ this._eof = this._rows.length == 0;
181
+ return !this._eof;
182
+ }
183
+ }
184
+ exports.OINODbMemoryDataSet = OINODbMemoryDataSet;
@@ -7,16 +7,15 @@
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINODbApi = exports.OINODbHtmlTemplate = exports.OINODbApiResult = void 0;
9
9
  const index_js_1 = require("./index.js");
10
- const types_1 = require("@oino-ts/types");
10
+ const common_1 = require("@oino-ts/common");
11
11
  const hashid_1 = require("@oino-ts/hashid");
12
- const types_2 = require("@oino-ts/types");
13
12
  const API_EMPTY_PARAMS = { sqlParams: {} };
14
13
  /**
15
14
  * OINO API request result object with returned data and/or http status code/message and
16
15
  * error / warning messages.
17
16
  *
18
17
  */
19
- class OINODbApiResult extends types_1.OINOResult {
18
+ class OINODbApiResult extends common_1.OINOResult {
20
19
  /** DbApi request params */
21
20
  params;
22
21
  /** Returned data if any */
@@ -296,7 +295,7 @@ class OINODbApi {
296
295
  rows = body;
297
296
  }
298
297
  else {
299
- rows = types_2.OINOParser.createRows(this.datamodel, body, params);
298
+ rows = index_js_1.OINODbParser.createRows(this.datamodel, body, params);
300
299
  }
301
300
  }
302
301
  catch (e) {
@@ -0,0 +1,466 @@
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.OINODbParser = void 0;
9
+ const index_js_1 = require("./index.js");
10
+ /**
11
+ * Static factory class for easily creating things based on data
12
+ *
13
+ */
14
+ class OINODbParser {
15
+ /**
16
+ * Create data rows from request body based on the datamodel.
17
+ *
18
+ * @param datamodel datamodel of the api
19
+ * @param data data as a string or Buffer or object
20
+ * @param requestParams parameters
21
+ *
22
+ */
23
+ static createRows(datamodel, data, requestParams) {
24
+ let result = [];
25
+ if (typeof data == "string") {
26
+ result = this.createRowsFromText(datamodel, data, requestParams);
27
+ }
28
+ else if (data instanceof Buffer) {
29
+ result = this.createRowsFromBlob(datamodel, data, requestParams);
30
+ }
31
+ else if (typeof data == "object") {
32
+ result = [this.createRowFromObject(datamodel, data)];
33
+ }
34
+ return result;
35
+ }
36
+ /**
37
+ * Create data rows from request body based on the datamodel.
38
+ *
39
+ * @param datamodel datamodel of the api
40
+ * @param data data as a string
41
+ * @param requestParams parameters
42
+ *
43
+ */
44
+ static createRowsFromText(datamodel, data, requestParams) {
45
+ if ((requestParams.requestType == index_js_1.OINOContentType.json) || (requestParams.requestType == undefined)) {
46
+ return this._createRowFromJson(datamodel, data);
47
+ }
48
+ else if (requestParams.requestType == index_js_1.OINOContentType.csv) {
49
+ return this._createRowFromCsv(datamodel, data);
50
+ }
51
+ else if (requestParams.requestType == index_js_1.OINOContentType.formdata) {
52
+ return this._createRowFromFormdata(datamodel, Buffer.from(data, "utf8"), requestParams.multipartBoundary || "");
53
+ }
54
+ else if (requestParams.requestType == index_js_1.OINOContentType.urlencode) {
55
+ return this._createRowFromUrlencoded(datamodel, data);
56
+ }
57
+ else if (requestParams.requestType == index_js_1.OINOContentType.html) {
58
+ index_js_1.OINOLog.error("HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
59
+ return [];
60
+ }
61
+ else {
62
+ index_js_1.OINOLog.error("Unrecognized input content type!", { contentType: requestParams.requestType });
63
+ return [];
64
+ }
65
+ }
66
+ /**
67
+ * Create data rows from request body based on the datamodel.
68
+ *
69
+ * @param datamodel datamodel of the api
70
+ * @param data data as an Buffer
71
+ * @param requestParams parameters
72
+ *
73
+ */
74
+ static createRowsFromBlob(datamodel, data, requestParams) {
75
+ if ((requestParams.requestType == index_js_1.OINOContentType.json) || (requestParams.requestType == undefined)) {
76
+ return this._createRowFromJson(datamodel, data.toString()); // JSON is always a string
77
+ }
78
+ else if (requestParams.requestType == index_js_1.OINOContentType.csv) {
79
+ return this._createRowFromCsv(datamodel, data.toString()); // binary data has to be base64 encoded so it's a string
80
+ }
81
+ else if (requestParams.requestType == index_js_1.OINOContentType.formdata) {
82
+ return this._createRowFromFormdata(datamodel, data, requestParams.multipartBoundary || "");
83
+ }
84
+ else if (requestParams.requestType == index_js_1.OINOContentType.urlencode) {
85
+ return this._createRowFromUrlencoded(datamodel, data.toString()); // data is urlencoded so it's a string
86
+ }
87
+ else if (requestParams.requestType == index_js_1.OINOContentType.html) {
88
+ index_js_1.OINOLog.error("HTML can't be used as an input content type!", { contentType: index_js_1.OINOContentType.html });
89
+ return [];
90
+ }
91
+ else {
92
+ index_js_1.OINOLog.error("Unrecognized input content type!", { contentType: requestParams.requestType });
93
+ return [];
94
+ }
95
+ }
96
+ /**
97
+ * Create one data row from javascript object based on the datamodel.
98
+ * NOTE! Data assumed to be unserialized i.e. of the native type (string, number, boolean, Buffer)
99
+ *
100
+ * @param datamodel datamodel of the api
101
+ * @param data data as javascript object
102
+ *
103
+ */
104
+ static createRowFromObject(datamodel, data) {
105
+ const fields = datamodel.fields;
106
+ let result = new Array(fields.length);
107
+ for (let i = 0; i < fields.length; i++) {
108
+ result[i] = data[fields[i].name];
109
+ }
110
+ return result;
111
+ }
112
+ static _findCsvLineEnd(csvData, start) {
113
+ const n = csvData.length;
114
+ if (start >= n) {
115
+ return start;
116
+ }
117
+ let end = start;
118
+ let quote_open = false;
119
+ while (end < n) {
120
+ if (csvData[end] == "\"") {
121
+ if (!quote_open) {
122
+ quote_open = true;
123
+ }
124
+ else if ((end < n - 1) && (csvData[end + 1] == "\"")) {
125
+ end++;
126
+ }
127
+ else {
128
+ quote_open = false;
129
+ }
130
+ }
131
+ else if ((!quote_open) && (csvData[end] == "\r")) {
132
+ return end;
133
+ }
134
+ end++;
135
+ }
136
+ return n;
137
+ }
138
+ static _parseCsvLine(csvLine) {
139
+ let result = [];
140
+ const n = csvLine.length;
141
+ let start = 0;
142
+ let end = 0;
143
+ let quote_open = false;
144
+ let has_quotes = false;
145
+ let has_escaped_quotes = false;
146
+ let found_field = false;
147
+ while (end < n) {
148
+ if (csvLine[end] == "\"") {
149
+ if (!quote_open) {
150
+ quote_open = true;
151
+ }
152
+ else if ((end < n - 1) && (csvLine[end + 1] == "\"")) {
153
+ end++;
154
+ has_escaped_quotes = true;
155
+ }
156
+ else {
157
+ has_quotes = true;
158
+ quote_open = false;
159
+ }
160
+ }
161
+ if ((!quote_open) && ((end == n - 1) || (csvLine[end] == ","))) {
162
+ found_field = true;
163
+ if (end == n - 1) {
164
+ end++;
165
+ }
166
+ }
167
+ if (found_field) {
168
+ // console.log("OINODB_csvParseLine: next field=" + csvLine.substring(start,end) + ", start="+start+", end="+end)
169
+ let field_str;
170
+ if (has_quotes) {
171
+ field_str = csvLine.substring(start + 1, end - 1);
172
+ }
173
+ else if (start == end) {
174
+ field_str = undefined;
175
+ }
176
+ else {
177
+ field_str = csvLine.substring(start, end);
178
+ if (field_str == "null") {
179
+ field_str = null;
180
+ }
181
+ }
182
+ result.push(field_str);
183
+ has_quotes = false;
184
+ has_escaped_quotes = true;
185
+ found_field = false;
186
+ start = end + 1;
187
+ }
188
+ end++;
189
+ }
190
+ return result;
191
+ }
192
+ static _createRowFromCsv(datamodel, data) {
193
+ let result = [];
194
+ const n = data.length;
195
+ let start = 0;
196
+ let end = this._findCsvLineEnd(data, start);
197
+ const header_str = data.substring(start, end);
198
+ const headers = this._parseCsvLine(header_str);
199
+ let field_to_header_mapping = new Array(datamodel.fields.length);
200
+ let headers_found = false;
201
+ for (let i = 0; i < field_to_header_mapping.length; i++) {
202
+ field_to_header_mapping[i] = headers.indexOf(datamodel.fields[i].name);
203
+ headers_found = headers_found || (field_to_header_mapping[i] >= 0);
204
+ }
205
+ // OINOLog.debug("createRowFromCsv", {headers:headers, field_to_header_mapping:field_to_header_mapping})
206
+ if (!headers_found) {
207
+ return result;
208
+ }
209
+ start = end + 1;
210
+ end = start;
211
+ while (end < n) {
212
+ while ((start < n) && ((data[start] == "\r") || (data[start] == "\n"))) {
213
+ start++;
214
+ }
215
+ if (start >= n) {
216
+ return result;
217
+ }
218
+ end = this._findCsvLineEnd(data, start);
219
+ const row_data = this._parseCsvLine(data.substring(start, end));
220
+ const row = new Array(field_to_header_mapping.length);
221
+ let has_data = false;
222
+ for (let i = 0; i < datamodel.fields.length; i++) {
223
+ const field = datamodel.fields[i];
224
+ let j = field_to_header_mapping[i];
225
+ let value = row_data[j];
226
+ if ((value === undefined) || (value === null)) { // null/undefined-decoding built into the parser
227
+ row[i] = value;
228
+ }
229
+ else if ((j >= 0) && (j < row_data.length)) {
230
+ value = index_js_1.OINOStr.decode(value, index_js_1.OINOContentType.csv);
231
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
232
+ value = datamodel.api.hashid.decode(value);
233
+ }
234
+ row[i] = field.deserializeCell(value);
235
+ }
236
+ else {
237
+ row[i] = undefined;
238
+ }
239
+ has_data = has_data || (row[i] !== undefined);
240
+ }
241
+ // console.log("createRowFromCsv: next row=" + row)
242
+ if (has_data) {
243
+ result.push(row);
244
+ }
245
+ else {
246
+ index_js_1.OINOLog.warning("createRowFromCsv: empty row skipped");
247
+ }
248
+ start = end;
249
+ end = start;
250
+ }
251
+ return result;
252
+ }
253
+ static _createRowFromJsonObj(obj, datamodel) {
254
+ // console.log("createRowFromJsonObj: obj=" + JSON.stringify(obj))
255
+ const fields = datamodel.fields;
256
+ let result = new Array(fields.length);
257
+ let has_data = false;
258
+ // console.log("createRowFromJsonObj: " + result)
259
+ for (let i = 0; i < fields.length; i++) {
260
+ const field = fields[i];
261
+ let value = obj[field.name];
262
+ // console.log("createRowFromJsonObj: key=" + field.name + ", val=" + val)
263
+ if ((value === null) || (value === undefined)) { // must be checed first as null is an object
264
+ result[i] = value;
265
+ }
266
+ else if (Array.isArray(value) || typeof value === "object") {
267
+ result[i] = JSON.stringify(value).replaceAll("\"", "\\\""); // only single level deep objects, rest is handled as JSON-strings
268
+ }
269
+ else if (typeof value === "string") {
270
+ value = index_js_1.OINOStr.decode(value, index_js_1.OINOContentType.json);
271
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
272
+ value = datamodel.api.hashid.decode(value);
273
+ }
274
+ result[i] = field.deserializeCell(value);
275
+ }
276
+ else {
277
+ result[i] = value; // value types are passed as-is
278
+ }
279
+ has_data = has_data || (result[i] !== undefined);
280
+ // console.log("createRowFromJsonObj: result["+i+"]=" + result[i])
281
+ }
282
+ // console.log("createRowFromJsonObj: " + result)
283
+ if (has_data) {
284
+ return result;
285
+ }
286
+ else {
287
+ index_js_1.OINOLog.warning("createRowFromJsonObj: empty row skipped");
288
+ return undefined;
289
+ }
290
+ }
291
+ static _createRowFromJson(datamodel, data) {
292
+ let result = [];
293
+ // console.log("OINORowFactoryJson: data=" + data)
294
+ const obj = JSON.parse(data);
295
+ if (Array.isArray(obj)) {
296
+ obj.forEach(row => {
297
+ const data_row = this._createRowFromJsonObj(row, datamodel);
298
+ if (data_row !== undefined) {
299
+ result.push(data_row);
300
+ }
301
+ });
302
+ }
303
+ else {
304
+ const data_row = this._createRowFromJsonObj(obj, datamodel);
305
+ if (data_row !== undefined) {
306
+ result.push(data_row);
307
+ }
308
+ }
309
+ return result;
310
+ }
311
+ static _findMultipartBoundary(formData, multipartBoundary, start) {
312
+ let n = formData.indexOf(multipartBoundary, start);
313
+ if (n >= 0) {
314
+ n += multipartBoundary.length + 2;
315
+ }
316
+ else {
317
+ n = formData.length;
318
+ }
319
+ return n;
320
+ }
321
+ static _parseMultipartLine(data, start) {
322
+ let line_end = data.indexOf('\r\n', start);
323
+ if (line_end >= start) {
324
+ return data.subarray(start, line_end).toString();
325
+ }
326
+ else {
327
+ return '';
328
+ }
329
+ }
330
+ static _multipartHeaderRegex = /Content-Disposition\: (form-data|file); name=\"([^\"]+)\"(; filename=.*)?/i;
331
+ static _createRowFromFormdata(datamodel, data, multipartBoundary) {
332
+ let result = [];
333
+ try {
334
+ const n = data.length;
335
+ let start = this._findMultipartBoundary(data, multipartBoundary, 0);
336
+ let end = this._findMultipartBoundary(data, multipartBoundary, start);
337
+ // OINOLog.debug("createRowFromFormdata: enter", {start:start, end:end, multipartBoundary:multipartBoundary})
338
+ const row = new Array(datamodel.fields.length);
339
+ let has_data = false;
340
+ while (end < n) {
341
+ // OINOLog.debug("createRowFromFormdata: next block", {start:start, end:end, block:data.substring(start, end)})
342
+ let block_ok = true;
343
+ let l = this._parseMultipartLine(data, start);
344
+ // OINOLog.debug("createRowFromFormdata: next line", {start:start, end:end, line:l})
345
+ start += l.length + 2;
346
+ const header_matches = OINODbParser._multipartHeaderRegex.exec(l);
347
+ if (!header_matches) {
348
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: unsupported block skipped!", { header_line: l });
349
+ block_ok = false;
350
+ }
351
+ else {
352
+ const field_name = header_matches[2];
353
+ const is_file = header_matches[3] != null;
354
+ let is_base64 = false;
355
+ const field_index = datamodel.findFieldIndexByName(field_name);
356
+ // OINOLog.debug("createRowFromFormdata: header", {field_name:field_name, field_index:field_index, is_file:is_file, is_base64:is_base64})
357
+ if (field_index < 0) {
358
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: form field not found and skipped!", { field_name: field_name });
359
+ block_ok = false;
360
+ }
361
+ else {
362
+ const field = datamodel.fields[field_index];
363
+ l = this._parseMultipartLine(data, start);
364
+ // OINOLog.debug("createRowFromFormdata: next line", {start:start, end:end, line:l})
365
+ while (block_ok && (l != '')) {
366
+ if (l.startsWith('Content-Type:') && (l.indexOf('multipart/mixed') >= 0)) {
367
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: mixed multipart files not supported and skipped!", { header_line: l });
368
+ block_ok = false;
369
+ }
370
+ else if (l.startsWith('Content-Transfer-Encoding:') && (l.indexOf('BASE64') >= 0)) {
371
+ is_base64 = true;
372
+ }
373
+ start += l.length + 2;
374
+ l = this._parseMultipartLine(data, start);
375
+ // OINOLog.debug("createRowFromFormdata: next line", {start:start, end:end, line:l})
376
+ }
377
+ start += 2;
378
+ if (!block_ok) {
379
+ index_js_1.OINOLog.warning("OINODbFactory.createRowFromFormdata: invalid block skipped", { field_name: field_name });
380
+ }
381
+ else if (start + multipartBoundary.length + 2 >= end) {
382
+ // OINOLog.debug("OINODbFactory.createRowFromFormdata: null value", {field_name:field_name})
383
+ row[field_index] = null;
384
+ }
385
+ else if (is_file) {
386
+ if (is_base64) {
387
+ const value = this._parseMultipartLine(data, start).trim();
388
+ row[field_index] = field.deserializeCell(index_js_1.OINOStr.decode(value, index_js_1.OINOContentType.formdata));
389
+ }
390
+ else {
391
+ const e = this._findMultipartBoundary(data, multipartBoundary, start);
392
+ const value = data.subarray(start, e - 2);
393
+ row[field_index] = value;
394
+ }
395
+ // console.log("OINODbFactory.createRowFromFormdata: file field", {field_name:field_name, value:row[field_index]})
396
+ }
397
+ else {
398
+ let value = index_js_1.OINOStr.decode(this._parseMultipartLine(data, start).trim(), index_js_1.OINOContentType.formdata);
399
+ // OINOLog.debug("OINODbFactory.createRowFromFormdata: parse form field", {field_name:field_name, value:value})
400
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
401
+ value = datamodel.api.hashid.decode(value);
402
+ }
403
+ row[field_index] = field.deserializeCell(value);
404
+ }
405
+ has_data = has_data || (row[field_index] !== undefined);
406
+ }
407
+ }
408
+ start = end;
409
+ end = this._findMultipartBoundary(data, multipartBoundary, start);
410
+ }
411
+ // OINOLog.debug("createRowFromFormdata: next row", {row:row})
412
+ if (has_data) {
413
+ result.push(row);
414
+ }
415
+ else {
416
+ index_js_1.OINOLog.warning("createRowFromFormdata: empty row skipped");
417
+ }
418
+ }
419
+ catch (e) {
420
+ index_js_1.OINOLog.error("createRowFromFormdata: error parsing formdata", { exception: e.message });
421
+ }
422
+ return result;
423
+ }
424
+ static _createRowFromUrlencoded(datamodel, data) {
425
+ // OINOLog.debug("createRowFromUrlencoded: enter", {data:data})
426
+ let result = [];
427
+ const row = new Array(datamodel.fields.length);
428
+ let has_data = false;
429
+ const data_parts = data.trim().split('&');
430
+ try {
431
+ for (let i = 0; i < data_parts.length; i++) {
432
+ const param_parts = data_parts[i].split('=');
433
+ // OINOLog.debug("createRowFromUrlencoded: next param", {param_parts:param_parts})
434
+ if (param_parts.length == 2) {
435
+ const key = index_js_1.OINOStr.decodeUrlencode(param_parts[0]) || "";
436
+ const field_index = datamodel.findFieldIndexByName(key);
437
+ if (field_index < 0) {
438
+ index_js_1.OINOLog.info("createRowFromUrlencoded: param field not found", { field: key });
439
+ }
440
+ else {
441
+ const field = datamodel.fields[field_index];
442
+ let value = index_js_1.OINOStr.decode(param_parts[1], index_js_1.OINOContentType.urlencode);
443
+ if (value && (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) && (field instanceof index_js_1.OINONumberDataField) && (datamodel.api.hashid)) {
444
+ value = datamodel.api.hashid.decode(value);
445
+ }
446
+ row[field_index] = field.deserializeCell(value);
447
+ has_data = has_data || (row[field_index] !== undefined);
448
+ }
449
+ }
450
+ // const value = requestParams[]
451
+ }
452
+ if (has_data) {
453
+ result.push(row);
454
+ }
455
+ else {
456
+ index_js_1.OINOLog.warning("createRowFromUrlencoded: empty row skipped");
457
+ }
458
+ }
459
+ catch (e) {
460
+ index_js_1.OINOLog.error("createRowFromUrlencoded: error parsing urlencoded data", { exception: e.message });
461
+ }
462
+ // console.log("createRowFromUrlencoded: next row=" + row)
463
+ return result;
464
+ }
465
+ }
466
+ exports.OINODbParser = OINODbParser;