cache-builder.ts 1011 B

123456789101112131415161718192021222324252627282930
  1. import { Logger } from '@nestjs/common';
  2. import { RedisService } from '@box/db/redis/redis.service';
  3. import { MongoPrismaService } from '@box/db/prisma/mongo-prisma.service';
  4. /**
  5. * Base class for cache builders that read from MongoDB and publish JSON into Redis.
  6. */
  7. export abstract class BaseCacheBuilder {
  8. protected readonly logger: Logger;
  9. protected constructor(
  10. protected readonly redis: RedisService,
  11. protected readonly mongoPrisma: MongoPrismaService,
  12. loggerContext?: string,
  13. ) {
  14. this.logger = new Logger(loggerContext ?? new.target.name);
  15. }
  16. /** Build every cache item managed by this builder. */
  17. abstract buildAll(): Promise<void>;
  18. /**
  19. * Convert Prisma BigInt timestamps to numbers for JSON payloads.
  20. * Falls back to the original number when already numeric.
  21. */
  22. protected toMillis(value?: bigint | number | null): number | null {
  23. if (value === null || value === undefined) return null;
  24. return typeof value === 'bigint' ? Number(value) : value;
  25. }
  26. }