-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsequelize_repository.ts
83 lines (76 loc) · 2.77 KB
/
sequelize_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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import { MakeNullishOptional } from 'sequelize/types/utils';
import { Repository } from './repository';
import { Model, ModelCtor } from 'sequelize-typescript';
/**
* A repository implementation for managing Sequelize models with Redis caching.
* @typeparam T - The type of the Sequelize model managed by this repository.
*/
export class SequelizeRepository<T extends Model> extends Repository<T> {
/**
* Constructs a `SequelizeRepository` instance.
* @param model - The Sequelize model constructor for the entity this repository manages.
* @param relatedModel - Array of related models to include in queries.
* @param ttl - Time-to-live (TTL) for cached items, in seconds.
*/
constructor(private model: ModelCtor<T>, private relatedModel: ModelCtor<Model>[], ttl: number, private noCache: boolean = false) {
super(model.name, ttl);
}
/**
* Retrieves an item by its ID from the cache or database.
* @param id - The unique identifier of the item.
* @param noCache - If true, bypasses the cache and directly queries the database.
* @returns The item if found in cache or database; otherwise, null.
*/
override async getById(id: string, noCache: boolean = false): Promise<T | null> {
// Check if the object is in cache
let item;
if (!this.noCache) {
item = await super.getById(id, noCache);
if (item) return item;
}
// Otherwise retrieve it with Sequelize ORM
item = await this.model.findByPk(id, { include: this.relatedModel });
await super.setCache(id, item);
return item;
}
/**
* Retrieves all items from the repository, including related models.
* @returns An array of all items.
*/
async getAll(): Promise<T[]> {
return this.model.findAll({ include: this.relatedModel });
}
/**
* Creates a new item in the repository.
* @param item - The item to create.
* @returns The created item.
*/
async create(item: T): Promise<T> {
const itemWithId = await this.model.create(item as MakeNullishOptional<T>);
// await super.setCache(itemWithId.id, itemWithId);
return itemWithId;
}
/**
* Updates an existing item in the repository.
* @param id - The unique identifier of the item to update.
* @param item - The partial item data to update.
*/
async update(id: string, item: Partial<T>): Promise<void> {
const existingItem = await this.getById(id, true);
super.invalidateCache(id);
if (existingItem) {
await existingItem.update(item as MakeNullishOptional<T>);
}
}
/**
* Deletes an item from the repository.
* @param id - The unique identifier of the item to delete.
*/
async delete(id: string): Promise<void> {
const item = await this.getById(id);
super.invalidateCache(id);
if (item) {
await item.destroy();
}
}
}