feat: new user resource integrating postgresql

This commit is contained in:
Daniel Ledda
2022-07-08 09:31:34 +02:00
parent 3e5c53f9f5
commit ba68f953f0
18 changed files with 281 additions and 121 deletions

View File

@@ -1,38 +1,41 @@
import { Client } from "https://deno.land/x/mysql/mod.ts";
import config from "./config.json" assert { type: "json" };
import { Client } from "postgres";
import config from "./config.ts";
import {Maybe} from "./Maybe.ts";
import {JSONObject} from "./JSON.ts";
export type WithoutId<T> = Omit<T, "id">;
export interface StoccaTreDbConn {
query<T>(query: string): Promise<Maybe<T>>;
query<T extends JSONObject | JSONObject[]>(query: string): Promise<Maybe<T>>,
}
export default async function createNewDbConnection(): Promise<StoccaTreDbConn> {
const mysqlClient = await new Client().connect({
export default function createNewDbConnection(): StoccaTreDbConn {
const postgresClient = new Client({
hostname: config.hostname,
username: config.username,
db: "stocca_tre",
password: config.password,
user: config.username,
database: "stocca_tre",
port: config.port,
});
await postgresClient.connect();
return {
query: async <T>(query: string): Promise<Maybe<T>> => {
let errMessage: string;
async queryMany<T extends JSONObject[]>(query: string): Promise<Maybe<{ rows: T, count: number }>> {
try {
const result = await mysqlClient.query(query);
const result = await postgresClient.queryArray<T>(query);
return {
just: result as T,
just: {
rows: result.rows,
count: result.rowCount ?? NaN,
},
};
} 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 {
error: {
message: errMessage
},
};
},
queryOne<T extends JSONObject>(query: string): Promise<Maybe<T>> {
try {
const result = await postgresClient.queryObject<T>(query);
return { just: result };
}
}
}
}