@sqb/connect 4.14.0 → 4.15.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/cjs/client/sqb-connection.js +1 -1
- package/cjs/orm/repository.class.js +77 -19
- package/esm/client/adapter.js +2 -1
- package/esm/client/cursor-stream.js +9 -4
- package/esm/client/cursor.js +22 -17
- package/esm/client/extensions.js +5 -1
- package/esm/client/field-info-map.js +5 -1
- package/esm/client/helpers.js +19 -12
- package/esm/client/sqb-client.js +32 -27
- package/esm/client/sqb-connection.js +42 -37
- package/esm/client/types.js +5 -1
- package/esm/index.js +40 -32
- package/esm/orm/backward.js +19 -12
- package/esm/orm/base-entity.js +10 -6
- package/esm/orm/commands/command.helper.js +34 -28
- package/esm/orm/commands/count.command.js +10 -6
- package/esm/orm/commands/create.command.js +18 -14
- package/esm/orm/commands/delete.command.js +10 -6
- package/esm/orm/commands/find.command.js +34 -30
- package/esm/orm/commands/row-converter.js +7 -3
- package/esm/orm/commands/update.command.js +18 -14
- package/esm/orm/decorators/column.decorator.js +10 -7
- package/esm/orm/decorators/embedded.decorator.js +7 -4
- package/esm/orm/decorators/entity.decorator.js +56 -53
- package/esm/orm/decorators/events.decorator.js +27 -19
- package/esm/orm/decorators/foreignkey.decorator.js +7 -4
- package/esm/orm/decorators/index.decorator.js +9 -6
- package/esm/orm/decorators/link.decorator.js +11 -8
- package/esm/orm/decorators/primarykey.decorator.js +11 -8
- package/esm/orm/decorators/transform.decorator.js +11 -7
- package/esm/orm/model/association-field-metadata.js +8 -4
- package/esm/orm/model/association-node.js +6 -2
- package/esm/orm/model/association.js +20 -16
- package/esm/orm/model/column-field-metadata.js +8 -4
- package/esm/orm/model/embedded-field-metadata.js +7 -4
- package/esm/orm/model/entity-metadata.js +50 -47
- package/esm/orm/model/field-metadata.js +2 -1
- package/esm/orm/model/index-metadata.js +2 -1
- package/esm/orm/model/link-chain.js +8 -4
- package/esm/orm/orm.const.js +6 -3
- package/esm/orm/orm.type.js +2 -1
- package/esm/orm/repository.class.js +103 -41
- package/esm/orm/util/apply-mixins.js +4 -1
- package/esm/orm/util/extract-keyvalues.js +9 -6
- package/esm/orm/util/orm.helper.js +20 -10
- package/esm/orm/util/parse-fields-projection.js +11 -6
- package/esm/orm/util/serialize-field.js +13 -10
- package/package.json +30 -24
- package/types/orm/repository.class.d.ts +110 -11
- package/cjs/package.json +0 -3
package/esm/index.js
CHANGED
|
@@ -1,32 +1,40 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.isEntityClass = exports.isEmbeddedField = exports.isColumnField = exports.isAssociationField = void 0;
|
|
4
|
+
const tslib_1 = require("tslib");
|
|
5
|
+
require("reflect-metadata");
|
|
6
|
+
tslib_1.__exportStar(require("./client/adapter.js"), exports);
|
|
7
|
+
tslib_1.__exportStar(require("./client/cursor.js"), exports);
|
|
8
|
+
tslib_1.__exportStar(require("./client/extensions.js"), exports);
|
|
9
|
+
tslib_1.__exportStar(require("./client/sqb-client.js"), exports);
|
|
10
|
+
tslib_1.__exportStar(require("./client/sqb-connection.js"), exports);
|
|
11
|
+
tslib_1.__exportStar(require("./client/types.js"), exports);
|
|
12
|
+
tslib_1.__exportStar(require("./orm/backward.js"), exports);
|
|
13
|
+
tslib_1.__exportStar(require("./orm/base-entity.js"), exports);
|
|
14
|
+
tslib_1.__exportStar(require("./orm/decorators/column.decorator.js"), exports);
|
|
15
|
+
tslib_1.__exportStar(require("./orm/decorators/embedded.decorator.js"), exports);
|
|
16
|
+
tslib_1.__exportStar(require("./orm/decorators/entity.decorator.js"), exports);
|
|
17
|
+
tslib_1.__exportStar(require("./orm/decorators/events.decorator.js"), exports);
|
|
18
|
+
tslib_1.__exportStar(require("./orm/decorators/foreignkey.decorator.js"), exports);
|
|
19
|
+
tslib_1.__exportStar(require("./orm/decorators/index.decorator.js"), exports);
|
|
20
|
+
tslib_1.__exportStar(require("./orm/decorators/link.decorator.js"), exports);
|
|
21
|
+
tslib_1.__exportStar(require("./orm/decorators/primarykey.decorator.js"), exports);
|
|
22
|
+
tslib_1.__exportStar(require("./orm/decorators/transform.decorator.js"), exports);
|
|
23
|
+
tslib_1.__exportStar(require("./orm/model/association.js"), exports);
|
|
24
|
+
tslib_1.__exportStar(require("./orm/model/association-field-metadata.js"), exports);
|
|
25
|
+
tslib_1.__exportStar(require("./orm/model/association-node.js"), exports);
|
|
26
|
+
tslib_1.__exportStar(require("./orm/model/column-field-metadata.js"), exports);
|
|
27
|
+
tslib_1.__exportStar(require("./orm/model/embedded-field-metadata.js"), exports);
|
|
28
|
+
tslib_1.__exportStar(require("./orm/model/entity-metadata.js"), exports);
|
|
29
|
+
tslib_1.__exportStar(require("./orm/model/field-metadata.js"), exports);
|
|
30
|
+
tslib_1.__exportStar(require("./orm/model/index-metadata.js"), exports);
|
|
31
|
+
tslib_1.__exportStar(require("./orm/model/link-chain.js"), exports);
|
|
32
|
+
tslib_1.__exportStar(require("./orm/orm.const.js"), exports);
|
|
33
|
+
tslib_1.__exportStar(require("./orm/orm.type.js"), exports);
|
|
34
|
+
tslib_1.__exportStar(require("./orm/repository.class.js"), exports);
|
|
35
|
+
var orm_helper_js_1 = require("./orm/util/orm.helper.js");
|
|
36
|
+
Object.defineProperty(exports, "isAssociationField", { enumerable: true, get: function () { return orm_helper_js_1.isAssociationField; } });
|
|
37
|
+
Object.defineProperty(exports, "isColumnField", { enumerable: true, get: function () { return orm_helper_js_1.isColumnField; } });
|
|
38
|
+
Object.defineProperty(exports, "isEmbeddedField", { enumerable: true, get: function () { return orm_helper_js_1.isEmbeddedField; } });
|
|
39
|
+
Object.defineProperty(exports, "isEntityClass", { enumerable: true, get: function () { return orm_helper_js_1.isEntityClass; } });
|
|
40
|
+
tslib_1.__exportStar(require("./orm/util/parse-fields-projection.js"), exports);
|
package/esm/orm/backward.js
CHANGED
|
@@ -1,17 +1,24 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UnionEntity = exports.PickEntity = exports.OmitEntity = void 0;
|
|
4
|
+
exports.getInsertColumnNames = getInsertColumnNames;
|
|
5
|
+
exports.getUpdateColumnNames = getUpdateColumnNames;
|
|
6
|
+
exports.getNonAssociationElementNames = getNonAssociationElementNames;
|
|
7
|
+
exports.mixinEntities = mixinEntities;
|
|
8
|
+
const entity_decorator_js_1 = require("./decorators/entity.decorator.js");
|
|
9
|
+
function getInsertColumnNames(ctor) {
|
|
10
|
+
return entity_decorator_js_1.Entity.getInsertColumnNames(ctor);
|
|
4
11
|
}
|
|
5
|
-
|
|
6
|
-
return Entity.getUpdateColumnNames(ctor);
|
|
12
|
+
function getUpdateColumnNames(ctor) {
|
|
13
|
+
return entity_decorator_js_1.Entity.getUpdateColumnNames(ctor);
|
|
7
14
|
}
|
|
8
|
-
|
|
9
|
-
return Entity.getNonAssociationFieldNames(ctor);
|
|
15
|
+
function getNonAssociationElementNames(ctor) {
|
|
16
|
+
return entity_decorator_js_1.Entity.getNonAssociationFieldNames(ctor);
|
|
10
17
|
}
|
|
11
|
-
|
|
18
|
+
function mixinEntities(derivedCtor, ...bases) {
|
|
12
19
|
// @ts-ignore
|
|
13
|
-
return Entity.mixin(derivedCtor, ...bases);
|
|
20
|
+
return entity_decorator_js_1.Entity.mixin(derivedCtor, ...bases);
|
|
14
21
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
22
|
+
exports.OmitEntity = entity_decorator_js_1.Entity.Omit;
|
|
23
|
+
exports.PickEntity = entity_decorator_js_1.Entity.Pick;
|
|
24
|
+
exports.UnionEntity = entity_decorator_js_1.Entity.Union;
|
package/esm/orm/base-entity.js
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BaseEntity = void 0;
|
|
4
|
+
const entity_decorator_js_1 = require("./decorators/entity.decorator.js");
|
|
5
|
+
const orm_const_js_1 = require("./orm.const.js");
|
|
6
|
+
class BaseEntity {
|
|
4
7
|
constructor(partial) {
|
|
5
|
-
const fields = Entity.getColumnFieldNames(Object.getPrototypeOf(this).constructor);
|
|
8
|
+
const fields = entity_decorator_js_1.Entity.getColumnFieldNames(Object.getPrototypeOf(this).constructor);
|
|
6
9
|
if (fields && partial) {
|
|
7
10
|
for (const k of fields)
|
|
8
11
|
if (partial[k] !== undefined)
|
|
@@ -10,11 +13,11 @@ export class BaseEntity {
|
|
|
10
13
|
}
|
|
11
14
|
}
|
|
12
15
|
async destroy() {
|
|
13
|
-
const repo = this[REPOSITORY_KEY];
|
|
16
|
+
const repo = this[orm_const_js_1.REPOSITORY_KEY];
|
|
14
17
|
return !!(repo && repo.destroy(this));
|
|
15
18
|
}
|
|
16
19
|
async exists() {
|
|
17
|
-
const repo = this[REPOSITORY_KEY];
|
|
20
|
+
const repo = this[orm_const_js_1.REPOSITORY_KEY];
|
|
18
21
|
return !!(repo && repo.exists(this));
|
|
19
22
|
}
|
|
20
23
|
toJSON() {
|
|
@@ -22,3 +25,4 @@ export class BaseEntity {
|
|
|
22
25
|
return this;
|
|
23
26
|
}
|
|
24
27
|
}
|
|
28
|
+
exports.BaseEntity = BaseEntity;
|
|
@@ -1,16 +1,22 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.joinAssociationGetFirst = joinAssociationGetFirst;
|
|
4
|
+
exports.joinAssociationGetLast = joinAssociationGetLast;
|
|
5
|
+
exports.joinAssociation = joinAssociation;
|
|
6
|
+
exports.prepareFilter = prepareFilter;
|
|
7
|
+
const builder_1 = require("@sqb/builder");
|
|
8
|
+
const embedded_field_metadata_js_1 = require("../model/embedded-field-metadata.js");
|
|
9
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
10
|
+
const orm_helper_js_1 = require("../util/orm.helper.js");
|
|
11
|
+
async function joinAssociationGetFirst(joinInfos, association, parentAlias, innerJoin) {
|
|
6
12
|
const joins = await joinAssociation(joinInfos, association, parentAlias, innerJoin);
|
|
7
13
|
return joins[0];
|
|
8
14
|
}
|
|
9
|
-
|
|
15
|
+
async function joinAssociationGetLast(joinInfos, association, parentAlias, innerJoin) {
|
|
10
16
|
const joins = await joinAssociation(joinInfos, association, parentAlias, innerJoin);
|
|
11
17
|
return joins[joins.length - 1];
|
|
12
18
|
}
|
|
13
|
-
|
|
19
|
+
async function joinAssociation(joinInfos, association, parentAlias, innerJoin) {
|
|
14
20
|
let joinInfo;
|
|
15
21
|
let node = association;
|
|
16
22
|
const result = [];
|
|
@@ -23,9 +29,9 @@ export async function joinAssociation(joinInfos, association, parentAlias, inner
|
|
|
23
29
|
const targetCol = await node.resolveTargetProperty();
|
|
24
30
|
const joinAlias = 'J' + (joinInfos.length + 1);
|
|
25
31
|
const join = innerJoin
|
|
26
|
-
? InnerJoin(targetEntity.tableName + ' as ' + joinAlias)
|
|
27
|
-
: LeftOuterJoin(targetEntity.tableName + ' as ' + joinAlias);
|
|
28
|
-
join.on(Eq(Field(joinAlias + '.' + targetCol.fieldName, targetCol.dataType, targetCol.isArray), Field(parentAlias + '.' + keyCol.fieldName, keyCol.dataType, keyCol.isArray)));
|
|
32
|
+
? (0, builder_1.InnerJoin)(targetEntity.tableName + ' as ' + joinAlias)
|
|
33
|
+
: (0, builder_1.LeftOuterJoin)(targetEntity.tableName + ' as ' + joinAlias);
|
|
34
|
+
join.on((0, builder_1.Eq)((0, builder_1.Field)(joinAlias + '.' + targetCol.fieldName, targetCol.dataType, targetCol.isArray), (0, builder_1.Field)(parentAlias + '.' + keyCol.fieldName, keyCol.dataType, keyCol.isArray)));
|
|
29
35
|
if (node.conditions)
|
|
30
36
|
await prepareFilter(targetEntity, node.conditions, join._conditions, joinAlias);
|
|
31
37
|
joinInfo = {
|
|
@@ -45,12 +51,12 @@ export async function joinAssociation(joinInfos, association, parentAlias, inner
|
|
|
45
51
|
}
|
|
46
52
|
return result;
|
|
47
53
|
}
|
|
48
|
-
|
|
54
|
+
async function prepareFilter(entityDef, filter, trgOp, tableAlias = 'T') {
|
|
49
55
|
let srcOp;
|
|
50
|
-
if (isLogicalOperator(filter) && filter._operatorType === trgOp._operatorType)
|
|
56
|
+
if ((0, builder_1.isLogicalOperator)(filter) && filter._operatorType === trgOp._operatorType)
|
|
51
57
|
srcOp = filter;
|
|
52
58
|
else {
|
|
53
|
-
srcOp = And();
|
|
59
|
+
srcOp = (0, builder_1.And)();
|
|
54
60
|
if (Array.isArray(filter))
|
|
55
61
|
srcOp.add(...filter);
|
|
56
62
|
else
|
|
@@ -58,14 +64,14 @@ export async function prepareFilter(entityDef, filter, trgOp, tableAlias = 'T')
|
|
|
58
64
|
}
|
|
59
65
|
const associationPathCache = {};
|
|
60
66
|
for (const item of srcOp._items) {
|
|
61
|
-
if (isLogicalOperator(item)) {
|
|
67
|
+
if ((0, builder_1.isLogicalOperator)(item)) {
|
|
62
68
|
const ctor = Object.getPrototypeOf(item).constructor;
|
|
63
69
|
const logOp = new ctor();
|
|
64
70
|
await prepareFilter(entityDef, item, logOp, tableAlias);
|
|
65
71
|
trgOp.add(logOp);
|
|
66
72
|
continue;
|
|
67
73
|
}
|
|
68
|
-
if (isCompOperator(item)) {
|
|
74
|
+
if ((0, builder_1.isCompOperator)(item)) {
|
|
69
75
|
if (typeof item._left === 'string') {
|
|
70
76
|
const itemPath = item._left.split('.');
|
|
71
77
|
const l = itemPath.length;
|
|
@@ -91,38 +97,38 @@ export async function prepareFilter(entityDef, filter, trgOp, tableAlias = 'T')
|
|
|
91
97
|
}
|
|
92
98
|
for (i; i < l; i++) {
|
|
93
99
|
pt = itemPath[i];
|
|
94
|
-
const col = EntityMetadata.getField(_curEntity, pt);
|
|
100
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(_curEntity, pt);
|
|
95
101
|
if (!col)
|
|
96
102
|
throw new Error(`Unknown property (${item._left}) defined in filter`);
|
|
97
103
|
/** if last item on path */
|
|
98
104
|
if (i === l - 1) {
|
|
99
|
-
if (!isColumnField(col))
|
|
105
|
+
if (!(0, orm_helper_js_1.isColumnField)(col))
|
|
100
106
|
throw new Error(`Invalid column expression (${item._left}) defined in filter`);
|
|
101
107
|
const ctor = Object.getPrototypeOf(item).constructor;
|
|
102
|
-
currentOp.add(new ctor(Field(_curAlias + '.' + _curPrefix + col.fieldName + _curSuffix, col.dataType, col.isArray), item._right));
|
|
108
|
+
currentOp.add(new ctor((0, builder_1.Field)(_curAlias + '.' + _curPrefix + col.fieldName + _curSuffix, col.dataType, col.isArray), item._right));
|
|
103
109
|
}
|
|
104
110
|
else {
|
|
105
111
|
/** if not last item on path */
|
|
106
|
-
if (isColumnField(col))
|
|
112
|
+
if ((0, orm_helper_js_1.isColumnField)(col))
|
|
107
113
|
throw new Error(`Invalid column (${item._left}) defined in filter`);
|
|
108
|
-
if (isEmbeddedField(col)) {
|
|
109
|
-
_curEntity = await EmbeddedFieldMetadata.resolveType(col);
|
|
114
|
+
if ((0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
115
|
+
_curEntity = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
110
116
|
_curPrefix = _curPrefix + (col.fieldNamePrefix || '');
|
|
111
117
|
_curSuffix = (col.fieldNameSuffix || '') + _curSuffix;
|
|
112
118
|
continue;
|
|
113
119
|
}
|
|
114
|
-
if (!isAssociationField(col))
|
|
120
|
+
if (!(0, orm_helper_js_1.isAssociationField)(col))
|
|
115
121
|
throw new Error(`Invalid column (${item._left}) defined in filter`);
|
|
116
122
|
let node;
|
|
117
123
|
_curEntity = await col.association.resolveTarget();
|
|
118
124
|
if (!subSelect) {
|
|
119
125
|
const keyCol = await col.association.resolveSourceProperty();
|
|
120
126
|
const targetCol = await col.association.resolveTargetProperty();
|
|
121
|
-
subSelect = Select(Raw('1')).from(_curEntity.tableName + ' K');
|
|
122
|
-
subSelect.where(Eq(Field('K.' + targetCol.fieldName, targetCol.dataType, targetCol.isArray), Field(tableAlias + '.' + keyCol.fieldName, keyCol.dataType, keyCol.isArray)));
|
|
123
|
-
currentOp.add(Exists(subSelect));
|
|
124
|
-
if (currentOp._operatorType !== OperatorType.and) {
|
|
125
|
-
currentOp = op.or();
|
|
127
|
+
subSelect = (0, builder_1.Select)((0, builder_1.Raw)('1')).from(_curEntity.tableName + ' K');
|
|
128
|
+
subSelect.where((0, builder_1.Eq)((0, builder_1.Field)('K.' + targetCol.fieldName, targetCol.dataType, targetCol.isArray), (0, builder_1.Field)(tableAlias + '.' + keyCol.fieldName, keyCol.dataType, keyCol.isArray)));
|
|
129
|
+
currentOp.add((0, builder_1.Exists)(subSelect));
|
|
130
|
+
if (currentOp._operatorType !== builder_1.OperatorType.and) {
|
|
131
|
+
currentOp = builder_1.op.or();
|
|
126
132
|
subSelect.where(currentOp);
|
|
127
133
|
}
|
|
128
134
|
else
|
|
@@ -140,7 +146,7 @@ export async function prepareFilter(entityDef, filter, trgOp, tableAlias = 'T')
|
|
|
140
146
|
const sourceColumn = await node.resolveSourceProperty();
|
|
141
147
|
const targetColumn = await node.resolveTargetProperty();
|
|
142
148
|
const joinAlias = 'J' + ((subSelect?._joins?.length || 0) + 1);
|
|
143
|
-
subSelect.join(InnerJoin(targetEntity.tableName + ' ' + joinAlias).on(Eq(Field(joinAlias + '.' + targetColumn.fieldName, targetColumn.dataType, targetColumn.isArray), Field(_curAlias + '.' + sourceColumn.fieldName, sourceColumn.dataType, sourceColumn.isArray))));
|
|
149
|
+
subSelect.join((0, builder_1.InnerJoin)(targetEntity.tableName + ' ' + joinAlias).on((0, builder_1.Eq)((0, builder_1.Field)(joinAlias + '.' + targetColumn.fieldName, targetColumn.dataType, targetColumn.isArray), (0, builder_1.Field)(_curAlias + '.' + sourceColumn.fieldName, sourceColumn.dataType, sourceColumn.isArray))));
|
|
144
150
|
_curEntity = targetEntity;
|
|
145
151
|
_curAlias = joinAlias;
|
|
146
152
|
node = node.next;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CountCommand = void 0;
|
|
4
|
+
const builder_1 = require("@sqb/builder");
|
|
5
|
+
const command_helper_js_1 = require("./command.helper.js");
|
|
6
|
+
class CountCommand {
|
|
4
7
|
// istanbul ignore next
|
|
5
8
|
constructor() {
|
|
6
9
|
throw new Error('This class is abstract');
|
|
@@ -9,10 +12,10 @@ export class CountCommand {
|
|
|
9
12
|
const { connection, entity, filter, params } = args;
|
|
10
13
|
let where;
|
|
11
14
|
if (filter) {
|
|
12
|
-
where = And();
|
|
13
|
-
await prepareFilter(entity, filter, where, 'T');
|
|
15
|
+
where = (0, builder_1.And)();
|
|
16
|
+
await (0, command_helper_js_1.prepareFilter)(entity, filter, where, 'T');
|
|
14
17
|
}
|
|
15
|
-
const query = Select(Count()).from(entity.tableName + ' T');
|
|
18
|
+
const query = (0, builder_1.Select)((0, builder_1.Count)()).from(entity.tableName + ' T');
|
|
16
19
|
if (where)
|
|
17
20
|
query.where(where);
|
|
18
21
|
// Execute query
|
|
@@ -24,3 +27,4 @@ export class CountCommand {
|
|
|
24
27
|
return (resp && resp.rows && resp.rows[0][0]) || 0;
|
|
25
28
|
}
|
|
26
29
|
}
|
|
30
|
+
exports.CountCommand = CountCommand;
|
|
@@ -1,9 +1,12 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CreateCommand = void 0;
|
|
4
|
+
const builder_1 = require("@sqb/builder");
|
|
5
|
+
const column_field_metadata_js_1 = require("../model/column-field-metadata.js");
|
|
6
|
+
const embedded_field_metadata_js_1 = require("../model/embedded-field-metadata.js");
|
|
7
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
8
|
+
const orm_helper_js_1 = require("../util/orm.helper.js");
|
|
9
|
+
class CreateCommand {
|
|
7
10
|
// istanbul ignore next
|
|
8
11
|
constructor() {
|
|
9
12
|
throw new Error('This class is abstract');
|
|
@@ -24,9 +27,9 @@ export class CreateCommand {
|
|
|
24
27
|
await this._prepareParams(ctx, entity, args.values);
|
|
25
28
|
if (!ctx.colCount)
|
|
26
29
|
throw new Error('No field given to create new entity instance');
|
|
27
|
-
const query = Insert(tableName, ctx.queryValues);
|
|
30
|
+
const query = (0, builder_1.Insert)(tableName, ctx.queryValues);
|
|
28
31
|
if (args.returning) {
|
|
29
|
-
const primaryIndexColumns = EntityMetadata.getPrimaryIndexColumns(entity);
|
|
32
|
+
const primaryIndexColumns = entity_metadata_js_1.EntityMetadata.getPrimaryIndexColumns(entity);
|
|
30
33
|
if (primaryIndexColumns.length)
|
|
31
34
|
query.returning(...primaryIndexColumns.map(col => col.fieldName));
|
|
32
35
|
}
|
|
@@ -38,7 +41,7 @@ export class CreateCommand {
|
|
|
38
41
|
if (args.returning && qr.fields && qr.rows?.length) {
|
|
39
42
|
const keyValues = {};
|
|
40
43
|
for (const f of qr.fields.values()) {
|
|
41
|
-
const el = EntityMetadata.getColumnFieldByFieldName(entity, f.fieldName);
|
|
44
|
+
const el = entity_metadata_js_1.EntityMetadata.getColumnFieldByFieldName(entity, f.fieldName);
|
|
42
45
|
if (el)
|
|
43
46
|
keyValues[el.name] = qr.rows[0][f.index];
|
|
44
47
|
}
|
|
@@ -51,7 +54,7 @@ export class CreateCommand {
|
|
|
51
54
|
suffix = suffix || '';
|
|
52
55
|
for (const col of Object.values(entity.fields)) {
|
|
53
56
|
v = values[col.name];
|
|
54
|
-
if (isColumnField(col)) {
|
|
57
|
+
if ((0, orm_helper_js_1.isColumnField)(col)) {
|
|
55
58
|
if (col.noInsert)
|
|
56
59
|
continue;
|
|
57
60
|
if (v == null && col.default !== undefined) {
|
|
@@ -65,10 +68,10 @@ export class CreateCommand {
|
|
|
65
68
|
}
|
|
66
69
|
continue;
|
|
67
70
|
}
|
|
68
|
-
ColumnFieldMetadata.checkEnumValue(col, v);
|
|
71
|
+
column_field_metadata_js_1.ColumnFieldMetadata.checkEnumValue(col, v);
|
|
69
72
|
const fieldName = prefix + col.fieldName + suffix;
|
|
70
73
|
const k = ('I$_' + fieldName).substring(0, 30);
|
|
71
|
-
ctx.queryValues[fieldName] = Param({
|
|
74
|
+
ctx.queryValues[fieldName] = (0, builder_1.Param)({
|
|
72
75
|
name: k,
|
|
73
76
|
dataType: col.dataType,
|
|
74
77
|
isArray: col.isArray,
|
|
@@ -76,10 +79,11 @@ export class CreateCommand {
|
|
|
76
79
|
ctx.queryParams[k] = v;
|
|
77
80
|
ctx.colCount++;
|
|
78
81
|
}
|
|
79
|
-
else if (v != null && isEmbeddedField(col)) {
|
|
80
|
-
const type = await EmbeddedFieldMetadata.resolveType(col);
|
|
82
|
+
else if (v != null && (0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
83
|
+
const type = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
81
84
|
await this._prepareParams(ctx, type, v, col.fieldNamePrefix, col.fieldNameSuffix);
|
|
82
85
|
}
|
|
83
86
|
}
|
|
84
87
|
}
|
|
85
88
|
}
|
|
89
|
+
exports.CreateCommand = CreateCommand;
|
|
@@ -1,6 +1,9 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DeleteCommand = void 0;
|
|
4
|
+
const builder_1 = require("@sqb/builder");
|
|
5
|
+
const command_helper_js_1 = require("./command.helper.js");
|
|
6
|
+
class DeleteCommand {
|
|
4
7
|
// istanbul ignore next
|
|
5
8
|
constructor() {
|
|
6
9
|
throw new Error('This class is abstract');
|
|
@@ -9,10 +12,10 @@ export class DeleteCommand {
|
|
|
9
12
|
const { connection, entity, filter, params } = args;
|
|
10
13
|
let where;
|
|
11
14
|
if (filter) {
|
|
12
|
-
where = And();
|
|
13
|
-
await prepareFilter(entity, filter, where);
|
|
15
|
+
where = (0, builder_1.And)();
|
|
16
|
+
await (0, command_helper_js_1.prepareFilter)(entity, filter, where);
|
|
14
17
|
}
|
|
15
|
-
const query = Delete(entity.tableName + ' T');
|
|
18
|
+
const query = (0, builder_1.Delete)(entity.tableName + ' T');
|
|
16
19
|
if (where)
|
|
17
20
|
query.where(...where._items);
|
|
18
21
|
// Execute query
|
|
@@ -24,3 +27,4 @@ export class DeleteCommand {
|
|
|
24
27
|
return (resp && resp.rowsAffected) || 0;
|
|
25
28
|
}
|
|
26
29
|
}
|
|
30
|
+
exports.DeleteCommand = DeleteCommand;
|
|
@@ -1,13 +1,16 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.FindCommand = void 0;
|
|
4
|
+
const builder_1 = require("@sqb/builder");
|
|
5
|
+
const association_node_js_1 = require("../model/association-node.js");
|
|
6
|
+
const embedded_field_metadata_js_1 = require("../model/embedded-field-metadata.js");
|
|
7
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
8
|
+
const orm_helper_js_1 = require("../util/orm.helper.js");
|
|
9
|
+
const parse_fields_projection_js_1 = require("../util/parse-fields-projection.js");
|
|
10
|
+
const command_helper_js_1 = require("./command.helper.js");
|
|
11
|
+
const row_converter_js_1 = require("./row-converter.js");
|
|
9
12
|
const SORT_ORDER_PATTERN = /^([-+])?(.*)$/;
|
|
10
|
-
|
|
13
|
+
class FindCommand {
|
|
11
14
|
constructor(selectEntity, outputEntity) {
|
|
12
15
|
this.maxEagerFetch = 100000;
|
|
13
16
|
this.maxSubQueries = 5;
|
|
@@ -15,15 +18,15 @@ export class FindCommand {
|
|
|
15
18
|
this.resultAlias = 'T';
|
|
16
19
|
this._joins = [];
|
|
17
20
|
this._selectColumns = {};
|
|
18
|
-
this._filter = And();
|
|
21
|
+
this._filter = (0, builder_1.And)();
|
|
19
22
|
this.mainEntity = selectEntity;
|
|
20
23
|
this.resultEntity = outputEntity;
|
|
21
|
-
this.converter = new RowConverter(outputEntity.ctor);
|
|
24
|
+
this.converter = new row_converter_js_1.RowConverter(outputEntity.ctor);
|
|
22
25
|
}
|
|
23
26
|
static async create(source, opts = {}) {
|
|
24
27
|
let command;
|
|
25
28
|
let listingEntity;
|
|
26
|
-
if (source instanceof AssociationNode) {
|
|
29
|
+
if (source instanceof association_node_js_1.AssociationNode) {
|
|
27
30
|
const node = source;
|
|
28
31
|
listingEntity = await node.resolveTarget();
|
|
29
32
|
const resultEntity = await node.getLast().resolveTarget();
|
|
@@ -31,7 +34,7 @@ export class FindCommand {
|
|
|
31
34
|
if (node.conditions)
|
|
32
35
|
await command.filter(node.conditions);
|
|
33
36
|
if (node.next) {
|
|
34
|
-
const join = await joinAssociationGetLast(command._joins, node.next, command.mainAlias);
|
|
37
|
+
const join = await (0, command_helper_js_1.joinAssociationGetLast)(command._joins, node.next, command.mainAlias);
|
|
35
38
|
command.resultAlias = join.joinAlias;
|
|
36
39
|
}
|
|
37
40
|
}
|
|
@@ -67,14 +70,14 @@ export class FindCommand {
|
|
|
67
70
|
const entity = opts.entity || this._getEntityFromAlias(tableAlias);
|
|
68
71
|
const converter = opts.converter || this.converter;
|
|
69
72
|
const projection = typeof opts.projection === 'string' || Array.isArray(opts.projection)
|
|
70
|
-
? parseFieldsProjection(opts.projection)
|
|
73
|
+
? (0, parse_fields_projection_js_1.parseFieldsProjection)(opts.projection)
|
|
71
74
|
: opts.projection;
|
|
72
75
|
const defaultFields = !projection || !Object.values(projection).find(p => !p.sign);
|
|
73
76
|
const sortFields = opts.sort && opts.sort.length ? opts.sort.map(x => x.toLowerCase()) : undefined;
|
|
74
77
|
const prefix = opts.prefix || '';
|
|
75
78
|
const suffix = opts.suffix || '';
|
|
76
79
|
for (const key of Object.keys(entity.fields)) {
|
|
77
|
-
const col = EntityMetadata.getField(entity, key);
|
|
80
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(entity, key);
|
|
78
81
|
if (!col || col.hidden)
|
|
79
82
|
continue;
|
|
80
83
|
const colNameLower = col.name.toLowerCase();
|
|
@@ -89,7 +92,7 @@ export class FindCommand {
|
|
|
89
92
|
continue;
|
|
90
93
|
}
|
|
91
94
|
// Add field to select list if field is a column
|
|
92
|
-
if (isColumnField(col)) {
|
|
95
|
+
if ((0, orm_helper_js_1.isColumnField)(col)) {
|
|
93
96
|
const fieldAlias = this._selectColumn(tableAlias, col, prefix, suffix);
|
|
94
97
|
// Add column to converter
|
|
95
98
|
converter.addValueProperty({
|
|
@@ -100,8 +103,8 @@ export class FindCommand {
|
|
|
100
103
|
});
|
|
101
104
|
continue;
|
|
102
105
|
}
|
|
103
|
-
if (isEmbeddedField(col)) {
|
|
104
|
-
const typ = await EmbeddedFieldMetadata.resolveType(col);
|
|
106
|
+
if ((0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
107
|
+
const typ = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
105
108
|
const subConverter = converter.addObjectProperty({
|
|
106
109
|
name: col.name,
|
|
107
110
|
type: typ.ctor,
|
|
@@ -117,10 +120,10 @@ export class FindCommand {
|
|
|
117
120
|
});
|
|
118
121
|
continue;
|
|
119
122
|
}
|
|
120
|
-
if (isAssociationField(col)) {
|
|
123
|
+
if ((0, orm_helper_js_1.isAssociationField)(col)) {
|
|
121
124
|
// OtO relation
|
|
122
125
|
if (!col.association.returnsMany()) {
|
|
123
|
-
const joinInfo = await joinAssociationGetLast(this._joins, col.association, tableAlias);
|
|
126
|
+
const joinInfo = await (0, command_helper_js_1.joinAssociationGetLast)(this._joins, col.association, tableAlias);
|
|
124
127
|
const subConverter = converter.addObjectProperty({
|
|
125
128
|
name: col.name,
|
|
126
129
|
type: joinInfo.targetEntity.ctor,
|
|
@@ -147,7 +150,7 @@ export class FindCommand {
|
|
|
147
150
|
maxEagerFetch: this.maxEagerFetch,
|
|
148
151
|
});
|
|
149
152
|
findCommand.converter.parent = this.converter;
|
|
150
|
-
await findCommand.filter(In(targetCol.name, Param(parentField)));
|
|
153
|
+
await findCommand.filter((0, builder_1.In)(targetCol.name, (0, builder_1.Param)(parentField)));
|
|
151
154
|
const sort = sortFields && extractSubFields(colNameLower, sortFields);
|
|
152
155
|
await findCommand.addFields({
|
|
153
156
|
projection: p?.projection,
|
|
@@ -179,7 +182,7 @@ export class FindCommand {
|
|
|
179
182
|
return fieldAlias;
|
|
180
183
|
}
|
|
181
184
|
async filter(filter) {
|
|
182
|
-
await prepareFilter(this.mainEntity, filter, this._filter);
|
|
185
|
+
await (0, command_helper_js_1.prepareFilter)(this.mainEntity, filter, this._filter);
|
|
183
186
|
}
|
|
184
187
|
async sort(sortFields) {
|
|
185
188
|
const out = [];
|
|
@@ -195,20 +198,20 @@ export class FindCommand {
|
|
|
195
198
|
if (elName.includes('.')) {
|
|
196
199
|
const a = elName.split('.');
|
|
197
200
|
while (a.length > 1) {
|
|
198
|
-
const col = EntityMetadata.getField(_entityDef, a.shift() || '');
|
|
199
|
-
if (isEmbeddedField(col)) {
|
|
200
|
-
_entityDef = await EmbeddedFieldMetadata.resolveType(col);
|
|
201
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(_entityDef, a.shift() || '');
|
|
202
|
+
if ((0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
203
|
+
_entityDef = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
201
204
|
if (col.fieldNamePrefix)
|
|
202
205
|
prefix += col.fieldNamePrefix;
|
|
203
206
|
if (col.fieldNameSuffix)
|
|
204
207
|
suffix = col.fieldNameSuffix + suffix;
|
|
205
208
|
}
|
|
206
|
-
else if (isAssociationField(col)) {
|
|
209
|
+
else if ((0, orm_helper_js_1.isAssociationField)(col)) {
|
|
207
210
|
if (col.association.returnsMany()) {
|
|
208
211
|
elName = '';
|
|
209
212
|
break;
|
|
210
213
|
}
|
|
211
|
-
const joinInfo = await joinAssociationGetLast(this._joins, col.association, tableAlias);
|
|
214
|
+
const joinInfo = await (0, command_helper_js_1.joinAssociationGetLast)(this._joins, col.association, tableAlias);
|
|
212
215
|
tableAlias = joinInfo.joinAlias;
|
|
213
216
|
_entityDef = joinInfo.targetEntity;
|
|
214
217
|
}
|
|
@@ -219,10 +222,10 @@ export class FindCommand {
|
|
|
219
222
|
continue;
|
|
220
223
|
elName = a.shift() || '';
|
|
221
224
|
}
|
|
222
|
-
const col = EntityMetadata.getField(_entityDef, elName);
|
|
225
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(_entityDef, elName);
|
|
223
226
|
if (!col)
|
|
224
227
|
throw new Error(`Unknown field (${elName}) declared in sort property`);
|
|
225
|
-
if (!isColumnField(col))
|
|
228
|
+
if (!(0, orm_helper_js_1.isColumnField)(col))
|
|
226
229
|
throw new Error(`Can not sort by "${elName}", because it is not a data column`);
|
|
227
230
|
const dir = m[1] || '+';
|
|
228
231
|
out.push((dir || '') + tableAlias + '.' + prefix + col.fieldName + suffix);
|
|
@@ -234,7 +237,7 @@ export class FindCommand {
|
|
|
234
237
|
const columnSqls = Object.keys(this._selectColumns).map(x => this._selectColumns[x].statement);
|
|
235
238
|
if (!columnSqls.length)
|
|
236
239
|
columnSqls.push('1');
|
|
237
|
-
const query = Select(...columnSqls).from(this.mainEntity.tableName + ' as ' + this.mainAlias);
|
|
240
|
+
const query = (0, builder_1.Select)(...columnSqls).from(this.mainEntity.tableName + ' as ' + this.mainAlias);
|
|
238
241
|
if (args.distinct)
|
|
239
242
|
query.distinct();
|
|
240
243
|
query.where(...this._filter._items);
|
|
@@ -276,6 +279,7 @@ export class FindCommand {
|
|
|
276
279
|
throw new Error(`Unknown table alias "${tableAlias}"`);
|
|
277
280
|
}
|
|
278
281
|
}
|
|
282
|
+
exports.FindCommand = FindCommand;
|
|
279
283
|
function extractSubFields(colNameLower, fields) {
|
|
280
284
|
if (!(fields && fields.length))
|
|
281
285
|
return;
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.RowConverter = void 0;
|
|
4
|
+
const builder_1 = require("@sqb/builder");
|
|
2
5
|
const isValueProperty = (prop) => prop.fieldAlias && !prop.converter;
|
|
3
6
|
const isObjectProperty = (prop) => prop.converter && !prop.findCommand;
|
|
4
7
|
const isNestedProperty = (prop) => prop.converter && prop.findCommand;
|
|
5
|
-
|
|
8
|
+
class RowConverter {
|
|
6
9
|
constructor(resultType, parent) {
|
|
7
10
|
this.resultType = resultType;
|
|
8
11
|
this.parent = parent;
|
|
@@ -81,7 +84,7 @@ export class RowConverter {
|
|
|
81
84
|
v = prop.parse(v, elKey);
|
|
82
85
|
if (v != null) {
|
|
83
86
|
result = result || {};
|
|
84
|
-
if (prop.dataType === DataType.JSON && typeof v === 'string')
|
|
87
|
+
if (prop.dataType === builder_1.DataType.JSON && typeof v === 'string')
|
|
85
88
|
v = JSON.parse(v);
|
|
86
89
|
result[elKey] = v;
|
|
87
90
|
}
|
|
@@ -179,3 +182,4 @@ export class RowConverter {
|
|
|
179
182
|
this.parent._checkCircularDep(t);
|
|
180
183
|
}
|
|
181
184
|
}
|
|
185
|
+
exports.RowConverter = RowConverter;
|