@sqb/connect 4.0.7 → 4.0.8
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/package.json +4 -4
- package/dist/client/Adapter.d.ts +0 -57
- package/dist/client/Adapter.js +0 -2
- package/dist/client/Cursor.d.ts +0 -116
- package/dist/client/Cursor.js +0 -250
- package/dist/client/CursorStream.d.ts +0 -29
- package/dist/client/CursorStream.js +0 -98
- package/dist/client/FieldInfoMap.d.ts +0 -10
- package/dist/client/FieldInfoMap.js +0 -33
- package/dist/client/SqbClient.d.ts +0 -70
- package/dist/client/SqbClient.js +0 -165
- package/dist/client/SqbConnection.d.ts +0 -74
- package/dist/client/SqbConnection.js +0 -272
- package/dist/client/extensions.d.ts +0 -4
- package/dist/client/extensions.js +0 -14
- package/dist/client/helpers.d.ts +0 -9
- package/dist/client/helpers.js +0 -133
- package/dist/client/types.d.ts +0 -189
- package/dist/client/types.js +0 -5
- package/dist/index.d.ts +0 -28
- package/dist/index.js +0 -37
- package/dist/orm/base-entity.class.d.ts +0 -16
- package/dist/orm/base-entity.class.js +0 -56
- package/dist/orm/commands/command.helper.d.ts +0 -14
- package/dist/orm/commands/command.helper.js +0 -140
- package/dist/orm/commands/count.command.d.ts +0 -11
- package/dist/orm/commands/count.command.js +0 -30
- package/dist/orm/commands/create.command.d.ts +0 -20
- package/dist/orm/commands/create.command.js +0 -83
- package/dist/orm/commands/destroy.command.d.ts +0 -11
- package/dist/orm/commands/destroy.command.js +0 -30
- package/dist/orm/commands/find.command.d.ts +0 -44
- package/dist/orm/commands/find.command.js +0 -308
- package/dist/orm/commands/row-converter.d.ts +0 -55
- package/dist/orm/commands/row-converter.js +0 -187
- package/dist/orm/commands/update.command.d.ts +0 -22
- package/dist/orm/commands/update.command.js +0 -82
- package/dist/orm/decorators/column.decorator.d.ts +0 -4
- package/dist/orm/decorators/column.decorator.js +0 -14
- package/dist/orm/decorators/embedded.decorator.d.ts +0 -2
- package/dist/orm/decorators/embedded.decorator.js +0 -17
- package/dist/orm/decorators/entity.decorator.d.ts +0 -6
- package/dist/orm/decorators/entity.decorator.js +0 -23
- package/dist/orm/decorators/events.decorator.d.ts +0 -6
- package/dist/orm/decorators/events.decorator.js +0 -76
- package/dist/orm/decorators/foreignkey.decorator.d.ts +0 -2
- package/dist/orm/decorators/foreignkey.decorator.js +0 -13
- package/dist/orm/decorators/index.decorator.d.ts +0 -3
- package/dist/orm/decorators/index.decorator.js +0 -22
- package/dist/orm/decorators/link.decorator.d.ts +0 -23
- package/dist/orm/decorators/link.decorator.js +0 -88
- package/dist/orm/decorators/primarykey.decorator.d.ts +0 -3
- package/dist/orm/decorators/primarykey.decorator.js +0 -23
- package/dist/orm/decorators/transform.decorator.d.ts +0 -3
- package/dist/orm/decorators/transform.decorator.js +0 -24
- package/dist/orm/model/abstract-entityelement.d.ts +0 -8
- package/dist/orm/model/abstract-entityelement.js +0 -10
- package/dist/orm/model/association-node.d.ts +0 -9
- package/dist/orm/model/association-node.js +0 -25
- package/dist/orm/model/association.d.ts +0 -28
- package/dist/orm/model/association.js +0 -129
- package/dist/orm/model/entity-association-element.d.ts +0 -9
- package/dist/orm/model/entity-association-element.js +0 -13
- package/dist/orm/model/entity-column-element.d.ts +0 -68
- package/dist/orm/model/entity-column-element.js +0 -27
- package/dist/orm/model/entity-model.d.ts +0 -59
- package/dist/orm/model/entity-model.js +0 -295
- package/dist/orm/model/entity-object-element.d.ts +0 -12
- package/dist/orm/model/entity-object-element.js +0 -24
- package/dist/orm/model/index-meta.d.ts +0 -9
- package/dist/orm/model/index-meta.js +0 -16
- package/dist/orm/model/link-chain.d.ts +0 -15
- package/dist/orm/model/link-chain.js +0 -50
- package/dist/orm/orm.const.d.ts +0 -2
- package/dist/orm/orm.const.js +0 -5
- package/dist/orm/orm.type.d.ts +0 -112
- package/dist/orm/orm.type.js +0 -2
- package/dist/orm/repository.class.d.ts +0 -125
- package/dist/orm/repository.class.js +0 -217
- package/dist/orm/util/entity-mapping.d.ts +0 -12
- package/dist/orm/util/entity-mapping.js +0 -154
- package/dist/orm/util/extract-keyvalues.d.ts +0 -2
- package/dist/orm/util/extract-keyvalues.js +0 -60
- package/dist/orm/util/orm.helper.d.ts +0 -14
- package/dist/orm/util/orm.helper.js +0 -39
- package/dist/orm/util/serialize-element.d.ts +0 -2
- package/dist/orm/util/serialize-element.js +0 -43
- package/dist/types.d.ts +0 -2
- package/dist/types.js +0 -2
|
@@ -1,30 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.DestroyCommand = void 0;
|
|
4
|
-
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const command_helper_1 = require("./command.helper");
|
|
6
|
-
class DestroyCommand {
|
|
7
|
-
// istanbul ignore next
|
|
8
|
-
constructor() {
|
|
9
|
-
throw new Error('This class is abstract');
|
|
10
|
-
}
|
|
11
|
-
static async execute(args) {
|
|
12
|
-
const { connection, entity, filter, params } = args;
|
|
13
|
-
let where;
|
|
14
|
-
if (filter) {
|
|
15
|
-
where = (0, builder_1.And)();
|
|
16
|
-
await (0, command_helper_1.prepareFilter)(entity, filter, where);
|
|
17
|
-
}
|
|
18
|
-
const query = (0, builder_1.Delete)(entity.tableName + ' T');
|
|
19
|
-
if (where)
|
|
20
|
-
query.where(...where._items);
|
|
21
|
-
// Execute query
|
|
22
|
-
const resp = await connection.execute(query, {
|
|
23
|
-
params,
|
|
24
|
-
objectRows: false,
|
|
25
|
-
cursor: false,
|
|
26
|
-
});
|
|
27
|
-
return (resp && resp.rowsAffected) || 0;
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
exports.DestroyCommand = DestroyCommand;
|
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import type { Repository } from '../repository.class';
|
|
2
|
-
import type { EntityModel } from '../model/entity-model';
|
|
3
|
-
import { RowConverter } from './row-converter';
|
|
4
|
-
import { AssociationNode } from '../model/association-node';
|
|
5
|
-
import { SqbConnection } from '../../client/SqbConnection';
|
|
6
|
-
export declare type FindCommandArgs = {
|
|
7
|
-
entity: EntityModel;
|
|
8
|
-
connection: SqbConnection;
|
|
9
|
-
} & Repository.FindAllOptions;
|
|
10
|
-
export declare class FindCommand {
|
|
11
|
-
maxEagerFetch: number;
|
|
12
|
-
maxSubQueries: number;
|
|
13
|
-
readonly mainEntity: EntityModel;
|
|
14
|
-
readonly resultEntity: EntityModel;
|
|
15
|
-
readonly converter: RowConverter;
|
|
16
|
-
readonly mainAlias: string;
|
|
17
|
-
resultAlias: string;
|
|
18
|
-
private _joins;
|
|
19
|
-
private _selectColumns;
|
|
20
|
-
private _filter;
|
|
21
|
-
private _sort?;
|
|
22
|
-
protected constructor(selectEntity: EntityModel, outputEntity: EntityModel);
|
|
23
|
-
static create(source: EntityModel | AssociationNode, opts?: {
|
|
24
|
-
maxSubQueries?: number;
|
|
25
|
-
maxEagerFetch?: number;
|
|
26
|
-
}): Promise<FindCommand>;
|
|
27
|
-
static execute(args: FindCommandArgs): Promise<any[]>;
|
|
28
|
-
addElements(opts?: {
|
|
29
|
-
tableAlias?: string;
|
|
30
|
-
converter?: RowConverter;
|
|
31
|
-
entity?: EntityModel;
|
|
32
|
-
elements?: string[];
|
|
33
|
-
include?: string[];
|
|
34
|
-
exclude?: string[];
|
|
35
|
-
sort?: string[];
|
|
36
|
-
prefix?: string;
|
|
37
|
-
suffix?: string;
|
|
38
|
-
}): Promise<void>;
|
|
39
|
-
private _selectColumn;
|
|
40
|
-
filter(filter: any): Promise<void>;
|
|
41
|
-
sort(sortElements: string[]): Promise<void>;
|
|
42
|
-
execute(args: Pick<FindCommandArgs, 'connection' | 'distinct' | 'offset' | 'limit' | 'params' | 'onTransformRow'>): Promise<any[]>;
|
|
43
|
-
private _getEntityFromAlias;
|
|
44
|
-
}
|
|
@@ -1,308 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.FindCommand = void 0;
|
|
4
|
-
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const row_converter_1 = require("./row-converter");
|
|
6
|
-
const association_node_1 = require("../model/association-node");
|
|
7
|
-
const orm_helper_1 = require("../util/orm.helper");
|
|
8
|
-
const command_helper_1 = require("./command.helper");
|
|
9
|
-
const base_entity_class_1 = require("../base-entity.class");
|
|
10
|
-
const SORT_ORDER_PATTERN = /^([-+])?(.*)$/;
|
|
11
|
-
class FindCommand {
|
|
12
|
-
constructor(selectEntity, outputEntity) {
|
|
13
|
-
this.maxEagerFetch = 100000;
|
|
14
|
-
this.maxSubQueries = 5;
|
|
15
|
-
this.mainAlias = 'T';
|
|
16
|
-
this.resultAlias = 'T';
|
|
17
|
-
this._joins = [];
|
|
18
|
-
this._selectColumns = {};
|
|
19
|
-
this._filter = (0, builder_1.And)();
|
|
20
|
-
this.mainEntity = selectEntity;
|
|
21
|
-
this.resultEntity = outputEntity;
|
|
22
|
-
this.converter = new row_converter_1.RowConverter(outputEntity.ctor);
|
|
23
|
-
}
|
|
24
|
-
static async create(source, opts = {}) {
|
|
25
|
-
let command;
|
|
26
|
-
let listingEntity;
|
|
27
|
-
if (source instanceof association_node_1.AssociationNode) {
|
|
28
|
-
const node = source;
|
|
29
|
-
listingEntity = await node.resolveTarget();
|
|
30
|
-
const resultEntity = await node.getLast().resolveTarget();
|
|
31
|
-
command = new FindCommand(listingEntity, resultEntity);
|
|
32
|
-
if (node.conditions)
|
|
33
|
-
await command.filter(node.conditions);
|
|
34
|
-
if (node.next) {
|
|
35
|
-
const join = await (0, command_helper_1.joinAssociationGetLast)(command._joins, node.next, command.mainAlias);
|
|
36
|
-
command.resultAlias = join.joinAlias;
|
|
37
|
-
}
|
|
38
|
-
}
|
|
39
|
-
else {
|
|
40
|
-
listingEntity = source;
|
|
41
|
-
command = new FindCommand(listingEntity, listingEntity);
|
|
42
|
-
}
|
|
43
|
-
if (!listingEntity.tableName)
|
|
44
|
-
throw new Error(`${listingEntity.ctor.name} is not decorated with @Entity decorator`);
|
|
45
|
-
if (typeof opts.maxSubQueries === 'number')
|
|
46
|
-
command.maxSubQueries = opts.maxSubQueries;
|
|
47
|
-
if (typeof opts.maxEagerFetch === 'number')
|
|
48
|
-
command.maxEagerFetch = opts.maxEagerFetch;
|
|
49
|
-
return command;
|
|
50
|
-
}
|
|
51
|
-
static async execute(args) {
|
|
52
|
-
const command = await FindCommand.create(args.entity, {
|
|
53
|
-
maxSubQueries: args.maxSubQueries,
|
|
54
|
-
maxEagerFetch: args.maxEagerFetch,
|
|
55
|
-
});
|
|
56
|
-
await command.addElements({
|
|
57
|
-
elements: args.elements,
|
|
58
|
-
include: args.include,
|
|
59
|
-
exclude: args.exclude,
|
|
60
|
-
sort: args.sort
|
|
61
|
-
});
|
|
62
|
-
if (args.filter)
|
|
63
|
-
await command.filter(args.filter);
|
|
64
|
-
if (args.sort)
|
|
65
|
-
await command.sort(args.sort);
|
|
66
|
-
return await command.execute(args);
|
|
67
|
-
}
|
|
68
|
-
async addElements(opts = {}) {
|
|
69
|
-
const tableAlias = opts.tableAlias || this.resultAlias;
|
|
70
|
-
const entity = opts.entity || this._getEntityFromAlias(tableAlias);
|
|
71
|
-
const converter = opts.converter || this.converter;
|
|
72
|
-
const _elements = opts.elements ?
|
|
73
|
-
opts.elements.map(x => x.toLowerCase()) : undefined;
|
|
74
|
-
const _include = opts.include ?
|
|
75
|
-
opts.include.map(x => x.toLowerCase()) : undefined;
|
|
76
|
-
const _exclude = opts.exclude ?
|
|
77
|
-
opts.exclude.map(x => x.toLowerCase()) : undefined;
|
|
78
|
-
const requestElements = _elements ? [..._elements] :
|
|
79
|
-
(0, base_entity_class_1.getNonAssociationElementNames)(entity.ctor)
|
|
80
|
-
.map(x => x.toLowerCase());
|
|
81
|
-
if (_include)
|
|
82
|
-
requestElements.push(..._include);
|
|
83
|
-
const sortElements = opts.sort && opts.sort.length ?
|
|
84
|
-
opts.sort.map(x => x.toLowerCase()) : undefined;
|
|
85
|
-
const prefix = opts.prefix || '';
|
|
86
|
-
const suffix = opts.suffix || '';
|
|
87
|
-
for (const key of entity.elementKeys) {
|
|
88
|
-
const col = entity.getElement(key);
|
|
89
|
-
if (!col)
|
|
90
|
-
continue;
|
|
91
|
-
const colNameLower = col.name.toLowerCase();
|
|
92
|
-
// Ignore element if in excluded list
|
|
93
|
-
if (_exclude && _exclude.includes(colNameLower))
|
|
94
|
-
continue;
|
|
95
|
-
// Check if element request list
|
|
96
|
-
if (!requestElements.find((x) => x === colNameLower || x.startsWith(colNameLower + '.')))
|
|
97
|
-
continue;
|
|
98
|
-
// Add field to select list if element is a column
|
|
99
|
-
if ((0, orm_helper_1.isColumnElement)(col)) {
|
|
100
|
-
const fieldAlias = this._selectColumn(tableAlias, col, prefix, suffix);
|
|
101
|
-
// Add column to converter
|
|
102
|
-
if (!col.hidden)
|
|
103
|
-
converter.addValueProperty({
|
|
104
|
-
name: col.name,
|
|
105
|
-
fieldAlias,
|
|
106
|
-
dataType: col.dataType,
|
|
107
|
-
parse: col.parse
|
|
108
|
-
});
|
|
109
|
-
continue;
|
|
110
|
-
}
|
|
111
|
-
if ((0, orm_helper_1.isObjectElement)(col)) {
|
|
112
|
-
const typ = await col.resolveType();
|
|
113
|
-
const subConverter = converter.addObjectProperty({
|
|
114
|
-
name: col.name,
|
|
115
|
-
type: typ.ctor
|
|
116
|
-
}).converter;
|
|
117
|
-
await this.addElements({
|
|
118
|
-
tableAlias,
|
|
119
|
-
converter: subConverter,
|
|
120
|
-
entity: typ,
|
|
121
|
-
prefix: col.fieldNamePrefix,
|
|
122
|
-
suffix: col.fieldNameSuffix,
|
|
123
|
-
elements: _elements?.includes(colNameLower) ? undefined :
|
|
124
|
-
extractSubElements(colNameLower, _elements),
|
|
125
|
-
include: extractSubElements(colNameLower, _include),
|
|
126
|
-
exclude: extractSubElements(colNameLower, _exclude),
|
|
127
|
-
sort: extractSubElements(colNameLower, sortElements),
|
|
128
|
-
});
|
|
129
|
-
continue;
|
|
130
|
-
}
|
|
131
|
-
if ((0, orm_helper_1.isAssociationElement)(col)) {
|
|
132
|
-
// OtO relation
|
|
133
|
-
if (!col.association.returnsMany()) {
|
|
134
|
-
const joinInfo = await (0, command_helper_1.joinAssociationGetLast)(this._joins, col.association, tableAlias);
|
|
135
|
-
const subConverter = converter.addObjectProperty({
|
|
136
|
-
name: col.name,
|
|
137
|
-
type: joinInfo.targetEntity.ctor
|
|
138
|
-
}).converter;
|
|
139
|
-
// Add join fields to select columns list
|
|
140
|
-
await this.addElements({
|
|
141
|
-
tableAlias: joinInfo.joinAlias,
|
|
142
|
-
converter: subConverter,
|
|
143
|
-
entity: joinInfo.targetEntity,
|
|
144
|
-
elements: _elements?.includes(colNameLower) ? undefined :
|
|
145
|
-
extractSubElements(colNameLower, _elements),
|
|
146
|
-
include: extractSubElements(colNameLower, _include),
|
|
147
|
-
exclude: extractSubElements(colNameLower, _exclude),
|
|
148
|
-
sort: extractSubElements(colNameLower, sortElements),
|
|
149
|
-
});
|
|
150
|
-
continue;
|
|
151
|
-
}
|
|
152
|
-
// One-2-Many Eager relation
|
|
153
|
-
if (this.maxSubQueries > 0) {
|
|
154
|
-
const targetCol = await col.association.resolveTargetProperty();
|
|
155
|
-
const sourceCol = await col.association.resolveSourceProperty();
|
|
156
|
-
// We need to know key value to filter sub query.
|
|
157
|
-
// So add key field into select columns
|
|
158
|
-
const parentField = this._selectColumn(tableAlias, sourceCol);
|
|
159
|
-
const findCommand = await FindCommand.create(col.association, {
|
|
160
|
-
maxSubQueries: this.maxSubQueries - 1,
|
|
161
|
-
maxEagerFetch: this.maxEagerFetch
|
|
162
|
-
});
|
|
163
|
-
await findCommand.filter((0, builder_1.In)(targetCol.name, (0, builder_1.Param)(parentField)));
|
|
164
|
-
const sort = sortElements && extractSubElements(colNameLower, sortElements);
|
|
165
|
-
await findCommand.addElements({
|
|
166
|
-
elements: _elements?.includes(colNameLower) ? undefined :
|
|
167
|
-
extractSubElements(colNameLower, _elements),
|
|
168
|
-
include: extractSubElements(colNameLower, _include),
|
|
169
|
-
exclude: extractSubElements(colNameLower, _exclude),
|
|
170
|
-
sort,
|
|
171
|
-
});
|
|
172
|
-
if (sort)
|
|
173
|
-
await findCommand.sort(sort);
|
|
174
|
-
const keyField = findCommand._selectColumn(findCommand.mainAlias, targetCol);
|
|
175
|
-
const resultType = await col.association.getLast().resolveTarget();
|
|
176
|
-
converter.addNestedProperty({
|
|
177
|
-
name: col.name,
|
|
178
|
-
type: resultType.ctor,
|
|
179
|
-
findCommand,
|
|
180
|
-
parentField,
|
|
181
|
-
keyField,
|
|
182
|
-
sort: extractSubElements(colNameLower, sortElements)
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
}
|
|
188
|
-
_selectColumn(tableAlias, el, prefix, suffix) {
|
|
189
|
-
const fieldName = (prefix || '').toLowerCase() +
|
|
190
|
-
el.fieldName.toUpperCase() +
|
|
191
|
-
(suffix || '').toLowerCase();
|
|
192
|
-
const fieldAlias = tableAlias + '_' + fieldName;
|
|
193
|
-
this._selectColumns[fieldAlias] = {
|
|
194
|
-
element: el,
|
|
195
|
-
statement: tableAlias + '.' + fieldName + ' as ' + fieldAlias
|
|
196
|
-
};
|
|
197
|
-
return fieldAlias;
|
|
198
|
-
}
|
|
199
|
-
async filter(filter) {
|
|
200
|
-
await (0, command_helper_1.prepareFilter)(this.mainEntity, filter, this._filter);
|
|
201
|
-
}
|
|
202
|
-
async sort(sortElements) {
|
|
203
|
-
const orderColumns = [];
|
|
204
|
-
for (const item of sortElements) {
|
|
205
|
-
const m = item.match(SORT_ORDER_PATTERN);
|
|
206
|
-
if (!m)
|
|
207
|
-
throw new Error(`"${item}" is not a valid order expression`);
|
|
208
|
-
let elName = m[2];
|
|
209
|
-
let prefix = '';
|
|
210
|
-
let suffix = '';
|
|
211
|
-
let _entityDef = this.resultEntity;
|
|
212
|
-
let tableAlias = this.resultAlias;
|
|
213
|
-
if (elName.includes('.')) {
|
|
214
|
-
const a = elName.split('.');
|
|
215
|
-
while (a.length > 1) {
|
|
216
|
-
const col = _entityDef.getElement(a.shift() || '');
|
|
217
|
-
if ((0, orm_helper_1.isObjectElement)(col)) {
|
|
218
|
-
_entityDef = await col.resolveType();
|
|
219
|
-
if (col.fieldNamePrefix)
|
|
220
|
-
prefix += col.fieldNamePrefix;
|
|
221
|
-
if (col.fieldNameSuffix)
|
|
222
|
-
suffix = col.fieldNameSuffix + suffix;
|
|
223
|
-
}
|
|
224
|
-
else if ((0, orm_helper_1.isAssociationElement)(col)) {
|
|
225
|
-
if (col.association.returnsMany()) {
|
|
226
|
-
elName = '';
|
|
227
|
-
break;
|
|
228
|
-
}
|
|
229
|
-
const joinInfo = await (0, command_helper_1.joinAssociationGetLast)(this._joins, col.association, tableAlias);
|
|
230
|
-
tableAlias = joinInfo.joinAlias;
|
|
231
|
-
_entityDef = joinInfo.targetEntity;
|
|
232
|
-
}
|
|
233
|
-
else
|
|
234
|
-
throw new Error(`Invalid column (${elName}) declared in sort property`);
|
|
235
|
-
}
|
|
236
|
-
if (!elName)
|
|
237
|
-
continue;
|
|
238
|
-
elName = a.shift() || '';
|
|
239
|
-
}
|
|
240
|
-
const col = _entityDef.getElement(elName);
|
|
241
|
-
if (!col)
|
|
242
|
-
throw new Error(`Unknown element (${elName}) declared in sort property`);
|
|
243
|
-
if (!(0, orm_helper_1.isColumnElement)(col))
|
|
244
|
-
throw new Error(`Can not sort by "${elName}", because it is not a data column`);
|
|
245
|
-
const dir = m[1] || '+';
|
|
246
|
-
orderColumns.push((dir || '') + tableAlias + '.' + prefix + col.fieldName + suffix);
|
|
247
|
-
}
|
|
248
|
-
this._sort = orderColumns;
|
|
249
|
-
}
|
|
250
|
-
async execute(args) {
|
|
251
|
-
// Generate select query
|
|
252
|
-
const columnSqls = Object.keys(this._selectColumns)
|
|
253
|
-
.map(x => this._selectColumns[x].statement);
|
|
254
|
-
if (!columnSqls.length)
|
|
255
|
-
columnSqls.push('1');
|
|
256
|
-
const query = (0, builder_1.Select)(...columnSqls)
|
|
257
|
-
.from(this.mainEntity.tableName + ' as ' + this.mainAlias);
|
|
258
|
-
if (args.distinct)
|
|
259
|
-
query.distinct();
|
|
260
|
-
query.where(...this._filter._items);
|
|
261
|
-
if (this._sort) {
|
|
262
|
-
query.orderBy(...this._sort);
|
|
263
|
-
}
|
|
264
|
-
if (args.offset)
|
|
265
|
-
query.offset(args.offset);
|
|
266
|
-
// joins must be added last
|
|
267
|
-
if (this._joins)
|
|
268
|
-
for (const j of this._joins) {
|
|
269
|
-
query.join(j.join);
|
|
270
|
-
}
|
|
271
|
-
// Execute query
|
|
272
|
-
const resp = await args.connection.execute(query, {
|
|
273
|
-
params: args?.params,
|
|
274
|
-
fetchRows: args?.limit,
|
|
275
|
-
objectRows: false,
|
|
276
|
-
cursor: false
|
|
277
|
-
});
|
|
278
|
-
// Create objects
|
|
279
|
-
if (resp.rows && resp.fields) {
|
|
280
|
-
return this.converter.transform(args.connection, resp.fields, resp.rows, args.onTransformRow);
|
|
281
|
-
}
|
|
282
|
-
return [];
|
|
283
|
-
}
|
|
284
|
-
_getEntityFromAlias(tableAlias) {
|
|
285
|
-
if (tableAlias === this.mainAlias)
|
|
286
|
-
return this.mainEntity;
|
|
287
|
-
if (tableAlias === this.resultAlias)
|
|
288
|
-
return this.resultEntity;
|
|
289
|
-
if (this._joins) {
|
|
290
|
-
const join = this._joins.find(j => j.joinAlias === tableAlias);
|
|
291
|
-
if (join)
|
|
292
|
-
return join.targetEntity;
|
|
293
|
-
}
|
|
294
|
-
throw new Error(`Unknown table alias "${tableAlias}"`);
|
|
295
|
-
}
|
|
296
|
-
}
|
|
297
|
-
exports.FindCommand = FindCommand;
|
|
298
|
-
function extractSubElements(colNameLower, elements) {
|
|
299
|
-
if (!(elements && elements.length))
|
|
300
|
-
return;
|
|
301
|
-
if (elements) {
|
|
302
|
-
return elements.reduce((trg, v) => {
|
|
303
|
-
if (v.startsWith(colNameLower + '.'))
|
|
304
|
-
trg.push(v.substring(colNameLower.length + 1).toLowerCase());
|
|
305
|
-
return trg;
|
|
306
|
-
}, []);
|
|
307
|
-
}
|
|
308
|
-
}
|
|
@@ -1,55 +0,0 @@
|
|
|
1
|
-
import { Type } from 'ts-gems';
|
|
2
|
-
import type { FieldInfoMap } from '../../client/FieldInfoMap';
|
|
3
|
-
import type { FindCommand } from './find.command';
|
|
4
|
-
import type { Repository } from '../repository.class';
|
|
5
|
-
import { ColumnTransformFunction } from '../orm.type';
|
|
6
|
-
import { SqbConnection } from '../../client/SqbConnection';
|
|
7
|
-
import { DataType } from '@sqb/builder';
|
|
8
|
-
export interface ValueProperty {
|
|
9
|
-
fieldAlias: string;
|
|
10
|
-
dataType?: DataType;
|
|
11
|
-
parse?: ColumnTransformFunction;
|
|
12
|
-
}
|
|
13
|
-
export interface ObjectProperty {
|
|
14
|
-
converter: RowConverter;
|
|
15
|
-
type: Type;
|
|
16
|
-
}
|
|
17
|
-
export interface NestedProperty {
|
|
18
|
-
converter: RowConverter;
|
|
19
|
-
type: Type;
|
|
20
|
-
parentField: string;
|
|
21
|
-
keyField: string;
|
|
22
|
-
findCommand: FindCommand;
|
|
23
|
-
sort?: string[];
|
|
24
|
-
paramValues: any[];
|
|
25
|
-
}
|
|
26
|
-
export declare class RowConverter {
|
|
27
|
-
resultType: Type;
|
|
28
|
-
parent?: RowConverter | undefined;
|
|
29
|
-
private _properties;
|
|
30
|
-
private _propertyKeys?;
|
|
31
|
-
constructor(resultType: Type, parent?: RowConverter | undefined);
|
|
32
|
-
addValueProperty(args: {
|
|
33
|
-
name: string;
|
|
34
|
-
fieldAlias: string;
|
|
35
|
-
dataType?: DataType;
|
|
36
|
-
parse?: ColumnTransformFunction;
|
|
37
|
-
}): ValueProperty;
|
|
38
|
-
addObjectProperty(args: {
|
|
39
|
-
name: string;
|
|
40
|
-
type: Type;
|
|
41
|
-
}): ObjectProperty;
|
|
42
|
-
addNestedProperty(args: {
|
|
43
|
-
name: string;
|
|
44
|
-
type: Type;
|
|
45
|
-
findCommand: FindCommand;
|
|
46
|
-
parentField: string;
|
|
47
|
-
keyField: string;
|
|
48
|
-
sort?: string[];
|
|
49
|
-
}): NestedProperty;
|
|
50
|
-
get keys(): string[];
|
|
51
|
-
transform(connection: SqbConnection, fields: FieldInfoMap, rows: any, onTransform?: Repository.TransformRowFunction): Promise<any[]>;
|
|
52
|
-
private _rowToObject;
|
|
53
|
-
private _iterateForNested;
|
|
54
|
-
private _checkCircularDep;
|
|
55
|
-
}
|
|
@@ -1,187 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.RowConverter = void 0;
|
|
4
|
-
const builder_1 = require("@sqb/builder");
|
|
5
|
-
const isValueProperty = (prop) => {
|
|
6
|
-
return prop.fieldAlias && !prop.converter;
|
|
7
|
-
};
|
|
8
|
-
const isObjectProperty = (prop) => {
|
|
9
|
-
return prop.converter && !prop.findCommand;
|
|
10
|
-
};
|
|
11
|
-
const isNestedProperty = (prop) => {
|
|
12
|
-
return prop.converter && prop.findCommand;
|
|
13
|
-
};
|
|
14
|
-
class RowConverter {
|
|
15
|
-
constructor(resultType, parent) {
|
|
16
|
-
this.resultType = resultType;
|
|
17
|
-
this.parent = parent;
|
|
18
|
-
this._properties = {};
|
|
19
|
-
}
|
|
20
|
-
addValueProperty(args) {
|
|
21
|
-
this._propertyKeys = undefined;
|
|
22
|
-
const item = {
|
|
23
|
-
fieldAlias: args.fieldAlias,
|
|
24
|
-
dataType: args.dataType,
|
|
25
|
-
parse: args.parse
|
|
26
|
-
};
|
|
27
|
-
this._properties[args.name] = item;
|
|
28
|
-
return item;
|
|
29
|
-
}
|
|
30
|
-
addObjectProperty(args) {
|
|
31
|
-
this._checkCircularDep(args.type);
|
|
32
|
-
this._propertyKeys = undefined;
|
|
33
|
-
const converter = new RowConverter(args.type, this);
|
|
34
|
-
const item = {
|
|
35
|
-
converter,
|
|
36
|
-
type: args.type
|
|
37
|
-
};
|
|
38
|
-
this._properties[args.name] = item;
|
|
39
|
-
return item;
|
|
40
|
-
}
|
|
41
|
-
addNestedProperty(args) {
|
|
42
|
-
this._checkCircularDep(args.type);
|
|
43
|
-
const converter = new RowConverter(args.type, this);
|
|
44
|
-
const item = {
|
|
45
|
-
converter,
|
|
46
|
-
type: args.type,
|
|
47
|
-
parentField: args.parentField,
|
|
48
|
-
keyField: args.keyField,
|
|
49
|
-
findCommand: args.findCommand,
|
|
50
|
-
sort: args.sort,
|
|
51
|
-
paramValues: []
|
|
52
|
-
};
|
|
53
|
-
this._properties[args.name] = item;
|
|
54
|
-
return item;
|
|
55
|
-
}
|
|
56
|
-
get keys() {
|
|
57
|
-
if (!this._propertyKeys)
|
|
58
|
-
this._propertyKeys = Object.keys(this._properties);
|
|
59
|
-
return this._propertyKeys;
|
|
60
|
-
}
|
|
61
|
-
async transform(connection, fields, rows, onTransform) {
|
|
62
|
-
const rowLen = rows.length;
|
|
63
|
-
const result = [];
|
|
64
|
-
for (let rowIdx = 0; rowIdx < rowLen; rowIdx++) {
|
|
65
|
-
const row = rows[rowIdx];
|
|
66
|
-
const o = (await this._rowToObject(connection, fields, row)) || {};
|
|
67
|
-
if (onTransform)
|
|
68
|
-
onTransform(fields, row, o);
|
|
69
|
-
result.push(o);
|
|
70
|
-
}
|
|
71
|
-
if (!this.keys)
|
|
72
|
-
return result;
|
|
73
|
-
await this._iterateForNested(this, connection, fields, rows, result);
|
|
74
|
-
// Return only non empty objects
|
|
75
|
-
return result.filter(x => !!x);
|
|
76
|
-
}
|
|
77
|
-
_rowToObject(executor, fields, row) {
|
|
78
|
-
// Cache keys for better performance
|
|
79
|
-
const elementKeys = this.keys;
|
|
80
|
-
const elementLen = elementKeys.length;
|
|
81
|
-
let result;
|
|
82
|
-
for (let elIdx = 0; elIdx < elementLen; elIdx++) {
|
|
83
|
-
const elKey = elementKeys[elIdx];
|
|
84
|
-
const prop = this._properties[elKey];
|
|
85
|
-
if (isValueProperty(prop)) {
|
|
86
|
-
const field = fields.get(prop.fieldAlias);
|
|
87
|
-
if (field) {
|
|
88
|
-
let v = row[field.index];
|
|
89
|
-
if (typeof prop.parse === 'function')
|
|
90
|
-
v = prop.parse(v, elKey);
|
|
91
|
-
if (v != null) {
|
|
92
|
-
result = result || {};
|
|
93
|
-
if (prop.dataType === builder_1.DataType.JSON && typeof v === 'string')
|
|
94
|
-
v = JSON.parse(v);
|
|
95
|
-
result[elKey] = v;
|
|
96
|
-
}
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
else if (isNestedProperty(prop)) {
|
|
100
|
-
// One2Many Eager element
|
|
101
|
-
// Keep a list of key field/value pairs to fetch rows for eager relation
|
|
102
|
-
const _params = prop.paramValues;
|
|
103
|
-
const f = fields.get(prop.parentField);
|
|
104
|
-
const v = f && row[f.index];
|
|
105
|
-
if (v != null && !_params.includes(v))
|
|
106
|
-
_params.push(v);
|
|
107
|
-
}
|
|
108
|
-
else if (isObjectProperty(prop)) {
|
|
109
|
-
const v = prop.converter._rowToObject(executor, fields, row);
|
|
110
|
-
if (v != null) {
|
|
111
|
-
result = result || {};
|
|
112
|
-
result[elKey] = v;
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
if (result)
|
|
117
|
-
Object.setPrototypeOf(result, this.resultType.prototype);
|
|
118
|
-
return result;
|
|
119
|
-
}
|
|
120
|
-
async _iterateForNested(node, connection, fields, rows, result) {
|
|
121
|
-
// Fetch one-2-many related rows and merge with result rows
|
|
122
|
-
const keys = node.keys;
|
|
123
|
-
const propertyLen = keys.length;
|
|
124
|
-
const promises = [];
|
|
125
|
-
for (let propIdx = 0; propIdx < propertyLen; propIdx++) {
|
|
126
|
-
const propKey = keys[propIdx];
|
|
127
|
-
const prop = node._properties[propKey];
|
|
128
|
-
if (isNestedProperty(prop)) {
|
|
129
|
-
if (!(prop.paramValues && prop.paramValues.length))
|
|
130
|
-
continue;
|
|
131
|
-
const resultType = this.resultType;
|
|
132
|
-
const promise = async function (p) {
|
|
133
|
-
const findCommand = p.findCommand;
|
|
134
|
-
let fld;
|
|
135
|
-
const map = new Map();
|
|
136
|
-
const r = await findCommand.execute({
|
|
137
|
-
connection,
|
|
138
|
-
limit: findCommand.maxEagerFetch + 1,
|
|
139
|
-
params: { [p.parentField]: p.paramValues },
|
|
140
|
-
onTransformRow: (_fields, row, obj) => {
|
|
141
|
-
fld = fld || _fields.get('' + p.keyField);
|
|
142
|
-
const keyValue = fld && row[fld.index];
|
|
143
|
-
if (keyValue != null) {
|
|
144
|
-
let arr = map.get(keyValue);
|
|
145
|
-
if (!arr) {
|
|
146
|
-
arr = [];
|
|
147
|
-
map.set(keyValue, arr);
|
|
148
|
-
}
|
|
149
|
-
arr.push(obj);
|
|
150
|
-
}
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
if (r.length > findCommand.maxEagerFetch)
|
|
154
|
-
throw new Error(`Number of returning rows for "${propKey}" exceeds maxEagerFetch limit`);
|
|
155
|
-
for (let i = 0; i < result.length; i++) {
|
|
156
|
-
const row = rows[i];
|
|
157
|
-
let obj = result[i];
|
|
158
|
-
if (!obj) {
|
|
159
|
-
obj = {};
|
|
160
|
-
Object.setPrototypeOf(result, resultType.prototype);
|
|
161
|
-
}
|
|
162
|
-
const f = fields.get('' + p.parentField);
|
|
163
|
-
const keyValue = f && row[f.index];
|
|
164
|
-
if (keyValue != null) {
|
|
165
|
-
const arr = map.get(keyValue);
|
|
166
|
-
if (arr) {
|
|
167
|
-
obj[propKey] = arr;
|
|
168
|
-
}
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
}(prop);
|
|
172
|
-
promises.push(promise);
|
|
173
|
-
}
|
|
174
|
-
else if (isObjectProperty(prop)) {
|
|
175
|
-
promises.push(this._iterateForNested(prop.converter, connection, fields, rows, result));
|
|
176
|
-
}
|
|
177
|
-
}
|
|
178
|
-
await Promise.all(promises);
|
|
179
|
-
}
|
|
180
|
-
_checkCircularDep(t) {
|
|
181
|
-
if (this.resultType === t)
|
|
182
|
-
throw new Error('Circular elements requested');
|
|
183
|
-
if (this.parent)
|
|
184
|
-
this.parent._checkCircularDep(t);
|
|
185
|
-
}
|
|
186
|
-
}
|
|
187
|
-
exports.RowConverter = RowConverter;
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
import { EntityModel } from '../model/entity-model';
|
|
2
|
-
import { Repository } from '../repository.class';
|
|
3
|
-
import { SqbConnection } from '../../client/SqbConnection';
|
|
4
|
-
export declare type UpdateCommandArgs = {
|
|
5
|
-
entity: EntityModel;
|
|
6
|
-
connection: SqbConnection;
|
|
7
|
-
values: any;
|
|
8
|
-
} & Repository.UpdateAllOptions;
|
|
9
|
-
declare type UpdateCommandContext = {
|
|
10
|
-
entity: EntityModel;
|
|
11
|
-
queryParams: any;
|
|
12
|
-
queryValues: any;
|
|
13
|
-
queryFilter: any[];
|
|
14
|
-
colCount: number;
|
|
15
|
-
};
|
|
16
|
-
export declare class UpdateCommand {
|
|
17
|
-
protected constructor();
|
|
18
|
-
static execute(args: UpdateCommandArgs): Promise<number>;
|
|
19
|
-
protected static _prepareFilter(ctx: UpdateCommandContext, filter: any): Promise<void>;
|
|
20
|
-
protected static _prepareParams(ctx: UpdateCommandContext, entity: EntityModel, values: any, prefix?: string, suffix?: string): Promise<void>;
|
|
21
|
-
}
|
|
22
|
-
export {};
|