Last active
August 8, 2021 15:28
-
-
Save MariusBongarts/7bb5b1e969ed6e1f96c129730c92cd6a to your computer and use it in GitHub Desktop.
DRY - CRUDService
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
interface Entity { | |
id: string; | |
} | |
interface CrudOperationOptions<T extends Entity> { | |
user: User; | |
entity: T; | |
} | |
type CrudReturnType<T> = Promise<T | undefined>; | |
abstract class CRUDService<T extends Entity> implements DB<T> { | |
private db: DB<T>; | |
constructor(private entity: string) { | |
this.db = new DB<T>(this.entity); | |
} | |
async create(options: CrudOperationOptions<T>): CrudReturnType<T> { | |
console.log(`Create ${this.entity}.`); | |
return this.db.create(options); | |
} | |
async read(options: CrudOperationOptions<T>): CrudReturnType<T> { | |
console.log(`Get ${this.entity}`); | |
return this.db.read(options); | |
} | |
async update(options: CrudOperationOptions<T>): CrudReturnType<T> { | |
console.log(`Update ${this.entity}.`); | |
return this.db.update(options); | |
} | |
async delete(options: CrudOperationOptions<T>): CrudReturnType<T> { | |
console.log(`Delete${this.entity}.`); | |
return this.db.delete(options); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment