Skip to content

Instantly share code, notes, and snippets.

@MariusBongarts
Last active August 8, 2021 15:28
Show Gist options
  • Save MariusBongarts/7bb5b1e969ed6e1f96c129730c92cd6a to your computer and use it in GitHub Desktop.
Save MariusBongarts/7bb5b1e969ed6e1f96c129730c92cd6a to your computer and use it in GitHub Desktop.
DRY - CRUDService
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