|
| 1 | +import type { IApiKey } from "../../../../types/impl/database/impl/schema/apiKey"; |
| 2 | +import DatabaseHandler from "../../handler"; |
| 3 | + |
| 4 | +export class ApiKeyRepository { |
| 5 | + /** |
| 6 | + * @description The table name for API keys. |
| 7 | + */ |
| 8 | + public static readonly tableName = "anify.api_key"; |
| 9 | + |
| 10 | + /** |
| 11 | + * @method getById Retrieves a single API key record by ID. |
| 12 | + * @param db The DatabaseHandler instance. |
| 13 | + * @param id The ID of the API key to retrieve. |
| 14 | + * @returns IApiKey or null if not found. |
| 15 | + */ |
| 16 | + public static async getById(db: DatabaseHandler, id: string): Promise<IApiKey | null> { |
| 17 | + const rows = await db.select<IApiKey>(ApiKeyRepository.tableName, { id }); |
| 18 | + return rows.length > 0 ? rows[0] : null; |
| 19 | + } |
| 20 | + |
| 21 | + /** |
| 22 | + * @method getByKey Retrieves a single API key record by key. |
| 23 | + * @param db The DatabaseHandler instance. |
| 24 | + * @param key The key of the API key to retrieve. |
| 25 | + * @returns IApiKey or null if not found. |
| 26 | + */ |
| 27 | + public static async getByKey(db: DatabaseHandler, key: string): Promise<IApiKey | null> { |
| 28 | + const rows = await db.select<IApiKey>(ApiKeyRepository.tableName, { key }); |
| 29 | + return rows.length > 0 ? rows[0] : null; |
| 30 | + } |
| 31 | + |
| 32 | + /** |
| 33 | + * @method insert Inserts a new API key record and returns the newly created row. |
| 34 | + * @param db The DatabaseHandler instance. |
| 35 | + * @param apiKey The API key object to insert. |
| 36 | + * @returns The inserted API key row (with defaults applied). |
| 37 | + */ |
| 38 | + public static async insert(db: DatabaseHandler, apiKey: IApiKey): Promise<IApiKey> { |
| 39 | + const inserted = await db.insert<IApiKey>(ApiKeyRepository.tableName, apiKey); |
| 40 | + return inserted; |
| 41 | + } |
| 42 | + |
| 43 | + /** |
| 44 | + * @method updatePartially Updates a subset of fields in the API key record. |
| 45 | + * @param db The DatabaseHandler instance. |
| 46 | + * @param id The API key ID to update. |
| 47 | + * @param newData The fields to update. |
| 48 | + */ |
| 49 | + public static async updatePartially(db: DatabaseHandler, id: string, newData: Partial<IApiKey>): Promise<void> { |
| 50 | + await db.update<IApiKey>(ApiKeyRepository.tableName, newData, { id }); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * @method deleteById |
| 55 | + * Deletes an API key record by its ID. |
| 56 | + * |
| 57 | + * @param db The DatabaseHandler instance. |
| 58 | + * @param id The ID of the record to delete. |
| 59 | + */ |
| 60 | + public static async deleteById(db: DatabaseHandler, id: string): Promise<void> { |
| 61 | + const sql = ` |
| 62 | + DELETE FROM "${ApiKeyRepository.tableName}" |
| 63 | + WHERE "id" = $1 |
| 64 | + `; |
| 65 | + await db.query(sql, [id]); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @method countAll |
| 70 | + * Returns the total number of API keys in the API key table. |
| 71 | + * |
| 72 | + * @param db The DatabaseHandler instance. |
| 73 | + */ |
| 74 | + public static async countAll(db: DatabaseHandler): Promise<number> { |
| 75 | + const sql = ` |
| 76 | + SELECT COUNT(*) AS "total" |
| 77 | + FROM "${ApiKeyRepository.tableName}" |
| 78 | + `; |
| 79 | + const result = await db.query(sql); |
| 80 | + // Convert the result's string to a number |
| 81 | + return parseInt(result.rows[0].total, 10); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @method fetchAll Retrieves all API key records. |
| 86 | + * @param db The DatabaseHandler instance. |
| 87 | + * @returns Array of IApiKey objects. |
| 88 | + */ |
| 89 | + public static async fetchAll(db: DatabaseHandler): Promise<IApiKey[]> { |
| 90 | + return db.select<IApiKey>(ApiKeyRepository.tableName); |
| 91 | + } |
| 92 | +} |
0 commit comments