41 lines
1.3 KiB
TypeScript
41 lines
1.3 KiB
TypeScript
import { Client } from "https://deno.land/x/mysql/mod.ts";
|
|
import config from "@/config.json" assert { type: "json" };
|
|
|
|
export type WithoutId<T> = Omit<T, "id">;
|
|
export interface StoccaTreDbConn {
|
|
query<T>(query: string): Promise<Maybe<T>>;
|
|
}
|
|
export default async function createNewDbConnection(): Promise<StoccaTreDbConn> {
|
|
const mysqlClient = await new Client().connect({
|
|
hostname: config.hostname,
|
|
username: config.username,
|
|
db: "stocca_tre",
|
|
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
|
|
},
|
|
};
|
|
}
|
|
}
|
|
}
|
|
|