Skip to content

Commit 1cc19b9

Browse files
authored
feat(repository): add countAll method (#145)
adds countAll function in soft crud base repositories to support counting all entries including soft deleted ones GH-144
1 parent 5e04c43 commit 1cc19b9

File tree

5 files changed

+28
-0
lines changed

5 files changed

+28
-0
lines changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -691,6 +691,21 @@ describe('SoftCrudRepositoryMixin', () => {
691691
});
692692
});
693693

694+
describe('countAll', () => {
695+
beforeEach(setupTestData);
696+
afterEach(clearTestData);
697+
it('should return total count when no conditions are passed', async () => {
698+
const count = await repo.countAll();
699+
expect(count.count).to.be.equal(4);
700+
});
701+
it('should return count for soft deleted entries when conditions specified', async () => {
702+
const count = await repo.countAll({
703+
704+
});
705+
expect(count.count).to.be.equal(1);
706+
});
707+
});
708+
694709
describe('deleteById', () => {
695710
beforeEach(setupTestData);
696711
afterEach(clearTestData);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,10 @@ export abstract class DefaultTransactionSoftCrudRepository<
151151
return super.count(filter.where, options);
152152
}
153153

154+
countAll(where?: Where<E>, options?: Options): Promise<Count> {
155+
return super.count(where, options);
156+
}
157+
154158
// soft delete
155159
async delete(entity: E, options?: Options): Promise<void> {
156160
return this.deleteById(entity.getId(), options);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,10 @@ export abstract class SequelizeSoftCrudRepository<
133133
return super.count(filter.where, options);
134134
}
135135

136+
countAll(where?: Where<E>, options?: Options): Promise<Count> {
137+
return super.count(where, options);
138+
}
139+
136140
// soft delete
137141
async delete(entity: E, options?: Options): Promise<void> {
138142
return this.deleteById(entity.getId(), options);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,10 @@ export abstract class SoftCrudRepository<
135135
return super.count(filter.where, options);
136136
}
137137

138+
countAll(where?: Where<E>, options?: Options): Promise<Count> {
139+
return super.count(where, options);
140+
}
141+
138142
// soft delete
139143
async delete(entity: E, options?: Options): Promise<void> {
140144
return this.deleteById(entity.getId(), options);

src/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,4 +54,5 @@ export interface ISoftCrudRepositoryMixin<E extends object, ID, R> {
5454
options?: Options,
5555
): Promise<(E & R) | null>;
5656
findById(id: ID, filter?: Filter<E>, options?: Options): Promise<E & R>;
57+
countAll(where?: Where<E>, options?: Options): Promise<Count>;
5758
}

0 commit comments

Comments
 (0)