From c228a9f6c3211eb058aa88865e649a71ba692ca2 Mon Sep 17 00:00:00 2001 From: Sylvie Date: Mon, 4 Dec 2023 08:54:21 -0700 Subject: [PATCH] fix: use simpler types & normalize within db implementations --- backend/data.ts | 13 ++++++------- backend/routers/api.ts | 4 ++-- backend/sql/database.ts | 10 +++++----- backend/sql/json.ts | 6 +++--- backend/sql/mysql.ts | 13 +++++-------- backend/sql/postgres.ts | 4 ++-- common/types.d.ts | 4 ++-- 7 files changed, 25 insertions(+), 29 deletions(-) diff --git a/backend/data.ts b/backend/data.ts index fb08ec0..617197c 100644 --- a/backend/data.ts +++ b/backend/data.ts @@ -1,4 +1,4 @@ -import { AssFile, AssUser, NID } from 'ass'; +import { AssFile, AssUser, DatabaseValue, NID } from 'ass'; import { log } from './log.js'; import { UserConfig } from './UserConfig.js'; @@ -35,19 +35,18 @@ export const put = (sector: DataSector, key: NID, data: AssFile | AssUser): Prom } }); -export const get = (sector: DataSector, key: NID): Promise => new Promise(async (resolve, reject) => { +export const get = (sector: DataSector, key: NID): Promise => new Promise(async (resolve, reject) => { try { - const data: AssFile | AssUser | undefined = await DBManager.get(sector === 'files' ? 'assfiles' : 'assusers', key) as AssFile | AssUser | undefined - (!data) ? resolve(false) : resolve(data); + const data = await DBManager.get(sector === 'files' ? 'assfiles' : 'assusers', key); + resolve(data); } catch (err) { reject(err); } }); -export const getAll = (sector: DataSector): Promise<{ [key: string]: AssFile | AssUser }> => new Promise(async (resolve, reject) => { +export const getAll = (sector: DataSector): Promise => new Promise(async (resolve, reject) => { try { - // todo: fix MySQL - const data: { [key: string]: AssFile | AssUser } = await DBManager.getAll(sector === 'files' ? 'assfiles' : 'assusers') as /* AssFile[] | AssUser[] | */ {} + const data = await DBManager.getAll(sector === 'files' ? 'assfiles' : 'assusers'); resolve(data); } catch (err) { reject(err); diff --git a/backend/routers/api.ts b/backend/routers/api.ts index 5b07334..08bd9d7 100644 --- a/backend/routers/api.ts +++ b/backend/routers/api.ts @@ -65,8 +65,8 @@ router.post('/login', rateLimiterMiddleware('login', UserConfig.config?.rateLimi data.getAll('users') .then((users) => { if (!users) throw new Error('Missing users data'); - else return Object.entries(users as { [key: string]: AssUser }) - .filter(([_uid, user]: [string, AssUser]) => user.username === username)[0][1]; // [0] is the first item in the filter results, [1] is is AssUser + else return Object.entries(users as AssUser[]) + .filter(([_uid, user]: [string, AssUser]) => user.username === username)[0][1]; // [0] is the first item in the filter results, [1] is AssUser }) .then((user) => Promise.all([bcrypt.compare(password, user.password), user])) .then(([success, user]) => { diff --git a/backend/sql/database.ts b/backend/sql/database.ts index 39b8593..68f1bb8 100644 --- a/backend/sql/database.ts +++ b/backend/sql/database.ts @@ -41,7 +41,7 @@ export class DBManager { /** * put a value in the database */ - public static put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise { + public static put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise { if (this._db && this._dbReady) { return this._db.put(table, key, data); } else throw new Error("No database active"); @@ -50,18 +50,18 @@ export class DBManager { /** * get a value from the database */ - public static get(table: DatabaseTable, key: NID): Promise { + public static get(table: DatabaseTable, key: NID): Promise { if (this._db && this._dbReady) { return this._db.get(table, key); - } else throw new Error("No database active"); + } else throw new Error("No database active"); } /** * get all values from the database */ - public static getAll(table: DatabaseTable): Promise<{ [key: string]: DatabaseValue | undefined }[]> { + public static getAll(table: DatabaseTable): Promise { if (this._db && this._dbReady) { return this._db.getAll(table); - } else throw new Error("No database active"); + } else throw new Error("No database active"); } } \ No newline at end of file diff --git a/backend/sql/json.ts b/backend/sql/json.ts index 789772f..c09febb 100644 --- a/backend/sql/json.ts +++ b/backend/sql/json.ts @@ -135,17 +135,17 @@ export class JSONDatabase implements Database { }) } - public get(table: DatabaseTable, key: string): Promise { + public get(table: DatabaseTable, key: string): Promise { return new Promise(async (resolve, reject) => { const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]][key]; (!data) ? resolve(undefined) : resolve(data); }); } - // todo: fix this - public getAll(table: DatabaseTable): Promise<{ Data: DatabaseValue | undefined }[]> { + public getAll(table: DatabaseTable): Promise { return new Promise(async (resolve, reject) => { const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]]; + // todo: fix this (!data) ? resolve(data) : resolve(data); }); } diff --git a/backend/sql/mysql.ts b/backend/sql/mysql.ts index 4a9dd81..d8e4186 100644 --- a/backend/sql/mysql.ts +++ b/backend/sql/mysql.ts @@ -144,7 +144,7 @@ VALUES ('${key}', '${JSON.stringify(data)}'); }); } - public get(table: DatabaseTable, key: NID): Promise { + public get(table: DatabaseTable, key: NID): Promise { return new Promise(async (resolve, reject) => { try { // Run query @@ -160,19 +160,16 @@ VALUES ('${key}', '${JSON.stringify(data)}'); }); } - // todo: unknown if this works - public getAll(table: DatabaseTable): Promise<{ Data: DatabaseValue | undefined }[]> { + public getAll(table: DatabaseTable): Promise { return new Promise(async (resolve, reject) => { try { - // Run query // ! this may not work as expected + // Run query const [rowz, _fields] = await this._pool.query(`SELECT Data FROM ${table}`); // Interpret results this is pain - const rows = (rowz as unknown as { Data: UploadToken | AssFile | AssUser | undefined }[]); - + const rows = (rowz as unknown as { Data: UploadToken | AssFile | AssUser }[]); - // aaaaaaaaaaaa - resolve(rows); + resolve(rows.map((row) => row.Data)); } catch (err) { reject(err); } diff --git a/backend/sql/postgres.ts b/backend/sql/postgres.ts index 033f7d0..1dfe875 100644 --- a/backend/sql/postgres.ts +++ b/backend/sql/postgres.ts @@ -161,7 +161,7 @@ export class PostgreSQLDatabase implements Database { }); } - public get(table: DatabaseTable, key: string): Promise { + public get(table: DatabaseTable, key: string): Promise { return new Promise(async (resolve, reject) => { try { const queries = { @@ -180,7 +180,7 @@ export class PostgreSQLDatabase implements Database { } // todo: verify this works - public getAll(table: DatabaseTable): Promise<{ Data: DatabaseValue | undefined }[]> { + public getAll(table: DatabaseTable): Promise { return new Promise(async (resolve, reject) => { try { const queries = { diff --git a/common/types.d.ts b/common/types.d.ts index fe32941..299b1ad 100644 --- a/common/types.d.ts +++ b/common/types.d.ts @@ -82,12 +82,12 @@ declare module 'ass' { /** * get a value from the database */ - get(table: DatabaseTable, key: NID): Promise; + get(table: DatabaseTable, key: NID): Promise; /** * get all values from the database */ - getAll(table: DatabaseTable): Promise<{ [key: string]: DatabaseValue | undefined; }[]>; + getAll(table: DatabaseTable): Promise; } interface DatabaseConfiguration {