Skip to content

Commit d1e7060

Browse files
Samarpan  BhattacharyaSamarpan  Bhattacharya
authored andcommitted
Revert "refactor(repository): use FilterBuilder.impose() to merge {deleted:false} filter (#52)"
This reverts commit 4f3b905.
1 parent 4f3b905 commit d1e7060

File tree

3 files changed

+290
-69
lines changed

3 files changed

+290
-69
lines changed

src/repositories/default-transaction-soft-crud.repository.base.ts

Lines changed: 145 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
11
import {
2+
AndClause,
3+
Condition,
24
DataObject,
35
Filter,
46
juggler,
7+
OrClause,
58
DefaultTransactionalRepository,
69
Where,
710
Getter,
@@ -13,7 +16,6 @@ import {SoftDeleteEntity} from '../models';
1316
import {IAuthUser} from 'loopback4-authentication';
1417
import {HttpErrors} from '@loopback/rest';
1518
import {ErrorKeys} from '../error-keys';
16-
import {produceSoftDeleteFilter, produceSoftDeleteWhere} from './repo-utils';
1719

1820
export abstract class DefaultTransactionSoftCrudRepository<
1921
T extends SoftDeleteEntity,
@@ -32,7 +34,33 @@ export abstract class DefaultTransactionSoftCrudRepository<
3234

3335
find(filter?: Filter<T>, options?: Options): Promise<(T & Relations)[]> {
3436
// Filter out soft deleted entries
35-
filter = produceSoftDeleteFilter(filter);
37+
if (
38+
filter?.where &&
39+
(filter.where as AndClause<T>).and &&
40+
(filter.where as AndClause<T>).and.length > 0
41+
) {
42+
(filter.where as AndClause<T>).and.push({
43+
deleted: false,
44+
} as Condition<T>);
45+
} else if (
46+
filter?.where &&
47+
(filter.where as OrClause<T>).or &&
48+
(filter.where as OrClause<T>).or.length > 0
49+
) {
50+
(filter.where as AndClause<T>).and = [];
51+
(filter.where as AndClause<T>).and.push(
52+
{
53+
deleted: false,
54+
} as Condition<T>,
55+
{
56+
or: (filter.where as OrClause<T>).or,
57+
},
58+
);
59+
} else {
60+
filter = filter ?? {};
61+
filter.where = filter.where ?? {};
62+
(filter.where as Condition<T>).deleted = false;
63+
}
3664

3765
// Now call super
3866
return super.find(filter, options);
@@ -47,7 +75,34 @@ export abstract class DefaultTransactionSoftCrudRepository<
4775
filter?: Filter<T>,
4876
options?: Options,
4977
): Promise<(T & Relations) | null> {
50-
filter = produceSoftDeleteFilter(filter);
78+
// Filter out soft deleted entries
79+
if (
80+
filter?.where &&
81+
(filter.where as AndClause<T>).and &&
82+
(filter.where as AndClause<T>).and.length > 0
83+
) {
84+
(filter.where as AndClause<T>).and.push({
85+
deleted: false,
86+
} as Condition<T>);
87+
} else if (
88+
filter?.where &&
89+
(filter.where as OrClause<T>).or &&
90+
(filter.where as OrClause<T>).or.length > 0
91+
) {
92+
(filter.where as AndClause<T>).and = [];
93+
(filter.where as AndClause<T>).and.push(
94+
{
95+
deleted: false,
96+
} as Condition<T>,
97+
{
98+
or: (filter.where as OrClause<T>).or,
99+
},
100+
);
101+
} else {
102+
filter = filter ?? {};
103+
filter.where = filter.where ?? {};
104+
(filter.where as Condition<T>).deleted = false;
105+
}
51106

52107
// Now call super
53108
return super.findOne(filter, options);
@@ -66,9 +121,40 @@ export abstract class DefaultTransactionSoftCrudRepository<
66121
filter?: Filter<T>,
67122
options?: Options,
68123
): Promise<T & Relations> {
69-
filter = produceSoftDeleteFilter(filter, {
70-
id,
71-
});
124+
// Filter out soft deleted entries
125+
// Filter out soft deleted entries
126+
if (
127+
filter?.where &&
128+
(filter.where as AndClause<T>).and &&
129+
(filter.where as AndClause<T>).and.length > 0
130+
) {
131+
(filter.where as AndClause<T>).and.push({
132+
deleted: false,
133+
id: id,
134+
} as Condition<T>);
135+
} else if (
136+
filter?.where &&
137+
(filter.where as OrClause<T>).or &&
138+
(filter.where as OrClause<T>).or.length > 0
139+
) {
140+
filter.where = {
141+
and: [
142+
{
143+
deleted: false,
144+
id: id,
145+
} as Condition<T>,
146+
{
147+
or: (filter.where as OrClause<T>).or,
148+
},
149+
],
150+
};
151+
} else {
152+
filter = filter ?? {};
153+
filter.where = {
154+
deleted: false,
155+
id: id,
156+
} as Condition<T>;
157+
}
72158

73159
//As parent method findById have filter: FilterExcludingWhere<T>
74160
//so we need add check here.
@@ -95,15 +181,66 @@ export abstract class DefaultTransactionSoftCrudRepository<
95181
where?: Where<T>,
96182
options?: Options,
97183
): Promise<Count> {
98-
where = produceSoftDeleteWhere(where);
184+
// Filter out soft deleted entries
185+
if (
186+
where &&
187+
(where as AndClause<T>).and &&
188+
(where as AndClause<T>).and.length > 0
189+
) {
190+
(where as AndClause<T>).and.push({
191+
deleted: false,
192+
} as Condition<T>);
193+
} else if (
194+
where &&
195+
(where as OrClause<T>).or &&
196+
(where as OrClause<T>).or.length > 0
197+
) {
198+
(where as AndClause<T>).and = [];
199+
(where as AndClause<T>).and.push(
200+
{
201+
deleted: false,
202+
} as Condition<T>,
203+
{
204+
or: (where as OrClause<T>).or,
205+
},
206+
);
207+
} else {
208+
where = where ?? {};
209+
(where as Condition<T>).deleted = false;
210+
}
99211

100212
// Now call super
101213
return super.updateAll(data, where, options);
102214
}
103215

104216
count(where?: Where<T>, options?: Options): Promise<Count> {
105217
// Filter out soft deleted entries
106-
where = produceSoftDeleteWhere(where);
218+
if (
219+
where &&
220+
(where as AndClause<T>).and &&
221+
(where as AndClause<T>).and.length > 0
222+
) {
223+
(where as AndClause<T>).and.push({
224+
deleted: false,
225+
} as Condition<T>);
226+
} else if (
227+
where &&
228+
(where as OrClause<T>).or &&
229+
(where as OrClause<T>).or.length > 0
230+
) {
231+
(where as AndClause<T>).and = [];
232+
(where as AndClause<T>).and.push(
233+
{
234+
deleted: false,
235+
} as Condition<T>,
236+
{
237+
or: (where as OrClause<T>).or,
238+
},
239+
);
240+
} else {
241+
where = where ?? {};
242+
(where as Condition<T>).deleted = false;
243+
}
107244

108245
// Now call super
109246
return super.count(where, options);

src/repositories/repo-utils.ts

Lines changed: 0 additions & 52 deletions
This file was deleted.

0 commit comments

Comments
 (0)