feat: implementing server framework

This commit is contained in:
Daniel Ledda
2022-06-29 08:24:00 +02:00
parent c628f6b46e
commit bd177c7f09
17 changed files with 210 additions and 55 deletions

View File

@@ -1,16 +1,40 @@
import { Client } from "https://deno.land/x/mysql/mod.ts";
import dbconfig from "./config.json" assert { type: "json" };
import config from "@/config.json" assert { type: "json" };
export type WithoutId<T> = Omit<T, "id">;
export interface StoccaTreDbConn {
query<T>(query: string): Promise<T>;
query<T>(query: string): Promise<Maybe<T>>;
}
export default async function createNewDbConnection(): Promise<StoccaTreDbConn> {
return await new Client().connect({
hostname: dbconfig.hostname,
username: dbconfig.username,
const mysqlClient = await new Client().connect({
hostname: config.hostname,
username: config.username,
db: "stocca_tre",
password: dbconfig.password,
password: config.password,
});
return {
query: async <T>(query: string): Promise<Maybe<T>> => {
let errMessage: string;
try {
const result = await mysqlClient.query(query);
return {
just: result as T,
error: null,
};
} catch (e: unknown) {
if (e && typeof (e as { message?: any }).message === "string") {
errMessage = (e as { message: string}).message;
} else {
errMessage = "An unknown error occurred in the database.";
}
}
return {
just: null,
error: {
message: errMessage
},
};
}
}
}