@oino-ts/db 0.0.11
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.
- package/README.md +222 -0
- package/dist/cjs/OINODb.js +27 -0
- package/dist/cjs/OINODbApi.js +270 -0
- package/dist/cjs/OINODbConfig.js +86 -0
- package/dist/cjs/OINODbDataField.js +354 -0
- package/dist/cjs/OINODbDataModel.js +279 -0
- package/dist/cjs/OINODbDataSet.js +139 -0
- package/dist/cjs/OINODbFactory.js +563 -0
- package/dist/cjs/OINODbModelSet.js +267 -0
- package/dist/cjs/OINODbParams.js +280 -0
- package/dist/cjs/OINODbRequestParams.js +280 -0
- package/dist/cjs/OINODbSwagger.js +201 -0
- package/dist/cjs/index.js +51 -0
- package/dist/esm/OINODb.js +23 -0
- package/dist/esm/OINODbApi.js +265 -0
- package/dist/esm/OINODbConfig.js +82 -0
- package/dist/esm/OINODbDataField.js +345 -0
- package/dist/esm/OINODbDataModel.js +275 -0
- package/dist/esm/OINODbDataSet.js +134 -0
- package/dist/esm/OINODbFactory.js +559 -0
- package/dist/esm/OINODbModelSet.js +263 -0
- package/dist/esm/OINODbRequestParams.js +274 -0
- package/dist/esm/OINODbSwagger.js +197 -0
- package/dist/esm/index.js +17 -0
- package/dist/types/OINODb.d.ts +75 -0
- package/dist/types/OINODbApi.d.ts +57 -0
- package/dist/types/OINODbConfig.d.ts +52 -0
- package/dist/types/OINODbDataField.d.ts +202 -0
- package/dist/types/OINODbDataModel.d.ts +108 -0
- package/dist/types/OINODbDataSet.d.ts +95 -0
- package/dist/types/OINODbFactory.d.ts +99 -0
- package/dist/types/OINODbModelSet.d.ts +50 -0
- package/dist/types/OINODbRequestParams.d.ts +130 -0
- package/dist/types/OINODbSwagger.d.ts +25 -0
- package/dist/types/index.d.ts +103 -0
- package/package.json +35 -0
- package/src/OINODb.ts +98 -0
- package/src/OINODbApi.test.ts +243 -0
- package/src/OINODbApi.ts +270 -0
- package/src/OINODbConfig.ts +92 -0
- package/src/OINODbDataField.ts +372 -0
- package/src/OINODbDataModel.ts +290 -0
- package/src/OINODbDataSet.ts +170 -0
- package/src/OINODbFactory.ts +570 -0
- package/src/OINODbModelSet.ts +286 -0
- package/src/OINODbRequestParams.ts +281 -0
- package/src/OINODbSwagger.ts +209 -0
- package/src/index.ts +116 -0
|
@@ -0,0 +1,354 @@
|
|
|
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.OINODatetimeDataField = exports.OINOBlobDataField = exports.OINONumberDataField = exports.OINOBooleanDataField = exports.OINOStringDataField = exports.OINODbDataField = void 0;
|
|
9
|
+
/**
|
|
10
|
+
* Base class for a column of data responsible for appropriatelly serializing/deserializing the data.
|
|
11
|
+
*
|
|
12
|
+
*/
|
|
13
|
+
class OINODbDataField {
|
|
14
|
+
/** OINODB reference*/
|
|
15
|
+
db;
|
|
16
|
+
/** Name of the field */
|
|
17
|
+
name;
|
|
18
|
+
/** Internal type of field*/
|
|
19
|
+
type;
|
|
20
|
+
/** SQL type of the field */
|
|
21
|
+
sqlType;
|
|
22
|
+
/** Maximum length of the field (or 0) */
|
|
23
|
+
maxLength;
|
|
24
|
+
/** Parameters for the field */
|
|
25
|
+
fieldParams;
|
|
26
|
+
/**
|
|
27
|
+
* Constructor for a data field
|
|
28
|
+
*
|
|
29
|
+
* @param db OINODb reference
|
|
30
|
+
* @param name name of the field
|
|
31
|
+
* @param type internal type of the field
|
|
32
|
+
* @param sqlType column type in database
|
|
33
|
+
* @param fieldParams parameters of the field
|
|
34
|
+
* @param maxLength maximum length of the field (or 0)
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
constructor(db, name, type, sqlType, fieldParams, maxLength = 0) {
|
|
38
|
+
this.db = db;
|
|
39
|
+
this.name = name;
|
|
40
|
+
this.type = type;
|
|
41
|
+
this.maxLength = maxLength;
|
|
42
|
+
this.sqlType = sqlType;
|
|
43
|
+
this.fieldParams = fieldParams;
|
|
44
|
+
// OINOLog.debug("OINODbDataField.constructor", {this:this})
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Pring debug information for the field
|
|
48
|
+
*
|
|
49
|
+
* @param length length of the debug output (or 0 for as long as needed)
|
|
50
|
+
*
|
|
51
|
+
*/
|
|
52
|
+
printColumnDebug(length = 0) {
|
|
53
|
+
let params = "";
|
|
54
|
+
if (this.fieldParams.isPrimaryKey) {
|
|
55
|
+
params += "PK ";
|
|
56
|
+
}
|
|
57
|
+
if (this.fieldParams.isAutoInc) {
|
|
58
|
+
params += "AUTOINC ";
|
|
59
|
+
}
|
|
60
|
+
if (this.fieldParams.isNotNull) {
|
|
61
|
+
params += "NOTNUL ";
|
|
62
|
+
}
|
|
63
|
+
if (params != "") {
|
|
64
|
+
params = "{" + params.trim() + "}";
|
|
65
|
+
}
|
|
66
|
+
if (this.maxLength > 0) {
|
|
67
|
+
params = this.sqlType + "(" + this.maxLength + ")" + params;
|
|
68
|
+
}
|
|
69
|
+
else {
|
|
70
|
+
params = this.sqlType + params;
|
|
71
|
+
}
|
|
72
|
+
const name_length = length - 2 - 1 - params.length;
|
|
73
|
+
let result = this.name;
|
|
74
|
+
if (length > 0) {
|
|
75
|
+
if (result.length > name_length) {
|
|
76
|
+
result = result.substring(0, name_length - 2) + "..";
|
|
77
|
+
}
|
|
78
|
+
result = (result + ":" + params).padEnd(length - 2, " ");
|
|
79
|
+
}
|
|
80
|
+
else {
|
|
81
|
+
result = this.type + ":" + result + ":" + params;
|
|
82
|
+
}
|
|
83
|
+
return "[" + result + "]";
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Serialize cell value in the given content format.
|
|
87
|
+
*
|
|
88
|
+
* @param cellVal cell value
|
|
89
|
+
*
|
|
90
|
+
*/
|
|
91
|
+
serializeCell(cellVal) {
|
|
92
|
+
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType);
|
|
93
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
94
|
+
return cellVal; // let content type encoder worry what to do with the value (so not force it to string)
|
|
95
|
+
}
|
|
96
|
+
else {
|
|
97
|
+
return cellVal.toString();
|
|
98
|
+
}
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
102
|
+
*
|
|
103
|
+
* @param value string value
|
|
104
|
+
*
|
|
105
|
+
*/
|
|
106
|
+
deserializeCell(value) {
|
|
107
|
+
return value;
|
|
108
|
+
}
|
|
109
|
+
/**
|
|
110
|
+
* Print data cell (from deserialization) as SQL-string.
|
|
111
|
+
*
|
|
112
|
+
* @param cellVal cell value
|
|
113
|
+
*
|
|
114
|
+
*/
|
|
115
|
+
printCellAsSqlValue(cellVal) {
|
|
116
|
+
return this.db.printCellAsSqlValue(cellVal, this.sqlType);
|
|
117
|
+
}
|
|
118
|
+
/**
|
|
119
|
+
* Print name of column as SQL.
|
|
120
|
+
*
|
|
121
|
+
*/
|
|
122
|
+
printSqlColumnName() {
|
|
123
|
+
return this.db.printSqlColumnname(this.name);
|
|
124
|
+
}
|
|
125
|
+
}
|
|
126
|
+
exports.OINODbDataField = OINODbDataField;
|
|
127
|
+
/**
|
|
128
|
+
* Specialised class for a string column.
|
|
129
|
+
*
|
|
130
|
+
*/
|
|
131
|
+
class OINOStringDataField extends OINODbDataField {
|
|
132
|
+
/**
|
|
133
|
+
* Constructor for a string data field
|
|
134
|
+
*
|
|
135
|
+
* @param db OINODb reference
|
|
136
|
+
* @param name name of the field
|
|
137
|
+
* @param sqlType column type in database
|
|
138
|
+
* @param fieldParams parameters of the field
|
|
139
|
+
* @param maxLength maximum length of the field (or 0)
|
|
140
|
+
*
|
|
141
|
+
*/
|
|
142
|
+
constructor(db, name, sqlType, fieldParams, maxLength) {
|
|
143
|
+
super(db, name, "string", sqlType, fieldParams, maxLength);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
exports.OINOStringDataField = OINOStringDataField;
|
|
147
|
+
/**
|
|
148
|
+
* Specialised class for a boolean column.
|
|
149
|
+
*
|
|
150
|
+
*/
|
|
151
|
+
class OINOBooleanDataField extends OINODbDataField {
|
|
152
|
+
/**
|
|
153
|
+
* Constructor for a boolean data field
|
|
154
|
+
*
|
|
155
|
+
* @param db OINODb reference
|
|
156
|
+
* @param name name of the field
|
|
157
|
+
* @param sqlType column type in database
|
|
158
|
+
* @param fieldParams parameters of the field
|
|
159
|
+
*
|
|
160
|
+
*/
|
|
161
|
+
constructor(db, name, sqlType, fieldParams) {
|
|
162
|
+
super(db, name, "boolean", sqlType, fieldParams);
|
|
163
|
+
}
|
|
164
|
+
/**
|
|
165
|
+
* Serialize cell value in the given content format.
|
|
166
|
+
*
|
|
167
|
+
* @param cellVal cell value
|
|
168
|
+
*
|
|
169
|
+
*/
|
|
170
|
+
serializeCell(cellVal) {
|
|
171
|
+
const parsed_value = (this.db.parseSqlValueAsCell(cellVal, this.sqlType) || "").toString();
|
|
172
|
+
let result;
|
|
173
|
+
// console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
|
|
174
|
+
if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
|
|
175
|
+
result = "false";
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
result = "true";
|
|
179
|
+
}
|
|
180
|
+
return result;
|
|
181
|
+
}
|
|
182
|
+
/**
|
|
183
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
184
|
+
*
|
|
185
|
+
* @param value string value
|
|
186
|
+
*
|
|
187
|
+
*/
|
|
188
|
+
deserializeCell(value) {
|
|
189
|
+
if (value == null || value == "" || value.toString().toLowerCase() == "false" || value == "0") { // TODO: testaa poistaa .toString()
|
|
190
|
+
return false;
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
return true;
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
}
|
|
197
|
+
exports.OINOBooleanDataField = OINOBooleanDataField;
|
|
198
|
+
/**
|
|
199
|
+
* Specialised class for a number column.
|
|
200
|
+
*
|
|
201
|
+
*/
|
|
202
|
+
class OINONumberDataField extends OINODbDataField {
|
|
203
|
+
/**
|
|
204
|
+
* Constructor for a string data field
|
|
205
|
+
*
|
|
206
|
+
* @param db OINODb reference
|
|
207
|
+
* @param name name of the field
|
|
208
|
+
* @param sqlType column type in database
|
|
209
|
+
* @param fieldParams parameters of the field
|
|
210
|
+
*
|
|
211
|
+
*/
|
|
212
|
+
constructor(db, name, sqlType, fieldParams) {
|
|
213
|
+
super(db, name, "number", sqlType, fieldParams);
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Serialize cell value in the given content format.
|
|
217
|
+
*
|
|
218
|
+
* @param cellVal cell value
|
|
219
|
+
*
|
|
220
|
+
*/
|
|
221
|
+
serializeCell(cellVal) {
|
|
222
|
+
let result;
|
|
223
|
+
if ((cellVal === null) || (cellVal === undefined) || (cellVal === "")) {
|
|
224
|
+
result = null;
|
|
225
|
+
}
|
|
226
|
+
else {
|
|
227
|
+
result = cellVal.toString();
|
|
228
|
+
}
|
|
229
|
+
return result;
|
|
230
|
+
}
|
|
231
|
+
/**
|
|
232
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
233
|
+
*
|
|
234
|
+
* @param value string value
|
|
235
|
+
*
|
|
236
|
+
*/
|
|
237
|
+
deserializeCell(value) {
|
|
238
|
+
if (!value) {
|
|
239
|
+
return 0;
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
return Number.parseFloat(value);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
}
|
|
246
|
+
exports.OINONumberDataField = OINONumberDataField;
|
|
247
|
+
/**
|
|
248
|
+
* Specialised class for a blob column.
|
|
249
|
+
*
|
|
250
|
+
*/
|
|
251
|
+
class OINOBlobDataField extends OINODbDataField {
|
|
252
|
+
/**
|
|
253
|
+
* Constructor for a blob data field
|
|
254
|
+
*
|
|
255
|
+
* @param db OINODb reference
|
|
256
|
+
* @param name name of the field
|
|
257
|
+
* @param sqlType column type in database
|
|
258
|
+
* @param fieldParams parameters of the field
|
|
259
|
+
* @param maxLength maximum length of the field (or 0)
|
|
260
|
+
*
|
|
261
|
+
*/
|
|
262
|
+
constructor(db, name, sqlType, fieldParams, maxLength) {
|
|
263
|
+
super(db, name, "blob", sqlType, fieldParams, maxLength);
|
|
264
|
+
}
|
|
265
|
+
/**
|
|
266
|
+
* Serialize cell value in the given content format.
|
|
267
|
+
*
|
|
268
|
+
* @param cellVal cell value
|
|
269
|
+
*
|
|
270
|
+
*/
|
|
271
|
+
serializeCell(cellVal) {
|
|
272
|
+
// OINOLog.debug("OINOBlobDataField.serializeCell", {cellVal:cellVal})
|
|
273
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
274
|
+
return cellVal;
|
|
275
|
+
}
|
|
276
|
+
else if (cellVal instanceof Uint8Array) {
|
|
277
|
+
return Buffer.from(cellVal).toString('base64');
|
|
278
|
+
}
|
|
279
|
+
else {
|
|
280
|
+
return cellVal.toString();
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
/**
|
|
284
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
285
|
+
*
|
|
286
|
+
* @param value string value
|
|
287
|
+
*
|
|
288
|
+
*/
|
|
289
|
+
deserializeCell(value) {
|
|
290
|
+
if (value == null) {
|
|
291
|
+
return new Buffer(0);
|
|
292
|
+
}
|
|
293
|
+
else {
|
|
294
|
+
return Buffer.from(value, 'base64'); // Blob-field data is base64 encoded and converted internally to UInt8Array / Buffer
|
|
295
|
+
}
|
|
296
|
+
}
|
|
297
|
+
}
|
|
298
|
+
exports.OINOBlobDataField = OINOBlobDataField;
|
|
299
|
+
/**
|
|
300
|
+
* Specialised class for a datetime column.
|
|
301
|
+
*
|
|
302
|
+
*/
|
|
303
|
+
class OINODatetimeDataField extends OINODbDataField {
|
|
304
|
+
/**
|
|
305
|
+
* Constructor for a string data field
|
|
306
|
+
*
|
|
307
|
+
* @param db OINODb reference
|
|
308
|
+
* @param name name of the field
|
|
309
|
+
* @param sqlType column type in database
|
|
310
|
+
* @param fieldParams parameters of the field
|
|
311
|
+
*
|
|
312
|
+
*/
|
|
313
|
+
constructor(db, name, sqlType, fieldParams) {
|
|
314
|
+
super(db, name, "datetime", sqlType, fieldParams);
|
|
315
|
+
}
|
|
316
|
+
/**
|
|
317
|
+
* Serialize cell value in the given content format.
|
|
318
|
+
*
|
|
319
|
+
* @param cellVal cell value
|
|
320
|
+
*
|
|
321
|
+
*/
|
|
322
|
+
serializeCell(cellVal) {
|
|
323
|
+
// OINOLog.debug("OINODatetimeDataField.serializeCell", {cellVal:cellVal, type:typeof(cellVal)})
|
|
324
|
+
if (typeof (cellVal) == "string") {
|
|
325
|
+
cellVal = this.db.parseSqlValueAsCell(cellVal, this.sqlType);
|
|
326
|
+
// OINOLog.debug("OINODatetimeDataField.serializeCell parsed", {cellVal:cellVal, type:typeof(cellVal)})
|
|
327
|
+
}
|
|
328
|
+
if ((cellVal === null) || (cellVal === undefined)) {
|
|
329
|
+
return cellVal;
|
|
330
|
+
}
|
|
331
|
+
else if (cellVal instanceof Date) {
|
|
332
|
+
return cellVal.toISOString();
|
|
333
|
+
}
|
|
334
|
+
else {
|
|
335
|
+
return cellVal.toString();
|
|
336
|
+
}
|
|
337
|
+
}
|
|
338
|
+
/**
|
|
339
|
+
* Parce cell value from string using field type specific formatting rules.
|
|
340
|
+
*
|
|
341
|
+
* @param value string value
|
|
342
|
+
*
|
|
343
|
+
*/
|
|
344
|
+
deserializeCell(value) {
|
|
345
|
+
// OINOLog.debug("OINODatetimeDataField.deserializeCell", {strVal:strVal})
|
|
346
|
+
if ((value === null) || (value === undefined)) {
|
|
347
|
+
return value;
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
return new Date(value);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
}
|
|
354
|
+
exports.OINODatetimeDataField = OINODatetimeDataField;
|
|
@@ -0,0 +1,279 @@
|
|
|
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.OINODbDataModel = void 0;
|
|
9
|
+
const index_js_1 = require("./index.js");
|
|
10
|
+
/**
|
|
11
|
+
* OINO Datamodel object for representing one database table and it's columns.
|
|
12
|
+
*
|
|
13
|
+
*/
|
|
14
|
+
class OINODbDataModel {
|
|
15
|
+
_columnLookup;
|
|
16
|
+
/** Database refererence of the table */
|
|
17
|
+
api;
|
|
18
|
+
/** Field refererences of the API */
|
|
19
|
+
fields;
|
|
20
|
+
/**
|
|
21
|
+
* Constructor of the data model.
|
|
22
|
+
* NOTE! OINODbDataModel.initialize must be called after constructor to populate fields.
|
|
23
|
+
*
|
|
24
|
+
* @param api api of the data model
|
|
25
|
+
*
|
|
26
|
+
*/
|
|
27
|
+
constructor(api) {
|
|
28
|
+
this._columnLookup = {};
|
|
29
|
+
this.api = api;
|
|
30
|
+
this.fields = [];
|
|
31
|
+
// OINOLog_debug("OINODbDataModel (" + tableName + "):\n" + this._printTableDebug("\n"))
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Initialize datamodel from SQL schema.
|
|
35
|
+
*
|
|
36
|
+
*/
|
|
37
|
+
async initialize() {
|
|
38
|
+
await this.api.db.initializeApiDatamodel(this.api);
|
|
39
|
+
}
|
|
40
|
+
_printSqlColumnNames() {
|
|
41
|
+
let result = "";
|
|
42
|
+
for (let f of this.fields) {
|
|
43
|
+
if (result != "") {
|
|
44
|
+
result += ",";
|
|
45
|
+
}
|
|
46
|
+
result += f.printSqlColumnName();
|
|
47
|
+
}
|
|
48
|
+
return result;
|
|
49
|
+
}
|
|
50
|
+
_printSqlInsertColumnsAndValues(row) {
|
|
51
|
+
let columns = "";
|
|
52
|
+
let values = "";
|
|
53
|
+
for (let i = 0; i < this.fields.length; i++) {
|
|
54
|
+
const val = row[i];
|
|
55
|
+
// console.log("_printSqlInsertColumnsAndValues: row[" + i + "]=" + val)
|
|
56
|
+
if (val !== undefined) {
|
|
57
|
+
const f = this.fields[i];
|
|
58
|
+
if (values != "") {
|
|
59
|
+
columns += ",";
|
|
60
|
+
values += ",";
|
|
61
|
+
}
|
|
62
|
+
columns += f.printSqlColumnName();
|
|
63
|
+
values += f.printCellAsSqlValue(val);
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// console.log("_printSqlInsertColumnsAndValues: columns=" + columns + ", values=" + values)
|
|
67
|
+
return "(" + columns + ") VALUES (" + values + ")";
|
|
68
|
+
}
|
|
69
|
+
_printSqlUpdateValues(row) {
|
|
70
|
+
let result = "";
|
|
71
|
+
for (let i = 0; i < this.fields.length; i++) {
|
|
72
|
+
const f = this.fields[i];
|
|
73
|
+
const val = row[i];
|
|
74
|
+
// OINOLog_debug("OINODbDataModel._printSqlUpdateValues", {field:f.name, primary_key:f.fieldParams.isPrimaryKey, val:val})
|
|
75
|
+
if ((!f.fieldParams.isPrimaryKey) && (val !== undefined)) {
|
|
76
|
+
if (result != "") {
|
|
77
|
+
result += ",";
|
|
78
|
+
}
|
|
79
|
+
result += f.printSqlColumnName() + "=" + f.printCellAsSqlValue(val);
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
return result;
|
|
83
|
+
}
|
|
84
|
+
_printSqlPrimaryKeyCondition(id_value) {
|
|
85
|
+
let result = "";
|
|
86
|
+
let i = 0;
|
|
87
|
+
const id_parts = id_value.split(index_js_1.OINODbConfig.OINODB_ID_SEPARATOR);
|
|
88
|
+
for (let f of this.fields) {
|
|
89
|
+
if (f.fieldParams.isPrimaryKey) {
|
|
90
|
+
if (result != "") {
|
|
91
|
+
result += " AND ";
|
|
92
|
+
}
|
|
93
|
+
let value = decodeURIComponent(id_parts[i]);
|
|
94
|
+
if ((f instanceof index_js_1.OINONumberDataField) && (this.api.hashid)) {
|
|
95
|
+
value = this.api.hashid.decode(value);
|
|
96
|
+
}
|
|
97
|
+
result += f.printSqlColumnName() + "=" + f.printCellAsSqlValue(value);
|
|
98
|
+
i = i + 1;
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
if (i != id_parts.length) {
|
|
102
|
+
throw new Error(index_js_1.OINO_ERROR_PREFIX + ": id '" + id_value + "' is not a valid key for table " + this.api.params.tableName);
|
|
103
|
+
}
|
|
104
|
+
return "(" + result + ")";
|
|
105
|
+
}
|
|
106
|
+
/**
|
|
107
|
+
* Add a field to the datamodel.
|
|
108
|
+
*
|
|
109
|
+
* @param field dataset field
|
|
110
|
+
*
|
|
111
|
+
*/
|
|
112
|
+
addField(field) {
|
|
113
|
+
this.fields.push(field);
|
|
114
|
+
this._columnLookup[field.name] = this.fields.length - 1;
|
|
115
|
+
}
|
|
116
|
+
/**
|
|
117
|
+
* Find a field of a given name if any.
|
|
118
|
+
*
|
|
119
|
+
* @param name name of the field to find
|
|
120
|
+
*
|
|
121
|
+
*/
|
|
122
|
+
findFieldByName(name) {
|
|
123
|
+
// OINOLog.debug("OINODbDataModel.findFieldByName", {_columnLookup:this._columnLookup})
|
|
124
|
+
const i = this._columnLookup[name];
|
|
125
|
+
if (i >= 0) {
|
|
126
|
+
return this.fields[i];
|
|
127
|
+
}
|
|
128
|
+
else {
|
|
129
|
+
return null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Find index of a field of a given name if any.
|
|
134
|
+
*
|
|
135
|
+
* @param name name of the field to find
|
|
136
|
+
*
|
|
137
|
+
*/
|
|
138
|
+
findFieldIndexByName(name) {
|
|
139
|
+
// OINOLog.debug("OINODbDataModel.findFieldIndexByName", {_columnLookup:this._columnLookup})
|
|
140
|
+
const i = this._columnLookup[name];
|
|
141
|
+
if (i >= 0) {
|
|
142
|
+
return i;
|
|
143
|
+
}
|
|
144
|
+
else {
|
|
145
|
+
return -1;
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
/**
|
|
149
|
+
* Find all fields based of given filter callback criteria (e.g. fields of certain data type, primary keys etc.)
|
|
150
|
+
*
|
|
151
|
+
* @param filter callback called for each field to include or not
|
|
152
|
+
*
|
|
153
|
+
*/
|
|
154
|
+
filterFields(filter) {
|
|
155
|
+
let result = [];
|
|
156
|
+
for (let f of this.fields) {
|
|
157
|
+
if (filter(f)) {
|
|
158
|
+
result.push(f);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
return result;
|
|
162
|
+
}
|
|
163
|
+
/**
|
|
164
|
+
* Return the primary key values of one row in order of the data model
|
|
165
|
+
*
|
|
166
|
+
* @param row data row
|
|
167
|
+
* @param hashidValues apply hashid when applicable
|
|
168
|
+
*
|
|
169
|
+
*/
|
|
170
|
+
getRowPrimarykeyValues(row, hashidValues = false) {
|
|
171
|
+
let values = [];
|
|
172
|
+
for (let i = 0; i < this.fields.length; i++) {
|
|
173
|
+
const f = this.fields[i];
|
|
174
|
+
if (f.fieldParams.isPrimaryKey) {
|
|
175
|
+
const value = row[i]?.toString() || "";
|
|
176
|
+
if (hashidValues && value && (f instanceof index_js_1.OINONumberDataField) && this.api.hashid) {
|
|
177
|
+
values.push(this.api.hashid.encode(value));
|
|
178
|
+
}
|
|
179
|
+
else {
|
|
180
|
+
values.push(value);
|
|
181
|
+
}
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
return values;
|
|
185
|
+
}
|
|
186
|
+
/**
|
|
187
|
+
* Print debug information about the fields.
|
|
188
|
+
*
|
|
189
|
+
* @param separator string to separate field prints
|
|
190
|
+
*
|
|
191
|
+
*/
|
|
192
|
+
printDebug(separator = "") {
|
|
193
|
+
let result = this.api.params.tableName + ":" + separator;
|
|
194
|
+
for (let f of this.fields) {
|
|
195
|
+
result += f.printColumnDebug() + separator;
|
|
196
|
+
}
|
|
197
|
+
return result;
|
|
198
|
+
}
|
|
199
|
+
/**
|
|
200
|
+
* Print all public properties (db, table name, fields) of the datamodel. Used
|
|
201
|
+
* in automated testing validate schema has stayed the same.
|
|
202
|
+
*
|
|
203
|
+
*/
|
|
204
|
+
printFieldPublicPropertiesJson() {
|
|
205
|
+
const result = JSON.stringify(this.fields, (key, value) => {
|
|
206
|
+
if (key.startsWith("_")) {
|
|
207
|
+
return undefined;
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
return value;
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
return result;
|
|
214
|
+
}
|
|
215
|
+
/**
|
|
216
|
+
* Print SQL select statement using optional id and filter.
|
|
217
|
+
*
|
|
218
|
+
* @param id OINO ID (i.e. combined primary key values)
|
|
219
|
+
* @param params OINO reqest params
|
|
220
|
+
*
|
|
221
|
+
*/
|
|
222
|
+
printSqlSelect(id, params) {
|
|
223
|
+
let result = "SELECT " + this._printSqlColumnNames() + " FROM " + this.api.db.printSqlTablename(this.api.params.tableName);
|
|
224
|
+
const filter_sql = params.filter?.toSql(this) || "";
|
|
225
|
+
const order_sql = params.order?.toSql(this) || "";
|
|
226
|
+
const limit_sql = params.limit?.toSql(this) || "";
|
|
227
|
+
// OINOLog.debug("OINODbDataModel.printSqlSelect", {select_sql:result, filter_sql:filter_sql, order_sql:order_sql})
|
|
228
|
+
if ((id != "") && (filter_sql != "")) {
|
|
229
|
+
result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id) + " AND " + filter_sql;
|
|
230
|
+
}
|
|
231
|
+
else if (id != "") {
|
|
232
|
+
result += "\nWHERE " + this._printSqlPrimaryKeyCondition(id);
|
|
233
|
+
}
|
|
234
|
+
else if (filter_sql != "") {
|
|
235
|
+
result += "\nWHERE " + filter_sql;
|
|
236
|
+
}
|
|
237
|
+
if (order_sql) {
|
|
238
|
+
result += "\nORDER BY " + order_sql;
|
|
239
|
+
}
|
|
240
|
+
if (limit_sql) {
|
|
241
|
+
result += "\nLIMIT " + limit_sql;
|
|
242
|
+
}
|
|
243
|
+
result += ";";
|
|
244
|
+
index_js_1.OINOLog.debug("OINODbDataModel.printSqlSelect", { result: result });
|
|
245
|
+
return result;
|
|
246
|
+
}
|
|
247
|
+
/**
|
|
248
|
+
* Print SQL insert statement from one data row.
|
|
249
|
+
*
|
|
250
|
+
* @param row one row of data in the data model
|
|
251
|
+
*
|
|
252
|
+
*/
|
|
253
|
+
printSqlInsert(row) {
|
|
254
|
+
let result = "INSERT INTO " + this.api.db.printSqlTablename(this.api.params.tableName) + " " + this._printSqlInsertColumnsAndValues(row) + ";";
|
|
255
|
+
return result;
|
|
256
|
+
}
|
|
257
|
+
/**
|
|
258
|
+
* Print SQL insert statement from one data row.
|
|
259
|
+
*
|
|
260
|
+
* @param id OINO ID (i.e. combined primary key values)
|
|
261
|
+
* @param row one row of data in the data model
|
|
262
|
+
*
|
|
263
|
+
*/
|
|
264
|
+
printSqlUpdate(id, row) {
|
|
265
|
+
let result = "UPDATE " + this.api.db.printSqlTablename(this.api.params.tableName) + " SET " + this._printSqlUpdateValues(row) + " WHERE " + this._printSqlPrimaryKeyCondition(id) + ";";
|
|
266
|
+
return result;
|
|
267
|
+
}
|
|
268
|
+
/**
|
|
269
|
+
* Print SQL delete statement for id.
|
|
270
|
+
*
|
|
271
|
+
* @param id OINO ID (i.e. combined primary key values)
|
|
272
|
+
*
|
|
273
|
+
*/
|
|
274
|
+
printSqlDelete(id) {
|
|
275
|
+
let result = "DELETE FROM " + this.api.db.printSqlTablename(this.api.params.tableName) + " WHERE " + this._printSqlPrimaryKeyCondition(id) + ";";
|
|
276
|
+
return result;
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
exports.OINODbDataModel = OINODbDataModel;
|