|
| 1 | +import { connect } from "../helpers/database"; |
| 2 | +import { User } from "../entities/user"; |
| 3 | +import { Email } from "../entities/email"; |
| 4 | +import { Organization } from "../entities/organization"; |
| 5 | +import { Membership } from "../entities/membership"; |
| 6 | +import { BackupCode } from "../entities/backup-code"; |
| 7 | +import { mapStringToEntity } from "../helpers/mapStringToEntity"; |
| 8 | + |
| 9 | +export const create = async ( |
| 10 | + entity: string, |
| 11 | + data: User | Email | Organization | Membership | BackupCode |
| 12 | +) => { |
| 13 | + const connection = await connect(); |
| 14 | + const into = mapStringToEntity(entity); |
| 15 | + await connection |
| 16 | + .createQueryBuilder() |
| 17 | + .insert() |
| 18 | + .into(into) |
| 19 | + .values(data) |
| 20 | + .execute(); |
| 21 | + connection.close(); |
| 22 | +}; |
| 23 | + |
| 24 | +export const read = async (entity: string, id: number) => { |
| 25 | + const connection = await connect(); |
| 26 | + const into = mapStringToEntity(entity); |
| 27 | + const record = await connection |
| 28 | + .getRepository(into) |
| 29 | + .createQueryBuilder() |
| 30 | + .where(`${entity}.id = :id`, { id }) |
| 31 | + .getOne(); |
| 32 | + connection.close(); |
| 33 | + return record; |
| 34 | +}; |
| 35 | + |
| 36 | +export const update = async ( |
| 37 | + entity: string, |
| 38 | + id: number, |
| 39 | + data: User | Email | Organization | Membership | BackupCode |
| 40 | +) => { |
| 41 | + const connection = await connect(); |
| 42 | + const into = mapStringToEntity(entity); |
| 43 | + const updatedRecord = await connection |
| 44 | + .createQueryBuilder() |
| 45 | + .update(into) |
| 46 | + .set(data) |
| 47 | + .where("id = :id", { id }) |
| 48 | + .execute(); |
| 49 | + connection.close(); |
| 50 | + return updatedRecord; |
| 51 | +}; |
| 52 | + |
| 53 | +export const remove = async (entity: string, id: number) => { |
| 54 | + const connection = await connect(); |
| 55 | + const into = mapStringToEntity(entity); |
| 56 | + await connection |
| 57 | + .createQueryBuilder() |
| 58 | + .delete() |
| 59 | + .from(into) |
| 60 | + .where("id = :id", { id }) |
| 61 | + .execute(); |
| 62 | + connection.close(); |
| 63 | +}; |
0 commit comments