diff --git a/backend/routers/api.ts b/backend/routers/api.ts index 973c777..5f0f1c8 100644 --- a/backend/routers/api.ts +++ b/backend/routers/api.ts @@ -66,10 +66,11 @@ router.post('/setup', BodyParserJson(), async (req, res) => { router.post('/login', rateLimiterMiddleware('login', UserConfig.config?.rateLimit?.login), BodyParserJson(), (req, res) => { const { username, password } = req.body; + // something tells me we shouldnt be using getall here data.getAll('users') .then((users) => { if (!users) throw new Error('Missing users data'); - else return Object.entries(users as 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])) diff --git a/backend/sql/mongodb.ts b/backend/sql/mongodb.ts index 64b4450..b0d8eb4 100644 --- a/backend/sql/mongodb.ts +++ b/backend/sql/mongodb.ts @@ -209,6 +209,10 @@ export class MongoDBDatabase implements Database { id: key }).exec(); + if (result.length == 0) { + throw new Error(`Key '${key}' not found in '${table}'`); + } + resolve(result.length ? result[0].data : void 0); } catch (err) { reject(err); @@ -216,6 +220,7 @@ export class MongoDBDatabase implements Database { }); } + // TODO: Unsure if this works. getAll(table: DatabaseTable): Promise { return new Promise(async (resolve, reject) => { try { @@ -228,7 +233,7 @@ export class MongoDBDatabase implements Database { // more ts-ignore! // @ts-ignore let result = await models[table].find({}).exec() // @ts-ignore - .then(res => res.reduce((obj, doc) => (obj[doc.id] = doc.data, obj), {})); + .then(res => res.reduce((obj, doc) => (obj.push(doc.data)), [])); resolve(result); } catch (err) { diff --git a/backend/sql/postgres.ts b/backend/sql/postgres.ts index 6d4d5e0..0ea1d54 100644 --- a/backend/sql/postgres.ts +++ b/backend/sql/postgres.ts @@ -171,7 +171,11 @@ export class PostgreSQLDatabase implements Database { let result = await this._client.query(queries[table], [key]); - resolve(result.rowCount ? result.rows[0].data : void 0); + if (result.rowCount == 0) { + throw new Error(`Key '${key}' not found in '${table}'`); + } + + resolve(result.rows[0].data); } catch (err) { reject(err); } @@ -183,14 +187,14 @@ export class PostgreSQLDatabase implements Database { return new Promise(async (resolve, reject) => { try { const queries = { - assfiles: 'SELECT json_object_agg(id, data) AS stuff FROM assfiles;', - assusers: 'SELECT json_object_agg(id, data) AS stuff FROM assusers;', - asstokens: 'SELECT json_object_agg(id, data) AS stuff FROM asstokens;' + assfiles: 'SELECT json_agg(data) as stuff FROM assfiles;', + assusers: 'SELECT json_agg(data) as stuff FROM assusers;', + asstokens: 'SELECT json_agg(data) as stuff FROM asstokens;' }; let result = await this._client.query(queries[table]); - resolve(result.rowCount ? result.rows[0].stuff : void 0); + resolve(result.rowCount ? result.rows[0].stuff : []); } catch (err) { reject(err); }