-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathredis_repository.ts
55 lines (49 loc) · 1.58 KB
/
redis_repository.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import { Repository } from './repository';
import { IWithUID } from '../db/firestore';
/**
* A repository for managing entities with Redis cache.
* Extends `Repository` to provide cache-based storage for entities.
*
* @template T - Type of the entity, extending `IWithUID`.
*/
export class RedisRepository<T extends IWithUID> extends Repository<T> {
/**
* Retrieves an entity by its unique identifier from the cache.
* @param id - Unique identifier of the entity.
* @returns The entity instance or `null` if not found.
*/
override async getById(id: string): Promise<T | null> {
return super.getById(id, false);
}
/**
* Throws an error as this method is not supported in Redis-based repositories.
* @throws Error - Method not allowed when using RedisRepository.
*/
async getAll(): Promise<T[]> {
throw new Error('Method not allowed when using RedisRepository');
}
/**
* Creates a new entity and stores it in the cache.
* @param item - The entity instance to create.
* @returns The created entity.
*/
async create(item: T): Promise<T> {
await super.setCache(item.uid!, item);
return item;
}
/**
* Updates an existing entity in the cache.
* @param id - Unique identifier of the entity.
* @param item - Partial entity data to update.
*/
async update(id: string, item: Partial<T>): Promise<void> {
await super.setCache(id!, item as T);
}
/**
* Deletes an entity from the cache.
* @param id - Unique identifier of the entity.
*/
async delete(id: string): Promise<void> {
return super.invalidateCache(id);
}
}