@oino-ts/db 0.4.0 → 0.4.2
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/OINODbDataModel.js +3 -3
- package/dist/cjs/OINODbModelSet.js +1 -1
- package/dist/cjs/OINODbSqlParams.js +7 -5
- package/dist/esm/OINODbDataModel.js +4 -4
- package/dist/esm/OINODbModelSet.js +1 -1
- package/dist/esm/OINODbSqlParams.js +7 -5
- package/dist/types/OINODbSqlParams.d.ts +2 -1
- package/package.json +3 -3
- package/src/OINODbDataModel.ts +3 -3
- package/src/OINODbModelSet.ts +1 -1
- package/src/OINODbSqlParams.ts +7 -5
|
@@ -234,7 +234,7 @@ class OINODbDataModel {
|
|
|
234
234
|
const order_sql = params.order?.toSql(this) || "";
|
|
235
235
|
const limit_sql = params.limit?.toSql(this) || "";
|
|
236
236
|
const filter_sql = params.filter?.toSql(this) || "";
|
|
237
|
-
const
|
|
237
|
+
const groupby_sql = params.aggregate?.toSql(this, params.select) || "";
|
|
238
238
|
let where_sql = "";
|
|
239
239
|
// OINOLog.debug("OINODbDataModel.printSqlSelect", {order_sql:order_sql, limit_sql:limit_sql, filter_sql:filter_sql, aggregate_sql:aggregate_sql})
|
|
240
240
|
if ((id != null) && (id != "") && (filter_sql != "")) {
|
|
@@ -246,8 +246,8 @@ class OINODbDataModel {
|
|
|
246
246
|
else if (filter_sql != "") {
|
|
247
247
|
where_sql = filter_sql;
|
|
248
248
|
}
|
|
249
|
-
const result = this.api.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql,
|
|
250
|
-
|
|
249
|
+
const result = this.api.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql, groupby_sql);
|
|
250
|
+
index_js_1.OINOLog.debug("OINODbDataModel.printSqlSelect", { result: result });
|
|
251
251
|
return result;
|
|
252
252
|
}
|
|
253
253
|
/**
|
|
@@ -39,7 +39,7 @@ class OINODbModelSet {
|
|
|
39
39
|
_encodeAndHashFieldValue(field, value, contentType, primaryKeyValues, rowIdSeed) {
|
|
40
40
|
let result;
|
|
41
41
|
if (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) {
|
|
42
|
-
if (value && (field instanceof index_js_1.OINONumberDataField) && (this.datamodel.api.hashid)) {
|
|
42
|
+
if (value && (field instanceof index_js_1.OINONumberDataField) && (this.datamodel.api.hashid) && ((this.sqlParams?.aggregate === undefined) || (this.sqlParams.aggregate.isAggregated(field) == false))) {
|
|
43
43
|
value = this.datamodel.api.hashid.encode(value, rowIdSeed);
|
|
44
44
|
}
|
|
45
45
|
if (field.fieldParams.isPrimaryKey) {
|
|
@@ -398,16 +398,18 @@ class OINODbSqlAggregate {
|
|
|
398
398
|
* Print non-aggregated fields as SQL GROUP BY-condition based on the datamodel of the API.
|
|
399
399
|
*
|
|
400
400
|
* @param dataModel data model (and database) to use for formatting of values
|
|
401
|
+
* @param select what fields to select
|
|
401
402
|
*
|
|
402
403
|
*/
|
|
403
|
-
toSql(dataModel) {
|
|
404
|
+
toSql(dataModel, select) {
|
|
404
405
|
if (this.isEmpty()) {
|
|
405
406
|
return "";
|
|
406
407
|
}
|
|
407
408
|
let result = "";
|
|
408
409
|
for (let i = 0; i < dataModel.fields.length; i++) {
|
|
409
|
-
|
|
410
|
-
|
|
410
|
+
const f = dataModel.fields[i];
|
|
411
|
+
if (select?.isSelected(f) && (this._fields.includes(f.name) == false)) {
|
|
412
|
+
result += f.printSqlColumnName() + ",";
|
|
411
413
|
}
|
|
412
414
|
}
|
|
413
415
|
// OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
|
|
@@ -424,8 +426,8 @@ class OINODbSqlAggregate {
|
|
|
424
426
|
let result = "";
|
|
425
427
|
for (let i = 0; i < dataModel.fields.length; i++) {
|
|
426
428
|
const f = dataModel.fields[i];
|
|
427
|
-
if (select?.isSelected(f) == false) { // if a field is not selected, we include
|
|
428
|
-
result += f.db.printSqlString(index_js_1.OINODB_UNDEFINED) + " as " + f.printSqlColumnName() + ",";
|
|
429
|
+
if (select?.isSelected(f) == false) { // if a field is not selected, we include an aggregated constant (min of const string) and correct fieldname instead so that dimensions of the data don't change, it does not need a group by but no unnecessary data is fetched
|
|
430
|
+
result += OINODbSqlAggregateFunctions.min + "(" + f.db.printSqlString(index_js_1.OINODB_UNDEFINED) + ") as " + f.printSqlColumnName() + ",";
|
|
429
431
|
}
|
|
430
432
|
else {
|
|
431
433
|
const aggregate_index = this._fields.indexOf(f.name);
|
|
@@ -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 { OINO_ERROR_PREFIX, OINODbConfig, OINONumberDataField, OINODB_UNDEFINED } from "./index.js";
|
|
6
|
+
import { OINO_ERROR_PREFIX, OINODbConfig, OINONumberDataField, OINOLog, OINODB_UNDEFINED } from "./index.js";
|
|
7
7
|
/**
|
|
8
8
|
* OINO Datamodel object for representing one database table and it's columns.
|
|
9
9
|
*
|
|
@@ -231,7 +231,7 @@ export class OINODbDataModel {
|
|
|
231
231
|
const order_sql = params.order?.toSql(this) || "";
|
|
232
232
|
const limit_sql = params.limit?.toSql(this) || "";
|
|
233
233
|
const filter_sql = params.filter?.toSql(this) || "";
|
|
234
|
-
const
|
|
234
|
+
const groupby_sql = params.aggregate?.toSql(this, params.select) || "";
|
|
235
235
|
let where_sql = "";
|
|
236
236
|
// OINOLog.debug("OINODbDataModel.printSqlSelect", {order_sql:order_sql, limit_sql:limit_sql, filter_sql:filter_sql, aggregate_sql:aggregate_sql})
|
|
237
237
|
if ((id != null) && (id != "") && (filter_sql != "")) {
|
|
@@ -243,8 +243,8 @@ export class OINODbDataModel {
|
|
|
243
243
|
else if (filter_sql != "") {
|
|
244
244
|
where_sql = filter_sql;
|
|
245
245
|
}
|
|
246
|
-
const result = this.api.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql,
|
|
247
|
-
|
|
246
|
+
const result = this.api.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql, groupby_sql);
|
|
247
|
+
OINOLog.debug("OINODbDataModel.printSqlSelect", { result: result });
|
|
248
248
|
return result;
|
|
249
249
|
}
|
|
250
250
|
/**
|
|
@@ -36,7 +36,7 @@ export class OINODbModelSet {
|
|
|
36
36
|
_encodeAndHashFieldValue(field, value, contentType, primaryKeyValues, rowIdSeed) {
|
|
37
37
|
let result;
|
|
38
38
|
if (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) {
|
|
39
|
-
if (value && (field instanceof OINONumberDataField) && (this.datamodel.api.hashid)) {
|
|
39
|
+
if (value && (field instanceof OINONumberDataField) && (this.datamodel.api.hashid) && ((this.sqlParams?.aggregate === undefined) || (this.sqlParams.aggregate.isAggregated(field) == false))) {
|
|
40
40
|
value = this.datamodel.api.hashid.encode(value, rowIdSeed);
|
|
41
41
|
}
|
|
42
42
|
if (field.fieldParams.isPrimaryKey) {
|
|
@@ -392,16 +392,18 @@ export class OINODbSqlAggregate {
|
|
|
392
392
|
* Print non-aggregated fields as SQL GROUP BY-condition based on the datamodel of the API.
|
|
393
393
|
*
|
|
394
394
|
* @param dataModel data model (and database) to use for formatting of values
|
|
395
|
+
* @param select what fields to select
|
|
395
396
|
*
|
|
396
397
|
*/
|
|
397
|
-
toSql(dataModel) {
|
|
398
|
+
toSql(dataModel, select) {
|
|
398
399
|
if (this.isEmpty()) {
|
|
399
400
|
return "";
|
|
400
401
|
}
|
|
401
402
|
let result = "";
|
|
402
403
|
for (let i = 0; i < dataModel.fields.length; i++) {
|
|
403
|
-
|
|
404
|
-
|
|
404
|
+
const f = dataModel.fields[i];
|
|
405
|
+
if (select?.isSelected(f) && (this._fields.includes(f.name) == false)) {
|
|
406
|
+
result += f.printSqlColumnName() + ",";
|
|
405
407
|
}
|
|
406
408
|
}
|
|
407
409
|
// OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
|
|
@@ -418,8 +420,8 @@ export class OINODbSqlAggregate {
|
|
|
418
420
|
let result = "";
|
|
419
421
|
for (let i = 0; i < dataModel.fields.length; i++) {
|
|
420
422
|
const f = dataModel.fields[i];
|
|
421
|
-
if (select?.isSelected(f) == false) { // if a field is not selected, we include
|
|
422
|
-
result += f.db.printSqlString(OINODB_UNDEFINED) + " as " + f.printSqlColumnName() + ",";
|
|
423
|
+
if (select?.isSelected(f) == false) { // if a field is not selected, we include an aggregated constant (min of const string) and correct fieldname instead so that dimensions of the data don't change, it does not need a group by but no unnecessary data is fetched
|
|
424
|
+
result += OINODbSqlAggregateFunctions.min + "(" + f.db.printSqlString(OINODB_UNDEFINED) + ") as " + f.printSqlColumnName() + ",";
|
|
423
425
|
}
|
|
424
426
|
else {
|
|
425
427
|
const aggregate_index = this._fields.indexOf(f.name);
|
|
@@ -188,9 +188,10 @@ export declare class OINODbSqlAggregate {
|
|
|
188
188
|
* Print non-aggregated fields as SQL GROUP BY-condition based on the datamodel of the API.
|
|
189
189
|
*
|
|
190
190
|
* @param dataModel data model (and database) to use for formatting of values
|
|
191
|
+
* @param select what fields to select
|
|
191
192
|
*
|
|
192
193
|
*/
|
|
193
|
-
toSql(dataModel: OINODbDataModel): string;
|
|
194
|
+
toSql(dataModel: OINODbDataModel, select?: OINODbSqlSelect): string;
|
|
194
195
|
/**
|
|
195
196
|
* Print non-aggregated fields as SQL GROUP BY-condition based on the datamodel of the API.
|
|
196
197
|
*
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@oino-ts/db",
|
|
3
|
-
"version": "0.4.
|
|
3
|
+
"version": "0.4.2",
|
|
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,12 +19,12 @@
|
|
|
19
19
|
"module": "./dist/esm/index.js",
|
|
20
20
|
"types": "./dist/types/index.d.ts",
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@oino-ts/common": "0.4.
|
|
22
|
+
"@oino-ts/common": "0.4.2"
|
|
23
23
|
},
|
|
24
24
|
"devDependencies": {
|
|
25
25
|
"@types/node": "^20.14.10",
|
|
26
26
|
"@types/bun": "^1.1.14",
|
|
27
|
-
"@oino-ts/types": "0.4.
|
|
27
|
+
"@oino-ts/types": "0.4.2",
|
|
28
28
|
"typedoc": "^0.25.13"
|
|
29
29
|
},
|
|
30
30
|
"files": [
|
package/src/OINODbDataModel.ts
CHANGED
|
@@ -243,7 +243,7 @@ export class OINODbDataModel {
|
|
|
243
243
|
const order_sql = params.order?.toSql(this) || ""
|
|
244
244
|
const limit_sql = params.limit?.toSql(this) || ""
|
|
245
245
|
const filter_sql = params.filter?.toSql(this) || ""
|
|
246
|
-
const
|
|
246
|
+
const groupby_sql = params.aggregate?.toSql(this, params.select) || ""
|
|
247
247
|
|
|
248
248
|
let where_sql = ""
|
|
249
249
|
// OINOLog.debug("OINODbDataModel.printSqlSelect", {order_sql:order_sql, limit_sql:limit_sql, filter_sql:filter_sql, aggregate_sql:aggregate_sql})
|
|
@@ -254,8 +254,8 @@ export class OINODbDataModel {
|
|
|
254
254
|
} else if (filter_sql != "") {
|
|
255
255
|
where_sql = filter_sql
|
|
256
256
|
}
|
|
257
|
-
const result = this.api.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql,
|
|
258
|
-
|
|
257
|
+
const result = this.api.db.printSqlSelect(this.api.params.tableName, column_names, where_sql, order_sql, limit_sql, groupby_sql)
|
|
258
|
+
OINOLog.debug("OINODbDataModel.printSqlSelect", {result:result})
|
|
259
259
|
return result;
|
|
260
260
|
}
|
|
261
261
|
|
package/src/OINODbModelSet.ts
CHANGED
|
@@ -44,7 +44,7 @@ export class OINODbModelSet {
|
|
|
44
44
|
private _encodeAndHashFieldValue(field:OINODbDataField, value:string|null, contentType:OINOContentType, primaryKeyValues:string[], rowIdSeed:string):string {
|
|
45
45
|
let result:string
|
|
46
46
|
if (field.fieldParams.isPrimaryKey || field.fieldParams.isForeignKey) {
|
|
47
|
-
if (value && (field instanceof OINONumberDataField) && (this.datamodel.api.hashid)) {
|
|
47
|
+
if (value && (field instanceof OINONumberDataField) && (this.datamodel.api.hashid) && ((this.sqlParams?.aggregate === undefined) || (this.sqlParams.aggregate.isAggregated(field) == false))) {
|
|
48
48
|
value = this.datamodel.api.hashid.encode(value, rowIdSeed)
|
|
49
49
|
}
|
|
50
50
|
if (field.fieldParams.isPrimaryKey) {
|
package/src/OINODbSqlParams.ts
CHANGED
|
@@ -397,16 +397,18 @@ export class OINODbSqlAggregate {
|
|
|
397
397
|
* Print non-aggregated fields as SQL GROUP BY-condition based on the datamodel of the API.
|
|
398
398
|
*
|
|
399
399
|
* @param dataModel data model (and database) to use for formatting of values
|
|
400
|
+
* @param select what fields to select
|
|
400
401
|
*
|
|
401
402
|
*/
|
|
402
|
-
toSql(dataModel:OINODbDataModel):string {
|
|
403
|
+
toSql(dataModel:OINODbDataModel, select?:OINODbSqlSelect):string {
|
|
403
404
|
if (this.isEmpty()) {
|
|
404
405
|
return ""
|
|
405
406
|
}
|
|
406
407
|
let result:string = ""
|
|
407
408
|
for (let i=0; i<dataModel.fields.length; i++) {
|
|
408
|
-
|
|
409
|
-
|
|
409
|
+
const f = dataModel.fields[i]
|
|
410
|
+
if (select?.isSelected(f) && (this._fields.includes(f.name) == false)) {
|
|
411
|
+
result += f.printSqlColumnName() + ","
|
|
410
412
|
}
|
|
411
413
|
}
|
|
412
414
|
// OINOLog.debug("OINODbSqlAggregate.toSql", {result:result})
|
|
@@ -424,8 +426,8 @@ export class OINODbSqlAggregate {
|
|
|
424
426
|
let result:string = ""
|
|
425
427
|
for (let i=0; i<dataModel.fields.length; i++) {
|
|
426
428
|
const f:OINODbDataField = dataModel.fields[i]
|
|
427
|
-
if (select?.isSelected(f)==false) { // if a field is not selected, we include
|
|
428
|
-
result += f.db.printSqlString(OINODB_UNDEFINED) + " as " + f.printSqlColumnName()+","
|
|
429
|
+
if (select?.isSelected(f)==false) { // if a field is not selected, we include an aggregated constant (min of const string) and correct fieldname instead so that dimensions of the data don't change, it does not need a group by but no unnecessary data is fetched
|
|
430
|
+
result += OINODbSqlAggregateFunctions.min + "(" + f.db.printSqlString(OINODB_UNDEFINED) + ") as " + f.printSqlColumnName()+","
|
|
429
431
|
|
|
430
432
|
} else {
|
|
431
433
|
const aggregate_index = this._fields.indexOf(f.name)
|