@oino-ts/db 0.13.1 → 0.13.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.
@@ -12,7 +12,7 @@ const index_js_1 = require("./index.js");
12
12
  *
13
13
  */
14
14
  class OINODbDataModel {
15
- _columnLookup;
15
+ _fieldIndexLookup;
16
16
  /** Database refererence of the table */
17
17
  api;
18
18
  /** Field refererences of the API */
@@ -25,7 +25,7 @@ class OINODbDataModel {
25
25
  *
26
26
  */
27
27
  constructor(api) {
28
- this._columnLookup = {};
28
+ this._fieldIndexLookup = {};
29
29
  this.api = api;
30
30
  this.fields = [];
31
31
  }
@@ -116,7 +116,7 @@ class OINODbDataModel {
116
116
  */
117
117
  addField(field) {
118
118
  this.fields.push(field);
119
- this._columnLookup[field.name] = this.fields.length - 1;
119
+ this._fieldIndexLookup[field.name] = this.fields.length - 1;
120
120
  }
121
121
  /**
122
122
  * Find a field of a given name if any.
@@ -125,7 +125,7 @@ class OINODbDataModel {
125
125
  *
126
126
  */
127
127
  findFieldByName(name) {
128
- const i = this._columnLookup[name];
128
+ const i = this._fieldIndexLookup[name];
129
129
  if (i >= 0) {
130
130
  return this.fields[i];
131
131
  }
@@ -140,7 +140,7 @@ class OINODbDataModel {
140
140
  *
141
141
  */
142
142
  findFieldIndexByName(name) {
143
- const i = this._columnLookup[name];
143
+ const i = this._fieldIndexLookup[name];
144
144
  if (i >= 0) {
145
145
  return i;
146
146
  }
@@ -238,6 +238,9 @@ class OINODbModelSet {
238
238
  let result = {};
239
239
  for (let i = 0; i < fields.length; i++) {
240
240
  const f = fields[i];
241
+ if (f.fieldParams.isPrimaryKey) {
242
+ primary_key_values.push(f.serializeCell(row[i]) || "");
243
+ }
241
244
  if (this.sqlParams?.select?.isSelected(f) === false) {
242
245
  continue;
243
246
  }
@@ -305,13 +308,16 @@ class OINODbModelSet {
305
308
  /**
306
309
  * Export all rows as a record with OINOId as key and object with row cells as values.
307
310
  *
311
+ * @param idFieldName optional field name to use as key instead of OINOId
308
312
  */
309
- async exportAsRecord() {
313
+ async exportAsRecord(idFieldName) {
310
314
  const result = {};
315
+ const row_id_field = idFieldName || index_js_1.OINODbConfig.OINODB_ID_FIELD;
311
316
  while (!this.dataset.isEof()) {
312
317
  const row_data = this.dataset.getRow();
313
318
  const row_export = this._exportRow(row_data);
314
- result[row_export[index_js_1.OINODbConfig.OINODB_ID_FIELD]] = row_export;
319
+ const row_id = row_export[row_id_field];
320
+ result[row_id] = row_export;
315
321
  await this.dataset.next();
316
322
  }
317
323
  return result;
@@ -9,7 +9,7 @@ import { OINO_ERROR_PREFIX, OINODbConfig, OINONumberDataField, OINODB_UNDEFINED
9
9
  *
10
10
  */
11
11
  export class OINODbDataModel {
12
- _columnLookup;
12
+ _fieldIndexLookup;
13
13
  /** Database refererence of the table */
14
14
  api;
15
15
  /** Field refererences of the API */
@@ -22,7 +22,7 @@ export class OINODbDataModel {
22
22
  *
23
23
  */
24
24
  constructor(api) {
25
- this._columnLookup = {};
25
+ this._fieldIndexLookup = {};
26
26
  this.api = api;
27
27
  this.fields = [];
28
28
  }
@@ -113,7 +113,7 @@ export class OINODbDataModel {
113
113
  */
114
114
  addField(field) {
115
115
  this.fields.push(field);
116
- this._columnLookup[field.name] = this.fields.length - 1;
116
+ this._fieldIndexLookup[field.name] = this.fields.length - 1;
117
117
  }
118
118
  /**
119
119
  * Find a field of a given name if any.
@@ -122,7 +122,7 @@ export class OINODbDataModel {
122
122
  *
123
123
  */
124
124
  findFieldByName(name) {
125
- const i = this._columnLookup[name];
125
+ const i = this._fieldIndexLookup[name];
126
126
  if (i >= 0) {
127
127
  return this.fields[i];
128
128
  }
@@ -137,7 +137,7 @@ export class OINODbDataModel {
137
137
  *
138
138
  */
139
139
  findFieldIndexByName(name) {
140
- const i = this._columnLookup[name];
140
+ const i = this._fieldIndexLookup[name];
141
141
  if (i >= 0) {
142
142
  return i;
143
143
  }
@@ -235,6 +235,9 @@ export class OINODbModelSet {
235
235
  let result = {};
236
236
  for (let i = 0; i < fields.length; i++) {
237
237
  const f = fields[i];
238
+ if (f.fieldParams.isPrimaryKey) {
239
+ primary_key_values.push(f.serializeCell(row[i]) || "");
240
+ }
238
241
  if (this.sqlParams?.select?.isSelected(f) === false) {
239
242
  continue;
240
243
  }
@@ -302,13 +305,16 @@ export class OINODbModelSet {
302
305
  /**
303
306
  * Export all rows as a record with OINOId as key and object with row cells as values.
304
307
  *
308
+ * @param idFieldName optional field name to use as key instead of OINOId
305
309
  */
306
- async exportAsRecord() {
310
+ async exportAsRecord(idFieldName) {
307
311
  const result = {};
312
+ const row_id_field = idFieldName || OINODbConfig.OINODB_ID_FIELD;
308
313
  while (!this.dataset.isEof()) {
309
314
  const row_data = this.dataset.getRow();
310
315
  const row_export = this._exportRow(row_data);
311
- result[row_export[OINODbConfig.OINODB_ID_FIELD]] = row_export;
316
+ const row_id = row_export[row_id_field];
317
+ result[row_id] = row_export;
312
318
  await this.dataset.next();
313
319
  }
314
320
  return result;
@@ -4,7 +4,7 @@ import { OINODbDataField, OINODbApi, OINODataRow, OINODbDataFieldFilter, OINODbS
4
4
  *
5
5
  */
6
6
  export declare class OINODbDataModel {
7
- private _columnLookup;
7
+ private _fieldIndexLookup;
8
8
  /** Database refererence of the table */
9
9
  readonly api: OINODbApi;
10
10
  /** Field refererences of the API */
@@ -55,6 +55,7 @@ export declare class OINODbModelSet {
55
55
  /**
56
56
  * Export all rows as a record with OINOId as key and object with row cells as values.
57
57
  *
58
+ * @param idFieldName optional field name to use as key instead of OINOId
58
59
  */
59
- exportAsRecord(): Promise<Record<string, any>>;
60
+ exportAsRecord(idFieldName?: string): Promise<Record<string, any>>;
60
61
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db",
3
- "version": "0.13.1",
3
+ "version": "0.13.4",
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.13.1",
22
+ "@oino-ts/common": "0.13.4",
23
23
  "oino-ts": "file:.."
24
24
  },
25
25
  "devDependencies": {
26
- "@oino-ts/types": "0.13.1",
26
+ "@oino-ts/types": "0.13.4",
27
27
  "@types/bun": "^1.1.14",
28
28
  "@types/node": "^20.14.10",
29
29
  "typescript": "~5.9.0"
@@ -11,7 +11,7 @@ import { OINODbDataField, OINODbApi, OINODataRow, OINO_ERROR_PREFIX, OINODbDataF
11
11
  *
12
12
  */
13
13
  export class OINODbDataModel {
14
- private _columnLookup:Record<string, number>;
14
+ private _fieldIndexLookup:Record<string, number>;
15
15
 
16
16
  /** Database refererence of the table */
17
17
  readonly api:OINODbApi
@@ -27,7 +27,7 @@ export class OINODbDataModel {
27
27
  *
28
28
  */
29
29
  constructor(api:OINODbApi) {
30
- this._columnLookup = {}
30
+ this._fieldIndexLookup = {}
31
31
  this.api = api
32
32
  this.fields = []
33
33
  }
@@ -122,7 +122,7 @@ export class OINODbDataModel {
122
122
  */
123
123
  addField(field:OINODbDataField) {
124
124
  this.fields.push(field)
125
- this._columnLookup[field.name] = this.fields.length-1
125
+ this._fieldIndexLookup[field.name] = this.fields.length-1
126
126
  }
127
127
 
128
128
  /**
@@ -132,7 +132,7 @@ export class OINODbDataModel {
132
132
  *
133
133
  */
134
134
  findFieldByName(name:string):OINODbDataField|null {
135
- const i:number = this._columnLookup[name]
135
+ const i:number = this._fieldIndexLookup[name]
136
136
  if (i >= 0) {
137
137
  return this.fields[i]
138
138
  } else {
@@ -147,7 +147,7 @@ export class OINODbDataModel {
147
147
  *
148
148
  */
149
149
  findFieldIndexByName(name:string):number {
150
- const i:number = this._columnLookup[name]
150
+ const i:number = this._fieldIndexLookup[name]
151
151
  if (i >= 0) {
152
152
  return i
153
153
  } else {
@@ -256,6 +256,9 @@ export class OINODbModelSet {
256
256
  let result:any = {}
257
257
  for (let i=0; i<fields.length; i++) {
258
258
  const f = fields[i]
259
+ if (f.fieldParams.isPrimaryKey) {
260
+ primary_key_values.push(f.serializeCell(row[i]) || "")
261
+ }
259
262
  if (this.sqlParams?.select?.isSelected(f) === false) {
260
263
  continue
261
264
  }
@@ -326,15 +329,18 @@ export class OINODbModelSet {
326
329
 
327
330
  /**
328
331
  * Export all rows as a record with OINOId as key and object with row cells as values.
329
- *
332
+ *
333
+ * @param idFieldName optional field name to use as key instead of OINOId
330
334
  */
331
335
 
332
- async exportAsRecord():Promise<Record<string, any>> {
336
+ async exportAsRecord(idFieldName?:string):Promise<Record<string, any>> {
333
337
  const result:Record<string, any> = {}
338
+ const row_id_field = idFieldName || OINODbConfig.OINODB_ID_FIELD
334
339
  while (!this.dataset.isEof()) {
335
340
  const row_data:OINODataRow = this.dataset.getRow()
336
341
  const row_export = this._exportRow(row_data)
337
- result[row_export[OINODbConfig.OINODB_ID_FIELD]] = row_export
342
+ const row_id = row_export[row_id_field]
343
+ result[row_id] = row_export
338
344
  await this.dataset.next()
339
345
  }
340
346
  return result