@oino-ts/db 0.3.2 → 0.3.4

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.
@@ -1,298 +1,298 @@
1
- /*
2
- * This Source Code Form is subject to the terms of the Mozilla Public
3
- * License, v. 2.0. If a copy of the MPL was not distributed with this
4
- * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
- */
6
-
7
- import { OINODbDataSet, OINODbDataModel, OINODbDataField, OINODataRow, OINOContentType, OINOBlobDataField, OINOStr, OINODbConfig, OINONumberDataField, OINOBooleanDataField, OINODataCell, OINOLog } from "./index.js";
8
-
9
- /**
10
- * Class for dataset based on a data model that can be serialized to
11
- * a supported format:
12
- * - JSON (application/json)
13
- * - CSV (text/csv)
14
- *
15
- */
16
- export class OINODbModelSet {
17
-
18
- /** Reference to datamodel */
19
- readonly datamodel: OINODbDataModel
20
-
21
- /** Reference to data set */
22
- readonly dataset: OINODbDataSet
23
-
24
- /** Collection of errors */
25
- errors: string[]
26
-
27
- /**
28
- * Constructor for `OINODbModelSet`.
29
- *
30
- * @param datamodel data model
31
- * @param dataset data set
32
- */
33
- constructor(datamodel: OINODbDataModel, dataset: OINODbDataSet) {
34
- this.datamodel = datamodel
35
- this.dataset = dataset
36
- this.errors = this.dataset.messages
37
- }
38
-
39
- private _encodeAndHashFieldValue(field:OINODbDataField, value:string|null, contentType:OINOContentType, primaryKeyValues:string[], rowIdSeed:string):string {
40
- let result:string
41
- if (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) {
42
- if (value && (field instanceof OINONumberDataField) && (this.datamodel.api.hashid)) {
43
- value = this.datamodel.api.hashid.encode(value, rowIdSeed)
44
- }
45
- if (field.fieldParams.isPrimaryKey) {
46
- primaryKeyValues.push(value || "")
47
- }
48
- }
49
- result = OINOStr.encode(value, contentType)
50
- return result
51
- }
52
-
53
- private _writeRowJson(row:OINODataRow):string {
54
- // console.log("OINODbModelSet._writeRowJson: row=" + row)
55
- const model:OINODbDataModel = this.datamodel
56
- const fields:OINODbDataField[] = model.fields
57
- let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
58
- let primary_key_values:string[] = []
59
- let json_row:string = ""
60
- for (let i=0; i<fields.length; i++) {
61
- const f = fields[i]
62
- let value:string|null|undefined = f.serializeCell(row[i])
63
- if (value === undefined) {
64
- // OINOLog.info("OINODbModelSet._writeRowJson: undefined value skipped", {field_name:f.name})
65
-
66
- } else if (value === null) {
67
- json_row += "," + OINOStr.encode(f.name, OINOContentType.json) + ":null"
68
-
69
- } else {
70
-
71
- let is_hashed:boolean = (f.fieldParams.isPrimaryKey || f.fieldParams.isForeignKey) && (f instanceof OINONumberDataField) && (this.datamodel.api.hashid != null)
72
- let is_value = (f instanceof OINOBooleanDataField) || ((f instanceof OINONumberDataField) && !is_hashed)
73
- value = this._encodeAndHashFieldValue(f, value, OINOContentType.json, primary_key_values, f.name + " " + row_id_seed)
74
- if (is_value) {
75
- value = value.substring(1, value.length-1)
76
- }
77
- json_row += "," + OINOStr.encode(f.name, OINOContentType.json) + ":" + value
78
- }
79
- }
80
- json_row = OINOStr.encode(OINODbConfig.OINODB_ID_FIELD, OINOContentType.json) + ":" + OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.json) + json_row
81
- // OINOLog_debug("OINODbModelSet._writeRowJson="+json_row)
82
- return "{" + json_row + "}"
83
- }
84
-
85
- private async _writeStringJson():Promise<string> {
86
- let result:string = ""
87
- while (!this.dataset.isEof()) {
88
- if (result != "") {
89
- result += ",\r\n"
90
- }
91
- const row:OINODataRow = this.dataset.getRow()
92
- // OINOLog.debug("OINODbModelSet._writeStringJson: row", {row:row})
93
- result += this._writeRowJson(row)
94
- await this.dataset.next()
95
- }
96
- result = "[\r\n" + result + "\r\n]"
97
- // OINOLog_debug("OINODbModelSet._writeStringJson="+result)
98
- return result
99
- }
100
-
101
- private _writeHeaderCsv():string {
102
- const model:OINODbDataModel = this.datamodel
103
- const fields:OINODbDataField[] = model.fields
104
- let csv_header:string = "\"" + OINODbConfig.OINODB_ID_FIELD + "\""
105
- for (let i=0; i<fields.length; i++) {
106
- csv_header += ",\"" + fields[i].name + "\""
107
- }
108
- // OINOLog_debug("OINODbModelSet._writeHeaderCsv="+csv_header)
109
- return csv_header
110
- }
111
-
112
- private _writeRowCsv(row:OINODataRow):string {
113
- // OINOLog_debug("OINODbModelSet._writeRowCsv", {row:row})
114
- const model:OINODbDataModel = this.datamodel
115
- const fields:OINODbDataField[] = model.fields
116
- let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
117
- let primary_key_values:string[] = []
118
- let csv_row:string = ""
119
- for (let i=0; i<fields.length; i++) {
120
- const f = fields[i]
121
- let value:string|null|undefined = f.serializeCell(row[i])
122
- if (value == null) {
123
- csv_row += "," + OINOStr.encode(value, OINOContentType.csv) // either null or undefined
124
-
125
- } else {
126
- value = this._encodeAndHashFieldValue(f, value, OINOContentType.csv, primary_key_values, f.name + " " + row_id_seed)
127
- csv_row += "," + value
128
- }
129
- }
130
- csv_row = OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.csv) + csv_row
131
- // OINOLog_debug("OINODbModelSet._writeRowCsv="+csv_row)
132
- return csv_row
133
- }
134
-
135
- private async _writeStringCsv():Promise<string> {
136
- let result:string = this._writeHeaderCsv()
137
- while (!this.dataset.isEof()) {
138
- if (result != "") {
139
- result += "\r\n"
140
- }
141
- const row:OINODataRow = this.dataset.getRow()
142
- // OINOLog.debug("OINODbModelSet._writeStringCsv: row", {row:row})
143
- result += this._writeRowCsv(row)
144
- await this.dataset.next()
145
- }
146
- // OINOLog_debug("OINODbModelSet._writeStringCsv="+result)
147
- return result
148
- }
149
-
150
- private _writeRowFormdataParameterBlock(blockName:string, blockValue:string|null, multipartBoundary:string):string {
151
- if (blockValue === null) {
152
- return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"\r\n\r\n"
153
- } else {
154
- return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"\r\n\r\n" + blockValue + "\r\n"
155
- }
156
- }
157
-
158
- private _writeRowFormdataFileBlock(blockName:string, blockValue:string, multipartBoundary:string):string {
159
- 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"
160
- }
161
-
162
- private _writeRowFormdata(row:OINODataRow):string {
163
- // console.log("OINODbModelSet._writeRowFormdata: row", row)
164
- const multipart_boundary:string = "---------OINOMultipartBoundary35424568" // this method is just used for test data generation and we want it to be static
165
- const model:OINODbDataModel = this.datamodel
166
- const fields:OINODbDataField[] = model.fields
167
- let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
168
- let primary_key_values:string[] = []
169
- let result:string = ""
170
- for (let i=0; i<fields.length; i++) {
171
- const f = fields[i]
172
- let value:string|null|undefined = f.serializeCell(row[i])
173
- let formdata_block:string = ""
174
- let is_file = (f instanceof OINOBlobDataField)
175
-
176
- if (value === undefined) {
177
- OINOLog.info("OINODbModelSet._writeRowFormdata: undefined value skipped.", {field:f.name})
178
-
179
- } else if (value === null) {
180
- formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, null, multipart_boundary)
181
-
182
- } else {
183
- value = this._encodeAndHashFieldValue(f, value, OINOContentType.formdata, primary_key_values, f.name + " " + row_id_seed)
184
- if (is_file) {
185
- formdata_block = this._writeRowFormdataFileBlock(f.name, value, multipart_boundary)
186
- } else {
187
- formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, value, multipart_boundary)
188
- }
189
- }
190
-
191
-
192
- // OINOLog.debug("OINODbModelSet._writeRowFormdata next block", {formdata_block:formdata_block})
193
- result += formdata_block
194
- }
195
- result = this._writeRowFormdataParameterBlock(OINODbConfig.OINODB_ID_FIELD, OINODbConfig.printOINOId(primary_key_values), multipart_boundary) + result
196
- return result
197
- }
198
-
199
- private _writeStringFormdata():string {
200
- const row:OINODataRow = this.dataset.getRow()
201
- // OINOLog.debug("OINODbModelSet._writeStringFormdata: row", {row:row})
202
- let result:string = this._writeRowFormdata(row)
203
- return result
204
- }
205
-
206
-
207
- private _writeRowUrlencode(row:OINODataRow):string {
208
- // console.log("OINODbModelSet._writeRowUrlencode row=" + row)
209
- const model:OINODbDataModel = this.datamodel
210
- const fields:OINODbDataField[] = model.fields
211
- let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
212
- let primary_key_values:string[] = []
213
- let urlencode_row:string = ""
214
- for (let i=0; i<fields.length; i++) {
215
- const f = fields[i]
216
- let value:string|null|undefined = f.serializeCell(row[i])
217
- if ((value === undefined)) { // || (value === null)) {
218
- // console.log("OINODbModelSet._writeRowUrlencode undefined field value:" + fields[i].name)
219
- } else {
220
- value = this._encodeAndHashFieldValue(f, value, OINOContentType.urlencode, primary_key_values, f.name + " " + row_id_seed)
221
- if (urlencode_row != "") {
222
- urlencode_row += "&"
223
- }
224
- urlencode_row += OINOStr.encode(f.name, OINOContentType.urlencode) + "=" + value
225
- }
226
- }
227
- urlencode_row = OINOStr.encode(OINODbConfig.OINODB_ID_FIELD, OINOContentType.urlencode) + "=" + OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.urlencode) + "&" + urlencode_row
228
- // OINOLog_debug("OINODbModelSet._writeRowCsv="+csv_row)
229
- return urlencode_row
230
- }
231
-
232
- private async _writeStringUrlencode():Promise<string> {
233
- let result:string = ""
234
- let line_count = 0
235
- while (!this.dataset.isEof()) {
236
- const row:OINODataRow = this.dataset.getRow()
237
- // OINOLog.debug("OINODbModelSet._writeStringUrlencode: row", {row:row})
238
- result += this._writeRowUrlencode(row) + "\r\n"
239
- await this.dataset.next()
240
- line_count += 1
241
- }
242
- // OINOLog_debug("OINODbModelSet._writeStringCsv="+result)
243
- if (line_count > 1) {
244
- OINOLog.warning("OINODbModelSet._writeStringUrlencode: content type " + OINOContentType.urlencode + " does not officially support multiline content!")
245
- }
246
- return result
247
- }
248
-
249
- /**
250
- * Serialize model set in the given format.
251
- *
252
- * @param [contentType=OINOContentType.json] serialization content type
253
- *
254
- */
255
- async writeString(contentType:OINOContentType = OINOContentType.json):Promise<string> {
256
- let result:string = ""
257
- if (contentType == OINOContentType.csv) {
258
- result += await this._writeStringCsv()
259
-
260
- } else if (contentType == OINOContentType.json) {
261
- result += await this._writeStringJson()
262
-
263
- } else if (contentType == OINOContentType.formdata) {
264
- result += await this._writeStringFormdata()
265
-
266
- } else if (contentType == OINOContentType.urlencode) {
267
- result += await this._writeStringUrlencode()
268
-
269
- } else {
270
- OINOLog.error("OINODbModelSet.writeString: content type is only for input!", {contentType:contentType})
271
- }
272
- return result
273
- }
274
-
275
- /**
276
- * Get value of given field in the current row. Undefined if no rows,
277
- * field not found or value does not exist.
278
- *
279
- * @param fieldName name of the field
280
- * @param serialize serialize the value
281
- *
282
- */
283
- getValueByFieldName(fieldName:string, serialize:boolean = false):OINODataCell {
284
- let result:OINODataCell = undefined
285
- if (!this.dataset.isEof()) {
286
- const current_row:OINODataRow = this.dataset.getRow()
287
- const field_index:number = this.datamodel.findFieldIndexByName(fieldName)
288
- if (field_index >= 0) {
289
- result = current_row[field_index]
290
- if (serialize) {
291
- result = this.datamodel.fields[field_index].serializeCell(result)
292
- }
293
- }
294
- }
295
- return result
296
- }
297
- }
298
-
1
+ /*
2
+ * This Source Code Form is subject to the terms of the Mozilla Public
3
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
4
+ * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
+ */
6
+
7
+ import { OINODbDataSet, OINODbDataModel, OINODbDataField, OINODataRow, OINOContentType, OINOBlobDataField, OINOStr, OINODbConfig, OINONumberDataField, OINOBooleanDataField, OINODataCell, OINOLog } from "./index.js";
8
+
9
+ /**
10
+ * Class for dataset based on a data model that can be serialized to
11
+ * a supported format:
12
+ * - JSON (application/json)
13
+ * - CSV (text/csv)
14
+ *
15
+ */
16
+ export class OINODbModelSet {
17
+
18
+ /** Reference to datamodel */
19
+ readonly datamodel: OINODbDataModel
20
+
21
+ /** Reference to data set */
22
+ readonly dataset: OINODbDataSet
23
+
24
+ /** Collection of errors */
25
+ errors: string[]
26
+
27
+ /**
28
+ * Constructor for `OINODbModelSet`.
29
+ *
30
+ * @param datamodel data model
31
+ * @param dataset data set
32
+ */
33
+ constructor(datamodel: OINODbDataModel, dataset: OINODbDataSet) {
34
+ this.datamodel = datamodel
35
+ this.dataset = dataset
36
+ this.errors = this.dataset.messages
37
+ }
38
+
39
+ private _encodeAndHashFieldValue(field:OINODbDataField, value:string|null, contentType:OINOContentType, primaryKeyValues:string[], rowIdSeed:string):string {
40
+ let result:string
41
+ if (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) {
42
+ if (value && (field instanceof OINONumberDataField) && (this.datamodel.api.hashid)) {
43
+ value = this.datamodel.api.hashid.encode(value, rowIdSeed)
44
+ }
45
+ if (field.fieldParams.isPrimaryKey) {
46
+ primaryKeyValues.push(value || "")
47
+ }
48
+ }
49
+ result = OINOStr.encode(value, contentType)
50
+ return result
51
+ }
52
+
53
+ private _writeRowJson(row:OINODataRow):string {
54
+ // console.log("OINODbModelSet._writeRowJson: row=" + row)
55
+ const model:OINODbDataModel = this.datamodel
56
+ const fields:OINODbDataField[] = model.fields
57
+ let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
58
+ let primary_key_values:string[] = []
59
+ let json_row:string = ""
60
+ for (let i=0; i<fields.length; i++) {
61
+ const f = fields[i]
62
+ let value:string|null|undefined = f.serializeCell(row[i])
63
+ if (value === undefined) {
64
+ // OINOLog.info("OINODbModelSet._writeRowJson: undefined value skipped", {field_name:f.name})
65
+
66
+ } else if (value === null) {
67
+ json_row += "," + OINOStr.encode(f.name, OINOContentType.json) + ":null"
68
+
69
+ } else {
70
+
71
+ let is_hashed:boolean = (f.fieldParams.isPrimaryKey || f.fieldParams.isForeignKey) && (f instanceof OINONumberDataField) && (this.datamodel.api.hashid != null)
72
+ let is_value = (f instanceof OINOBooleanDataField) || ((f instanceof OINONumberDataField) && !is_hashed)
73
+ value = this._encodeAndHashFieldValue(f, value, OINOContentType.json, primary_key_values, f.name + " " + row_id_seed)
74
+ if (is_value) {
75
+ value = value.substring(1, value.length-1)
76
+ }
77
+ json_row += "," + OINOStr.encode(f.name, OINOContentType.json) + ":" + value
78
+ }
79
+ }
80
+ json_row = OINOStr.encode(OINODbConfig.OINODB_ID_FIELD, OINOContentType.json) + ":" + OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.json) + json_row
81
+ // OINOLog_debug("OINODbModelSet._writeRowJson="+json_row)
82
+ return "{" + json_row + "}"
83
+ }
84
+
85
+ private async _writeStringJson():Promise<string> {
86
+ let result:string = ""
87
+ while (!this.dataset.isEof()) {
88
+ if (result != "") {
89
+ result += ",\r\n"
90
+ }
91
+ const row:OINODataRow = this.dataset.getRow()
92
+ // OINOLog.debug("OINODbModelSet._writeStringJson: row", {row:row})
93
+ result += this._writeRowJson(row)
94
+ await this.dataset.next()
95
+ }
96
+ result = "[\r\n" + result + "\r\n]"
97
+ // OINOLog_debug("OINODbModelSet._writeStringJson="+result)
98
+ return result
99
+ }
100
+
101
+ private _writeHeaderCsv():string {
102
+ const model:OINODbDataModel = this.datamodel
103
+ const fields:OINODbDataField[] = model.fields
104
+ let csv_header:string = "\"" + OINODbConfig.OINODB_ID_FIELD + "\""
105
+ for (let i=0; i<fields.length; i++) {
106
+ csv_header += ",\"" + fields[i].name + "\""
107
+ }
108
+ // OINOLog_debug("OINODbModelSet._writeHeaderCsv="+csv_header)
109
+ return csv_header
110
+ }
111
+
112
+ private _writeRowCsv(row:OINODataRow):string {
113
+ // OINOLog_debug("OINODbModelSet._writeRowCsv", {row:row})
114
+ const model:OINODbDataModel = this.datamodel
115
+ const fields:OINODbDataField[] = model.fields
116
+ let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
117
+ let primary_key_values:string[] = []
118
+ let csv_row:string = ""
119
+ for (let i=0; i<fields.length; i++) {
120
+ const f = fields[i]
121
+ let value:string|null|undefined = f.serializeCell(row[i])
122
+ if (value == null) {
123
+ csv_row += "," + OINOStr.encode(value, OINOContentType.csv) // either null or undefined
124
+
125
+ } else {
126
+ value = this._encodeAndHashFieldValue(f, value, OINOContentType.csv, primary_key_values, f.name + " " + row_id_seed)
127
+ csv_row += "," + value
128
+ }
129
+ }
130
+ csv_row = OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.csv) + csv_row
131
+ // OINOLog_debug("OINODbModelSet._writeRowCsv="+csv_row)
132
+ return csv_row
133
+ }
134
+
135
+ private async _writeStringCsv():Promise<string> {
136
+ let result:string = this._writeHeaderCsv()
137
+ while (!this.dataset.isEof()) {
138
+ if (result != "") {
139
+ result += "\r\n"
140
+ }
141
+ const row:OINODataRow = this.dataset.getRow()
142
+ // OINOLog.debug("OINODbModelSet._writeStringCsv: row", {row:row})
143
+ result += this._writeRowCsv(row)
144
+ await this.dataset.next()
145
+ }
146
+ // OINOLog_debug("OINODbModelSet._writeStringCsv="+result)
147
+ return result
148
+ }
149
+
150
+ private _writeRowFormdataParameterBlock(blockName:string, blockValue:string|null, multipartBoundary:string):string {
151
+ if (blockValue === null) {
152
+ return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"\r\n\r\n"
153
+ } else {
154
+ return multipartBoundary + "\r\n" + "Content-Disposition: form-data; name=\"" + blockName + "\"\r\n\r\n" + blockValue + "\r\n"
155
+ }
156
+ }
157
+
158
+ private _writeRowFormdataFileBlock(blockName:string, blockValue:string, multipartBoundary:string):string {
159
+ 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"
160
+ }
161
+
162
+ private _writeRowFormdata(row:OINODataRow):string {
163
+ // console.log("OINODbModelSet._writeRowFormdata: row", row)
164
+ const multipart_boundary:string = "---------OINOMultipartBoundary35424568" // this method is just used for test data generation and we want it to be static
165
+ const model:OINODbDataModel = this.datamodel
166
+ const fields:OINODbDataField[] = model.fields
167
+ let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
168
+ let primary_key_values:string[] = []
169
+ let result:string = ""
170
+ for (let i=0; i<fields.length; i++) {
171
+ const f = fields[i]
172
+ let value:string|null|undefined = f.serializeCell(row[i])
173
+ let formdata_block:string = ""
174
+ let is_file = (f instanceof OINOBlobDataField)
175
+
176
+ if (value === undefined) {
177
+ OINOLog.info("OINODbModelSet._writeRowFormdata: undefined value skipped.", {field:f.name})
178
+
179
+ } else if (value === null) {
180
+ formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, null, multipart_boundary)
181
+
182
+ } else {
183
+ value = this._encodeAndHashFieldValue(f, value, OINOContentType.formdata, primary_key_values, f.name + " " + row_id_seed)
184
+ if (is_file) {
185
+ formdata_block = this._writeRowFormdataFileBlock(f.name, value, multipart_boundary)
186
+ } else {
187
+ formdata_block = this._writeRowFormdataParameterBlock(fields[i].name, value, multipart_boundary)
188
+ }
189
+ }
190
+
191
+
192
+ // OINOLog.debug("OINODbModelSet._writeRowFormdata next block", {formdata_block:formdata_block})
193
+ result += formdata_block
194
+ }
195
+ result = this._writeRowFormdataParameterBlock(OINODbConfig.OINODB_ID_FIELD, OINODbConfig.printOINOId(primary_key_values), multipart_boundary) + result
196
+ return result
197
+ }
198
+
199
+ private _writeStringFormdata():string {
200
+ const row:OINODataRow = this.dataset.getRow()
201
+ // OINOLog.debug("OINODbModelSet._writeStringFormdata: row", {row:row})
202
+ let result:string = this._writeRowFormdata(row)
203
+ return result
204
+ }
205
+
206
+
207
+ private _writeRowUrlencode(row:OINODataRow):string {
208
+ // console.log("OINODbModelSet._writeRowUrlencode row=" + row)
209
+ const model:OINODbDataModel = this.datamodel
210
+ const fields:OINODbDataField[] = model.fields
211
+ let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
212
+ let primary_key_values:string[] = []
213
+ let urlencode_row:string = ""
214
+ for (let i=0; i<fields.length; i++) {
215
+ const f = fields[i]
216
+ let value:string|null|undefined = f.serializeCell(row[i])
217
+ if ((value === undefined)) { // || (value === null)) {
218
+ // console.log("OINODbModelSet._writeRowUrlencode undefined field value:" + fields[i].name)
219
+ } else {
220
+ value = this._encodeAndHashFieldValue(f, value, OINOContentType.urlencode, primary_key_values, f.name + " " + row_id_seed)
221
+ if (urlencode_row != "") {
222
+ urlencode_row += "&"
223
+ }
224
+ urlencode_row += OINOStr.encode(f.name, OINOContentType.urlencode) + "=" + value
225
+ }
226
+ }
227
+ urlencode_row = OINOStr.encode(OINODbConfig.OINODB_ID_FIELD, OINOContentType.urlencode) + "=" + OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.urlencode) + "&" + urlencode_row
228
+ // OINOLog_debug("OINODbModelSet._writeRowCsv="+csv_row)
229
+ return urlencode_row
230
+ }
231
+
232
+ private async _writeStringUrlencode():Promise<string> {
233
+ let result:string = ""
234
+ let line_count = 0
235
+ while (!this.dataset.isEof()) {
236
+ const row:OINODataRow = this.dataset.getRow()
237
+ // OINOLog.debug("OINODbModelSet._writeStringUrlencode: row", {row:row})
238
+ result += this._writeRowUrlencode(row) + "\r\n"
239
+ await this.dataset.next()
240
+ line_count += 1
241
+ }
242
+ // OINOLog_debug("OINODbModelSet._writeStringCsv="+result)
243
+ if (line_count > 1) {
244
+ OINOLog.warning("OINODbModelSet._writeStringUrlencode: content type " + OINOContentType.urlencode + " does not officially support multiline content!")
245
+ }
246
+ return result
247
+ }
248
+
249
+ /**
250
+ * Serialize model set in the given format.
251
+ *
252
+ * @param [contentType=OINOContentType.json] serialization content type
253
+ *
254
+ */
255
+ async writeString(contentType:OINOContentType = OINOContentType.json):Promise<string> {
256
+ let result:string = ""
257
+ if (contentType == OINOContentType.csv) {
258
+ result += await this._writeStringCsv()
259
+
260
+ } else if (contentType == OINOContentType.json) {
261
+ result += await this._writeStringJson()
262
+
263
+ } else if (contentType == OINOContentType.formdata) {
264
+ result += await this._writeStringFormdata()
265
+
266
+ } else if (contentType == OINOContentType.urlencode) {
267
+ result += await this._writeStringUrlencode()
268
+
269
+ } else {
270
+ OINOLog.error("OINODbModelSet.writeString: content type is only for input!", {contentType:contentType})
271
+ }
272
+ return result
273
+ }
274
+
275
+ /**
276
+ * Get value of given field in the current row. Undefined if no rows,
277
+ * field not found or value does not exist.
278
+ *
279
+ * @param fieldName name of the field
280
+ * @param serialize serialize the value
281
+ *
282
+ */
283
+ getValueByFieldName(fieldName:string, serialize:boolean = false):OINODataCell {
284
+ let result:OINODataCell = undefined
285
+ if (!this.dataset.isEof()) {
286
+ const current_row:OINODataRow = this.dataset.getRow()
287
+ const field_index:number = this.datamodel.findFieldIndexByName(fieldName)
288
+ if (field_index >= 0) {
289
+ result = current_row[field_index]
290
+ if (serialize) {
291
+ result = this.datamodel.fields[field_index].serializeCell(result)
292
+ }
293
+ }
294
+ }
295
+ return result
296
+ }
297
+ }
298
+