@oino-ts/db 0.12.2 → 0.13.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.
@@ -63,7 +63,7 @@ class OINODbModelSet {
63
63
  }
64
64
  let value = f.serializeCell(row[i]);
65
65
  if (value === undefined) {
66
- index_js_1.OINOLog.info("@oino-ts/db", "OINODbModelSet", "_writeRowJson", "Undefined value skipped", { field_name: f.name });
66
+ // skip undefined values
67
67
  }
68
68
  else if (value === null) {
69
69
  json_row += "," + index_js_1.OINOStr.encode(f.name, index_js_1.OINOContentType.json) + ":null";
@@ -229,6 +229,33 @@ class OINODbModelSet {
229
229
  }
230
230
  return result;
231
231
  }
232
+ _exportRow(row) {
233
+ // console.log("OINODbModelSet._exportRow: row=" + row)
234
+ const model = this.datamodel;
235
+ const fields = model.fields;
236
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
237
+ let primary_key_values = [];
238
+ let result = {};
239
+ for (let i = 0; i < fields.length; i++) {
240
+ const f = fields[i];
241
+ if (this.sqlParams?.select?.isSelected(f) === false) {
242
+ continue;
243
+ }
244
+ let value = f.serializeCell(row[i]);
245
+ if (value === undefined) {
246
+ // skip undefined values
247
+ }
248
+ else if (value === null) {
249
+ result[f.name] = null;
250
+ }
251
+ else {
252
+ value = this._encodeAndHashFieldValue(f, value, index_js_1.OINOContentType.json, primary_key_values, f.name + " " + row_id_seed);
253
+ result[f.name] = value;
254
+ }
255
+ }
256
+ result[index_js_1.OINODbConfig.OINODB_ID_FIELD] = index_js_1.OINODbConfig.printOINOId(primary_key_values);
257
+ return result;
258
+ }
232
259
  /**
233
260
  * Serialize model set in the given format.
234
261
  *
@@ -276,5 +303,15 @@ class OINODbModelSet {
276
303
  }
277
304
  return result;
278
305
  }
306
+ async exportAsRecord() {
307
+ const result = {};
308
+ while (!this.dataset.isEof()) {
309
+ const row_data = this.dataset.getRow();
310
+ const row_export = this._exportRow(row_data);
311
+ result[row_export[index_js_1.OINODbConfig.OINODB_ID_FIELD]] = row_export;
312
+ await this.dataset.next();
313
+ }
314
+ return result;
315
+ }
279
316
  }
280
317
  exports.OINODbModelSet = OINODbModelSet;
@@ -60,7 +60,7 @@ export class OINODbModelSet {
60
60
  }
61
61
  let value = f.serializeCell(row[i]);
62
62
  if (value === undefined) {
63
- OINOLog.info("@oino-ts/db", "OINODbModelSet", "_writeRowJson", "Undefined value skipped", { field_name: f.name });
63
+ // skip undefined values
64
64
  }
65
65
  else if (value === null) {
66
66
  json_row += "," + OINOStr.encode(f.name, OINOContentType.json) + ":null";
@@ -226,6 +226,33 @@ export class OINODbModelSet {
226
226
  }
227
227
  return result;
228
228
  }
229
+ _exportRow(row) {
230
+ // console.log("OINODbModelSet._exportRow: row=" + row)
231
+ const model = this.datamodel;
232
+ const fields = model.fields;
233
+ let row_id_seed = model.getRowPrimarykeyValues(row).join(' ');
234
+ let primary_key_values = [];
235
+ let result = {};
236
+ for (let i = 0; i < fields.length; i++) {
237
+ const f = fields[i];
238
+ if (this.sqlParams?.select?.isSelected(f) === false) {
239
+ continue;
240
+ }
241
+ let value = f.serializeCell(row[i]);
242
+ if (value === undefined) {
243
+ // skip undefined values
244
+ }
245
+ else if (value === null) {
246
+ result[f.name] = null;
247
+ }
248
+ else {
249
+ value = this._encodeAndHashFieldValue(f, value, OINOContentType.json, primary_key_values, f.name + " " + row_id_seed);
250
+ result[f.name] = value;
251
+ }
252
+ }
253
+ result[OINODbConfig.OINODB_ID_FIELD] = OINODbConfig.printOINOId(primary_key_values);
254
+ return result;
255
+ }
229
256
  /**
230
257
  * Serialize model set in the given format.
231
258
  *
@@ -273,4 +300,14 @@ export class OINODbModelSet {
273
300
  }
274
301
  return result;
275
302
  }
303
+ async exportAsRecord() {
304
+ const result = {};
305
+ while (!this.dataset.isEof()) {
306
+ const row_data = this.dataset.getRow();
307
+ const row_export = this._exportRow(row_data);
308
+ result[row_export[OINODbConfig.OINODB_ID_FIELD]] = row_export;
309
+ await this.dataset.next();
310
+ }
311
+ return result;
312
+ }
276
313
  }
@@ -35,6 +35,7 @@ export declare class OINODbModelSet {
35
35
  private _writeStringFormdata;
36
36
  private _writeRowUrlencode;
37
37
  private _writeStringUrlencode;
38
+ private _exportRow;
38
39
  /**
39
40
  * Serialize model set in the given format.
40
41
  *
@@ -51,4 +52,5 @@ export declare class OINODbModelSet {
51
52
  *
52
53
  */
53
54
  getValueByFieldName(fieldName: string, serialize?: boolean): OINODataCell;
55
+ exportAsRecord(): Promise<Record<string, any>>;
54
56
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db",
3
- "version": "0.12.2",
3
+ "version": "0.13.0",
4
4
  "description": "OINO TS library package for publishing an SQL database tables as a REST API.",
5
5
  "author": "Matias Kiviniemi (pragmatta)",
6
6
  "license": "MPL-2.0",
@@ -19,11 +19,11 @@
19
19
  "module": "./dist/esm/index.js",
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "dependencies": {
22
- "@oino-ts/common": "0.12.2",
22
+ "@oino-ts/common": "0.13.0",
23
23
  "oino-ts": "file:.."
24
24
  },
25
25
  "devDependencies": {
26
- "@oino-ts/types": "0.12.2",
26
+ "@oino-ts/types": "0.13.0",
27
27
  "@types/bun": "^1.1.14",
28
28
  "@types/node": "^20.14.10",
29
29
  "typescript": "~5.9.0"
@@ -108,6 +108,7 @@ const OWASP_TESTS:OINOTestParams[] = [
108
108
  const API_CROSSCHECKS:string[] = [
109
109
  "[HTTP GET] select *: GET JSON 1",
110
110
  "[HTTP GET] select * with template: GET HTML 1",
111
+ "[HTTP GET] select *: GET RECORD 1",
111
112
  "[HTTP POST] insert: GET JSON 1",
112
113
  "[HTTP POST] insert: GET CSV 1",
113
114
  "[HTTP PUT] update JSON: GET JSON 1",
@@ -218,6 +219,10 @@ export async function OINOTestApi(dbParams:OINODbParams, testParams: OINOTestPar
218
219
  expect(encodeData(await (await api.doRequest("GET", "", "", empty_params)).data?.writeString(OINOContentType.csv))).toMatchSnapshot("GET CSV")
219
220
  })
220
221
 
222
+ await test(target_name + target_db + target_table + target_group + " select *", async () => {
223
+ expect(encodeData(JSON.stringify(await (await api.doRequest("GET", "", "", empty_params)).data?.exportAsRecord()))).toMatchSnapshot("GET RECORD")
224
+ })
225
+
221
226
  await test(target_name + target_db + target_table + target_group + " select * with template", async () => {
222
227
  const template = createApiTemplate(api)
223
228
  const api_result:OINODbApiResult = await api.doRequest("GET", "", "", empty_params)
@@ -228,6 +233,7 @@ export async function OINOTestApi(dbParams:OINODbParams, testParams: OINOTestPar
228
233
  await test(target_name + target_db + target_table + target_group + " select * with filter", async () => {
229
234
  expect(encodeData(await (await api.doRequest("GET", "", "", request_params_with_filters)).data?.writeString())).toMatchSnapshot("GET JSON FILTER")
230
235
  })
236
+
231
237
 
232
238
  // remove filter so it does not affect rest of the tests
233
239
  request_params.sqlParams.filter = undefined
@@ -69,7 +69,7 @@ export class OINODbModelSet {
69
69
  }
70
70
  let value:string|null|undefined = f.serializeCell(row[i])
71
71
  if (value === undefined) {
72
- OINOLog.info("@oino-ts/db", "OINODbModelSet", "_writeRowJson", "Undefined value skipped", {field_name:f.name})
72
+ // skip undefined values
73
73
 
74
74
  } else if (value === null) {
75
75
  json_row += "," + OINOStr.encode(f.name, OINOContentType.json) + ":null"
@@ -247,6 +247,35 @@ export class OINODbModelSet {
247
247
  return result
248
248
  }
249
249
 
250
+ private _exportRow(row:OINODataRow):any {
251
+ // console.log("OINODbModelSet._exportRow: row=" + row)
252
+ const model:OINODbDataModel = this.datamodel
253
+ const fields:OINODbDataField[] = model.fields
254
+ let row_id_seed:string = model.getRowPrimarykeyValues(row).join(' ')
255
+ let primary_key_values:string[] = []
256
+ let result:any = {}
257
+ for (let i=0; i<fields.length; i++) {
258
+ const f = fields[i]
259
+ if (this.sqlParams?.select?.isSelected(f) === false) {
260
+ continue
261
+ }
262
+ let value:string|null|undefined = f.serializeCell(row[i])
263
+ if (value === undefined) {
264
+ // skip undefined values
265
+
266
+ } else if (value === null) {
267
+ result[f.name] = null
268
+
269
+ } else {
270
+ value = this._encodeAndHashFieldValue(f, value, OINOContentType.json, primary_key_values, f.name + " " + row_id_seed)
271
+ result[f.name] = value
272
+ }
273
+ }
274
+ result[OINODbConfig.OINODB_ID_FIELD] = OINODbConfig.printOINOId(primary_key_values)
275
+ return result
276
+ }
277
+
278
+
250
279
  /**
251
280
  * Serialize model set in the given format.
252
281
  *
@@ -295,5 +324,21 @@ export class OINODbModelSet {
295
324
  }
296
325
  return result
297
326
  }
298
- }
299
327
 
328
+ /**
329
+ * Export all rows as a record with OINOId as key and object with row cells as values.
330
+ *
331
+ */
332
+
333
+ async exportAsRecord():Promise<Record<string, any>> {
334
+ const result:Record<string, any> = {}
335
+ while (!this.dataset.isEof()) {
336
+ const row_data:OINODataRow = this.dataset.getRow()
337
+ const row_export = this._exportRow(row_data)
338
+ result[row_export[OINODbConfig.OINODB_ID_FIELD]] = row_export
339
+ await this.dataset.next()
340
+ }
341
+ return result
342
+ }
343
+
344
+ }