@provis/provis-common-be-module 2.1.2 → 2.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/class/main.repository.d.ts +4 -2
- package/dist/class/main.repository.js +52 -10
- package/dist/interface/find.all.count.interface.d.ts +10 -0
- package/dist/interface/find.all.count.interface.js +2 -0
- package/dist/interface/multi.join.interface.d.ts +6 -0
- package/dist/interface/multi.join.interface.js +2 -0
- package/dist/interface/search.conditions.interface.d.ts +8 -0
- package/dist/interface/search.conditions.interface.js +2 -0
- package/dist/interface/search.summary.interface.d.ts +9 -0
- package/dist/interface/search.summary.interface.js +2 -0
- package/dist/interface/summary.field.interface.d.ts +4 -0
- package/dist/interface/summary.field.interface.js +3 -0
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
import { DeepPartial, FindConditions, Repository } from 'typeorm';
|
|
2
2
|
import ISearchQuery from '../interface/search.query.interface';
|
|
3
3
|
import { QueryDeepPartialEntity } from 'typeorm/query-builder/QueryPartialEntity';
|
|
4
|
+
import IFindAllCount from '../interface/find.all.count.interface';
|
|
5
|
+
import ISearchSummary from '../interface/search.summary.interface';
|
|
4
6
|
export declare class MainRepository<T> extends Repository<T> {
|
|
5
7
|
saveRepo(data: DeepPartial<T>): Promise<T>;
|
|
6
8
|
updateRepo(obj: DeepPartial<T>, updated: QueryDeepPartialEntity<T>): Promise<T>;
|
|
@@ -14,8 +16,8 @@ export declare class MainRepository<T> extends Repository<T> {
|
|
|
14
16
|
orderDirection: 'ASC' | 'DESC';
|
|
15
17
|
};
|
|
16
18
|
searchBracket(searchOr: any[], searchValues: (value: any) => Promise<ISearchQuery[]>): Promise<ISearchQuery[][]>;
|
|
17
|
-
summaryField(
|
|
18
|
-
findAllAndCount(search
|
|
19
|
+
summaryField({ fieldToSum, search, additionalSearchOr, relations, }: ISearchSummary): Promise<any>;
|
|
20
|
+
findAllAndCount({ search, additionalSearchOr, relations, sortBy, offset, maxCount, }: IFindAllCount): Promise<[number, T[]]>;
|
|
19
21
|
private applySearchConditions;
|
|
20
22
|
private generateRawCount;
|
|
21
23
|
private generateRawOrder;
|
|
@@ -116,22 +116,32 @@ class MainRepository extends typeorm_1.Repository {
|
|
|
116
116
|
return result;
|
|
117
117
|
});
|
|
118
118
|
}
|
|
119
|
-
summaryField(search,
|
|
119
|
+
summaryField({ fieldToSum, search, additionalSearchOr, relations, }) {
|
|
120
120
|
return __awaiter(this, void 0, void 0, function* () {
|
|
121
121
|
let query = this.createQueryBuilder()
|
|
122
122
|
.select([
|
|
123
123
|
'COUNT(1) AS totalRows',
|
|
124
|
-
...fieldToSum.map((field) => `SUM(${field}) AS "${field.
|
|
124
|
+
...fieldToSum.map((field) => `SUM(${field.columnName}) AS "${field.aliasColumnName || field.columnName}"`),
|
|
125
125
|
]);
|
|
126
|
-
query = this.applySearchConditions(
|
|
126
|
+
query = this.applySearchConditions({
|
|
127
|
+
query,
|
|
128
|
+
search,
|
|
129
|
+
additionalSearchOr,
|
|
130
|
+
relations,
|
|
131
|
+
});
|
|
127
132
|
return query.getRawOne();
|
|
128
133
|
});
|
|
129
134
|
}
|
|
130
|
-
findAllAndCount(search,
|
|
135
|
+
findAllAndCount({ search, additionalSearchOr, relations, sortBy, offset = 1, maxCount = 50, }) {
|
|
131
136
|
return __awaiter(this, void 0, void 0, function* () {
|
|
132
137
|
const skipItems = ((offset < 1 ? 1 : offset) - 1) * maxCount;
|
|
133
138
|
let query = this.createQueryBuilder().select(this.selectFieldOnFindAllAndCount());
|
|
134
|
-
query = this.applySearchConditions(
|
|
139
|
+
query = this.applySearchConditions({
|
|
140
|
+
query,
|
|
141
|
+
search,
|
|
142
|
+
additionalSearchOr,
|
|
143
|
+
relations,
|
|
144
|
+
});
|
|
135
145
|
const { orderBy, orderDirection } = this.getOrderData(sortBy);
|
|
136
146
|
return [
|
|
137
147
|
yield query.getCount(),
|
|
@@ -143,17 +153,18 @@ class MainRepository extends typeorm_1.Repository {
|
|
|
143
153
|
];
|
|
144
154
|
});
|
|
145
155
|
}
|
|
146
|
-
applySearchConditions(query, search, additionalSearchOr) {
|
|
156
|
+
applySearchConditions({ query, search, additionalSearchOr, relations, }) {
|
|
157
|
+
const modelName = this.modelName();
|
|
147
158
|
if (search && search.length > 0) {
|
|
148
159
|
let index = 0;
|
|
149
160
|
search.forEach((value) => {
|
|
150
161
|
if (value) {
|
|
151
162
|
if (index === 0) {
|
|
152
|
-
query = query.where(`${
|
|
163
|
+
query = query.where(`${modelName}.${value.query}`, { [value.key]: value.value });
|
|
153
164
|
index++;
|
|
154
165
|
}
|
|
155
166
|
else {
|
|
156
|
-
query = query.andWhere(`${
|
|
167
|
+
query = query.andWhere(`${modelName}.${value.query}`, { [value.key]: value.value });
|
|
157
168
|
}
|
|
158
169
|
}
|
|
159
170
|
});
|
|
@@ -166,11 +177,11 @@ class MainRepository extends typeorm_1.Repository {
|
|
|
166
177
|
group.forEach((value) => {
|
|
167
178
|
if (value) {
|
|
168
179
|
if (index === 0) {
|
|
169
|
-
qb = qb.where(`${
|
|
180
|
+
qb = qb.where(`${modelName}.${value.query}`, { [value.key]: value.value });
|
|
170
181
|
index++;
|
|
171
182
|
}
|
|
172
183
|
else {
|
|
173
|
-
qb = qb.orWhere(`${
|
|
184
|
+
qb = qb.orWhere(`${modelName}.${value.query}`, { [value.key]: value.value });
|
|
174
185
|
}
|
|
175
186
|
}
|
|
176
187
|
});
|
|
@@ -178,6 +189,37 @@ class MainRepository extends typeorm_1.Repository {
|
|
|
178
189
|
}
|
|
179
190
|
});
|
|
180
191
|
}
|
|
192
|
+
if (relations && relations.length > 0) {
|
|
193
|
+
relations.forEach((relation) => {
|
|
194
|
+
var _a, _b;
|
|
195
|
+
if (relation) {
|
|
196
|
+
query.innerJoin(`${modelName}.${relation.model}`, relation.model);
|
|
197
|
+
(_a = relation.searchs) === null || _a === void 0 ? void 0 : _a.forEach((valueSearch) => {
|
|
198
|
+
if (valueSearch) {
|
|
199
|
+
query = query.andWhere(`${relation.model}.${valueSearch.query}`, { [valueSearch.key]: valueSearch.value });
|
|
200
|
+
}
|
|
201
|
+
});
|
|
202
|
+
(_b = relation.additionalSearchOr) === null || _b === void 0 ? void 0 : _b.forEach((group) => {
|
|
203
|
+
if (group) {
|
|
204
|
+
query.andWhere(new typeorm_1.Brackets((qb) => {
|
|
205
|
+
let index = 0;
|
|
206
|
+
group.forEach((value) => {
|
|
207
|
+
if (value) {
|
|
208
|
+
if (index === 0) {
|
|
209
|
+
qb = qb.where(`${relation.model}.${value.query}`, { [value.key]: value.value });
|
|
210
|
+
index++;
|
|
211
|
+
}
|
|
212
|
+
else {
|
|
213
|
+
qb = qb.orWhere(`${relation.model}.${value.query}`, { [value.key]: value.value });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
});
|
|
217
|
+
}));
|
|
218
|
+
}
|
|
219
|
+
});
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
}
|
|
181
223
|
return query;
|
|
182
224
|
}
|
|
183
225
|
generateRawCount(query) {
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import IMultiJoin from "./multi.join.interface";
|
|
2
|
+
import ISearchQuery from "./search.query.interface";
|
|
3
|
+
export default interface IFindAllCount {
|
|
4
|
+
search: ISearchQuery[];
|
|
5
|
+
additionalSearchOr?: ISearchQuery[][];
|
|
6
|
+
sortBy?: string;
|
|
7
|
+
relations?: IMultiJoin[];
|
|
8
|
+
maxCount?: number;
|
|
9
|
+
offset?: number;
|
|
10
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import IMultiJoin from "./multi.join.interface";
|
|
2
|
+
import ISearchQuery from "./search.query.interface";
|
|
3
|
+
import ISummaryField from "./summary.field.interface";
|
|
4
|
+
export default interface ISearchSummary {
|
|
5
|
+
fieldToSum: ISummaryField[];
|
|
6
|
+
search: ISearchQuery[];
|
|
7
|
+
additionalSearchOr?: ISearchQuery[][];
|
|
8
|
+
relations?: IMultiJoin[];
|
|
9
|
+
}
|