@@ -7,6 +7,7 @@ import {expect} from '@loopback/testlab';
7
7
8
8
import { Constructor , Getter } from '@loopback/context' ;
9
9
import {
10
+ DefaultCrudRepository ,
10
11
DefaultTransactionalRepository ,
11
12
Entity ,
12
13
EntityNotFoundError ,
@@ -32,6 +33,16 @@ class Customer extends SoftDeleteEntity {
32
33
email : string ;
33
34
}
34
35
36
+ @model ( )
37
+ class Customer2 extends SoftDeleteEntity {
38
+ @property ( {
39
+ id : true ,
40
+ } )
41
+ id : number ;
42
+ @property ( )
43
+ email : string ;
44
+ }
45
+
35
46
class CustomerCrudRepo extends SoftCrudRepositoryMixin <
36
47
Customer ,
37
48
typeof Customer . prototype . id ,
@@ -51,19 +62,45 @@ class CustomerCrudRepo extends SoftCrudRepositoryMixin<
51
62
}
52
63
}
53
64
65
+ class Customer2CrudRepo extends SoftCrudRepositoryMixin <
66
+ Customer ,
67
+ typeof Customer . prototype . id ,
68
+ Constructor <
69
+ DefaultCrudRepository < Customer , typeof Customer . prototype . id , { } >
70
+ > ,
71
+ { }
72
+ > ( DefaultCrudRepository ) {
73
+ constructor (
74
+ entityClass : typeof Entity & {
75
+ prototype : Customer ;
76
+ } ,
77
+ dataSource : juggler . DataSource ,
78
+ readonly getCurrentUser : Getter < IAuthUser | undefined > ,
79
+ readonly deletedByIdKey : string = 'id' ,
80
+ ) {
81
+ super ( entityClass , dataSource , getCurrentUser ) ;
82
+ }
83
+ }
84
+
54
85
describe ( 'SoftCrudRepositoryMixin' , ( ) => {
55
86
let repo : CustomerCrudRepo ;
87
+ let repoWithCustomDeletedByKey : Customer2CrudRepo ;
88
+ const userData = {
89
+ id : '1' ,
90
+ username : 'test' ,
91
+ } ;
56
92
57
93
before ( ( ) => {
58
94
const ds : juggler . DataSource = new juggler . DataSource ( {
59
95
name : 'db' ,
60
96
connector : 'memory' ,
61
97
} ) ;
62
- repo = new CustomerCrudRepo ( Customer , ds , ( ) =>
63
- Promise . resolve ( {
64
- id : '1' ,
65
- username : 'test' ,
66
- } ) ,
98
+ repo = new CustomerCrudRepo ( Customer , ds , ( ) => Promise . resolve ( userData ) ) ;
99
+ repoWithCustomDeletedByKey = new Customer2CrudRepo (
100
+ Customer2 ,
101
+ ds ,
102
+ ( ) => Promise . resolve ( userData ) ,
103
+ 'username' ,
67
104
) ;
68
105
} ) ;
69
106
@@ -504,6 +541,56 @@ describe('SoftCrudRepositoryMixin', () => {
504
541
} ) ;
505
542
} ) ;
506
543
544
+ describe ( 'deleteById' , ( ) => {
545
+ beforeEach ( setupTestData ) ;
546
+ afterEach ( clearTestData ) ;
547
+
548
+ it ( 'should soft delete entries' , async ( ) => {
549
+ await repo . deleteById ( 1 ) ;
550
+ try {
551
+ await repo . findById ( 1 ) ;
552
+ fail ( ) ;
553
+ } catch ( e ) {
554
+ expect ( e . message ) . to . be . equal ( 'EntityNotFound' ) ;
555
+ }
556
+ const afterDeleteIncludeSoftDeleted =
557
+ await repo . findByIdIncludeSoftDelete ( 1 ) ;
558
+ expect ( afterDeleteIncludeSoftDeleted )
559
+ . to . have . property ( 'email' )
560
+ . equal ( 'john@example.com' ) ;
561
+ } ) ;
562
+
563
+ it ( 'should soft delete entries with deletedBy set to id' , async ( ) => {
564
+ await repo . deleteById ( 1 ) ;
565
+ try {
566
+ await repo . findById ( 1 ) ;
567
+ fail ( ) ;
568
+ } catch ( e ) {
569
+ expect ( e . message ) . to . be . equal ( 'EntityNotFound' ) ;
570
+ }
571
+ const afterDeleteIncludeSoftDeleted =
572
+ await repo . findByIdIncludeSoftDelete ( 1 ) ;
573
+ expect ( afterDeleteIncludeSoftDeleted )
574
+ . to . have . property ( 'deletedBy' )
575
+ . equal ( userData . id ) ;
576
+ } ) ;
577
+
578
+ it ( 'should soft delete entries with deletedBy set to custom key provided' , async ( ) => {
579
+ await repoWithCustomDeletedByKey . deleteById ( 1 ) ;
580
+ try {
581
+ await repoWithCustomDeletedByKey . findById ( 1 ) ;
582
+ fail ( ) ;
583
+ } catch ( e ) {
584
+ expect ( e . message ) . to . be . equal ( 'EntityNotFound' ) ;
585
+ }
586
+ const afterDeleteIncludeSoftDeleted =
587
+ await repoWithCustomDeletedByKey . findByIdIncludeSoftDelete ( 1 ) ;
588
+ expect ( afterDeleteIncludeSoftDeleted )
589
+ . to . have . property ( 'deletedBy' )
590
+ . equal ( userData . username ) ;
591
+ } ) ;
592
+ } ) ;
593
+
507
594
describe ( 'delete' , ( ) => {
508
595
beforeEach ( setupTestData ) ;
509
596
afterEach ( clearTestData ) ;
@@ -522,18 +609,73 @@ describe('SoftCrudRepositoryMixin', () => {
522
609
. to . have . property ( 'email' )
523
610
. equal ( 'john@example.com' ) ;
524
611
} ) ;
612
+
613
+ it ( 'should soft delete entries with deletedBy set to id' , async ( ) => {
614
+ const entity = await repo . findById ( 1 ) ;
615
+ await repo . delete ( entity ) ;
616
+ try {
617
+ await repo . findById ( 1 ) ;
618
+ fail ( ) ;
619
+ } catch ( e ) {
620
+ expect ( e . message ) . to . be . equal ( 'EntityNotFound' ) ;
621
+ }
622
+ const afterDeleteIncludeSoftDeleted =
623
+ await repo . findByIdIncludeSoftDelete ( 1 ) ;
624
+ expect ( afterDeleteIncludeSoftDeleted )
625
+ . to . have . property ( 'deletedBy' )
626
+ . equal ( userData . id ) ;
627
+ } ) ;
628
+
629
+ it ( 'should soft delete entries with deletedBy set to custom key provided' , async ( ) => {
630
+ const entity = await repoWithCustomDeletedByKey . findById ( 1 ) ;
631
+ await repoWithCustomDeletedByKey . delete ( entity ) ;
632
+ try {
633
+ await repoWithCustomDeletedByKey . findById ( 1 ) ;
634
+ fail ( ) ;
635
+ } catch ( e ) {
636
+ expect ( e . message ) . to . be . equal ( 'EntityNotFound' ) ;
637
+ }
638
+ const afterDeleteIncludeSoftDeleted =
639
+ await repoWithCustomDeletedByKey . findByIdIncludeSoftDelete ( 1 ) ;
640
+ expect ( afterDeleteIncludeSoftDeleted )
641
+ . to . have . property ( 'deletedBy' )
642
+ . equal ( userData . username ) ;
643
+ } ) ;
525
644
} ) ;
526
645
527
646
describe ( 'deleteAll' , ( ) => {
528
647
beforeEach ( setupTestData ) ;
529
648
afterEach ( clearTestData ) ;
649
+
530
650
it ( 'should soft delete all entries' , async ( ) => {
531
651
await repo . deleteAll ( ) ;
532
652
const customers = await repo . find ( ) ;
533
653
expect ( customers ) . to . have . length ( 0 ) ;
534
654
const afterDeleteAll = await repo . findAll ( ) ;
535
655
expect ( afterDeleteAll ) . to . have . length ( 4 ) ;
536
656
} ) ;
657
+
658
+ it ( 'should soft delete entries with deletedBy set to id' , async ( ) => {
659
+ await repo . deleteAll ( ) ;
660
+ const customers = await repo . find ( ) ;
661
+ expect ( customers ) . to . have . length ( 0 ) ;
662
+ const afterDeleteAll = await repo . findAll ( ) ;
663
+ expect ( afterDeleteAll ) . to . have . length ( 4 ) ;
664
+ afterDeleteAll . forEach ( ( rec ) => {
665
+ expect ( rec ) . to . have . property ( 'deletedBy' ) . equal ( userData . id ) ;
666
+ } ) ;
667
+ } ) ;
668
+
669
+ it ( 'should soft delete entries with deletedBy set to custom key provided' , async ( ) => {
670
+ await repoWithCustomDeletedByKey . deleteAll ( ) ;
671
+ const customers = await repoWithCustomDeletedByKey . find ( ) ;
672
+ expect ( customers ) . to . have . length ( 0 ) ;
673
+ const afterDeleteAll = await repoWithCustomDeletedByKey . findAll ( ) ;
674
+ expect ( afterDeleteAll ) . to . have . length ( 4 ) ;
675
+ afterDeleteAll . forEach ( ( rec ) => {
676
+ expect ( rec ) . to . have . property ( 'deletedBy' ) . equal ( userData . username ) ;
677
+ } ) ;
678
+ } ) ;
537
679
} ) ;
538
680
539
681
describe ( 'deleteHard' , ( ) => {
@@ -571,9 +713,19 @@ describe('SoftCrudRepositoryMixin', () => {
571
713
await repo . create ( { id : 3 , email : 'alice@example.com' } ) ;
572
714
await repo . create ( { id : 4 , email : 'bob@example.com' } ) ;
573
715
await repo . deleteById ( 3 ) ;
716
+
717
+ await repoWithCustomDeletedByKey . create ( { id : 1 , email : 'john@example.com' } ) ;
718
+ await repoWithCustomDeletedByKey . create ( { id : 2 , email : 'mary@example.com' } ) ;
719
+ await repoWithCustomDeletedByKey . create ( {
720
+ id : 3 ,
721
+ email : 'alice@example.com' ,
722
+ } ) ;
723
+ await repoWithCustomDeletedByKey . create ( { id : 4 , email : 'bob@example.com' } ) ;
724
+ await repoWithCustomDeletedByKey . deleteById ( 3 ) ;
574
725
}
575
726
576
727
async function clearTestData ( ) {
577
728
await repo . deleteAllHard ( ) ;
729
+ await repoWithCustomDeletedByKey . deleteAllHard ( ) ;
578
730
}
579
731
} ) ;
0 commit comments