@oino-ts/db 0.21.2 → 1.0.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.
- package/dist/cjs/OINODb.js +6 -144
- package/dist/cjs/OINODbApi.js +50 -318
- package/dist/cjs/OINODbConfig.js +10 -10
- package/dist/cjs/OINODbConstants.js +10 -0
- package/dist/cjs/OINODbDataField.js +28 -70
- package/dist/cjs/OINODbDataModel.js +29 -143
- package/dist/cjs/OINODbFactory.js +2 -2
- package/dist/cjs/OINODbModelSet.js +23 -23
- package/dist/cjs/OINODbQueryParams.js +201 -0
- package/dist/cjs/index.js +12 -41
- package/dist/esm/OINODb.js +6 -142
- package/dist/esm/OINODbApi.js +49 -314
- package/dist/esm/OINODbConstants.js +7 -0
- package/dist/esm/OINODbDataModel.js +29 -143
- package/dist/esm/OINODbFactory.js +1 -1
- package/dist/esm/OINODbQueryParams.js +194 -0
- package/dist/esm/index.js +4 -14
- package/dist/types/OINODb.d.ts +6 -173
- package/dist/types/OINODbApi.d.ts +18 -104
- package/dist/types/OINODbConstants.d.ts +23 -0
- package/dist/types/OINODbDataModel.d.ts +7 -61
- package/dist/types/OINODbFactory.d.ts +5 -2
- package/dist/types/OINODbQueryParams.d.ts +72 -0
- package/dist/types/index.d.ts +4 -108
- package/package.json +37 -37
- package/src/OINODb.ts +99 -348
- package/src/OINODbApi.test.ts +507 -498
- package/src/OINODbApi.ts +389 -667
- package/src/OINODbConstants.ts +32 -0
- package/src/OINODbDataModel.ts +191 -307
- package/src/OINODbFactory.ts +73 -68
- package/src/OINODbQueryParams.ts +203 -0
- package/src/index.ts +6 -118
- package/src/OINODbConfig.ts +0 -98
- package/src/OINODbDataField.ts +0 -405
- package/src/OINODbModelSet.ts +0 -353
- package/src/OINODbParser.ts +0 -438
- package/src/OINODbSqlParams.ts +0 -593
- package/src/OINODbSwagger.ts +0 -209
package/dist/cjs/OINODbConfig.js
CHANGED
|
@@ -8,13 +8,13 @@ class OINODbConfig {
|
|
|
8
8
|
/** Private key separator of the synthetic OINO ID field */
|
|
9
9
|
static OINODB_ID_SEPARATOR = "_";
|
|
10
10
|
static OINODB_ID_SEPARATOR_ESCAPED = "%";
|
|
11
|
-
/** Name of the
|
|
11
|
+
/** Name of the OINODbQueryFilter-parameter in request */
|
|
12
12
|
static OINODB_SQL_FILTER_PARAM = "oinosqlfilter";
|
|
13
|
-
/** Name of the
|
|
13
|
+
/** Name of the OINODbQueryOrder-parameter in request */
|
|
14
14
|
static OINODB_SQL_ORDER_PARAM = "oinosqlorder";
|
|
15
|
-
/** Name of the
|
|
15
|
+
/** Name of the OINODbQueryLimit-parameter in request */
|
|
16
16
|
static OINODB_SQL_LIMIT_PARAM = "oinosqllimit";
|
|
17
|
-
/** Name of the
|
|
17
|
+
/** Name of the OINODbQueryAggregate-parameter in request */
|
|
18
18
|
static OINODB_SQL_AGGREGATE_PARAM = "oinosqlaggregate";
|
|
19
19
|
/** Name of the OINODbSqlSelect-parameter in request */
|
|
20
20
|
static OINODB_SQL_SELECT_PARAM = "oinosqlselect";
|
|
@@ -54,9 +54,9 @@ class OINODbConfig {
|
|
|
54
54
|
return result;
|
|
55
55
|
}
|
|
56
56
|
/**
|
|
57
|
-
* Set the name of the
|
|
57
|
+
* Set the name of the OINODbQueryFilter-param field
|
|
58
58
|
*
|
|
59
|
-
* @param sqlFilterParam name of the http parameter with `
|
|
59
|
+
* @param sqlFilterParam name of the http parameter with `OINODbQueryFilter` definition
|
|
60
60
|
*
|
|
61
61
|
*/
|
|
62
62
|
static setOinoSqlFilterParam(sqlFilterParam) {
|
|
@@ -65,9 +65,9 @@ class OINODbConfig {
|
|
|
65
65
|
}
|
|
66
66
|
}
|
|
67
67
|
/**
|
|
68
|
-
* Set the name of the
|
|
68
|
+
* Set the name of the OINODbQueryOrder-param field
|
|
69
69
|
*
|
|
70
|
-
* @param sqlOrderParam name of the http parameter with `
|
|
70
|
+
* @param sqlOrderParam name of the http parameter with `OINODbQueryOrder` definition
|
|
71
71
|
*
|
|
72
72
|
*/
|
|
73
73
|
static setOinoSqlOrderParam(sqlOrderParam) {
|
|
@@ -76,9 +76,9 @@ class OINODbConfig {
|
|
|
76
76
|
}
|
|
77
77
|
}
|
|
78
78
|
/**
|
|
79
|
-
* Set the name of the
|
|
79
|
+
* Set the name of the OINODbQueryLimit-param field
|
|
80
80
|
*
|
|
81
|
-
* @param sqlLimitParam name of the http parameter with `
|
|
81
|
+
* @param sqlLimitParam name of the http parameter with `OINODbQueryLimit` definition
|
|
82
82
|
*
|
|
83
83
|
*/
|
|
84
84
|
static setOinoSqlLimitParam(sqlLimitParam) {
|
|
@@ -0,0 +1,10 @@
|
|
|
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.OINODB_UNDEFINED = void 0;
|
|
9
|
+
/** Constant for undefined values */
|
|
10
|
+
exports.OINODB_UNDEFINED = ""; // original idea was to have a defined literal that get's swapped back to undefined, but current implementation just leaves it out at serialization (so value does not matter)
|
|
@@ -13,8 +13,8 @@ const common_1 = require("@oino-ts/common");
|
|
|
13
13
|
*
|
|
14
14
|
*/
|
|
15
15
|
class OINODbDataField {
|
|
16
|
-
/**
|
|
17
|
-
|
|
16
|
+
/** OINO data source reference*/
|
|
17
|
+
datasource;
|
|
18
18
|
/** Name of the field */
|
|
19
19
|
name;
|
|
20
20
|
/** Internal type of field*/
|
|
@@ -28,7 +28,7 @@ class OINODbDataField {
|
|
|
28
28
|
/**
|
|
29
29
|
* Constructor for a data field
|
|
30
30
|
*
|
|
31
|
-
* @param
|
|
31
|
+
* @param datasource OINO data source reference
|
|
32
32
|
* @param name name of the field
|
|
33
33
|
* @param type internal type of the field
|
|
34
34
|
* @param sqlType column type in database
|
|
@@ -36,56 +36,14 @@ class OINODbDataField {
|
|
|
36
36
|
* @param maxLength maximum length of the field (or 0)
|
|
37
37
|
*
|
|
38
38
|
*/
|
|
39
|
-
constructor(
|
|
40
|
-
this.
|
|
39
|
+
constructor(datasource, name, type, sqlType, fieldParams, maxLength = 0) {
|
|
40
|
+
this.datasource = datasource;
|
|
41
41
|
this.name = name;
|
|
42
42
|
this.type = type;
|
|
43
43
|
this.maxLength = maxLength;
|
|
44
44
|
this.sqlType = sqlType;
|
|
45
45
|
this.fieldParams = fieldParams;
|
|
46
46
|
}
|
|
47
|
-
/**
|
|
48
|
-
* Pring debug information for the field
|
|
49
|
-
*
|
|
50
|
-
* @param length length of the debug output (or 0 for as long as needed)
|
|
51
|
-
*
|
|
52
|
-
*/
|
|
53
|
-
printColumnDebug(length = 0) {
|
|
54
|
-
let params = "";
|
|
55
|
-
if (this.fieldParams.isPrimaryKey) {
|
|
56
|
-
params += "PK ";
|
|
57
|
-
}
|
|
58
|
-
if (this.fieldParams.isForeignKey) {
|
|
59
|
-
params += "FK ";
|
|
60
|
-
}
|
|
61
|
-
if (this.fieldParams.isAutoInc) {
|
|
62
|
-
params += "AUTOINC ";
|
|
63
|
-
}
|
|
64
|
-
if (this.fieldParams.isNotNull) {
|
|
65
|
-
params += "NOTNUL ";
|
|
66
|
-
}
|
|
67
|
-
if (params != "") {
|
|
68
|
-
params = "{" + params.trim() + "}";
|
|
69
|
-
}
|
|
70
|
-
if (this.maxLength > 0) {
|
|
71
|
-
params = this.sqlType + "(" + this.maxLength + ")" + params;
|
|
72
|
-
}
|
|
73
|
-
else {
|
|
74
|
-
params = this.sqlType + params;
|
|
75
|
-
}
|
|
76
|
-
const name_length = length - 2 - 1 - params.length;
|
|
77
|
-
let result = this.name;
|
|
78
|
-
if (length > 0) {
|
|
79
|
-
if (result.length > name_length) {
|
|
80
|
-
result = result.substring(0, name_length - 2) + "..";
|
|
81
|
-
}
|
|
82
|
-
result = (result + ":" + params).padEnd(length - 2, " ");
|
|
83
|
-
}
|
|
84
|
-
else {
|
|
85
|
-
result = this.type + ":" + result + ":" + params;
|
|
86
|
-
}
|
|
87
|
-
return "[" + result + "]";
|
|
88
|
-
}
|
|
89
47
|
/**
|
|
90
48
|
* Serialize cell value in the given content format.
|
|
91
49
|
*
|
|
@@ -93,7 +51,7 @@ class OINODbDataField {
|
|
|
93
51
|
*
|
|
94
52
|
*/
|
|
95
53
|
serializeCell(cellVal) {
|
|
96
|
-
cellVal = this.
|
|
54
|
+
cellVal = this.datasource.parseValueAsCell(cellVal, this.sqlType);
|
|
97
55
|
if ((cellVal === null) || (cellVal === undefined)) {
|
|
98
56
|
return cellVal; // let content type encoder worry what to do with the value (so not force it to string)
|
|
99
57
|
}
|
|
@@ -116,15 +74,15 @@ class OINODbDataField {
|
|
|
116
74
|
* @param cellVal cell value
|
|
117
75
|
*
|
|
118
76
|
*/
|
|
119
|
-
|
|
120
|
-
return this.
|
|
77
|
+
printCellAsValue(cellVal) {
|
|
78
|
+
return this.datasource.printCellAsValue(cellVal, this.sqlType);
|
|
121
79
|
}
|
|
122
80
|
/**
|
|
123
81
|
* Print name of column as SQL.
|
|
124
82
|
*
|
|
125
83
|
*/
|
|
126
|
-
|
|
127
|
-
return this.
|
|
84
|
+
printColumnName() {
|
|
85
|
+
return this.datasource.printColumnName(this.name);
|
|
128
86
|
}
|
|
129
87
|
}
|
|
130
88
|
exports.OINODbDataField = OINODbDataField;
|
|
@@ -143,8 +101,8 @@ class OINOStringDataField extends OINODbDataField {
|
|
|
143
101
|
* @param maxLength maximum length of the field (or 0)
|
|
144
102
|
*
|
|
145
103
|
*/
|
|
146
|
-
constructor(
|
|
147
|
-
super(
|
|
104
|
+
constructor(datasource, name, sqlType, fieldParams, maxLength) {
|
|
105
|
+
super(datasource, name, "string", sqlType, fieldParams, maxLength);
|
|
148
106
|
}
|
|
149
107
|
}
|
|
150
108
|
exports.OINOStringDataField = OINOStringDataField;
|
|
@@ -156,14 +114,14 @@ class OINOBooleanDataField extends OINODbDataField {
|
|
|
156
114
|
/**
|
|
157
115
|
* Constructor for a boolean data field
|
|
158
116
|
*
|
|
159
|
-
* @param
|
|
117
|
+
* @param datasource OINODb reference
|
|
160
118
|
* @param name name of the field
|
|
161
119
|
* @param sqlType column type in database
|
|
162
120
|
* @param fieldParams parameters of the field
|
|
163
121
|
*
|
|
164
122
|
*/
|
|
165
|
-
constructor(
|
|
166
|
-
super(
|
|
123
|
+
constructor(datasource, name, sqlType, fieldParams) {
|
|
124
|
+
super(datasource, name, "boolean", sqlType, fieldParams);
|
|
167
125
|
}
|
|
168
126
|
/**
|
|
169
127
|
* Serialize cell value in the given content format.
|
|
@@ -172,7 +130,7 @@ class OINOBooleanDataField extends OINODbDataField {
|
|
|
172
130
|
*
|
|
173
131
|
*/
|
|
174
132
|
serializeCell(cellVal) {
|
|
175
|
-
const parsed_value = (this.
|
|
133
|
+
const parsed_value = (this.datasource.parseValueAsCell(cellVal, this.sqlType) || "").toString();
|
|
176
134
|
let result;
|
|
177
135
|
// console.log("OINOBooleanDataField.serializeCell: parsed_value=" + parsed_value)
|
|
178
136
|
if ((parsed_value == "") || (parsed_value.toLowerCase() == "false") || (parsed_value.match(/^0+$/))) {
|
|
@@ -207,14 +165,14 @@ class OINONumberDataField extends OINODbDataField {
|
|
|
207
165
|
/**
|
|
208
166
|
* Constructor for a string data field
|
|
209
167
|
*
|
|
210
|
-
* @param
|
|
168
|
+
* @param datasource OINODb reference
|
|
211
169
|
* @param name name of the field
|
|
212
170
|
* @param sqlType column type in database
|
|
213
171
|
* @param fieldParams parameters of the field
|
|
214
172
|
*
|
|
215
173
|
*/
|
|
216
|
-
constructor(
|
|
217
|
-
super(
|
|
174
|
+
constructor(datasource, name, sqlType, fieldParams) {
|
|
175
|
+
super(datasource, name, "number", sqlType, fieldParams);
|
|
218
176
|
}
|
|
219
177
|
/**
|
|
220
178
|
* Serialize cell value in the given content format.
|
|
@@ -264,15 +222,15 @@ class OINOBlobDataField extends OINODbDataField {
|
|
|
264
222
|
/**
|
|
265
223
|
* Constructor for a blob data field
|
|
266
224
|
*
|
|
267
|
-
* @param
|
|
225
|
+
* @param datasource OINODb reference
|
|
268
226
|
* @param name name of the field
|
|
269
227
|
* @param sqlType column type in database
|
|
270
228
|
* @param fieldParams parameters of the field
|
|
271
229
|
* @param maxLength maximum length of the field (or 0)
|
|
272
230
|
*
|
|
273
231
|
*/
|
|
274
|
-
constructor(
|
|
275
|
-
super(
|
|
232
|
+
constructor(datasource, name, sqlType, fieldParams, maxLength) {
|
|
233
|
+
super(datasource, name, "blob", sqlType, fieldParams, maxLength);
|
|
276
234
|
}
|
|
277
235
|
/**
|
|
278
236
|
* Serialize cell value in the given content format.
|
|
@@ -292,7 +250,7 @@ class OINOBlobDataField extends OINODbDataField {
|
|
|
292
250
|
return buffer_1.Buffer.from(cellVal).toString('base64');
|
|
293
251
|
}
|
|
294
252
|
else {
|
|
295
|
-
return this.
|
|
253
|
+
return this.datasource.parseValueAsCell(cellVal, this.sqlType)?.toString();
|
|
296
254
|
}
|
|
297
255
|
}
|
|
298
256
|
/**
|
|
@@ -319,14 +277,14 @@ class OINODatetimeDataField extends OINODbDataField {
|
|
|
319
277
|
/**
|
|
320
278
|
* Constructor for a string data field
|
|
321
279
|
*
|
|
322
|
-
* @param
|
|
280
|
+
* @param datasource OINODb reference
|
|
323
281
|
* @param name name of the field
|
|
324
282
|
* @param sqlType column type in database
|
|
325
283
|
* @param fieldParams parameters of the field
|
|
326
284
|
*
|
|
327
285
|
*/
|
|
328
|
-
constructor(
|
|
329
|
-
super(
|
|
286
|
+
constructor(datasource, name, sqlType, fieldParams) {
|
|
287
|
+
super(datasource, name, "datetime", sqlType, fieldParams);
|
|
330
288
|
}
|
|
331
289
|
/**
|
|
332
290
|
* Serialize cell value in the given content format.
|
|
@@ -336,7 +294,7 @@ class OINODatetimeDataField extends OINODbDataField {
|
|
|
336
294
|
*/
|
|
337
295
|
serializeCell(cellVal) {
|
|
338
296
|
if (typeof (cellVal) == "string") {
|
|
339
|
-
cellVal = this.
|
|
297
|
+
cellVal = this.datasource.parseValueAsCell(cellVal, this.sqlType);
|
|
340
298
|
}
|
|
341
299
|
if ((cellVal === null) || (cellVal === undefined)) {
|
|
342
300
|
return cellVal;
|
|
@@ -357,7 +315,7 @@ class OINODatetimeDataField extends OINODbDataField {
|
|
|
357
315
|
*/
|
|
358
316
|
serializeCellWithLocale(cellVal, locale) {
|
|
359
317
|
if (typeof (cellVal) == "string") {
|
|
360
|
-
cellVal = this.
|
|
318
|
+
cellVal = this.datasource.parseValueAsCell(cellVal, this.sqlType);
|
|
361
319
|
}
|
|
362
320
|
if ((cellVal === null) || (cellVal === undefined)) {
|
|
363
321
|
return cellVal;
|
|
@@ -7,15 +7,15 @@
|
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbDataModel = void 0;
|
|
9
9
|
const common_1 = require("@oino-ts/common");
|
|
10
|
-
const
|
|
10
|
+
const OINODbConstants_js_1 = require("./OINODbConstants.js");
|
|
11
|
+
const OINODbQueryParams_js_1 = require("./OINODbQueryParams.js");
|
|
11
12
|
/**
|
|
12
13
|
* OINO Datamodel object for representing one database table and it's columns.
|
|
13
14
|
*
|
|
14
15
|
*/
|
|
15
|
-
class OINODbDataModel {
|
|
16
|
-
_fieldIndexLookup;
|
|
16
|
+
class OINODbDataModel extends common_1.OINODataModel {
|
|
17
17
|
/** Database refererence of the table */
|
|
18
|
-
|
|
18
|
+
dbApi;
|
|
19
19
|
/** Field refererences of the API */
|
|
20
20
|
fields;
|
|
21
21
|
/**
|
|
@@ -26,26 +26,19 @@ class OINODbDataModel {
|
|
|
26
26
|
*
|
|
27
27
|
*/
|
|
28
28
|
constructor(api) {
|
|
29
|
-
|
|
30
|
-
this.
|
|
29
|
+
super(api);
|
|
30
|
+
this.dbApi = api;
|
|
31
31
|
this.fields = [];
|
|
32
32
|
}
|
|
33
|
-
|
|
34
|
-
* Initialize datamodel from SQL schema.
|
|
35
|
-
*
|
|
36
|
-
*/
|
|
37
|
-
async initialize() {
|
|
38
|
-
await this.api.db.initializeApiDatamodel(this.api);
|
|
39
|
-
}
|
|
40
|
-
_printSqlColumnNames(select) {
|
|
33
|
+
_printColumnNames(select) {
|
|
41
34
|
let result = "";
|
|
42
35
|
for (let i = 0; i < this.fields.length; i++) {
|
|
43
36
|
const f = this.fields[i];
|
|
44
|
-
if (select?.isSelected(f) === false) { // if a field is not selected, we include a constant and correct fieldname instead so that dimensions of the data don't change but no unnecessary data is fetched
|
|
45
|
-
result += f.
|
|
37
|
+
if ((select?.isSelected(f.name) === false) && (f.fieldParams.isPrimaryKey == false)) { // if a field is not selected, we include a constant and correct fieldname instead so that dimensions of the data don't change but no unnecessary data is fetched
|
|
38
|
+
result += f.datasource.printStringValue(OINODbConstants_js_1.OINODB_UNDEFINED) + " as " + f.printColumnName() + ",";
|
|
46
39
|
}
|
|
47
40
|
else {
|
|
48
|
-
result += f.
|
|
41
|
+
result += f.printColumnName() + ",";
|
|
49
42
|
}
|
|
50
43
|
}
|
|
51
44
|
return result.substring(0, result.length - 1);
|
|
@@ -62,8 +55,8 @@ class OINODbDataModel {
|
|
|
62
55
|
columns += ",";
|
|
63
56
|
values += ",";
|
|
64
57
|
}
|
|
65
|
-
columns += f.
|
|
66
|
-
values += f.
|
|
58
|
+
columns += f.printColumnName();
|
|
59
|
+
values += f.printCellAsValue(val);
|
|
67
60
|
}
|
|
68
61
|
}
|
|
69
62
|
// console.log("_printSqlInsertColumnsAndValues: columns=" + columns + ", values=" + values)
|
|
@@ -78,7 +71,7 @@ class OINODbDataModel {
|
|
|
78
71
|
if (result != "") {
|
|
79
72
|
result += ",";
|
|
80
73
|
}
|
|
81
|
-
result += f.
|
|
74
|
+
result += f.printColumnName() + "=" + f.printCellAsValue(val);
|
|
82
75
|
}
|
|
83
76
|
}
|
|
84
77
|
if (result == "") {
|
|
@@ -89,21 +82,21 @@ class OINODbDataModel {
|
|
|
89
82
|
_printSqlPrimaryKeyCondition(id_value) {
|
|
90
83
|
let result = "";
|
|
91
84
|
let i = 0;
|
|
92
|
-
const id_parts = id_value.split(
|
|
85
|
+
const id_parts = id_value.split(common_1.OINOConfig.OINO_ID_SEPARATOR);
|
|
93
86
|
for (let f of this.fields) {
|
|
94
87
|
if (f.fieldParams.isPrimaryKey) {
|
|
95
88
|
if (result != "") {
|
|
96
89
|
result += " AND ";
|
|
97
90
|
}
|
|
98
91
|
let value = decodeURIComponent(id_parts[i]);
|
|
99
|
-
if ((f instanceof
|
|
92
|
+
if ((f instanceof common_1.OINONumberDataField) && (this.api.hashid)) {
|
|
100
93
|
value = this.api.hashid.decode(value);
|
|
101
94
|
}
|
|
102
|
-
value = f.
|
|
95
|
+
value = f.printCellAsValue(value);
|
|
103
96
|
if (value == "") { // ids are user input and could be specially crafted to be empty
|
|
104
97
|
throw new Error(common_1.OINO_ERROR_PREFIX + ": empty condition for id '" + id_value + "' for table " + this.api.params.tableName);
|
|
105
98
|
}
|
|
106
|
-
result += f.
|
|
99
|
+
result += f.printColumnName() + "=" + value;
|
|
107
100
|
i = i + 1;
|
|
108
101
|
}
|
|
109
102
|
}
|
|
@@ -116,118 +109,11 @@ class OINODbDataModel {
|
|
|
116
109
|
let result = [];
|
|
117
110
|
for (let f of this.fields) {
|
|
118
111
|
if (f.fieldParams.isPrimaryKey) {
|
|
119
|
-
result.push(this.
|
|
112
|
+
result.push(this.dbApi.db.printColumnName(f.name));
|
|
120
113
|
}
|
|
121
114
|
}
|
|
122
115
|
return result;
|
|
123
116
|
}
|
|
124
|
-
/**
|
|
125
|
-
* Add a field to the datamodel.
|
|
126
|
-
*
|
|
127
|
-
* @param field dataset field
|
|
128
|
-
*
|
|
129
|
-
*/
|
|
130
|
-
addField(field) {
|
|
131
|
-
this.fields.push(field);
|
|
132
|
-
this._fieldIndexLookup[field.name] = this.fields.length - 1;
|
|
133
|
-
}
|
|
134
|
-
/**
|
|
135
|
-
* Find a field of a given name if any.
|
|
136
|
-
*
|
|
137
|
-
* @param name name of the field to find
|
|
138
|
-
*
|
|
139
|
-
*/
|
|
140
|
-
findFieldByName(name) {
|
|
141
|
-
const i = this._fieldIndexLookup[name];
|
|
142
|
-
if (i >= 0) {
|
|
143
|
-
return this.fields[i];
|
|
144
|
-
}
|
|
145
|
-
else {
|
|
146
|
-
return null;
|
|
147
|
-
}
|
|
148
|
-
}
|
|
149
|
-
/**
|
|
150
|
-
* Find index of a field of a given name if any.
|
|
151
|
-
*
|
|
152
|
-
* @param name name of the field to find
|
|
153
|
-
*
|
|
154
|
-
*/
|
|
155
|
-
findFieldIndexByName(name) {
|
|
156
|
-
const i = this._fieldIndexLookup[name];
|
|
157
|
-
if (i >= 0) {
|
|
158
|
-
return i;
|
|
159
|
-
}
|
|
160
|
-
else {
|
|
161
|
-
return -1;
|
|
162
|
-
}
|
|
163
|
-
}
|
|
164
|
-
/**
|
|
165
|
-
* Find all fields based of given filter callback criteria (e.g. fields of certain data type, primary keys etc.)
|
|
166
|
-
*
|
|
167
|
-
* @param filter callback called for each field to include or not
|
|
168
|
-
*
|
|
169
|
-
*/
|
|
170
|
-
filterFields(filter) {
|
|
171
|
-
let result = [];
|
|
172
|
-
for (let f of this.fields) {
|
|
173
|
-
if (filter(f)) {
|
|
174
|
-
result.push(f);
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
return result;
|
|
178
|
-
}
|
|
179
|
-
/**
|
|
180
|
-
* Return the primary key values of one row in order of the data model
|
|
181
|
-
*
|
|
182
|
-
* @param row data row
|
|
183
|
-
* @param hashidValues apply hashid when applicable
|
|
184
|
-
*
|
|
185
|
-
*/
|
|
186
|
-
getRowPrimarykeyValues(row, hashidValues = false) {
|
|
187
|
-
let values = [];
|
|
188
|
-
for (let i = 0; i < this.fields.length; i++) {
|
|
189
|
-
const f = this.fields[i];
|
|
190
|
-
if (f.fieldParams.isPrimaryKey) {
|
|
191
|
-
const value = row[i]?.toString() || "";
|
|
192
|
-
if (hashidValues && value && (f instanceof index_js_1.OINONumberDataField) && this.api.hashid) {
|
|
193
|
-
values.push(this.api.hashid.encode(value));
|
|
194
|
-
}
|
|
195
|
-
else {
|
|
196
|
-
values.push(value);
|
|
197
|
-
}
|
|
198
|
-
}
|
|
199
|
-
}
|
|
200
|
-
return values;
|
|
201
|
-
}
|
|
202
|
-
/**
|
|
203
|
-
* Print debug information about the fields.
|
|
204
|
-
*
|
|
205
|
-
* @param separator string to separate field prints
|
|
206
|
-
*
|
|
207
|
-
*/
|
|
208
|
-
printDebug(separator = "") {
|
|
209
|
-
let result = this.api.params.tableName + ":" + separator;
|
|
210
|
-
for (let f of this.fields) {
|
|
211
|
-
result += f.printColumnDebug() + separator;
|
|
212
|
-
}
|
|
213
|
-
return result;
|
|
214
|
-
}
|
|
215
|
-
/**
|
|
216
|
-
* Print all public properties (db, table name, fields) of the datamodel. Used
|
|
217
|
-
* in automated testing validate schema has stayed the same.
|
|
218
|
-
*
|
|
219
|
-
*/
|
|
220
|
-
printFieldPublicPropertiesJson() {
|
|
221
|
-
const result = JSON.stringify(this.fields, (key, value) => {
|
|
222
|
-
if (key.startsWith("_")) {
|
|
223
|
-
return undefined;
|
|
224
|
-
}
|
|
225
|
-
else {
|
|
226
|
-
return value;
|
|
227
|
-
}
|
|
228
|
-
});
|
|
229
|
-
return result;
|
|
230
|
-
}
|
|
231
117
|
/**
|
|
232
118
|
* Print SQL select statement using optional id and filter.
|
|
233
119
|
*
|
|
@@ -238,15 +124,15 @@ class OINODbDataModel {
|
|
|
238
124
|
printSqlSelect(id, params) {
|
|
239
125
|
let column_names = "";
|
|
240
126
|
if (params.aggregate) {
|
|
241
|
-
column_names = params.aggregate
|
|
127
|
+
column_names = OINODbQueryParams_js_1.OINODbQueryAggregate.printColumnNames(params.aggregate, this, params.select);
|
|
242
128
|
}
|
|
243
129
|
else {
|
|
244
|
-
column_names = this.
|
|
130
|
+
column_names = this._printColumnNames(params.select);
|
|
245
131
|
}
|
|
246
|
-
const order_sql = params.order
|
|
247
|
-
const limit_sql = params.limit
|
|
248
|
-
const filter_sql = params.filter
|
|
249
|
-
const groupby_sql = params.aggregate
|
|
132
|
+
const order_sql = params.order ? OINODbQueryParams_js_1.OINODbQueryOrder.printSql(params.order, this) : "";
|
|
133
|
+
const limit_sql = params.limit ? OINODbQueryParams_js_1.OINODbQueryLimit.printSql(params.limit, this) : "";
|
|
134
|
+
const filter_sql = params.filter ? OINODbQueryParams_js_1.OINODbQueryFilter.printSql(params.filter, this) : "";
|
|
135
|
+
const groupby_sql = params.aggregate ? OINODbQueryParams_js_1.OINODbQueryAggregate.printSql(params.aggregate, this, params.select) : "";
|
|
250
136
|
let where_sql = "";
|
|
251
137
|
if ((id != null) && (id != "") && (filter_sql != "")) {
|
|
252
138
|
where_sql = this._printSqlPrimaryKeyCondition(id) + " AND " + filter_sql;
|
|
@@ -257,7 +143,7 @@ class OINODbDataModel {
|
|
|
257
143
|
else if (filter_sql != "") {
|
|
258
144
|
where_sql = filter_sql;
|
|
259
145
|
}
|
|
260
|
-
const result = this.
|
|
146
|
+
const result = this.dbApi.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql, groupby_sql);
|
|
261
147
|
return result;
|
|
262
148
|
}
|
|
263
149
|
/**
|
|
@@ -267,10 +153,10 @@ class OINODbDataModel {
|
|
|
267
153
|
*
|
|
268
154
|
*/
|
|
269
155
|
printSqlInsert(row) {
|
|
270
|
-
const table_name = this.
|
|
156
|
+
const table_name = this.dbApi.db.printTableName(this.api.params.tableName);
|
|
271
157
|
const [columns, values] = this._printSqlInsertColumnsAndValues(row);
|
|
272
158
|
const return_fields = this.api.params.returnInsertedIds ? this._printSqlPrimaryKeyColumns() : undefined;
|
|
273
|
-
return this.
|
|
159
|
+
return this.dbApi.db.printSqlInsert(table_name, columns, values, return_fields);
|
|
274
160
|
}
|
|
275
161
|
/**
|
|
276
162
|
* Print SQL insert statement from one data row.
|
|
@@ -280,7 +166,7 @@ class OINODbDataModel {
|
|
|
280
166
|
*
|
|
281
167
|
*/
|
|
282
168
|
printSqlUpdate(id, row) {
|
|
283
|
-
let result = "UPDATE " + this.
|
|
169
|
+
let result = "UPDATE " + this.dbApi.db.printTableName(this.api.params.tableName) + " SET " + this._printSqlUpdateValues(row) + " WHERE " + this._printSqlPrimaryKeyCondition(id) + ";";
|
|
284
170
|
return result;
|
|
285
171
|
}
|
|
286
172
|
/**
|
|
@@ -290,7 +176,7 @@ class OINODbDataModel {
|
|
|
290
176
|
*
|
|
291
177
|
*/
|
|
292
178
|
printSqlDelete(id) {
|
|
293
|
-
let result = "DELETE FROM " + this.
|
|
179
|
+
let result = "DELETE FROM " + this.dbApi.db.printTableName(this.api.params.tableName) + " WHERE " + this._printSqlPrimaryKeyCondition(id) + ";";
|
|
294
180
|
return result;
|
|
295
181
|
}
|
|
296
182
|
}
|
|
@@ -6,7 +6,7 @@
|
|
|
6
6
|
*/
|
|
7
7
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
8
|
exports.OINODbFactory = void 0;
|
|
9
|
-
const
|
|
9
|
+
const OINODbApi_js_1 = require("./OINODbApi.js");
|
|
10
10
|
/**
|
|
11
11
|
* Static factory class for easily creating things based on data
|
|
12
12
|
*
|
|
@@ -60,7 +60,7 @@ class OINODbFactory {
|
|
|
60
60
|
* @param params parameters of the API
|
|
61
61
|
*/
|
|
62
62
|
static async createApi(db, params) {
|
|
63
|
-
let result = new
|
|
63
|
+
let result = new OINODbApi_js_1.OINODbApi(db, params);
|
|
64
64
|
await db.initializeApiDatamodel(result);
|
|
65
65
|
return result;
|
|
66
66
|
}
|