mirror of
https://github.com/FranP-code/Baileys.git
synced 2025-10-13 00:32:22 +00:00
32 lines
553 B
TypeScript
32 lines
553 B
TypeScript
export class ObjectRepository<T extends object> {
|
|
readonly entityMap: Map<string, T>
|
|
|
|
constructor(entities: Record<string, T> = {}) {
|
|
this.entityMap = new Map(Object.entries(entities))
|
|
}
|
|
|
|
findById(id: string) {
|
|
return this.entityMap.get(id)
|
|
}
|
|
|
|
findAll() {
|
|
return Array.from(this.entityMap.values())
|
|
}
|
|
|
|
upsertById(id: string, entity: T) {
|
|
return this.entityMap.set(id, { ...entity })
|
|
}
|
|
|
|
deleteById(id: string) {
|
|
return this.entityMap.delete(id)
|
|
}
|
|
|
|
count() {
|
|
return this.entityMap.size
|
|
}
|
|
|
|
toJSON() {
|
|
return this.findAll()
|
|
}
|
|
|
|
} |