feat: implementing server framework
This commit is contained in:
@@ -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
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user