@oino-ts/db 0.0.13 → 0.0.15

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.
@@ -62,23 +62,29 @@ class OINODbHtmlTemplate extends index_js_1.OINOHtmlTemplate {
62
62
  * Creates HTML Response from API modelset.
63
63
  *
64
64
  * @param modelset OINO API dataset
65
+ * @param overrideValues values to override in the data
65
66
  *
66
67
  */
67
- renderFromDbData(modelset) {
68
+ renderFromDbData(modelset, overrideValues) {
69
+ index_js_1.OINOBenchmark.start("OINOHtmlTemplate", "renderFromDbData");
68
70
  let html = "";
69
71
  const dataset = modelset.dataset;
70
72
  const datamodel = modelset.datamodel;
71
73
  const api = modelset.datamodel.api;
72
74
  const modified_index = datamodel.findFieldIndexByName(api.params.cacheModifiedField || "");
73
75
  let last_modified = this.modified;
76
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
74
77
  while (!dataset.isEof()) {
75
78
  const row = dataset.getRow();
76
79
  if (modified_index >= 0) {
77
80
  last_modified = Math.max(last_modified, new Date(row[modified_index]).getTime());
81
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
78
82
  }
79
83
  let row_id_seed = datamodel.getRowPrimarykeyValues(row).join(' ');
80
84
  let primary_key_values = [];
81
- let html_row = this.template.replaceAll('###' + index_js_1.OINODbConfig.OINODB_ID_FIELD + '###', '###createHtmlFromData_temporary_oinoid###');
85
+ this.clearVariables();
86
+ this.setVariableFromValue(index_js_1.OINODbConfig.OINODB_ID_FIELD, "");
87
+ // let html_row:string = this.template.replaceAll('###' + OINODbConfig.OINODB_ID_FIELD + '###', '###createHtmlFromData_temporary_oinoid###')
82
88
  for (let i = 0; i < datamodel.fields.length; i++) {
83
89
  const f = datamodel.fields[i];
84
90
  let value = f.serializeCell(row[i]);
@@ -89,14 +95,18 @@ class OINODbHtmlTemplate extends index_js_1.OINOHtmlTemplate {
89
95
  primary_key_values.push(value || "");
90
96
  }
91
97
  // OINOLog.debug("renderFromDbData replace field value", {field:f.name, value:value })
92
- html_row = html_row.replaceAll('###' + f.name + '###', index_js_1.OINOStr.encode(value, index_js_1.OINOContentType.html));
98
+ this.setVariableFromValue(f.name, value || "");
93
99
  }
94
- html_row = html_row.replaceAll('###createHtmlFromData_temporary_oinoid###', index_js_1.OINOStr.encode(index_js_1.OINODbConfig.printOINOId(primary_key_values), index_js_1.OINOContentType.html));
95
- html += html_row + "\r\n";
100
+ this.setVariableFromProperties(overrideValues);
101
+ this.setVariableFromValue(index_js_1.OINODbConfig.OINODB_ID_FIELD, index_js_1.OINODbConfig.printOINOId(primary_key_values));
102
+ // html_row = html_row.replaceAll('###createHtmlFromData_temporary_oinoid###', OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.html))
103
+ html += this._renderHtml() + "\r\n";
96
104
  dataset.next();
97
105
  }
98
- const result = new index_js_1.OINOHttpResult(html);
99
- result.lastModified = last_modified;
106
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
107
+ this.modified = last_modified;
108
+ const result = this._createHttpResult(html, false);
109
+ index_js_1.OINOBenchmark.end("OINOHtmlTemplate", "renderFromDbData");
100
110
  return result;
101
111
  }
102
112
  }
@@ -131,7 +141,7 @@ class OINODbApi {
131
141
  this.params = params;
132
142
  this.datamodel = new index_js_1.OINODbDataModel(this);
133
143
  if (this.params.hashidKey) {
134
- this.hashid = new hashid_1.OINOHashid(this.params.hashidKey, this.db.name, this.params.hashidLength, this.params.hashidRandomIds);
144
+ this.hashid = new hashid_1.OINOHashid(this.params.hashidKey, this.db.name, this.params.hashidLength, this.params.hashidStaticIds);
135
145
  }
136
146
  else {
137
147
  this.hashid = null;
@@ -171,7 +181,6 @@ class OINODbApi {
171
181
  //logDebug("OINODbApi.validateHttpValues", {result:result})
172
182
  }
173
183
  async _doGet(result, id, params) {
174
- index_js_1.OINOBenchmark.start("doGet");
175
184
  const sql = this.datamodel.printSqlSelect(id, params.sqlParams || {});
176
185
  // OINOLog.debug("OINODbApi.doGet sql", {sql:sql})
177
186
  try {
@@ -189,10 +198,8 @@ class OINODbApi {
189
198
  result.setError(500, "Unhandled exception in doGet: " + e.message, "DoGet");
190
199
  result.addDebug("OINO GET SQL [" + sql + "]", "DoGet");
191
200
  }
192
- index_js_1.OINOBenchmark.end("doGet");
193
201
  }
194
202
  async _doPost(result, rows) {
195
- index_js_1.OINOBenchmark.start("doPost");
196
203
  let sql = "";
197
204
  try {
198
205
  let i = 0;
@@ -223,10 +230,8 @@ class OINODbApi {
223
230
  result.setError(500, "Unhandled exception in doPost: " + e.message, "DoPost");
224
231
  result.addDebug("OINO POST SQL [" + sql + "]", "DoPost");
225
232
  }
226
- index_js_1.OINOBenchmark.end("doPost");
227
233
  }
228
234
  async _doPut(result, id, row) {
229
- index_js_1.OINOBenchmark.start("doPut");
230
235
  let sql = "";
231
236
  try {
232
237
  this._validateRowValues(result, row, false);
@@ -246,10 +251,8 @@ class OINODbApi {
246
251
  result.setError(500, "Unhandled exception: " + e.message, "DoPut");
247
252
  result.addDebug("OINO POST SQL [" + sql + "]", "DoPut");
248
253
  }
249
- index_js_1.OINOBenchmark.end("doPut");
250
254
  }
251
255
  async _doDelete(result, id) {
252
- index_js_1.OINOBenchmark.start("doDelete");
253
256
  let sql = "";
254
257
  try {
255
258
  sql = this.datamodel.printSqlDelete(id);
@@ -266,7 +269,6 @@ class OINODbApi {
266
269
  result.setError(500, "Unhandled exception: " + e.message, "DoDelete");
267
270
  result.addDebug("OINO DELETE SQL [" + sql + "]", "DoDelete");
268
271
  }
269
- index_js_1.OINOBenchmark.end("doDelete");
270
272
  }
271
273
  /**
272
274
  * Method for handlind a HTTP REST request with GET, POST, PUT, DELETE corresponding to
@@ -279,7 +281,7 @@ class OINODbApi {
279
281
  *
280
282
  */
281
283
  async doRequest(method, id, body, params = API_EMPTY_PARAMS) {
282
- index_js_1.OINOBenchmark.start("doRequest");
284
+ index_js_1.OINOBenchmark.start("OINODbApi", "doRequest");
283
285
  // OINOLog.debug("OINODbApi.doRequest enter", {method:method, id:id, body:body, params:params})
284
286
  let result = new OINODbApiResult(params);
285
287
  let rows = [];
@@ -347,7 +349,7 @@ class OINODbApi {
347
349
  else {
348
350
  result.setError(405, "Unsupported HTTP method '" + method + "'", "DoRequest");
349
351
  }
350
- index_js_1.OINOBenchmark.end("doRequest");
352
+ index_js_1.OINOBenchmark.end("OINODbApi", "doRequest", method);
351
353
  return result;
352
354
  }
353
355
  }
@@ -238,7 +238,7 @@ class OINONumberDataField extends OINODbDataField {
238
238
  if (value === undefined) {
239
239
  return undefined;
240
240
  }
241
- else if ((value == "") || (value == null)) {
241
+ else if ((value === "") || (value === null)) {
242
242
  return null;
243
243
  }
244
244
  else {
@@ -225,10 +225,10 @@ class OINODbDataModel {
225
225
  const order_sql = params.order?.toSql(this) || "";
226
226
  const limit_sql = params.limit?.toSql(this) || "";
227
227
  // OINOLog.debug("OINODbDataModel.printSqlSelect", {select_sql:result, filter_sql:filter_sql, order_sql:order_sql})
228
- if ((id != "") && (filter_sql != "")) {
228
+ if ((id != null) && (id != "") && (filter_sql != "")) {
229
229
  result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id) + " AND " + filter_sql;
230
230
  }
231
- else if (id != "") {
231
+ else if ((id != null) && (id != "")) {
232
232
  result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id);
233
233
  }
234
234
  else if (filter_sql != "") {
@@ -6,7 +6,6 @@
6
6
  */
7
7
  Object.defineProperty(exports, "__esModule", { value: true });
8
8
  exports.OINODbFactory = void 0;
9
- const OINOResult_js_1 = require("../../types/src/OINOResult.js");
10
9
  const index_js_1 = require("./index.js");
11
10
  /**
12
11
  * Static factory class for easily creating things based on data
@@ -143,7 +142,7 @@ class OINODbFactory {
143
142
  html += html_row + "\r\n";
144
143
  dataset.next();
145
144
  }
146
- const result = new OINOResult_js_1.OINOHttpResult(html);
145
+ const result = new index_js_1.OINOHttpResult(html);
147
146
  return result;
148
147
  }
149
148
  /**
@@ -180,7 +179,7 @@ class OINODbFactory {
180
179
  html = html.replaceAll('###messages###', "<ul>" + messages + "</ul>");
181
180
  }
182
181
  html = html.replace(/###[^#]*###/g, "");
183
- const http_result = new OINOResult_js_1.OINOHttpResult(html);
182
+ const http_result = new index_js_1.OINOHttpResult(html);
184
183
  return http_result;
185
184
  }
186
185
  static _findCsvLineEnd(csvData, start) {
@@ -250,15 +250,19 @@ class OINODbModelSet {
250
250
  * field not found or value does not exist.
251
251
  *
252
252
  * @param fieldName name of the field
253
+ * @param serialize serialize the value
253
254
  *
254
255
  */
255
- getValueByFieldName(fieldName) {
256
+ getValueByFieldName(fieldName, serialize = false) {
256
257
  let result = undefined;
257
258
  if (!this.dataset.isEof()) {
258
259
  const current_row = this.dataset.getRow();
259
260
  const field_index = this.datamodel.findFieldIndexByName(fieldName);
260
261
  if (field_index >= 0) {
261
262
  result = current_row[field_index];
263
+ if (serialize) {
264
+ result = this.datamodel.fields[field_index].serializeCell(result);
265
+ }
262
266
  }
263
267
  }
264
268
  return result;
@@ -3,7 +3,7 @@
3
3
  * License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import { OINODbDataModel, OINOStringDataField, OINO_ERROR_PREFIX, OINODbModelSet, OINOBenchmark, OINODbFactory, OINOLog, OINODbConfig, OINOHttpResult, OINOHtmlTemplate, OINONumberDataField, OINOContentType, OINOStr } from "./index.js";
6
+ import { OINODbDataModel, OINOStringDataField, OINO_ERROR_PREFIX, OINODbModelSet, OINOBenchmark, OINODbFactory, OINOLog, OINODbConfig, OINOHtmlTemplate, OINONumberDataField } from "./index.js";
7
7
  import { OINOResult } from "@oino-ts/types";
8
8
  import { OINOHashid } from "@oino-ts/hashid";
9
9
  const API_EMPTY_PARAMS = { sqlParams: {} };
@@ -58,23 +58,29 @@ export class OINODbHtmlTemplate extends OINOHtmlTemplate {
58
58
  * Creates HTML Response from API modelset.
59
59
  *
60
60
  * @param modelset OINO API dataset
61
+ * @param overrideValues values to override in the data
61
62
  *
62
63
  */
63
- renderFromDbData(modelset) {
64
+ renderFromDbData(modelset, overrideValues) {
65
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromDbData");
64
66
  let html = "";
65
67
  const dataset = modelset.dataset;
66
68
  const datamodel = modelset.datamodel;
67
69
  const api = modelset.datamodel.api;
68
70
  const modified_index = datamodel.findFieldIndexByName(api.params.cacheModifiedField || "");
69
71
  let last_modified = this.modified;
72
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
70
73
  while (!dataset.isEof()) {
71
74
  const row = dataset.getRow();
72
75
  if (modified_index >= 0) {
73
76
  last_modified = Math.max(last_modified, new Date(row[modified_index]).getTime());
77
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
74
78
  }
75
79
  let row_id_seed = datamodel.getRowPrimarykeyValues(row).join(' ');
76
80
  let primary_key_values = [];
77
- let html_row = this.template.replaceAll('###' + OINODbConfig.OINODB_ID_FIELD + '###', '###createHtmlFromData_temporary_oinoid###');
81
+ this.clearVariables();
82
+ this.setVariableFromValue(OINODbConfig.OINODB_ID_FIELD, "");
83
+ // let html_row:string = this.template.replaceAll('###' + OINODbConfig.OINODB_ID_FIELD + '###', '###createHtmlFromData_temporary_oinoid###')
78
84
  for (let i = 0; i < datamodel.fields.length; i++) {
79
85
  const f = datamodel.fields[i];
80
86
  let value = f.serializeCell(row[i]);
@@ -85,14 +91,18 @@ export class OINODbHtmlTemplate extends OINOHtmlTemplate {
85
91
  primary_key_values.push(value || "");
86
92
  }
87
93
  // OINOLog.debug("renderFromDbData replace field value", {field:f.name, value:value })
88
- html_row = html_row.replaceAll('###' + f.name + '###', OINOStr.encode(value, OINOContentType.html));
94
+ this.setVariableFromValue(f.name, value || "");
89
95
  }
90
- html_row = html_row.replaceAll('###createHtmlFromData_temporary_oinoid###', OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.html));
91
- html += html_row + "\r\n";
96
+ this.setVariableFromProperties(overrideValues);
97
+ this.setVariableFromValue(OINODbConfig.OINODB_ID_FIELD, OINODbConfig.printOINOId(primary_key_values));
98
+ // html_row = html_row.replaceAll('###createHtmlFromData_temporary_oinoid###', OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.html))
99
+ html += this._renderHtml() + "\r\n";
92
100
  dataset.next();
93
101
  }
94
- const result = new OINOHttpResult(html);
95
- result.lastModified = last_modified;
102
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
103
+ this.modified = last_modified;
104
+ const result = this._createHttpResult(html, false);
105
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromDbData");
96
106
  return result;
97
107
  }
98
108
  }
@@ -126,7 +136,7 @@ export class OINODbApi {
126
136
  this.params = params;
127
137
  this.datamodel = new OINODbDataModel(this);
128
138
  if (this.params.hashidKey) {
129
- this.hashid = new OINOHashid(this.params.hashidKey, this.db.name, this.params.hashidLength, this.params.hashidRandomIds);
139
+ this.hashid = new OINOHashid(this.params.hashidKey, this.db.name, this.params.hashidLength, this.params.hashidStaticIds);
130
140
  }
131
141
  else {
132
142
  this.hashid = null;
@@ -166,7 +176,6 @@ export class OINODbApi {
166
176
  //logDebug("OINODbApi.validateHttpValues", {result:result})
167
177
  }
168
178
  async _doGet(result, id, params) {
169
- OINOBenchmark.start("doGet");
170
179
  const sql = this.datamodel.printSqlSelect(id, params.sqlParams || {});
171
180
  // OINOLog.debug("OINODbApi.doGet sql", {sql:sql})
172
181
  try {
@@ -184,10 +193,8 @@ export class OINODbApi {
184
193
  result.setError(500, "Unhandled exception in doGet: " + e.message, "DoGet");
185
194
  result.addDebug("OINO GET SQL [" + sql + "]", "DoGet");
186
195
  }
187
- OINOBenchmark.end("doGet");
188
196
  }
189
197
  async _doPost(result, rows) {
190
- OINOBenchmark.start("doPost");
191
198
  let sql = "";
192
199
  try {
193
200
  let i = 0;
@@ -218,10 +225,8 @@ export class OINODbApi {
218
225
  result.setError(500, "Unhandled exception in doPost: " + e.message, "DoPost");
219
226
  result.addDebug("OINO POST SQL [" + sql + "]", "DoPost");
220
227
  }
221
- OINOBenchmark.end("doPost");
222
228
  }
223
229
  async _doPut(result, id, row) {
224
- OINOBenchmark.start("doPut");
225
230
  let sql = "";
226
231
  try {
227
232
  this._validateRowValues(result, row, false);
@@ -241,10 +246,8 @@ export class OINODbApi {
241
246
  result.setError(500, "Unhandled exception: " + e.message, "DoPut");
242
247
  result.addDebug("OINO POST SQL [" + sql + "]", "DoPut");
243
248
  }
244
- OINOBenchmark.end("doPut");
245
249
  }
246
250
  async _doDelete(result, id) {
247
- OINOBenchmark.start("doDelete");
248
251
  let sql = "";
249
252
  try {
250
253
  sql = this.datamodel.printSqlDelete(id);
@@ -261,7 +264,6 @@ export class OINODbApi {
261
264
  result.setError(500, "Unhandled exception: " + e.message, "DoDelete");
262
265
  result.addDebug("OINO DELETE SQL [" + sql + "]", "DoDelete");
263
266
  }
264
- OINOBenchmark.end("doDelete");
265
267
  }
266
268
  /**
267
269
  * Method for handlind a HTTP REST request with GET, POST, PUT, DELETE corresponding to
@@ -274,7 +276,7 @@ export class OINODbApi {
274
276
  *
275
277
  */
276
278
  async doRequest(method, id, body, params = API_EMPTY_PARAMS) {
277
- OINOBenchmark.start("doRequest");
279
+ OINOBenchmark.start("OINODbApi", "doRequest");
278
280
  // OINOLog.debug("OINODbApi.doRequest enter", {method:method, id:id, body:body, params:params})
279
281
  let result = new OINODbApiResult(params);
280
282
  let rows = [];
@@ -342,7 +344,7 @@ export class OINODbApi {
342
344
  else {
343
345
  result.setError(405, "Unsupported HTTP method '" + method + "'", "DoRequest");
344
346
  }
345
- OINOBenchmark.end("doRequest");
347
+ OINOBenchmark.end("OINODbApi", "doRequest", method);
346
348
  return result;
347
349
  }
348
350
  }
@@ -232,7 +232,7 @@ export class OINONumberDataField extends OINODbDataField {
232
232
  if (value === undefined) {
233
233
  return undefined;
234
234
  }
235
- else if ((value == "") || (value == null)) {
235
+ else if ((value === "") || (value === null)) {
236
236
  return null;
237
237
  }
238
238
  else {
@@ -222,10 +222,10 @@ export class OINODbDataModel {
222
222
  const order_sql = params.order?.toSql(this) || "";
223
223
  const limit_sql = params.limit?.toSql(this) || "";
224
224
  // OINOLog.debug("OINODbDataModel.printSqlSelect", {select_sql:result, filter_sql:filter_sql, order_sql:order_sql})
225
- if ((id != "") && (filter_sql != "")) {
225
+ if ((id != null) && (id != "") && (filter_sql != "")) {
226
226
  result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id) + " AND " + filter_sql;
227
227
  }
228
- else if (id != "") {
228
+ else if ((id != null) && (id != "")) {
229
229
  result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id);
230
230
  }
231
231
  else if (filter_sql != "") {
@@ -3,8 +3,7 @@
3
3
  * License, v. 2.0. If a copy of the MPL was not distributed with this
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
- import { OINOHttpResult } from "../../types/src/OINOResult.js";
7
- import { OINODbApi, OINOContentType, OINODbSqlFilter, OINOStr, OINODbConfig, OINONumberDataField, OINODbSqlOrder, OINODbSqlLimit, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOLog } from "./index.js";
6
+ import { OINOHttpResult, OINODbApi, OINOContentType, OINODbSqlFilter, OINOStr, OINODbConfig, OINONumberDataField, OINODbSqlOrder, OINODbSqlLimit, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOLog } from "./index.js";
8
7
  /**
9
8
  * Static factory class for easily creating things based on data
10
9
  *
@@ -247,15 +247,19 @@ export class OINODbModelSet {
247
247
  * field not found or value does not exist.
248
248
  *
249
249
  * @param fieldName name of the field
250
+ * @param serialize serialize the value
250
251
  *
251
252
  */
252
- getValueByFieldName(fieldName) {
253
+ getValueByFieldName(fieldName, serialize = false) {
253
254
  let result = undefined;
254
255
  if (!this.dataset.isEof()) {
255
256
  const current_row = this.dataset.getRow();
256
257
  const field_index = this.datamodel.findFieldIndexByName(fieldName);
257
258
  if (field_index >= 0) {
258
259
  result = current_row[field_index];
260
+ if (serialize) {
261
+ result = this.datamodel.fields[field_index].serializeCell(result);
262
+ }
259
263
  }
260
264
  }
261
265
  return result;
@@ -25,7 +25,7 @@ export declare class OINODbApiResult extends OINOResult {
25
25
  * @param headers Headers to include in the response
26
26
  *
27
27
  */
28
- getResponse(headers?: HeadersInit): Response;
28
+ getResponse(headers?: Record<string, string>): Response;
29
29
  }
30
30
  /**
31
31
  * Specialized HTML template that can render ´OINODbApiResult´.
@@ -36,9 +36,10 @@ export declare class OINODbHtmlTemplate extends OINOHtmlTemplate {
36
36
  * Creates HTML Response from API modelset.
37
37
  *
38
38
  * @param modelset OINO API dataset
39
+ * @param overrideValues values to override in the data
39
40
  *
40
41
  */
41
- renderFromDbData(modelset: OINODbModelSet): OINOHttpResult;
42
+ renderFromDbData(modelset: OINODbModelSet, overrideValues?: any): OINOHttpResult;
42
43
  }
43
44
  /**
44
45
  * API class with method to process HTTP REST requests.
@@ -1,5 +1,4 @@
1
- import { OINOHttpResult } from "../../types/src/OINOResult.js";
2
- import { OINODbApi, OINODbApiParams, OINODbParams, OINODbDataModel, OINODb, OINODataRow, OINODbConstructor, OINODbApiRequestParams, OINODbModelSet, OINOResult } from "./index.js";
1
+ import { OINOHttpResult, OINODbApi, OINODbApiParams, OINODbParams, OINODbDataModel, OINODb, OINODataRow, OINODbConstructor, OINODbApiRequestParams, OINODbModelSet, OINOResult } from "./index.js";
3
2
  /**
4
3
  * Static factory class for easily creating things based on data
5
4
  *
@@ -44,7 +44,8 @@ export declare class OINODbModelSet {
44
44
  * field not found or value does not exist.
45
45
  *
46
46
  * @param fieldName name of the field
47
+ * @param serialize serialize the value
47
48
  *
48
49
  */
49
- getValueByFieldName(fieldName: string): OINODataCell;
50
+ getValueByFieldName(fieldName: string, serialize?: boolean): OINODataCell;
50
51
  }
@@ -35,7 +35,7 @@ export type OINODbApiParams = {
35
35
  /** Set (minimum) length (12-32 chars) of the hashids */
36
36
  hashidLength?: number;
37
37
  /** Make hashids static per row/table */
38
- hashidRandomIds?: boolean;
38
+ hashidStaticIds?: boolean;
39
39
  /** Name of field that has the modified field */
40
40
  cacheModifiedField?: string;
41
41
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@oino-ts/db",
3
- "version": "0.0.13",
3
+ "version": "0.0.15",
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",
@@ -20,7 +20,7 @@
20
20
  "types": "./dist/types/index.d.ts",
21
21
  "dependencies": {
22
22
  "@types/node": "^20.14.10",
23
- "@oino-ts/types": "0.0.13"
23
+ "@oino-ts/types": "0.0.15"
24
24
  },
25
25
  "devDependencies": {
26
26
  "@types/bun": "latest",
@@ -192,7 +192,7 @@ const apis:OINOTestApiParams[] = [
192
192
  putRow: [99, "Umeshu", 1, 1, undefined, 24.99, 3, 0, 20, 0]
193
193
  },
194
194
  {
195
- apiParams: { tableName: "Employees", hashidKey: "12345678901234567890123456789012" },
195
+ apiParams: { tableName: "Employees", hashidKey: "12345678901234567890123456789012", hashidStaticIds:true },
196
196
  requestParams: {
197
197
  sqlParams: { filter: OINODbSqlFilter.parse("(TitleOfCourtesy)-eq(Ms.)"), order: OINODbSqlOrder.parse("LastName asc") }
198
198
  },
package/src/OINODbApi.ts CHANGED
@@ -41,7 +41,7 @@ export class OINODbApiResult extends OINOResult {
41
41
  * @param headers Headers to include in the response
42
42
  *
43
43
  */
44
- getResponse(headers:HeadersInit = {}):Response {
44
+ getResponse(headers:Record<string, string> = {}):Response {
45
45
  let response:Response|null = null
46
46
  if (this.success && this.data) {
47
47
  response = new Response(this.data.writeString(this.params.responseType), {status:this.statusCode, statusText: this.statusMessage, headers: headers })
@@ -65,24 +65,30 @@ export class OINODbHtmlTemplate extends OINOHtmlTemplate {
65
65
  * Creates HTML Response from API modelset.
66
66
  *
67
67
  * @param modelset OINO API dataset
68
+ * @param overrideValues values to override in the data
68
69
  *
69
70
  */
70
- renderFromDbData(modelset:OINODbModelSet):OINOHttpResult {
71
+ renderFromDbData(modelset:OINODbModelSet, overrideValues?:any):OINOHttpResult {
72
+ OINOBenchmark.start("OINOHtmlTemplate", "renderFromDbData")
71
73
  let html:string = ""
72
74
  const dataset:OINODbDataSet|undefined = modelset.dataset
73
75
  const datamodel:OINODbDataModel = modelset.datamodel
74
76
  const api:OINODbApi = modelset.datamodel.api
75
77
  const modified_index = datamodel.findFieldIndexByName(api.params.cacheModifiedField || "")
76
78
  let last_modified:number = this.modified
79
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
77
80
 
78
81
  while (!dataset.isEof()) {
79
82
  const row:OINODataRow = dataset.getRow()
80
83
  if (modified_index >= 0) {
81
84
  last_modified = Math.max(last_modified, new Date(row[modified_index] as Date).getTime())
85
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
82
86
  }
83
87
  let row_id_seed:string = datamodel.getRowPrimarykeyValues(row).join(' ')
84
88
  let primary_key_values:string[] = []
85
- let html_row:string = this.template.replaceAll('###' + OINODbConfig.OINODB_ID_FIELD + '###', '###createHtmlFromData_temporary_oinoid###')
89
+ this.clearVariables()
90
+ this.setVariableFromValue(OINODbConfig.OINODB_ID_FIELD, "")
91
+ // let html_row:string = this.template.replaceAll('###' + OINODbConfig.OINODB_ID_FIELD + '###', '###createHtmlFromData_temporary_oinoid###')
86
92
  for (let i=0; i<datamodel.fields.length; i++) {
87
93
  const f:OINODbDataField = datamodel.fields[i]
88
94
  let value:string|null|undefined = f.serializeCell(row[i])
@@ -93,14 +99,18 @@ export class OINODbHtmlTemplate extends OINOHtmlTemplate {
93
99
  primary_key_values.push(value || "")
94
100
  }
95
101
  // OINOLog.debug("renderFromDbData replace field value", {field:f.name, value:value })
96
- html_row = html_row.replaceAll('###' + f.name + '###', OINOStr.encode(value, OINOContentType.html))
102
+ this.setVariableFromValue(f.name, value || "")
97
103
  }
98
- html_row = html_row.replaceAll('###createHtmlFromData_temporary_oinoid###', OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.html))
99
- html += html_row + "\r\n"
104
+ this.setVariableFromProperties(overrideValues)
105
+ this.setVariableFromValue(OINODbConfig.OINODB_ID_FIELD, OINODbConfig.printOINOId(primary_key_values))
106
+ // html_row = html_row.replaceAll('###createHtmlFromData_temporary_oinoid###', OINOStr.encode(OINODbConfig.printOINOId(primary_key_values), OINOContentType.html))
107
+ html += this._renderHtml() + "\r\n"
100
108
  dataset.next()
101
109
  }
102
- const result:OINOHttpResult = new OINOHttpResult(html)
103
- result.lastModified = last_modified
110
+ // OINOLog.debug("OINOHtmlTemplate.renderFromDbData", {last_modified:last_modified})
111
+ this.modified = last_modified
112
+ const result:OINOHttpResult = this._createHttpResult(html, false)
113
+ OINOBenchmark.end("OINOHtmlTemplate", "renderFromDbData")
104
114
  return result
105
115
  }
106
116
 
@@ -141,7 +151,7 @@ export class OINODbApi {
141
151
  this.params = params
142
152
  this.datamodel = new OINODbDataModel(this)
143
153
  if (this.params.hashidKey) {
144
- this.hashid = new OINOHashid(this.params.hashidKey, this.db.name, this.params.hashidLength, this.params.hashidRandomIds)
154
+ this.hashid = new OINOHashid(this.params.hashidKey, this.db.name, this.params.hashidLength, this.params.hashidStaticIds)
145
155
  } else {
146
156
  this.hashid = null
147
157
  }
@@ -182,7 +192,6 @@ export class OINODbApi {
182
192
  }
183
193
 
184
194
  private async _doGet(result:OINODbApiResult, id:string, params:OINODbApiRequestParams):Promise<void> {
185
- OINOBenchmark.start("doGet")
186
195
  const sql:string = this.datamodel.printSqlSelect(id, params.sqlParams || {})
187
196
  // OINOLog.debug("OINODbApi.doGet sql", {sql:sql})
188
197
  try {
@@ -198,11 +207,9 @@ export class OINODbApi {
198
207
  result.setError(500, "Unhandled exception in doGet: " + e.message, "DoGet")
199
208
  result.addDebug("OINO GET SQL [" + sql + "]", "DoGet")
200
209
  }
201
- OINOBenchmark.end("doGet")
202
210
  }
203
211
 
204
212
  private async _doPost(result:OINODbApiResult, rows:OINODataRow[]):Promise<void> {
205
- OINOBenchmark.start("doPost")
206
213
  let sql:string = ""
207
214
  try {
208
215
  let i:number = 0
@@ -232,11 +239,9 @@ export class OINODbApi {
232
239
  result.setError(500, "Unhandled exception in doPost: " + e.message, "DoPost")
233
240
  result.addDebug("OINO POST SQL [" + sql + "]", "DoPost")
234
241
  }
235
- OINOBenchmark.end("doPost")
236
242
  }
237
243
 
238
244
  private async _doPut(result:OINODbApiResult, id:string, row:OINODataRow):Promise<void> {
239
- OINOBenchmark.start("doPut")
240
245
  let sql:string = ""
241
246
  try {
242
247
  this._validateRowValues(result, row, false)
@@ -255,11 +260,9 @@ export class OINODbApi {
255
260
  result.setError(500, "Unhandled exception: " + e.message, "DoPut")
256
261
  result.addDebug("OINO POST SQL [" + sql + "]", "DoPut")
257
262
  }
258
- OINOBenchmark.end("doPut")
259
263
  }
260
264
 
261
265
  private async _doDelete(result:OINODbApiResult, id:string):Promise<void> {
262
- OINOBenchmark.start("doDelete")
263
266
  let sql:string = ""
264
267
  try {
265
268
  sql = this.datamodel.printSqlDelete(id)
@@ -275,7 +278,6 @@ export class OINODbApi {
275
278
  result.setError(500, "Unhandled exception: " + e.message, "DoDelete")
276
279
  result.addDebug("OINO DELETE SQL [" + sql + "]", "DoDelete")
277
280
  }
278
- OINOBenchmark.end("doDelete")
279
281
  }
280
282
 
281
283
  /**
@@ -289,7 +291,7 @@ export class OINODbApi {
289
291
  *
290
292
  */
291
293
  async doRequest(method:string, id: string, body:string|OINODataRow[]|any, params:OINODbApiRequestParams = API_EMPTY_PARAMS):Promise<OINODbApiResult> {
292
- OINOBenchmark.start("doRequest")
294
+ OINOBenchmark.start("OINODbApi", "doRequest")
293
295
  // OINOLog.debug("OINODbApi.doRequest enter", {method:method, id:id, body:body, params:params})
294
296
  let result:OINODbApiResult = new OINODbApiResult(params)
295
297
  let rows:OINODataRow[] = []
@@ -354,7 +356,7 @@ export class OINODbApi {
354
356
  } else {
355
357
  result.setError(405, "Unsupported HTTP method '" + method + "'", "DoRequest")
356
358
  }
357
- OINOBenchmark.end("doRequest")
359
+ OINOBenchmark.end("OINODbApi", "doRequest", method)
358
360
  return result
359
361
  }
360
362
  }
@@ -250,7 +250,7 @@ export class OINONumberDataField extends OINODbDataField {
250
250
  deserializeCell(value: string|null|undefined): OINODataCell {
251
251
  if (value === undefined) {
252
252
  return undefined
253
- } else if ((value == "") || (value == null)) {
253
+ } else if ((value === "") || (value === null)) {
254
254
  return null
255
255
  } else {
256
256
  return Number.parseFloat(value)
@@ -236,9 +236,9 @@ export class OINODbDataModel {
236
236
  const order_sql = params.order?.toSql(this) || ""
237
237
  const limit_sql = params.limit?.toSql(this) || ""
238
238
  // OINOLog.debug("OINODbDataModel.printSqlSelect", {select_sql:result, filter_sql:filter_sql, order_sql:order_sql})
239
- if ((id != "") && (filter_sql != "")) {
239
+ if ((id != null) && (id != "") && (filter_sql != "")) {
240
240
  result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id) + " AND " + filter_sql;
241
- } else if (id != "") {
241
+ } else if ((id != null) && (id != "")) {
242
242
  result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id);
243
243
  } else if (filter_sql != "") {
244
244
  result += "\nWHERE " + filter_sql;
@@ -4,8 +4,7 @@
4
4
  * file, You can obtain one at https://mozilla.org/MPL/2.0/.
5
5
  */
6
6
 
7
- import { OINOHttpResult } from "../../types/src/OINOResult.js"
8
- import { OINODbApi, OINODbApiParams, OINODbParams, OINOContentType, OINODbDataModel, OINODbDataField, OINODb, OINODataRow, OINODbConstructor, OINODbApiRequestParams, OINODbSqlFilter, OINOStr, OINOBlobDataField, OINODbApiResult, OINODbDataSet, OINODbModelSet, OINODbConfig, OINONumberDataField, OINODataCell, OINODbSqlOrder, OINODbSqlLimit, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOLog, OINODbSqlParams, OINOResult } from "./index.js"
7
+ import { OINOHttpResult, OINODbApi, OINODbApiParams, OINODbParams, OINOContentType, OINODbDataModel, OINODbDataField, OINODb, OINODataRow, OINODbConstructor, OINODbApiRequestParams, OINODbSqlFilter, OINOStr, OINOBlobDataField, OINODbApiResult, OINODbDataSet, OINODbModelSet, OINODbConfig, OINONumberDataField, OINODataCell, OINODbSqlOrder, OINODbSqlLimit, OINO_ERROR_PREFIX, OINO_WARNING_PREFIX, OINO_INFO_PREFIX, OINO_DEBUG_PREFIX, OINOLog, OINODbSqlParams, OINOResult } from "./index.js"
9
8
 
10
9
  /**
11
10
  * Static factory class for easily creating things based on data
@@ -269,15 +269,19 @@ export class OINODbModelSet {
269
269
  * field not found or value does not exist.
270
270
  *
271
271
  * @param fieldName name of the field
272
+ * @param serialize serialize the value
272
273
  *
273
274
  */
274
- getValueByFieldName(fieldName:string):OINODataCell {
275
+ getValueByFieldName(fieldName:string, serialize:boolean = false):OINODataCell {
275
276
  let result:OINODataCell = undefined
276
277
  if (!this.dataset.isEof()) {
277
278
  const current_row:OINODataRow = this.dataset.getRow()
278
279
  const field_index:number = this.datamodel.findFieldIndexByName(fieldName)
279
280
  if (field_index >= 0) {
280
281
  result = current_row[field_index]
282
+ if (serialize) {
283
+ result = this.datamodel.fields[field_index].serializeCell(result)
284
+ }
281
285
  }
282
286
  }
283
287
  return result
package/src/index.ts CHANGED
@@ -39,7 +39,7 @@ export type OINODbApiParams = {
39
39
  /** Set (minimum) length (12-32 chars) of the hashids */
40
40
  hashidLength?:number,
41
41
  /** Make hashids static per row/table */
42
- hashidRandomIds?: boolean,
42
+ hashidStaticIds?: boolean,
43
43
  /** Name of field that has the modified field */
44
44
  cacheModifiedField?:string
45
45
  }