Skip to content
This repository has been archived by the owner on Jan 15, 2022. It is now read-only.

Commit

Permalink
feat: add cache for complex queries
Browse files Browse the repository at this point in the history
  • Loading branch information
jonyw4 committed Aug 28, 2020
1 parent 615e26d commit 2c58e98
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 8 deletions.
1 change: 1 addition & 0 deletions src/consts/cache.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const DEFAULT_CACHE_TIMEOUT = 5 * (60 * 1000); // 5min
1 change: 1 addition & 0 deletions src/consts/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './cache';
15 changes: 11 additions & 4 deletions src/services/review-product.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,14 @@ describe('ReviewProductService', () => {
it('should return the average', async () => {
connection
.getRepository(ReviewProductEntity)
.createQueryBuilder('review_product')
.select('AVG(stars)', 'starasync s')
.where('state = :state', { state: 'Authorized' })
.getRawOne.mockImplementation(async () => ({ stars: 5 }));
// @ts-ignore
.createQueryBuilder.mockImplementation(() => ({
select: jest.fn().mockReturnThis(),
leftJoin: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
cache: jest.fn().mockReturnThis(),
getRawOne: jest.fn().mockImplementation(async () => ({ stars: 5 }))
}));
await expect(resolver.getAvgStars()).resolves.toBe(5);
});
});
Expand All @@ -77,6 +81,7 @@ describe('ReviewProductService', () => {
select: jest.fn().mockReturnThis(),
leftJoin: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
cache: jest.fn().mockReturnThis(),
getCount: jest.fn().mockImplementationOnce(async () => 1)
}));

Expand Down Expand Up @@ -111,6 +116,7 @@ describe('ReviewProductService', () => {
select: jest.fn().mockReturnThis(),
leftJoin: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
cache: jest.fn().mockReturnThis(),
getCount: jest.fn().mockImplementationOnce(async () => 0)
}));

Expand All @@ -136,6 +142,7 @@ describe('ReviewProductService', () => {
setParameters: jest.fn().mockReturnThis(),
getParameters: jest.fn().mockReturnThis(),
leftJoin: jest.fn().mockReturnThis(),
cache: jest.fn().mockReturnThis(),
andWhere: jest.fn().mockReturnThis(),
getRawMany: jest
.fn()
Expand Down
3 changes: 3 additions & 0 deletions src/services/review-product.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
} from '@vendure/core';
import { ReviewService } from '../helpers';
import { ReviewProductStateTransitionEvent } from '../events';
import { DEFAULT_CACHE_TIMEOUT } from '../consts';
import { ListQueryOptions } from '@vendure/core/dist/common/types/common-types';

@Injectable()
Expand Down Expand Up @@ -45,6 +46,7 @@ export class ReviewProductService extends ReviewService<
.createQueryBuilder('review_product')
.select('AVG(stars)', 'stars')
.where('state = :state', { state: 'Authorized' })
.cache(DEFAULT_CACHE_TIMEOUT)
.getRawOne();
return stars ? stars : 0;
}
Expand Down Expand Up @@ -112,6 +114,7 @@ export class ReviewProductService extends ReviewService<
`productVariant.productId NOT IN (${customerReviewQb.getQuery()})`
)
.setParameters(customerReviewQb.getParameters())
.cache(DEFAULT_CACHE_TIMEOUT)
.getRawMany<{
productId: ID;
}>();
Expand Down
12 changes: 8 additions & 4 deletions src/services/review-store.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -170,10 +170,14 @@ describe('ReviewStoreService', () => {
it('should return the average', async () => {
connection
.getRepository(ReviewStoreEntity)
.createQueryBuilder('review_store')
.select('AVG(nps)', 'nps')
.where('state = :state', { state: 'Authorized' })
.getRawOne.mockImplementation(async () => ({ nps: 10 }));
// @ts-ignore
.createQueryBuilder.mockImplementation(() => ({
select: jest.fn().mockReturnThis(),
leftJoin: jest.fn().mockReturnThis(),
where: jest.fn().mockReturnThis(),
cache: jest.fn().mockReturnThis(),
getRawOne: jest.fn().mockImplementation(async () => ({ nps: 10 }))
}));
await expect(resolver.getNPSAvg()).resolves.toBe(10);
});
});
Expand Down
2 changes: 2 additions & 0 deletions src/services/review-store.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
Customer
} from '@vendure/core';
import { ReviewService } from '../helpers';
import { DEFAULT_CACHE_TIMEOUT } from '../consts';
import { ReviewStoreStateTransitionEvent } from '../events';

@Injectable()
Expand Down Expand Up @@ -48,6 +49,7 @@ export class ReviewStoreService extends ReviewService<
.createQueryBuilder('review_store')
.select('AVG(nps)', 'nps')
.where('state = :state', { state: 'Authorized' })
.cache(DEFAULT_CACHE_TIMEOUT)
.getRawOne();
return nps ? nps : 0;
}
Expand Down

0 comments on commit 2c58e98

Please sign in to comment.