fix: use simpler types & normalize within db implementations

pull/249/head
Sylvie 6 months ago
parent fa38f14a0c
commit c228a9f6c3
No known key found for this signature in database
GPG Key ID: 75AB0FE5B983A3AF

@ -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<AssFile | AssUser | false> => new Promise(async (resolve, reject) => {
export const get = (sector: DataSector, key: NID): Promise<DatabaseValue> => 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<DatabaseValue[]> => 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);

@ -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]) => {

@ -41,7 +41,7 @@ export class DBManager {
/**
* put a value in the database
*/
public static put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise<void> {
public static put(table: DatabaseTable, key: NID, data: DatabaseValue): Promise<void> {
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<DatabaseValue | undefined> {
public static get(table: DatabaseTable, key: NID): Promise<DatabaseValue> {
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<DatabaseValue[]> {
if (this._db && this._dbReady) {
return this._db.getAll(table);
} else throw new Error("No database active");
} else throw new Error("No database active");
}
}

@ -135,17 +135,17 @@ export class JSONDatabase implements Database {
})
}
public get(table: DatabaseTable, key: string): Promise<DatabaseValue | undefined> {
public get(table: DatabaseTable, key: string): Promise<DatabaseValue> {
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<DatabaseValue[]> {
return new Promise(async (resolve, reject) => {
const data = (await fs.readJson(PATHMAP[table]))[SECTORMAP[table]];
// todo: fix this
(!data) ? resolve(data) : resolve(data);
});
}

@ -144,7 +144,7 @@ VALUES ('${key}', '${JSON.stringify(data)}');
});
}
public get(table: DatabaseTable, key: NID): Promise<DatabaseValue | undefined> {
public get(table: DatabaseTable, key: NID): Promise<DatabaseValue> {
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<DatabaseValue[]> {
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);
}

@ -161,7 +161,7 @@ export class PostgreSQLDatabase implements Database {
});
}
public get(table: DatabaseTable, key: string): Promise<DatabaseValue | undefined> {
public get(table: DatabaseTable, key: string): Promise<DatabaseValue> {
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<DatabaseValue[]> {
return new Promise(async (resolve, reject) => {
try {
const queries = {

4
common/types.d.ts vendored

@ -82,12 +82,12 @@ declare module 'ass' {
/**
* get a value from the database
*/
get(table: DatabaseTable, key: NID): Promise<DatabaseValue | undefined>;
get(table: DatabaseTable, key: NID): Promise<DatabaseValue>;
/**
* get all values from the database
*/
getAll(table: DatabaseTable): Promise<{ [key: string]: DatabaseValue | undefined; }[]>;
getAll(table: DatabaseTable): Promise<DatabaseValue[]>;
}
interface DatabaseConfiguration {

Loading…
Cancel
Save