Skip to content

Commit 8622d8e

Browse files
feat(repository): implement undo soft delete feature (#185)
GH-182 ## Description Add 2 functionalities to undo the soft delete of a record 1. undoSoftDeleteById() -> This will just undo soft delete entity by specified id 2. undoSoftDeleteAll() -> This will undo soft delete entities depending on where clause or all. Fixes # (issue) ## Type of change Please delete options that are not relevant. - [x] New feature (non-breaking change which adds functionality) ## How Has This Been Tested ? Please describe the tests that you ran to verify your changes. Provide instructions so we can reproduce. Please also list any relevant details for your test configuration - [x] Test A - [x] Test B ## Checklist: - [x] Performed a self-review of my own code - [x] npm test passes on your machine
1 parent bc202f4 commit 8622d8e

File tree

2 files changed

+104
-2
lines changed

2 files changed

+104
-2
lines changed

src/__tests__/unit/repository/soft-crud.repository.unit.ts

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ describe('SoftCrudRepository', () => {
133133
const customers = await repo.find();
134134
expect(customers).to.have.length(3);
135135
});
136+
136137
it('should find non soft deleted entries with and operator', async () => {
137138
const customers = await repo.find({
138139
where: {
@@ -793,6 +794,80 @@ describe('SoftCrudRepository', () => {
793794
});
794795
});
795796

797+
describe('undoSoftDelete', () => {
798+
beforeEach(setupTestData);
799+
afterEach(clearTestData);
800+
801+
it('should undo soft deleted entry by id', async () => {
802+
await repo.undoSoftDeleteById(3);
803+
const customer = await repo.findById(3);
804+
const customers = await repo.find();
805+
expect(customer.deleted).to.false();
806+
expect(customers).to.have.length(4);
807+
});
808+
809+
it('should check deletedOn flag is undefined after undo', async () => {
810+
const softDeletedCustomer = await repo.findByIdIncludeSoftDelete(3);
811+
expect(softDeletedCustomer.deletedOn).to.Date();
812+
await repo.undoSoftDeleteById(3);
813+
const customer = await repo.findById(3);
814+
expect(customer.deletedOn).to.undefined();
815+
});
816+
817+
it('should undo all soft deleted entries', async () => {
818+
await repo.deleteAll();
819+
await repo.undoSoftDeleteAll();
820+
const customers = await repo.find();
821+
expect(customers).to.have.length(4);
822+
});
823+
824+
it('should undo soft deleted entries with and operator', async () => {
825+
await repo.undoSoftDeleteAll({
826+
and: [{email: '[email protected]'}, {id: 3}],
827+
});
828+
const customers = await repo.find({
829+
where: {
830+
and: [
831+
{
832+
833+
},
834+
{
835+
id: 3,
836+
},
837+
],
838+
},
839+
});
840+
expect(customers).to.have.length(1);
841+
});
842+
843+
it('should undo soft deleted entries with or operator', async () => {
844+
await repo.deleteAll({email: '[email protected]'});
845+
await repo.undoSoftDeleteAll({
846+
or: [
847+
{
848+
849+
},
850+
{
851+
852+
},
853+
],
854+
});
855+
const customers = await repo.find({
856+
where: {
857+
or: [
858+
{
859+
860+
},
861+
{
862+
863+
},
864+
],
865+
},
866+
});
867+
expect(customers).to.have.length(2);
868+
});
869+
});
870+
796871
describe('deleteAll', () => {
797872
beforeEach(setupTestData);
798873
afterEach(clearTestData);

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

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@
44

55
import {Getter} from '@loopback/core';
66
import {
7+
Condition,
78
DataObject,
89
DefaultCrudRepository,
910
Entity,
1011
Filter,
11-
juggler,
1212
Where,
13-
Condition,
13+
juggler,
1414
} from '@loopback/repository';
1515
import {Count} from '@loopback/repository/src/common-types';
1616
import {HttpErrors} from '@loopback/rest';
@@ -168,6 +168,33 @@ export abstract class SoftCrudRepository<
168168
);
169169
}
170170

171+
/**
172+
* Method to perform undo the soft delete by Id.
173+
* @param id
174+
* @param options
175+
*/
176+
async undoSoftDeleteById(id: ID, options?: Options): Promise<void> {
177+
await this.undoSoftDeleteAll({id} as Where<E>, options);
178+
}
179+
180+
/**
181+
* Method to perform undo all the soft deletes
182+
* @param where
183+
* @param options
184+
*/
185+
async undoSoftDeleteAll(where?: Where<E>, options?: Options): Promise<Count> {
186+
const filter = new SoftFilterBuilder({where})
187+
.imposeCondition({
188+
deleted: true,
189+
} as Condition<E>)
190+
.build();
191+
return super.updateAll(
192+
{deleted: false, deletedOn: undefined},
193+
filter.where,
194+
options,
195+
);
196+
}
197+
171198
/**
172199
* Method to perform hard delete of entries. Take caution.
173200
* @param entity

0 commit comments

Comments
 (0)