From 5bef451fd0fb8b31fa6e341aaf637f11e5c4eda6 Mon Sep 17 00:00:00 2001 From: Khalim Conn-Kowlessar Date: Thu, 10 Aug 2023 11:36:56 +0100 Subject: [PATCH] added schemas for materials, recommendations and recommendationMaterials --- src/app/db/schema/materials.ts | 98 ++++++++++++++-------------- src/app/db/schema/recommendations.ts | 37 +++++++++++ 2 files changed, 85 insertions(+), 50 deletions(-) diff --git a/src/app/db/schema/materials.ts b/src/app/db/schema/materials.ts index 99dcccd..e8f8490 100644 --- a/src/app/db/schema/materials.ts +++ b/src/app/db/schema/materials.ts @@ -1,56 +1,54 @@ -// TODO: Materials database should live in a different database +import { + bigserial, + text, + timestamp, + pgTable, + real, + pgEnum, + json, +} from "drizzle-orm/pg-core"; -// import { -// bigserial, -// text, -// timestamp, -// pgTable, -// real, -// pgEnum, -// json, -// } from "drizzle-orm/pg-core"; +export const MaterialType: [string, ...string[]] = [ + "suspended_floor_insulation", + "solid_floor_insulation", + "external_wall_insulation", + "internal_wall_insulation", +]; +export const materialTypeEnum = pgEnum("type", MaterialType); -// export const MaterialType: [string, ...string[]] = [ -// "suspended_floor_insulation", -// "solid_floor_insulation", -// "external_wall_insulation", -// "internal_wall_insulation", -// ]; -// export const materialTypeEnum = pgEnum("type", MaterialType); +export const DepthUnit: [string, ...string[]] = ["mm"]; +export const depthUnitEnum = pgEnum("depth_unit", DepthUnit); -// export const DepthUnit: [string, ...string[]] = ["mm"]; -// export const depthUnitEnum = pgEnum("depth_unit", DepthUnit); +export const CostUnit: [string, ...string[]] = ["gbp_sq_meter"]; +export const costUnitEnum = pgEnum("cost_unit", CostUnit); -// export const CostUnit: [string, ...string[]] = ["gbp_sq_meter"]; -// export const costUnitEnum = pgEnum("cost_unit", CostUnit); +export const RValueUnit: [string, ...string[]] = [ + "square_meter_kelvin_per_watt", +]; +export const rValueUnitEnum = pgEnum("r_value_unit", RValueUnit); -// export const RValueUnit: [string, ...string[]] = [ -// "square_meter_kelvin_per_watt", -// ]; -// export const rValueUnitEnum = pgEnum("r_value_unit", RValueUnit); +export const ThermalConductivityUnit: [string, ...string[]] = [ + "watt_per_meter_kelvin", +]; +export const thermalConductivityUnitEnum = pgEnum( + "thermal_conductivity_unit", + ThermalConductivityUnit +); -// export const ThermalConductivityUnit: [string, ...string[]] = [ -// "watt_per_meter_kelvin", -// ]; -// export const thermalConductivityUnitEnum = pgEnum( -// "thermal_conductivity_unit", -// ThermalConductivityUnit -// ); - -// export const materials = pgTable("property", { -// id: bigserial("id", { mode: "bigint" }).primaryKey(), -// type: materialTypeEnum("type").notNull(), -// description: text("description").notNull(), -// depths: json("json").$type(), -// depth_unit: depthUnitEnum("depth_unit"), -// cost: real("cost"), -// costUnit: costUnitEnum("cost_unit"), -// rValuePerMm: real("r_value_per_mm"), -// rValueUnit: rValueUnitEnum("r_value_unit"), -// thermalConductivity: real("thermal_conductivity"), -// thermalConductivityUnit: thermalConductivityUnitEnum( -// "thermal_conductivity_unit" -// ), -// link: text("link"), -// createdAt: timestamp("created_at").notNull().defaultNow(), -// }); +export const material = pgTable("material", { + id: bigserial("id", { mode: "bigint" }).primaryKey(), + type: materialTypeEnum("type").notNull(), + description: text("description").notNull(), + depths: json("json").$type(), + depth_unit: depthUnitEnum("depth_unit"), + cost: real("cost"), + costUnit: costUnitEnum("cost_unit"), + rValuePerMm: real("r_value_per_mm"), + rValueUnit: rValueUnitEnum("r_value_unit"), + thermalConductivity: real("thermal_conductivity"), + thermalConductivityUnit: thermalConductivityUnitEnum( + "thermal_conductivity_unit" + ), + link: text("link"), + createdAt: timestamp("created_at").notNull().defaultNow(), +}); diff --git a/src/app/db/schema/recommendations.ts b/src/app/db/schema/recommendations.ts index 0fbb1fe..264e00b 100644 --- a/src/app/db/schema/recommendations.ts +++ b/src/app/db/schema/recommendations.ts @@ -1,3 +1,14 @@ +import { + bigserial, + text, + timestamp, + pgTable, + real, + boolean, + bigint, +} from "drizzle-orm/pg-core"; +import { material } from "./materials"; + export interface ComponentRecommendation { id: number; type: string; @@ -13,3 +24,29 @@ export interface Recommendation { Ventilation?: ComponentRecommendation[]; Floor?: ComponentRecommendation[]; } + +export const recommendation = pgTable("recommendation", { + id: bigserial("id", { mode: "bigint" }).primaryKey(), + type: text("type").notNull(), + description: text("description").notNull(), + estimatedCost: real("estimated_cost"), + default: boolean("default").notNull(), + startingUValue: real("new_u_value"), + newUValue: real("new_u_value"), + sapPoints: real("sap_points"), + heatDemand: real("heat_demand"), + createdAt: timestamp("created_at").notNull().defaultNow(), +}); + +export const recommendationMaterials = pgTable("recommendation_materials", { + id: bigserial("id", { mode: "bigint" }).primaryKey(), + recommendationId: bigint("recommendation_id", { + mode: "bigint", + }) + .notNull() + .references(() => recommendation.id), + materialId: bigint("material_id", { mode: "bigint" }) + .notNull() + .references(() => material.id), + createdAt: timestamp("created_at").notNull().defaultNow(), +});