Skip to content

Commit 049ffe2

Browse files
authoredJun 1, 2021
feat(repository): add ability to query soft delete records
2 parents f9e1cc1 + f7067ad commit 049ffe2

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-2
lines changed
 

‎README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -45,11 +45,11 @@ Right now, this extension exports three abstract classes which are actually help
4545
If not, then please add these columns to the DB table.
4646
- **SoftCrudRepository** -
4747
An abstract base class for all repositories which require soft delete feature.
48-
This class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses at all.
48+
This class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses, However if there is a need to query soft deleted entries as well,there is an options to achieve that and you can use findAll() in place of find() , findOneIncludeSoftDelete() in place of findOne() and findByIdIncludeSoftDelete() in place of findById(), these will give you the responses including soft deleted entries.
4949
This class is a wrapper over DefaultCrudRepository class from [@loopback/repository](https://github.com/strongloop/loopback-next/tree/master/packages/repository).
5050
- **DefaultTransactionSoftCrudRepository** -
5151
An abstract base class for all repositories which require soft delete feature with transaction support.
52-
This class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses at all.
52+
This class is going to be the one which handles soft delete operations and ensures soft deleted entries are not returned in responses, However if there is a need to query soft deleted entries as well,there is an options to achieve that and you can use findAll() in place of find() , findOneIncludeSoftDelete() in place of findOne() and findByIdIncludeSoftDelete() in place of findById(), these will give you the responses including soft deleted entries.
5353
This class is a wrapper over DefaultTransactionalRepository class from [@loopback/repository](https://github.com/strongloop/loopback-next/tree/master/packages/repository).
5454

5555
In order to use this extension in your LB4 application, please follow below steps.

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

+22
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ export abstract class DefaultTransactionSoftCrudRepository<
6464
return super.find(filter, options);
6565
}
6666

67+
//find all enteries even with soft deleted records
68+
findAll(filter?: Filter<T>, options?: Options): Promise<(T & Relations)[]> {
69+
return super.find(filter, options);
70+
}
71+
6772
findOne(
6873
filter?: Filter<T>,
6974
options?: Options,
@@ -101,6 +106,14 @@ export abstract class DefaultTransactionSoftCrudRepository<
101106
return super.findOne(filter, options);
102107
}
103108

109+
//findOne() including soft deleted entry
110+
findOneIncludeSoftDelete(
111+
filter?: Filter<T>,
112+
options?: Options,
113+
): Promise<(T & Relations) | null> {
114+
return super.findOne(filter, options);
115+
}
116+
104117
findById(
105118
id: ID,
106119
filter?: Filter<T>,
@@ -139,6 +152,15 @@ export abstract class DefaultTransactionSoftCrudRepository<
139152
return super.findById(id, filter, options);
140153
}
141154

155+
//find by Id including soft deleted record
156+
findByIdIncludeSoftDelete(
157+
id: ID,
158+
filter?: Filter<T>,
159+
options?: Options,
160+
): Promise<T & Relations> {
161+
return super.findById(id, filter, options);
162+
}
163+
142164
updateAll(
143165
data: DataObject<T>,
144166
where?: Where<T>,

‎src/repositories/soft-crud.repository.base.ts

+31
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ export abstract class SoftCrudRepository<
6868
return super.find(filter, options);
6969
}
7070

71+
//find all enteries including soft deleted records
72+
findAll(filter?: Filter<T>, options?: Options): Promise<(T & Relations)[]> {
73+
return super.find(filter, options);
74+
}
75+
7176
findOne(
7277
filter?: Filter<T>,
7378
options?: Options,
@@ -106,6 +111,14 @@ export abstract class SoftCrudRepository<
106111
return super.findOne(filter, options);
107112
}
108113

114+
//findOne() including soft deleted entry
115+
findOneIncludeSoftDelete(
116+
filter?: Filter<T>,
117+
options?: Options,
118+
): Promise<(T & Relations) | null> {
119+
return super.findOne(filter, options);
120+
}
121+
109122
async findById(
110123
id: ID,
111124
filter?: Filter<T>,
@@ -157,6 +170,24 @@ export abstract class SoftCrudRepository<
157170
}
158171
}
159172

173+
//find by Id including soft deleted record
174+
async findByIdIncludeSoftDelete(
175+
id: ID,
176+
filter?: Filter<T>,
177+
options?: Options,
178+
): Promise<T & Relations> {
179+
//As parent method findById have filter: FilterExcludingWhere<T>
180+
//so we need add check here.
181+
const entityToRemove = await super.findOne(filter, options);
182+
183+
if (entityToRemove) {
184+
// Now call super
185+
return super.findById(id, filter, options);
186+
} else {
187+
throw new HttpErrors.NotFound(ErrorKeys.EntityNotFound);
188+
}
189+
}
190+
160191
updateAll(
161192
data: DataObject<T>,
162193
where?: Where<T>,

0 commit comments

Comments
 (0)
Please sign in to comment.