Skip to content

Commit c678189

Browse files
authored
fix(repository): remove imposing behaviour of fields (#143)
which was setting deleted to true for each soft-delete query it is not required to run conditions GH-142
1 parent f80f6e5 commit c678189

File tree

7 files changed

+211
-138
lines changed

7 files changed

+211
-138
lines changed

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

Lines changed: 65 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,21 @@ describe('SoftCrudRepositoryMixin', () => {
179179
});
180180
expect(deletedCustomers).to.have.length(0);
181181
});
182+
183+
it('should find non soft deleted entries when deleted fields are not included', async () => {
184+
const customers = await repo.find({
185+
fields: {deleted: false, deletedBy: false, deletedOn: false},
186+
});
187+
expect(customers).to.have.length(3);
188+
});
189+
190+
it('should find non soft deleted entries when deleted fields are included', async () => {
191+
const customers = await repo.find({
192+
fields: {deleted: true},
193+
});
194+
expect(customers).to.have.length(3);
195+
});
196+
182197
it('should find non soft deleted entries with or operator', async () => {
183198
const customers = await repo.find({
184199
where: {
@@ -202,6 +217,14 @@ describe('SoftCrudRepositoryMixin', () => {
202217
const customers = await repo.findAll();
203218
expect(customers).to.have.length(4);
204219
});
220+
221+
it('should find all entries when deleted fields are not included', async () => {
222+
await setupTestData();
223+
const customers = await repo.findAll({
224+
fields: {deleted: false, deletedBy: false, deletedOn: false},
225+
});
226+
expect(customers).to.have.length(4);
227+
});
205228
});
206229

207230
describe('findOne', () => {
@@ -276,6 +299,17 @@ describe('SoftCrudRepositoryMixin', () => {
276299
expect(customer).to.have.property('email').equal('[email protected]');
277300
});
278301

302+
it('should find one soft deleted entry even when `deleted` field is not included', async () => {
303+
const customer = await repo.findOne({
304+
where: {
305+
id: 4,
306+
},
307+
fields: {deleted: false, deletedBy: false, deletedOn: false},
308+
});
309+
310+
expect(customer?.id).to.have.be.eql(4);
311+
});
312+
279313
it('shound find none soft deleted entry using or operator', async () => {
280314
const customer = await repo.findOne({
281315
where: {
@@ -303,6 +337,7 @@ describe('SoftCrudRepositoryMixin', () => {
303337
304338
},
305339
});
340+
306341
expect(customer).to.have.property('email').equal('[email protected]');
307342
});
308343
});
@@ -368,7 +403,7 @@ describe('SoftCrudRepositoryMixin', () => {
368403
expect(e.message).to.be.equal('EntityNotFound');
369404
}
370405
});
371-
it('should not return soft deleted entry by id, without using deleted in fields filter', async () => {
406+
it('should not return soft deleted entry by id when using fields filter without including deleted column', async () => {
372407
try {
373408
await repo.findById(3, {
374409
fields: {
@@ -398,7 +433,11 @@ describe('SoftCrudRepositoryMixin', () => {
398433
email: true,
399434
},
400435
});
401-
expect(customer).to.not.have.property('deleted');
436+
const customer2 = await repo.findById(4, {
437+
fields: ['id', 'email'],
438+
});
439+
expect(customer.deleted).to.be.undefined();
440+
expect(customer2.deleted).to.be.undefined();
402441
});
403442
it('should return requested fields matched with fields filter', async () => {
404443
const customer = await repo.findById(4, {
@@ -410,17 +449,38 @@ describe('SoftCrudRepositoryMixin', () => {
410449
});
411450
expect(customer).to.have.property('deleted');
412451
});
452+
453+
it('should return non soft deleted entry even if deleted column is not included in fields', async () => {
454+
const customer = await repo.findById(4, {
455+
fields: {
456+
deleted: false,
457+
deletedBy: false,
458+
deletedOn: false,
459+
},
460+
});
461+
462+
expect(customer.id).to.be.eql(4);
463+
expect(customer.email).to.be.eql('[email protected]');
464+
expect(customer.deleted).to.be.undefined();
465+
});
466+
413467
it('should return requested fields only when not using deleted in fields filter array', async () => {
414468
const customer = await repo.findById(4, {
415469
fields: ['id', 'email'],
416470
});
417-
expect(customer).to.not.have.property('deleted');
471+
472+
expect(customer.id).to.be.eql(4);
473+
expect(customer.deleted).to.be.undefined();
418474
});
475+
419476
it('should return requested fields matched with fields filter array', async () => {
420477
const customer = await repo.findById(4, {
421478
fields: ['id', 'email', 'deleted'],
422479
});
423-
expect(customer).to.have.property('deleted');
480+
expect(customer.id).to.be.eql(4);
481+
expect(customer.email).to.be.eql('[email protected]');
482+
expect(customer.deleted).to.be.false();
483+
expect(customer.deletedBy).to.be.undefined();
424484
});
425485
});
426486

@@ -684,6 +744,7 @@ describe('SoftCrudRepositoryMixin', () => {
684744
describe('delete', () => {
685745
beforeEach(setupTestData);
686746
afterEach(clearTestData);
747+
687748
it('should soft delete entries', async () => {
688749
const entity = await repo.findById(1);
689750
await repo.delete(entity);

src/__tests__/unit/repository/default-transaction-soft-crud.repository.base.ts

Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,21 @@ describe('DefaultTransactionSoftCrudRepository', () => {
169169
});
170170
expect(deletedCustomers).to.have.length(0);
171171
});
172+
173+
it('should find non soft deleted entries when deleted fields are not included', async () => {
174+
const customers = await repo.find({
175+
fields: {deleted: false, deletedBy: false, deletedOn: false},
176+
});
177+
expect(customers).to.have.length(3);
178+
});
179+
180+
it('should find non soft deleted entries when deleted fields are included', async () => {
181+
const customers = await repo.find({
182+
fields: {deleted: true},
183+
});
184+
expect(customers).to.have.length(3);
185+
});
186+
172187
it('should find non soft deleted entries with or operator', async () => {
173188
const customers = await repo.find({
174189
where: {
@@ -192,6 +207,14 @@ describe('DefaultTransactionSoftCrudRepository', () => {
192207
const customers = await repo.findAll();
193208
expect(customers).to.have.length(4);
194209
});
210+
211+
it('should find all entries when deleted fields are not included', async () => {
212+
await setupTestData();
213+
const customers = await repo.findAll({
214+
fields: {deleted: false, deletedBy: false, deletedOn: false},
215+
});
216+
expect(customers).to.have.length(4);
217+
});
195218
});
196219

197220
describe('findOne', () => {
@@ -266,6 +289,17 @@ describe('DefaultTransactionSoftCrudRepository', () => {
266289
expect(customer).to.have.property('email').equal('[email protected]');
267290
});
268291

292+
it('should find one soft deleted entry even when `deleted` field is not included', async () => {
293+
const customer = await repo.findOne({
294+
where: {
295+
id: 4,
296+
},
297+
fields: {deleted: false, deletedBy: false, deletedOn: false},
298+
});
299+
300+
expect(customer?.id).to.have.be.eql(4);
301+
});
302+
269303
it('shound find none soft deleted entry using or operator', async () => {
270304
const customer = await repo.findOne({
271305
where: {
@@ -293,6 +327,7 @@ describe('DefaultTransactionSoftCrudRepository', () => {
293327
294328
},
295329
});
330+
296331
expect(customer).to.have.property('email').equal('[email protected]');
297332
});
298333
});
@@ -358,7 +393,7 @@ describe('DefaultTransactionSoftCrudRepository', () => {
358393
expect(e.message).to.be.equal('EntityNotFound');
359394
}
360395
});
361-
it('should not return soft deleted entry by id, without using deleted in fields filter', async () => {
396+
it('should not return soft deleted entry by id when using fields filter without including deleted column', async () => {
362397
try {
363398
await repo.findById(3, {
364399
fields: {
@@ -388,7 +423,11 @@ describe('DefaultTransactionSoftCrudRepository', () => {
388423
email: true,
389424
},
390425
});
391-
expect(customer).to.not.have.property('deleted');
426+
const customer2 = await repo.findById(4, {
427+
fields: ['id', 'email'],
428+
});
429+
expect(customer.deleted).to.be.undefined();
430+
expect(customer2.deleted).to.be.undefined();
392431
});
393432
it('should return requested fields matched with fields filter', async () => {
394433
const customer = await repo.findById(4, {
@@ -400,17 +439,38 @@ describe('DefaultTransactionSoftCrudRepository', () => {
400439
});
401440
expect(customer).to.have.property('deleted');
402441
});
442+
443+
it('should return non soft deleted entry even if deleted column is not included in fields', async () => {
444+
const customer = await repo.findById(4, {
445+
fields: {
446+
deleted: false,
447+
deletedBy: false,
448+
deletedOn: false,
449+
},
450+
});
451+
452+
expect(customer.id).to.be.eql(4);
453+
expect(customer.email).to.be.eql('[email protected]');
454+
expect(customer.deleted).to.be.undefined();
455+
});
456+
403457
it('should return requested fields only when not using deleted in fields filter array', async () => {
404458
const customer = await repo.findById(4, {
405459
fields: ['id', 'email'],
406460
});
407-
expect(customer).to.not.have.property('deleted');
461+
462+
expect(customer.id).to.be.eql(4);
463+
expect(customer.deleted).to.be.undefined();
408464
});
465+
409466
it('should return requested fields matched with fields filter array', async () => {
410467
const customer = await repo.findById(4, {
411468
fields: ['id', 'email', 'deleted'],
412469
});
413-
expect(customer).to.have.property('deleted');
470+
expect(customer.id).to.be.eql(4);
471+
expect(customer.email).to.be.eql('[email protected]');
472+
expect(customer.deleted).to.be.false();
473+
expect(customer.deletedBy).to.be.undefined();
414474
});
415475
});
416476

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

Lines changed: 63 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,21 @@ describe('SoftCrudRepository', () => {
163163
});
164164
expect(deletedCustomers).to.have.length(0);
165165
});
166+
167+
it('should find non soft deleted entries when deleted fields are not included', async () => {
168+
const customers = await repo.find({
169+
fields: {deleted: false, deletedBy: false, deletedOn: false},
170+
});
171+
expect(customers).to.have.length(3);
172+
});
173+
174+
it('should find non soft deleted entries when deleted fields are included', async () => {
175+
const customers = await repo.find({
176+
fields: {deleted: true},
177+
});
178+
expect(customers).to.have.length(3);
179+
});
180+
166181
it('should find non soft deleted entries with or operator', async () => {
167182
const customers = await repo.find({
168183
where: {
@@ -186,6 +201,14 @@ describe('SoftCrudRepository', () => {
186201
const customers = await repo.findAll();
187202
expect(customers).to.have.length(4);
188203
});
204+
205+
it('should find all entries when deleted fields are not included', async () => {
206+
await setupTestData();
207+
const customers = await repo.findAll({
208+
fields: {deleted: false, deletedBy: false, deletedOn: false},
209+
});
210+
expect(customers).to.have.length(4);
211+
});
189212
});
190213

191214
describe('findOne', () => {
@@ -260,6 +283,17 @@ describe('SoftCrudRepository', () => {
260283
expect(customer).to.have.property('email').equal('[email protected]');
261284
});
262285

286+
it('should find one soft deleted entry even when `deleted` field is not included', async () => {
287+
const customer = await repo.findOne({
288+
where: {
289+
id: 4,
290+
},
291+
fields: {deleted: false, deletedBy: false, deletedOn: false},
292+
});
293+
294+
expect(customer?.id).to.have.be.eql(4);
295+
});
296+
263297
it('shound find none soft deleted entry using or operator', async () => {
264298
const customer = await repo.findOne({
265299
where: {
@@ -353,7 +387,7 @@ describe('SoftCrudRepository', () => {
353387
expect(e.message).to.be.equal('EntityNotFound');
354388
}
355389
});
356-
it('should not return soft deleted entry by id, without using deleted in fields filter', async () => {
390+
it('should not return soft deleted entry by id when using fields filter without including deleted column', async () => {
357391
try {
358392
await repo.findById(3, {
359393
fields: {
@@ -383,7 +417,11 @@ describe('SoftCrudRepository', () => {
383417
email: true,
384418
},
385419
});
386-
expect(customer).to.not.have.property('deleted');
420+
const customer2 = await repo.findById(4, {
421+
fields: ['id', 'email'],
422+
});
423+
expect(customer.deleted).to.be.undefined();
424+
expect(customer2.deleted).to.be.undefined();
387425
});
388426
it('should return requested fields matched with fields filter', async () => {
389427
const customer = await repo.findById(4, {
@@ -395,17 +433,38 @@ describe('SoftCrudRepository', () => {
395433
});
396434
expect(customer).to.have.property('deleted');
397435
});
436+
437+
it('should return non soft deleted entry even if deleted column is not included in fields', async () => {
438+
const customer = await repo.findById(4, {
439+
fields: {
440+
deleted: false,
441+
deletedBy: false,
442+
deletedOn: false,
443+
},
444+
});
445+
446+
expect(customer.id).to.be.eql(4);
447+
expect(customer.email).to.be.eql('[email protected]');
448+
expect(customer.deleted).to.be.undefined();
449+
});
450+
398451
it('should return requested fields only when not using deleted in fields filter array', async () => {
399452
const customer = await repo.findById(4, {
400453
fields: ['id', 'email'],
401454
});
402-
expect(customer).to.not.have.property('deleted');
455+
456+
expect(customer.id).to.be.eql(4);
457+
expect(customer.deleted).to.be.undefined();
403458
});
459+
404460
it('should return requested fields matched with fields filter array', async () => {
405461
const customer = await repo.findById(4, {
406462
fields: ['id', 'email', 'deleted'],
407463
});
408-
expect(customer).to.have.property('deleted');
464+
expect(customer.id).to.be.eql(4);
465+
expect(customer.email).to.be.eql('[email protected]');
466+
expect(customer.deleted).to.be.false();
467+
expect(customer.deletedBy).to.be.undefined();
409468
});
410469
});
411470

0 commit comments

Comments
 (0)