@sqb/connect 4.2.1 → 4.5.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/README.md +67 -0
- package/{dist/client/Adapter.js → cjs/client/adapter.js} +0 -0
- package/{dist/client/CursorStream.js → cjs/client/cursor-stream.js} +5 -2
- package/{dist/client/Cursor.js → cjs/client/cursor.js} +18 -11
- package/{dist → cjs}/client/extensions.js +0 -0
- package/{dist/client/FieldInfoMap.js → cjs/client/field-info-map.js} +2 -0
- package/{dist → cjs}/client/helpers.js +2 -2
- package/{dist/client/SqbClient.js → cjs/client/sqb-client.js} +16 -10
- package/{dist/client/SqbConnection.js → cjs/client/sqb-connection.js} +17 -14
- package/{dist → cjs}/client/types.js +0 -0
- package/cjs/index.js +42 -0
- package/{dist → cjs}/orm/backward.js +8 -8
- package/{dist → cjs}/orm/base-entity.js +8 -7
- package/{dist → cjs}/orm/commands/command.helper.js +9 -9
- package/{dist → cjs}/orm/commands/count.command.js +2 -2
- package/{dist → cjs}/orm/commands/create.command.js +12 -12
- package/{dist → cjs}/orm/commands/destroy.command.js +2 -2
- package/{dist → cjs}/orm/commands/find.command.js +100 -88
- package/{dist → cjs}/orm/commands/row-converter.js +10 -7
- package/{dist → cjs}/orm/commands/update.command.js +10 -10
- package/{dist → cjs}/orm/decorators/column.decorator.js +3 -3
- package/{dist → cjs}/orm/decorators/embedded.decorator.js +3 -3
- package/cjs/orm/decorators/entity.decorator.js +179 -0
- package/{dist → cjs}/orm/decorators/events.decorator.js +13 -13
- package/{dist → cjs}/orm/decorators/foreignkey.decorator.js +3 -3
- package/{dist → cjs}/orm/decorators/index.decorator.js +5 -5
- package/cjs/orm/decorators/link.decorator.js +70 -0
- package/{dist → cjs}/orm/decorators/primarykey.decorator.js +7 -7
- package/{dist → cjs}/orm/decorators/transform.decorator.js +5 -5
- package/cjs/orm/model/association-field-metadata.js +24 -0
- package/{dist → cjs}/orm/model/association-node.js +5 -2
- package/{dist → cjs}/orm/model/association.js +31 -39
- package/{dist/orm/model/column-element-metadata.js → cjs/orm/model/column-field-metadata.js} +8 -8
- package/{dist/orm/model/embedded-element-metadata.js → cjs/orm/model/embedded-field-metadata.js} +8 -8
- package/{dist → cjs}/orm/model/entity-metadata.js +90 -91
- package/{dist/orm/model/element-metadata.js → cjs/orm/model/field-metadata.js} +0 -0
- package/{dist → cjs}/orm/model/index-metadata.js +0 -0
- package/{dist → cjs}/orm/model/link-chain.js +12 -15
- package/{dist → cjs}/orm/orm.const.js +0 -0
- package/{dist → cjs}/orm/orm.type.js +0 -0
- package/{dist → cjs}/orm/repository.class.js +25 -22
- package/{dist → cjs}/orm/util/apply-mixins.js +0 -0
- package/{dist → cjs}/orm/util/extract-keyvalues.js +5 -5
- package/{dist → cjs}/orm/util/orm.helper.js +10 -10
- package/{dist/orm/util/serialize-element.js → cjs/orm/util/serialize-field.js} +2 -2
- package/cjs/package.json +3 -0
- package/{dist → cjs}/types.js +0 -0
- package/{dist/client/Adapter.d.ts → esm/client/adapter.d.ts} +1 -1
- package/esm/client/adapter.js +1 -0
- package/{dist/client/CursorStream.d.ts → esm/client/cursor-stream.d.ts} +1 -1
- package/esm/client/cursor-stream.js +96 -0
- package/{dist/client/Cursor.d.ts → esm/client/cursor.d.ts} +5 -5
- package/esm/client/cursor.js +252 -0
- package/{dist → esm}/client/extensions.d.ts +1 -1
- package/esm/client/extensions.js +9 -0
- package/{dist/client/FieldInfoMap.d.ts → esm/client/field-info-map.d.ts} +1 -1
- package/esm/client/field-info-map.js +50 -0
- package/{dist → esm}/client/helpers.d.ts +3 -3
- package/esm/client/helpers.js +125 -0
- package/{dist/client/SqbClient.d.ts → esm/client/sqb-client.d.ts} +4 -10
- package/esm/client/sqb-client.js +166 -0
- package/{dist/client/SqbConnection.d.ts → esm/client/sqb-connection.d.ts} +4 -4
- package/esm/client/sqb-connection.js +270 -0
- package/{dist → esm}/client/types.d.ts +5 -13
- package/esm/client/types.js +1 -0
- package/esm/index.d.ts +33 -0
- package/esm/index.js +32 -0
- package/{dist → esm}/orm/backward.d.ts +1 -1
- package/esm/orm/backward.js +17 -0
- package/{dist → esm}/orm/base-entity.d.ts +1 -1
- package/esm/orm/base-entity.js +25 -0
- package/{dist → esm}/orm/commands/command.helper.d.ts +2 -2
- package/esm/orm/commands/command.helper.js +135 -0
- package/{dist → esm}/orm/commands/count.command.d.ts +3 -3
- package/esm/orm/commands/count.command.js +26 -0
- package/{dist → esm}/orm/commands/create.command.d.ts +2 -2
- package/esm/orm/commands/create.command.js +85 -0
- package/{dist → esm}/orm/commands/destroy.command.d.ts +3 -3
- package/esm/orm/commands/destroy.command.js +26 -0
- package/{dist → esm}/orm/commands/find.command.d.ts +9 -9
- package/esm/orm/commands/find.command.js +318 -0
- package/{dist → esm}/orm/commands/row-converter.d.ts +5 -5
- package/esm/orm/commands/row-converter.js +186 -0
- package/{dist → esm}/orm/commands/update.command.d.ts +3 -3
- package/esm/orm/commands/update.command.js +80 -0
- package/esm/orm/decorators/column.decorator.d.ts +4 -0
- package/esm/orm/decorators/column.decorator.js +19 -0
- package/esm/orm/decorators/embedded.decorator.d.ts +3 -0
- package/esm/orm/decorators/embedded.decorator.js +12 -0
- package/{dist → esm}/orm/decorators/entity.decorator.d.ts +17 -17
- package/esm/orm/decorators/entity.decorator.js +175 -0
- package/{dist → esm}/orm/decorators/events.decorator.d.ts +0 -0
- package/esm/orm/decorators/events.decorator.js +55 -0
- package/{dist → esm}/orm/decorators/foreignkey.decorator.d.ts +1 -1
- package/esm/orm/decorators/foreignkey.decorator.js +9 -0
- package/{dist → esm}/orm/decorators/index.decorator.d.ts +1 -1
- package/esm/orm/decorators/index.decorator.js +17 -0
- package/esm/orm/decorators/link.decorator.d.ts +13 -0
- package/esm/orm/decorators/link.decorator.js +66 -0
- package/{dist → esm}/orm/decorators/primarykey.decorator.d.ts +1 -1
- package/esm/orm/decorators/primarykey.decorator.js +20 -0
- package/{dist → esm}/orm/decorators/transform.decorator.d.ts +1 -1
- package/esm/orm/decorators/transform.decorator.js +19 -0
- package/esm/orm/model/association-field-metadata.d.ts +12 -0
- package/esm/orm/model/association-field-metadata.js +20 -0
- package/{dist → esm}/orm/model/association-node.d.ts +1 -1
- package/esm/orm/model/association-node.js +24 -0
- package/{dist → esm}/orm/model/association.d.ts +6 -7
- package/esm/orm/model/association.js +118 -0
- package/{dist/orm/model/column-element-metadata.d.ts → esm/orm/model/column-field-metadata.d.ts} +10 -14
- package/esm/orm/model/column-field-metadata.js +28 -0
- package/esm/orm/model/embedded-field-metadata.d.ts +14 -0
- package/esm/orm/model/embedded-field-metadata.js +25 -0
- package/esm/orm/model/entity-metadata.d.ts +50 -0
- package/esm/orm/model/entity-metadata.js +337 -0
- package/esm/orm/model/field-metadata.d.ts +16 -0
- package/esm/orm/model/field-metadata.js +1 -0
- package/{dist → esm}/orm/model/index-metadata.d.ts +0 -0
- package/esm/orm/model/index-metadata.js +1 -0
- package/esm/orm/model/link-chain.d.ts +13 -0
- package/esm/orm/model/link-chain.js +43 -0
- package/{dist → esm}/orm/orm.const.d.ts +0 -0
- package/esm/orm/orm.const.js +2 -0
- package/{dist → esm}/orm/orm.type.d.ts +3 -4
- package/esm/orm/orm.type.js +1 -0
- package/{dist → esm}/orm/repository.class.d.ts +23 -23
- package/esm/orm/repository.class.js +238 -0
- package/{dist → esm}/orm/util/apply-mixins.d.ts +0 -0
- package/esm/orm/util/apply-mixins.js +9 -0
- package/{dist → esm}/orm/util/extract-keyvalues.d.ts +1 -1
- package/esm/orm/util/extract-keyvalues.js +57 -0
- package/esm/orm/util/orm.helper.d.ts +13 -0
- package/esm/orm/util/orm.helper.js +28 -0
- package/esm/orm/util/serialize-field.d.ts +2 -0
- package/esm/orm/util/serialize-field.js +39 -0
- package/esm/types.d.ts +7 -0
- package/esm/types.js +1 -0
- package/package.json +54 -41
- package/dist/index.d.ts +0 -33
- package/dist/index.js +0 -42
- package/dist/orm/decorators/column.decorator.d.ts +0 -4
- package/dist/orm/decorators/embedded.decorator.d.ts +0 -3
- package/dist/orm/decorators/entity.decorator.js +0 -179
- package/dist/orm/decorators/link.decorator.d.ts +0 -23
- package/dist/orm/decorators/link.decorator.js +0 -88
- package/dist/orm/model/association-element-metadata.d.ts +0 -10
- package/dist/orm/model/association-element-metadata.js +0 -15
- package/dist/orm/model/element-metadata.d.ts +0 -7
- package/dist/orm/model/embedded-element-metadata.d.ts +0 -14
- package/dist/orm/model/entity-metadata.d.ts +0 -50
- package/dist/orm/model/link-chain.d.ts +0 -15
- package/dist/orm/util/orm.helper.d.ts +0 -13
- package/dist/orm/util/serialize-element.d.ts +0 -2
- package/dist/types.d.ts +0 -2
|
@@ -2,31 +2,35 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.FindCommand = void 0;
|
|
4
4
|
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
9
|
-
const
|
|
10
|
-
const
|
|
11
|
-
const
|
|
5
|
+
const entity_decorator_js_1 = require("../decorators/entity.decorator.js");
|
|
6
|
+
const association_node_js_1 = require("../model/association-node.js");
|
|
7
|
+
const embedded_field_metadata_js_1 = require("../model/embedded-field-metadata.js");
|
|
8
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
9
|
+
const orm_helper_js_1 = require("../util/orm.helper.js");
|
|
10
|
+
const command_helper_js_1 = require("./command.helper.js");
|
|
11
|
+
const row_converter_js_1 = require("./row-converter.js");
|
|
12
12
|
const SORT_ORDER_PATTERN = /^([-+])?(.*)$/;
|
|
13
13
|
class FindCommand {
|
|
14
|
+
maxEagerFetch = 100000;
|
|
15
|
+
maxSubQueries = 5;
|
|
16
|
+
mainEntity;
|
|
17
|
+
resultEntity;
|
|
18
|
+
converter;
|
|
19
|
+
mainAlias = 'T';
|
|
20
|
+
resultAlias = 'T';
|
|
21
|
+
_joins = [];
|
|
22
|
+
_selectColumns = {};
|
|
23
|
+
_filter = (0, builder_1.And)();
|
|
24
|
+
_sort;
|
|
14
25
|
constructor(selectEntity, outputEntity) {
|
|
15
|
-
this.maxEagerFetch = 100000;
|
|
16
|
-
this.maxSubQueries = 5;
|
|
17
|
-
this.mainAlias = 'T';
|
|
18
|
-
this.resultAlias = 'T';
|
|
19
|
-
this._joins = [];
|
|
20
|
-
this._selectColumns = {};
|
|
21
|
-
this._filter = (0, builder_1.And)();
|
|
22
26
|
this.mainEntity = selectEntity;
|
|
23
27
|
this.resultEntity = outputEntity;
|
|
24
|
-
this.converter = new
|
|
28
|
+
this.converter = new row_converter_js_1.RowConverter(outputEntity.ctor);
|
|
25
29
|
}
|
|
26
30
|
static async create(source, opts = {}) {
|
|
27
31
|
let command;
|
|
28
32
|
let listingEntity;
|
|
29
|
-
if (source instanceof
|
|
33
|
+
if (source instanceof association_node_js_1.AssociationNode) {
|
|
30
34
|
const node = source;
|
|
31
35
|
listingEntity = await node.resolveTarget();
|
|
32
36
|
const resultEntity = await node.getLast().resolveTarget();
|
|
@@ -34,7 +38,7 @@ class FindCommand {
|
|
|
34
38
|
if (node.conditions)
|
|
35
39
|
await command.filter(node.conditions);
|
|
36
40
|
if (node.next) {
|
|
37
|
-
const join = await (0,
|
|
41
|
+
const join = await (0, command_helper_js_1.joinAssociationGetLast)(command._joins, node.next, command.mainAlias);
|
|
38
42
|
command.resultAlias = join.joinAlias;
|
|
39
43
|
}
|
|
40
44
|
}
|
|
@@ -55,10 +59,10 @@ class FindCommand {
|
|
|
55
59
|
maxSubQueries: args.maxSubQueries,
|
|
56
60
|
maxEagerFetch: args.maxEagerFetch,
|
|
57
61
|
});
|
|
58
|
-
await command.
|
|
59
|
-
|
|
62
|
+
await command.addFields({
|
|
63
|
+
pick: args.pick,
|
|
60
64
|
include: args.include,
|
|
61
|
-
|
|
65
|
+
omit: args.omit,
|
|
62
66
|
sort: args.sort
|
|
63
67
|
});
|
|
64
68
|
if (args.filter)
|
|
@@ -67,87 +71,94 @@ class FindCommand {
|
|
|
67
71
|
await command.sort(args.sort);
|
|
68
72
|
return await command.execute(args);
|
|
69
73
|
}
|
|
70
|
-
async
|
|
74
|
+
async addFields(opts = {}) {
|
|
71
75
|
const tableAlias = opts.tableAlias || this.resultAlias;
|
|
72
76
|
const entity = opts.entity || this._getEntityFromAlias(tableAlias);
|
|
73
77
|
const converter = opts.converter || this.converter;
|
|
74
|
-
const
|
|
75
|
-
opts.
|
|
78
|
+
const _pick = opts.pick ?
|
|
79
|
+
opts.pick.map(x => x.toLowerCase()) : undefined;
|
|
80
|
+
const _omit = opts.omit ?
|
|
81
|
+
opts.omit.map(x => x.toLowerCase()) : undefined;
|
|
76
82
|
const _include = opts.include ?
|
|
77
83
|
opts.include.map(x => x.toLowerCase()) : undefined;
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
84
|
+
let requestedFields;
|
|
85
|
+
if (_pick)
|
|
86
|
+
requestedFields = [..._pick];
|
|
87
|
+
else {
|
|
88
|
+
requestedFields = entity_decorator_js_1.Entity.getFieldNames(entity.ctor).reduce((a, x) => {
|
|
89
|
+
const f = entity_decorator_js_1.Entity.getField(entity.ctor, x);
|
|
90
|
+
if (f && !f.exclusive)
|
|
91
|
+
a.push(x.toLowerCase());
|
|
92
|
+
return a;
|
|
93
|
+
}, []);
|
|
94
|
+
}
|
|
83
95
|
if (_include)
|
|
84
|
-
|
|
85
|
-
const
|
|
96
|
+
requestedFields.push(..._include);
|
|
97
|
+
const sortFields = opts.sort && opts.sort.length ?
|
|
86
98
|
opts.sort.map(x => x.toLowerCase()) : undefined;
|
|
87
99
|
const prefix = opts.prefix || '';
|
|
88
100
|
const suffix = opts.suffix || '';
|
|
89
|
-
for (const key of Object.keys(entity.
|
|
90
|
-
const col =
|
|
91
|
-
if (!col)
|
|
101
|
+
for (const key of Object.keys(entity.fields)) {
|
|
102
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(entity, key);
|
|
103
|
+
if (!col || col.hidden)
|
|
92
104
|
continue;
|
|
93
105
|
const colNameLower = col.name.toLowerCase();
|
|
94
|
-
// Ignore
|
|
95
|
-
if (
|
|
106
|
+
// Ignore field if in excluded list
|
|
107
|
+
if (_omit && _omit.includes(colNameLower))
|
|
96
108
|
continue;
|
|
97
|
-
// Check if
|
|
98
|
-
if (!
|
|
109
|
+
// Check if field request list
|
|
110
|
+
if (!requestedFields.find((x) => x === colNameLower || x.startsWith(colNameLower + '.')))
|
|
99
111
|
continue;
|
|
100
|
-
// Add field to select list if
|
|
101
|
-
if ((0,
|
|
112
|
+
// Add field to select list if field is a column
|
|
113
|
+
if ((0, orm_helper_js_1.isColumnField)(col)) {
|
|
102
114
|
const fieldAlias = this._selectColumn(tableAlias, col, prefix, suffix);
|
|
103
115
|
// Add column to converter
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
});
|
|
116
|
+
converter.addValueProperty({
|
|
117
|
+
name: col.name,
|
|
118
|
+
fieldAlias,
|
|
119
|
+
dataType: col.dataType,
|
|
120
|
+
parse: col.parse
|
|
121
|
+
});
|
|
111
122
|
continue;
|
|
112
123
|
}
|
|
113
|
-
if ((0,
|
|
114
|
-
const typ = await
|
|
124
|
+
if ((0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
125
|
+
const typ = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
115
126
|
const subConverter = converter.addObjectProperty({
|
|
116
127
|
name: col.name,
|
|
117
128
|
type: typ.ctor
|
|
118
129
|
}).converter;
|
|
119
|
-
await this.
|
|
130
|
+
await this.addFields({
|
|
120
131
|
tableAlias,
|
|
121
132
|
converter: subConverter,
|
|
122
133
|
entity: typ,
|
|
123
134
|
prefix: col.fieldNamePrefix,
|
|
124
135
|
suffix: col.fieldNameSuffix,
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
include:
|
|
128
|
-
|
|
129
|
-
sort:
|
|
136
|
+
pick: _pick?.includes(colNameLower) ? undefined :
|
|
137
|
+
extractSubFields(colNameLower, _pick),
|
|
138
|
+
include: extractSubFields(colNameLower, _include),
|
|
139
|
+
omit: extractSubFields(colNameLower, _omit),
|
|
140
|
+
sort: extractSubFields(colNameLower, sortFields),
|
|
130
141
|
});
|
|
131
142
|
continue;
|
|
132
143
|
}
|
|
133
|
-
if ((0,
|
|
144
|
+
if ((0, orm_helper_js_1.isAssociationField)(col)) {
|
|
134
145
|
// OtO relation
|
|
135
146
|
if (!col.association.returnsMany()) {
|
|
136
|
-
const joinInfo = await (0,
|
|
147
|
+
const joinInfo = await (0, command_helper_js_1.joinAssociationGetLast)(this._joins, col.association, tableAlias);
|
|
137
148
|
const subConverter = converter.addObjectProperty({
|
|
138
149
|
name: col.name,
|
|
139
150
|
type: joinInfo.targetEntity.ctor
|
|
140
151
|
}).converter;
|
|
141
152
|
// Add join fields to select columns list
|
|
142
|
-
await this.
|
|
153
|
+
await this.addFields({
|
|
143
154
|
tableAlias: joinInfo.joinAlias,
|
|
144
155
|
converter: subConverter,
|
|
145
156
|
entity: joinInfo.targetEntity,
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
include:
|
|
149
|
-
|
|
150
|
-
sort:
|
|
157
|
+
pick: _pick?.includes(colNameLower) ? undefined :
|
|
158
|
+
extractSubFields(colNameLower, _pick),
|
|
159
|
+
include: extractSubFields(colNameLower, _include),
|
|
160
|
+
omit: extractSubFields(colNameLower, _omit),
|
|
161
|
+
sort: extractSubFields(colNameLower, sortFields),
|
|
151
162
|
});
|
|
152
163
|
continue;
|
|
153
164
|
}
|
|
@@ -162,13 +173,14 @@ class FindCommand {
|
|
|
162
173
|
maxSubQueries: this.maxSubQueries - 1,
|
|
163
174
|
maxEagerFetch: this.maxEagerFetch
|
|
164
175
|
});
|
|
176
|
+
findCommand.converter.parent = this.converter;
|
|
165
177
|
await findCommand.filter((0, builder_1.In)(targetCol.name, (0, builder_1.Param)(parentField)));
|
|
166
|
-
const sort =
|
|
167
|
-
await findCommand.
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
include:
|
|
171
|
-
|
|
178
|
+
const sort = sortFields && extractSubFields(colNameLower, sortFields);
|
|
179
|
+
await findCommand.addFields({
|
|
180
|
+
pick: _pick?.includes(colNameLower) ? undefined :
|
|
181
|
+
extractSubFields(colNameLower, _pick),
|
|
182
|
+
include: extractSubFields(colNameLower, _include),
|
|
183
|
+
omit: extractSubFields(colNameLower, _omit),
|
|
172
184
|
sort,
|
|
173
185
|
});
|
|
174
186
|
if (sort)
|
|
@@ -181,7 +193,7 @@ class FindCommand {
|
|
|
181
193
|
findCommand,
|
|
182
194
|
parentField,
|
|
183
195
|
keyField,
|
|
184
|
-
sort:
|
|
196
|
+
sort: extractSubFields(colNameLower, sortFields)
|
|
185
197
|
});
|
|
186
198
|
}
|
|
187
199
|
}
|
|
@@ -193,17 +205,17 @@ class FindCommand {
|
|
|
193
205
|
(suffix || '').toLowerCase();
|
|
194
206
|
const fieldAlias = tableAlias + '_' + fieldName;
|
|
195
207
|
this._selectColumns[fieldAlias] = {
|
|
196
|
-
|
|
208
|
+
field: el,
|
|
197
209
|
statement: tableAlias + '.' + fieldName + ' as ' + fieldAlias
|
|
198
210
|
};
|
|
199
211
|
return fieldAlias;
|
|
200
212
|
}
|
|
201
213
|
async filter(filter) {
|
|
202
|
-
await (0,
|
|
214
|
+
await (0, command_helper_js_1.prepareFilter)(this.mainEntity, filter, this._filter);
|
|
203
215
|
}
|
|
204
|
-
async sort(
|
|
205
|
-
const
|
|
206
|
-
for (const item of
|
|
216
|
+
async sort(sortFields) {
|
|
217
|
+
const out = [];
|
|
218
|
+
for (const item of sortFields) {
|
|
207
219
|
const m = item.match(SORT_ORDER_PATTERN);
|
|
208
220
|
if (!m)
|
|
209
221
|
throw new Error(`"${item}" is not a valid order expression`);
|
|
@@ -215,20 +227,20 @@ class FindCommand {
|
|
|
215
227
|
if (elName.includes('.')) {
|
|
216
228
|
const a = elName.split('.');
|
|
217
229
|
while (a.length > 1) {
|
|
218
|
-
const col =
|
|
219
|
-
if ((0,
|
|
220
|
-
_entityDef = await
|
|
230
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(_entityDef, a.shift() || '');
|
|
231
|
+
if ((0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
232
|
+
_entityDef = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
221
233
|
if (col.fieldNamePrefix)
|
|
222
234
|
prefix += col.fieldNamePrefix;
|
|
223
235
|
if (col.fieldNameSuffix)
|
|
224
236
|
suffix = col.fieldNameSuffix + suffix;
|
|
225
237
|
}
|
|
226
|
-
else if ((0,
|
|
238
|
+
else if ((0, orm_helper_js_1.isAssociationField)(col)) {
|
|
227
239
|
if (col.association.returnsMany()) {
|
|
228
240
|
elName = '';
|
|
229
241
|
break;
|
|
230
242
|
}
|
|
231
|
-
const joinInfo = await (0,
|
|
243
|
+
const joinInfo = await (0, command_helper_js_1.joinAssociationGetLast)(this._joins, col.association, tableAlias);
|
|
232
244
|
tableAlias = joinInfo.joinAlias;
|
|
233
245
|
_entityDef = joinInfo.targetEntity;
|
|
234
246
|
}
|
|
@@ -239,15 +251,15 @@ class FindCommand {
|
|
|
239
251
|
continue;
|
|
240
252
|
elName = a.shift() || '';
|
|
241
253
|
}
|
|
242
|
-
const col =
|
|
254
|
+
const col = entity_metadata_js_1.EntityMetadata.getField(_entityDef, elName);
|
|
243
255
|
if (!col)
|
|
244
|
-
throw new Error(`Unknown
|
|
245
|
-
if (!(0,
|
|
256
|
+
throw new Error(`Unknown field (${elName}) declared in sort property`);
|
|
257
|
+
if (!(0, orm_helper_js_1.isColumnField)(col))
|
|
246
258
|
throw new Error(`Can not sort by "${elName}", because it is not a data column`);
|
|
247
259
|
const dir = m[1] || '+';
|
|
248
|
-
|
|
260
|
+
out.push((dir || '') + tableAlias + '.' + prefix + col.fieldName + suffix);
|
|
249
261
|
}
|
|
250
|
-
this._sort =
|
|
262
|
+
this._sort = out;
|
|
251
263
|
}
|
|
252
264
|
async execute(args) {
|
|
253
265
|
// Generate select query
|
|
@@ -297,11 +309,11 @@ class FindCommand {
|
|
|
297
309
|
}
|
|
298
310
|
}
|
|
299
311
|
exports.FindCommand = FindCommand;
|
|
300
|
-
function
|
|
301
|
-
if (!(
|
|
312
|
+
function extractSubFields(colNameLower, fields) {
|
|
313
|
+
if (!(fields && fields.length))
|
|
302
314
|
return;
|
|
303
|
-
if (
|
|
304
|
-
return
|
|
315
|
+
if (fields) {
|
|
316
|
+
return fields.reduce((trg, v) => {
|
|
305
317
|
if (v.startsWith(colNameLower + '.'))
|
|
306
318
|
trg.push(v.substring(colNameLower.length + 1).toLowerCase());
|
|
307
319
|
return trg;
|
|
@@ -12,10 +12,13 @@ const isNestedProperty = (prop) => {
|
|
|
12
12
|
return prop.converter && prop.findCommand;
|
|
13
13
|
};
|
|
14
14
|
class RowConverter {
|
|
15
|
+
resultType;
|
|
16
|
+
parent;
|
|
17
|
+
_properties = {};
|
|
18
|
+
_propertyKeys;
|
|
15
19
|
constructor(resultType, parent) {
|
|
16
20
|
this.resultType = resultType;
|
|
17
21
|
this.parent = parent;
|
|
18
|
-
this._properties = {};
|
|
19
22
|
}
|
|
20
23
|
addValueProperty(args) {
|
|
21
24
|
this._propertyKeys = undefined;
|
|
@@ -76,11 +79,11 @@ class RowConverter {
|
|
|
76
79
|
}
|
|
77
80
|
_rowToObject(executor, fields, row) {
|
|
78
81
|
// Cache keys for better performance
|
|
79
|
-
const
|
|
80
|
-
const
|
|
82
|
+
const fieldKeys = this.keys;
|
|
83
|
+
const fieldsLen = fieldKeys.length;
|
|
81
84
|
let result;
|
|
82
|
-
for (let elIdx = 0; elIdx <
|
|
83
|
-
const elKey =
|
|
85
|
+
for (let elIdx = 0; elIdx < fieldsLen; elIdx++) {
|
|
86
|
+
const elKey = fieldKeys[elIdx];
|
|
84
87
|
const prop = this._properties[elKey];
|
|
85
88
|
if (isValueProperty(prop)) {
|
|
86
89
|
const field = fields.get(prop.fieldAlias);
|
|
@@ -97,7 +100,7 @@ class RowConverter {
|
|
|
97
100
|
}
|
|
98
101
|
}
|
|
99
102
|
else if (isNestedProperty(prop)) {
|
|
100
|
-
// One2Many Eager
|
|
103
|
+
// One2Many Eager field
|
|
101
104
|
// Keep a list of key field/value pairs to fetch rows for eager relation
|
|
102
105
|
const _params = prop.paramValues;
|
|
103
106
|
const f = fields.get(prop.parentField);
|
|
@@ -179,7 +182,7 @@ class RowConverter {
|
|
|
179
182
|
}
|
|
180
183
|
_checkCircularDep(t) {
|
|
181
184
|
if (this.resultType === t)
|
|
182
|
-
throw new Error('Circular
|
|
185
|
+
throw new Error('Circular fields requested');
|
|
183
186
|
if (this.parent)
|
|
184
187
|
this.parent._checkCircularDep(t);
|
|
185
188
|
}
|
|
@@ -2,10 +2,10 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UpdateCommand = void 0;
|
|
4
4
|
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const
|
|
6
|
-
const
|
|
7
|
-
const
|
|
8
|
-
const
|
|
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 orm_helper_js_1 = require("../util/orm.helper.js");
|
|
8
|
+
const command_helper_js_1 = require("./command.helper.js");
|
|
9
9
|
class UpdateCommand {
|
|
10
10
|
// istanbul ignore next
|
|
11
11
|
constructor() {
|
|
@@ -42,7 +42,7 @@ class UpdateCommand {
|
|
|
42
42
|
static async _prepareFilter(ctx, filter) {
|
|
43
43
|
if (filter) {
|
|
44
44
|
const where = (0, builder_1.And)();
|
|
45
|
-
await (0,
|
|
45
|
+
await (0, command_helper_js_1.prepareFilter)(ctx.entity, filter, where);
|
|
46
46
|
ctx.queryFilter.push(...where._items);
|
|
47
47
|
}
|
|
48
48
|
}
|
|
@@ -50,11 +50,11 @@ class UpdateCommand {
|
|
|
50
50
|
let v;
|
|
51
51
|
prefix = prefix || '';
|
|
52
52
|
suffix = suffix || '';
|
|
53
|
-
for (const col of Object.values(entity.
|
|
53
|
+
for (const col of Object.values(entity.fields)) {
|
|
54
54
|
v = values[col.name];
|
|
55
55
|
if (v === undefined)
|
|
56
56
|
continue;
|
|
57
|
-
if ((0,
|
|
57
|
+
if ((0, orm_helper_js_1.isColumnField)(col)) {
|
|
58
58
|
if (col.noUpdate)
|
|
59
59
|
continue;
|
|
60
60
|
if (typeof col.serialize === 'function')
|
|
@@ -63,7 +63,7 @@ class UpdateCommand {
|
|
|
63
63
|
throw new Error(`${entity.name}.${col.name} is required and can't be null`);
|
|
64
64
|
if (v === undefined)
|
|
65
65
|
continue;
|
|
66
|
-
|
|
66
|
+
column_field_metadata_js_1.ColumnFieldMetadata.checkEnumValue(col, v);
|
|
67
67
|
const fieldName = prefix + col.fieldName + suffix;
|
|
68
68
|
const k = '$input_' + fieldName;
|
|
69
69
|
ctx.queryValues[fieldName] = (0, builder_1.Param)({
|
|
@@ -74,8 +74,8 @@ class UpdateCommand {
|
|
|
74
74
|
ctx.queryParams[k] = v;
|
|
75
75
|
ctx.colCount++;
|
|
76
76
|
}
|
|
77
|
-
else if (v != null && (0,
|
|
78
|
-
const type = await
|
|
77
|
+
else if (v != null && (0, orm_helper_js_1.isEmbeddedField)(col)) {
|
|
78
|
+
const type = await embedded_field_metadata_js_1.EmbeddedFieldMetadata.resolveType(col);
|
|
79
79
|
await this._prepareParams(ctx, type, v, col.fieldNamePrefix, col.fieldNameSuffix);
|
|
80
80
|
}
|
|
81
81
|
}
|
|
@@ -1,13 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Column = void 0;
|
|
4
|
-
const
|
|
4
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
5
5
|
function Column(arg0) {
|
|
6
6
|
return (target, propertyKey) => {
|
|
7
7
|
if (typeof propertyKey !== 'string')
|
|
8
8
|
throw new Error('Symbol properties are not accepted');
|
|
9
9
|
const options = (typeof arg0 === 'string' ? { dataType: arg0 } : arg0) || {};
|
|
10
|
-
const entity =
|
|
10
|
+
const entity = entity_metadata_js_1.EntityMetadata.define(target.constructor);
|
|
11
11
|
if (!options.type) {
|
|
12
12
|
const typ = Reflect.getMetadata("design:type", entity.ctor.prototype, propertyKey);
|
|
13
13
|
if (typ === Array) {
|
|
@@ -17,7 +17,7 @@ function Column(arg0) {
|
|
|
17
17
|
else
|
|
18
18
|
options.type = typ;
|
|
19
19
|
}
|
|
20
|
-
|
|
20
|
+
entity_metadata_js_1.EntityMetadata.defineColumnField(entity, propertyKey, options);
|
|
21
21
|
};
|
|
22
22
|
}
|
|
23
23
|
exports.Column = Column;
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Embedded = void 0;
|
|
4
|
-
const
|
|
4
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
5
5
|
function Embedded(type, options) {
|
|
6
6
|
return (target, propertyKey) => {
|
|
7
7
|
if (typeof propertyKey !== 'string')
|
|
@@ -9,8 +9,8 @@ function Embedded(type, options) {
|
|
|
9
9
|
type = type || Reflect.getMetadata("design:type", target, propertyKey);
|
|
10
10
|
if (typeof type !== 'function')
|
|
11
11
|
throw new Error('"type" must be defined');
|
|
12
|
-
const entity =
|
|
13
|
-
|
|
12
|
+
const entity = entity_metadata_js_1.EntityMetadata.define(target.constructor);
|
|
13
|
+
entity_metadata_js_1.EntityMetadata.defineEmbeddedField(entity, propertyKey, type, options);
|
|
14
14
|
};
|
|
15
15
|
}
|
|
16
16
|
exports.Embedded = Embedded;
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Entity = void 0;
|
|
4
|
+
const entity_metadata_js_1 = require("../model/entity-metadata.js");
|
|
5
|
+
const apply_mixins_js_1 = require("../util/apply-mixins.js");
|
|
6
|
+
function Entity(options) {
|
|
7
|
+
return function (target) {
|
|
8
|
+
const opts = typeof options === 'object' ? options : {};
|
|
9
|
+
const tableName = typeof options === 'string' ? options : opts.tableName;
|
|
10
|
+
const entity = entity_metadata_js_1.EntityMetadata.define(target);
|
|
11
|
+
entity.tableName = tableName || target.name;
|
|
12
|
+
if (opts.schema)
|
|
13
|
+
entity.schema = opts.schema;
|
|
14
|
+
if (opts.comment)
|
|
15
|
+
entity.comment = opts.comment;
|
|
16
|
+
};
|
|
17
|
+
}
|
|
18
|
+
exports.Entity = Entity;
|
|
19
|
+
(function (Entity) {
|
|
20
|
+
Entity.getMetadata = entity_metadata_js_1.EntityMetadata.get;
|
|
21
|
+
Entity.getOwnMetadata = entity_metadata_js_1.EntityMetadata.getOwn;
|
|
22
|
+
function getField(ctor, key) {
|
|
23
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
24
|
+
return model && entity_metadata_js_1.EntityMetadata.getField(model, key);
|
|
25
|
+
}
|
|
26
|
+
Entity.getField = getField;
|
|
27
|
+
function getColumnField(ctor, key) {
|
|
28
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
29
|
+
return model && entity_metadata_js_1.EntityMetadata.getColumnField(model, key);
|
|
30
|
+
}
|
|
31
|
+
Entity.getColumnField = getColumnField;
|
|
32
|
+
function getEmbeddedField(ctor, key) {
|
|
33
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
34
|
+
return model && entity_metadata_js_1.EntityMetadata.getEmbeddedField(model, key);
|
|
35
|
+
}
|
|
36
|
+
Entity.getEmbeddedField = getEmbeddedField;
|
|
37
|
+
function getAssociationField(ctor, key) {
|
|
38
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
39
|
+
return model && entity_metadata_js_1.EntityMetadata.getAssociationField(model, key);
|
|
40
|
+
}
|
|
41
|
+
Entity.getAssociationField = getAssociationField;
|
|
42
|
+
function getColumnFieldByFieldName(ctor, fieldName) {
|
|
43
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
44
|
+
return model && entity_metadata_js_1.EntityMetadata.getColumnFieldByFieldName(model, fieldName);
|
|
45
|
+
}
|
|
46
|
+
Entity.getColumnFieldByFieldName = getColumnFieldByFieldName;
|
|
47
|
+
function find(ctor, predicate) {
|
|
48
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
49
|
+
return model && entity_metadata_js_1.EntityMetadata.findField(model, predicate);
|
|
50
|
+
}
|
|
51
|
+
Entity.find = find;
|
|
52
|
+
function getFieldNames(ctor, filter) {
|
|
53
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
54
|
+
return (model && entity_metadata_js_1.EntityMetadata.getFieldNames(model, filter)) || [];
|
|
55
|
+
}
|
|
56
|
+
Entity.getFieldNames = getFieldNames;
|
|
57
|
+
function getColumnFieldNames(ctor) {
|
|
58
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
59
|
+
return (model && entity_metadata_js_1.EntityMetadata.getColumnFieldNames(model)) || [];
|
|
60
|
+
}
|
|
61
|
+
Entity.getColumnFieldNames = getColumnFieldNames;
|
|
62
|
+
function getEmbeddedFieldNames(ctor) {
|
|
63
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
64
|
+
return (model && entity_metadata_js_1.EntityMetadata.getEmbeddedFieldNames(model)) || [];
|
|
65
|
+
}
|
|
66
|
+
Entity.getEmbeddedFieldNames = getEmbeddedFieldNames;
|
|
67
|
+
function getAssociationFieldNames(ctor) {
|
|
68
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
69
|
+
return (model && entity_metadata_js_1.EntityMetadata.getAssociationFieldNames(model)) || [];
|
|
70
|
+
}
|
|
71
|
+
Entity.getAssociationFieldNames = getAssociationFieldNames;
|
|
72
|
+
function getNonAssociationFieldNames(ctor) {
|
|
73
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
74
|
+
return (model && entity_metadata_js_1.EntityMetadata.getNonAssociationFieldNames(model)) || [];
|
|
75
|
+
}
|
|
76
|
+
Entity.getNonAssociationFieldNames = getNonAssociationFieldNames;
|
|
77
|
+
function getInsertColumnNames(ctor) {
|
|
78
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
79
|
+
return (model && entity_metadata_js_1.EntityMetadata.getInsertColumnNames(model)) || [];
|
|
80
|
+
}
|
|
81
|
+
Entity.getInsertColumnNames = getInsertColumnNames;
|
|
82
|
+
function getUpdateColumnNames(ctor) {
|
|
83
|
+
const model = entity_metadata_js_1.EntityMetadata.get(ctor);
|
|
84
|
+
return (model && entity_metadata_js_1.EntityMetadata.getUpdateColumnNames(model)) || [];
|
|
85
|
+
}
|
|
86
|
+
Entity.getUpdateColumnNames = getUpdateColumnNames;
|
|
87
|
+
function getPrimaryIndex(ctor) {
|
|
88
|
+
const model = entity_metadata_js_1.EntityMetadata.define(ctor);
|
|
89
|
+
return entity_metadata_js_1.EntityMetadata.getPrimaryIndex(model);
|
|
90
|
+
}
|
|
91
|
+
Entity.getPrimaryIndex = getPrimaryIndex;
|
|
92
|
+
function getPrimaryIndexColumns(ctor) {
|
|
93
|
+
const model = entity_metadata_js_1.EntityMetadata.define(ctor);
|
|
94
|
+
return entity_metadata_js_1.EntityMetadata.getPrimaryIndexColumns(model);
|
|
95
|
+
}
|
|
96
|
+
Entity.getPrimaryIndexColumns = getPrimaryIndexColumns;
|
|
97
|
+
function mixin(derivedCtor, ...bases) {
|
|
98
|
+
for (const base of bases) {
|
|
99
|
+
if (!base)
|
|
100
|
+
continue;
|
|
101
|
+
(0, apply_mixins_js_1.applyMixins)(derivedCtor, base);
|
|
102
|
+
const srcMeta = entity_metadata_js_1.EntityMetadata.get(base);
|
|
103
|
+
if (srcMeta) {
|
|
104
|
+
const trgMeta = entity_metadata_js_1.EntityMetadata.define(derivedCtor);
|
|
105
|
+
entity_metadata_js_1.EntityMetadata.mixin(trgMeta, srcMeta);
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
return derivedCtor;
|
|
109
|
+
}
|
|
110
|
+
Entity.mixin = mixin;
|
|
111
|
+
function Pick(classRef, keys) {
|
|
112
|
+
const PickEntityClass = class {
|
|
113
|
+
constructor(...args) {
|
|
114
|
+
applyConstructorProperties(this, classRef, args);
|
|
115
|
+
}
|
|
116
|
+
};
|
|
117
|
+
const pickKeys = keys.map(x => x.toLowerCase());
|
|
118
|
+
const filter = (k) => pickKeys.includes(k.toLowerCase());
|
|
119
|
+
(0, apply_mixins_js_1.applyMixins)(PickEntityClass, classRef, filter);
|
|
120
|
+
const srcMeta = entity_metadata_js_1.EntityMetadata.get(classRef);
|
|
121
|
+
if (srcMeta) {
|
|
122
|
+
const trgMeta = entity_metadata_js_1.EntityMetadata.define(PickEntityClass);
|
|
123
|
+
entity_metadata_js_1.EntityMetadata.mixin(trgMeta, srcMeta, filter);
|
|
124
|
+
}
|
|
125
|
+
return PickEntityClass;
|
|
126
|
+
}
|
|
127
|
+
Entity.Pick = Pick;
|
|
128
|
+
function Omit(classRef, keys) {
|
|
129
|
+
const OmitEntityClass = class {
|
|
130
|
+
constructor(...args) {
|
|
131
|
+
applyConstructorProperties(this, classRef, args);
|
|
132
|
+
}
|
|
133
|
+
};
|
|
134
|
+
const omitKeys = keys.map(x => x.toLowerCase());
|
|
135
|
+
const filter = (k) => !omitKeys.includes(k.toLowerCase());
|
|
136
|
+
(0, apply_mixins_js_1.applyMixins)(OmitEntityClass, classRef, filter);
|
|
137
|
+
const srcMeta = entity_metadata_js_1.EntityMetadata.get(classRef);
|
|
138
|
+
if (srcMeta) {
|
|
139
|
+
const trgMeta = entity_metadata_js_1.EntityMetadata.define(OmitEntityClass);
|
|
140
|
+
entity_metadata_js_1.EntityMetadata.mixin(trgMeta, srcMeta, filter);
|
|
141
|
+
}
|
|
142
|
+
return OmitEntityClass;
|
|
143
|
+
}
|
|
144
|
+
Entity.Omit = Omit;
|
|
145
|
+
function Union(...bases) {
|
|
146
|
+
const UnionClass = class {
|
|
147
|
+
constructor(...args) {
|
|
148
|
+
for (const c of bases)
|
|
149
|
+
applyConstructorProperties(this, c, args);
|
|
150
|
+
}
|
|
151
|
+
};
|
|
152
|
+
for (const base of bases) {
|
|
153
|
+
(0, apply_mixins_js_1.applyMixins)(UnionClass, base);
|
|
154
|
+
const srcMeta = entity_metadata_js_1.EntityMetadata.get(base);
|
|
155
|
+
if (srcMeta) {
|
|
156
|
+
const trgMeta = entity_metadata_js_1.EntityMetadata.define(UnionClass);
|
|
157
|
+
entity_metadata_js_1.EntityMetadata.mixin(trgMeta, srcMeta);
|
|
158
|
+
}
|
|
159
|
+
}
|
|
160
|
+
return UnionClass;
|
|
161
|
+
}
|
|
162
|
+
Entity.Union = Union;
|
|
163
|
+
})(Entity = exports.Entity || (exports.Entity = {}));
|
|
164
|
+
function applyConstructorProperties(target, sourceClass, constructorArgs, isPropertyInherited) {
|
|
165
|
+
try {
|
|
166
|
+
const tempInstance = new sourceClass(...constructorArgs);
|
|
167
|
+
const keys = Object.getOwnPropertyNames(tempInstance);
|
|
168
|
+
for (const key of keys) {
|
|
169
|
+
const srcDesc = Object.getOwnPropertyDescriptor(tempInstance, key);
|
|
170
|
+
const trgDesc = Object.getOwnPropertyDescriptor(target, key);
|
|
171
|
+
if (!srcDesc || trgDesc || (isPropertyInherited && !isPropertyInherited(key)))
|
|
172
|
+
continue;
|
|
173
|
+
Object.defineProperty(target, key, srcDesc);
|
|
174
|
+
}
|
|
175
|
+
}
|
|
176
|
+
catch {
|
|
177
|
+
//
|
|
178
|
+
}
|
|
179
|
+
}
|