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,2 +1,2 @@
export type Maybe<T> = Just<T> | { just?: never, error: { message: string }};
export type Just<T> = { just: T, error?: never };
export type Maybe<T> = Just<T> | { just?: never; error: { message: string } };
export type Just<T> = { just: T; error?: never };

View File

@@ -1,7 +1,22 @@
export type HttpMethod = "POST" | "GET" | "PUT" | "DELETE";
export default interface StoccaTreRequest {
method: HttpMethod,
route: string,
body: string | null,
export type RouteDefinition = {
pattern: RegExp;
method: HttpMethod;
};
export default class StoccaTreRequest {
constructor(
public method: HttpMethod,
public route: string,
public body: string | null,
) {}
match(route: RouteDefinition): RegExpExecArray | null {
const patternResult = route.pattern.exec(this.route);
if (route.method !== this.method) {
return null;
}
return patternResult;
}
}

View File

@@ -1,7 +1,7 @@
import StoccaTreRequest from "./StoccaTreRequest.ts";
import {Maybe} from "./Maybe.ts";
import {JSONObject} from "./JSON.ts";
import { Maybe } from "./Maybe.ts";
import { JSONObject } from "./JSON.ts";
export default interface StoccaTreRequestHandler {
handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>>,
}
handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>>;
}

View File

@@ -1,6 +0,0 @@
{
"username": "root",
"port": 8080,
"hostname": "127.0.0.1",
"password": "sekna123jk"
}

View File

@@ -1,6 +1,17 @@
type ServerConfig = {
username: string,
hostname: string,
password: string,
port: number,
}
const config = {
username: Deno.env.get("DB_USER") ?? "postgres",
hostname: Deno.env.get("DB_HOST") ?? "localhost",
password: Deno.env.get("DB_PW") ?? "",
dbPort: Number(Deno.env.get("DB_PORT") ?? 5432),
port: Number(Deno.env.get("PORT") ?? 8080),
};
console.log(`ENV:
db username: ${config.username}
db pass: ******
db port: ${config.dbPort}
server port: ${config.port}
server hostname: ${config.hostname}
`);
export default config;

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 };
}
}
}
}

View File

@@ -1,4 +1,7 @@
{
"imports": {
}
"imports": {
"zod": "https://deno.land/x/zod@v3.17.3/mod.ts",
"postgres": "https://deno.land/x/postgres@v0.16.1/mod.ts",
"bcrypt": "https://deno.land/x/bcrypt@v0.4.0/mod.ts"
}
}

View File

@@ -1,14 +1,14 @@
import createNewDbConnection, {StoccaTreDbConn} from "./database.ts";
import createNewDbConnection, { StoccaTreDbConn } from "./database.ts";
import * as resources from "./resources/main.ts";
import config from "./config.json" assert { type: "json" };
import StoccaTreRequest, {HttpMethod} from "./StoccaTreRequest.ts";
import config from "./config.ts";
import StoccaTreRequest, { HttpMethod } from "./StoccaTreRequest.ts";
import StoccaTreRequestHandler from "./StoccaTreRequestHandler.ts";
import {JSONObject} from "./JSON.ts";
import { JSONObject } from "./JSON.ts";
type StoccaTreApiBody = {
data: JSONObject,
error?: string,
}
data: JSONObject;
error?: string;
};
function newStoccaTreApiBody(): StoccaTreApiBody {
return {
@@ -17,22 +17,22 @@ function newStoccaTreApiBody(): StoccaTreApiBody {
}
type StoccaTreServer = StoccaTreRequestHandler & {
dbConnection: StoccaTreDbConn,
}
dbConnection: StoccaTreDbConn;
};
async function processRequest(server: StoccaTreServer, requestEvent: Deno.RequestEvent) {
const route = /^https?:\/\/[^\/]*(\/.*)$/.exec(requestEvent.request.url)?.[1] ?? "/";
const requestBody = (await requestEvent.request.body?.getReader().read())?.value?.toString() ?? null;
console.log(requestBody);
const requestBody = (await (await requestEvent.request.blob()).text()) ?? null;
const method: HttpMethod = requestEvent.request.method as HttpMethod;
const body: StoccaTreApiBody = newStoccaTreApiBody();
const result = await server.handleRequest({
route,
body: requestBody,
method,
});
const result = await server.handleRequest(new StoccaTreRequest(method, route, requestBody));
if (result.error) {
await requestEvent.respondWith(Response.json({ ...body, error: `Internal server error: ${result.error.message}` }, { status: 500 }));
await requestEvent.respondWith(
Response.json({
...body,
error: `Internal server error: ${result.error.message}`,
}, { status: 500 }),
);
} else {
body.data = result.just;
await requestEvent.respondWith(Response.json(body, { status: 200 }));
@@ -41,10 +41,11 @@ async function processRequest(server: StoccaTreServer, requestEvent: Deno.Reques
const stoccaTreListener = Deno.listen({ port: config.port ?? 8080 });
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${ config.port }/`);
console.log(`Stocca Tre Server is running. Access it at: http://localhost:${config.port}/`);
const database = await createNewDbConnection();
const database = createNewDbConnection();
const ingredientResource = new resources.IngredientResource(database);
const userResource = new resources.UserResource(database);
const stoccaTreServer: StoccaTreServer = {
dbConnection: database,
handleRequest: (request: StoccaTreRequest) => ingredientResource.handleRequest(request),

View File

@@ -1,10 +0,0 @@
{
"name": "stocca-tre-server",
"version": "0.0.1",
"scripts": {
"start": "deno run --allow-net --import-map=import_map.json main.ts"
},
"description": "Backend for StoccaTre",
"author": "Daniel Ledda",
"license": "MIT"
}

View File

@@ -1,31 +1,42 @@
import {StoccaTreDbConn, WithoutId} from "../../database.ts";
import {IngredientModel} from "./IngredientModel.ts";
import {Maybe} from "../../Maybe.ts";
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
import { IngredientModel } from "./IngredientModel.ts";
import { Maybe } from "../../Maybe.ts";
export default class IngredientCollection {
private dbConnection: StoccaTreDbConn;
private db: StoccaTreDbConn;
private mapById: Map<number, IngredientModel> = new Map<number, IngredientModel>();
private allGotten = false;
constructor(database: StoccaTreDbConn) {
this.dbConnection = database;
this.db = database;
}
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<any> {
return await this.dbConnection.query<any>(
`INSERT INTO ingredients (id, name, displayName, displayNameDE) VALUES (NULL, '${ingredient.name}', '${ingredient.displayName}', '${ingredient.displayNameDE}');`
async addIngredient(ingredient: WithoutId<IngredientModel>): Promise<Maybe<IngredientModel[]>> {
return await this.db.query(sql =>
sql<IngredientModel[]>`INSERT INTO ingredients ${ sql(ingredient) }`
);
}
async getById(id: number): Promise<Maybe<IngredientModel>> {
const found = this.mapById.get(id);
if (found) {
return { just: found };
}
const result = await this.db.query(sql => sql<IngredientModel[]>`SELECT * FROM ingredients WHERE id is ${ id }`);
if (result.error) {
return result;
}
const ingredient = result.just[0];
this.mapById.set(ingredient.id, ingredient);
return { just: ingredient };
}
async getAllIngredients(): Promise<Maybe<IterableIterator<IngredientModel>>> {
if (!this.allGotten) {
const result = await this.dbConnection.query<IngredientModel[]>("SELECT * FROM ingredients");
if (!result.error) {
result.just.forEach((ingredient: IngredientModel) => this.mapById.set(ingredient.id, ingredient));
} else {
return result;
}
this.allGotten = true;
const result: Maybe<IngredientModel[]> = await this.db.query(sql =>
sql<IngredientModel[]>`SELECT * FROM ingredients`
);
if (!result.error) {
result.just.forEach(ingredient => this.mapById.set(ingredient.id, ingredient));
} else {
return result;
}
return {
just: this.mapById.values(),

View File

@@ -1,6 +1,12 @@
export type IngredientModel = {
id: number,
name: string,
displayName: string,
displayNameDE: string,
};
import { z } from "zod";
export const IngredientSchema = z.object({
id: z.number(),
name: z.string().nonempty(),
displayName: z.string().nonempty(),
displayNameDE: z.string().nonempty(),
});
export const IngredientSchemaWithoutId = IngredientSchema.omit({ id: true });
export type IngredientModel = z.infer<typeof IngredientSchema>;

View File

@@ -1,45 +1,48 @@
import {StoccaTreDbConn} from "../../database.ts";
import IngredientCollection from "./IngredientCollection.ts";
import StoccaTreRequest from "../../StoccaTreRequest.ts";
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
import {Maybe} from "../../Maybe.ts";
import {JSONObject} from "../../JSON.ts";
import {IngredientModel} from "./IngredientModel.ts";
import {IngredientSchemaWithoutId} from "./IngredientModel.ts";
export default class IngredientResource {
private dbConnection: StoccaTreDbConn;
private collection: IngredientCollection;
private routes: Readonly<Record<string, RouteDefinition>> = {
Add: {
pattern: /\/add/,
method: "POST"
},
GetAll: {
pattern: /\/all/,
method: "GET"
},
} as const;
constructor(dbConnection: StoccaTreDbConn) {
this.dbConnection = dbConnection;
this.collection = new IngredientCollection(dbConnection);
}
static isIngredient(json: JSONObject): json is IngredientModel {
if (json) {
return true;
}
return false;
}
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
switch (request.route) {
case "/add":
if (request.method === "POST") {
const ingredient = JSON.parse(request.body ?? "");
return await this.collection.addIngredient(JSON.parse(request.body));
}
break;
case "/all":
return await this.allIngredients(request);
default:
break;
if (request.match(this.routes.Add)) {
return await this.addIngredient(request);
}
return { error: {message: "Invalid route" }};
if (request.match(this.routes.GetAll)) {
return await this.allIngredients(request);
}
return { error: { message: "Invalid route" }};
}
async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
private async addIngredient(request: StoccaTreRequest): Promise<Maybe<{ insertedId: number }>> {
const ingredient = IngredientSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
if (!ingredient.success) {
return { error: new Error("Ingredient was malformed.") };
}
return await this.collection.addIngredient(ingredient.data);
}
private async allIngredients(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
const getAllIngredientResult = await this.collection.getAllIngredients();
if (getAllIngredientResult.error) {
return getAllIngredientResult;

View File

@@ -1 +1,2 @@
export * from "./user/main.ts";
export * from "./ingredient/main.ts";

View File

@@ -0,0 +1,50 @@
import * as bcrypt from "bcrypt";
import { StoccaTreDbConn, WithoutId } from "../../database.ts";
import { UserModel } from "./UserModel.ts";
import { Maybe } from "../../Maybe.ts";
export default class UserCollection {
private db: StoccaTreDbConn;
private mapById: Map<number, UserModel> = new Map<number, UserModel>();
constructor(database: StoccaTreDbConn) {
this.db = database;
}
async addUser(user: WithoutId<UserModel>): Promise<Maybe<{ insertedId: number }>> {
let hash: string;
try {
hash = await bcrypt.hash(user.password);
} catch (e: unknown) {
const error = e as { message?: string };
if (typeof error.message === "string") {
return { error: { message: error.message }};
}
return { error: new Error("Failed to create user") };
}
user.password = hash;
const result: Maybe<UserModel[]> = await this.db.query(sql => sql`
INSERT INTO users ${ sql(user, "displayName") }
RETURNING *
`);
if (result.error) {
return result;
}
return {
just: { insertedId: result.just[0]?.id ?? NaN }
}
}
async getAllUsers(): Promise<Maybe<UserModel[]>> {
const result = await this.db.query((sql) => sql<UserModel[]>`SELECT * FROM users`);
if (!result.error) {
result.just.forEach(user => this.mapById.set(user.id, user));
} else {
return result;
}
return {
just: Array.from(this.mapById.values()),
};
}
}

View File

@@ -0,0 +1,12 @@
import { z } from "zod";
export const UserSchema = z.object({
id: z.number(),
displayName: z.string(),
email: z.string().email(),
password: z.string().min(256).max(256),
});
export const UserSchemaWithoutId = UserSchema.omit({ id: true });
export type UserModel = z.infer<typeof UserSchema>;

View File

@@ -0,0 +1,56 @@
import {StoccaTreDbConn} from "../../database.ts";
import StoccaTreRequest, {RouteDefinition} from "../../StoccaTreRequest.ts";
import {Maybe} from "../../Maybe.ts";
import {JSONObject} from "../../JSON.ts";
import UserCollection from "./UserCollection.ts";
import {UserSchemaWithoutId} from "./UserModel.ts";
export default class UserResource {
private dbConnection: StoccaTreDbConn;
private collection: UserCollection;
private routes: Readonly<Record<string, RouteDefinition>> = {
Add: {
pattern: /\/add/,
method: "POST"
},
GetAll: {
pattern: /\/all/,
method: "GET"
},
} as const;
constructor(dbConnection: StoccaTreDbConn) {
this.dbConnection = dbConnection;
this.collection = new UserCollection(dbConnection);
}
async handleRequest(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
if (request.match(this.routes.Add)) {
return await this.addUser(request);
}
if (request.match(this.routes.GetAll)) {
return await this.allUsers(request);
}
return { error: { message: "Invalid route" }};
}
private async addUser(request: StoccaTreRequest): Promise<Maybe<{ insertedId: number }>> {
const user = UserSchemaWithoutId.safeParse(JSON.parse(request.body ?? "{}"));
if (!user.success) {
return { error: new Error("Ingredient was malformed.") };
}
return await this.collection.addUser(user.data);
}
private async allUsers(request: StoccaTreRequest): Promise<Maybe<JSONObject>> {
const allUsers = await this.collection.getAllUsers();
if (allUsers.error) {
return allUsers;
}
return {
just: {
ingredients: Array.from(allUsers.just),
},
};
}
}

View File

@@ -0,0 +1,3 @@
export * from "./UserModel.ts";
export * from "./UserCollection.ts";
export * from "./UserResource.ts";

1
server/start.sh Normal file
View File

@@ -0,0 +1 @@
DB_USER=postgres && DB_PASS=postgres && deno run --allow-env --allow-net --import-map=import_map.json main.ts