Restructuring calculator data in database and improve api surface

This commit is contained in:
Dominic Ferrando
2026-07-08 12:11:25 -04:00
parent 91a14252cf
commit cec0d17312
8 changed files with 199 additions and 87 deletions
+14 -9
View File
@@ -39,7 +39,6 @@ export async function up(db: Kysely<any>): Promise<void> {
.$call(addDefaultColumns)
.addColumn("name", "text", (cb) => cb.notNull().defaultTo(""))
.addColumn("params", "jsonb", (cb) => cb.notNull())
.addColumn("is_selected", "boolean", (cb) => cb.notNull().defaultTo(false))
.addColumn("dataset_id", "bigint", (cb) => cb.notNull())
.addForeignKeyConstraint(
"fk_standards_configs_dataset_id",
@@ -50,12 +49,18 @@ export async function up(db: Kysely<any>): Promise<void> {
)
.execute();
// INDEX: ONE_SELECTED_STANDARDS_CONFIG
await db.schema.createIndex("idx_one_selected_standards_config")
.on("standards_configs")
.column("is_selected")
.unique()
.where("is_selected", "=", true)
// TABLE: CALCULATORS
await db.schema.createTable("calculators")
.$call(addDefaultColumns)
.addColumn("name", "text", (cb) => cb.notNull().unique())
.addColumn("standards_config_id", "bigint", (cb) => cb.notNull())
.addForeignKeyConstraint(
"fk_calculators_standards_config_id",
["standards_config_id"],
"standards_configs",
["id"],
(cb) => cb.onDelete("restrict")
)
.execute();
}
@@ -66,8 +71,8 @@ export async function down(db: Kysely<any>): Promise<void> {
// INDEX: ONE_ACTIVE_PRODUCT_SYNC
await db.schema.dropIndex("idx_one_active_product_sync").ifExists().execute();
// INDEX: ONE_SELECTED_STANDARDS_CONFIG
await db.schema.dropIndex("idx_one_selected_standards_config").ifExists().execute();
// TABLE: CALCULATORS
await db.schema.dropTable("calculators").ifExists().execute()
// TABLE: STANDARDS_CONFIGS
await db.schema.dropTable("standards_configs").ifExists().execute()
+27 -13
View File
@@ -3,11 +3,9 @@ import {
} from "@blade-and-brawn/calculator";
import { Value } from "@sinclair/typebox/value";
import { db } from "./db";
import { env, log } from "../util";
import { DEFAULT_NAME, env, log } from "../util";
import standardsConfig from "./seed-data/standards-config.json" with {type: "json"};
const DEFAULT_NAME = "Default";
Value.Assert(StandardsConfigSchema, standardsConfig);
async function seedStandardsDataset(): Promise<string> {
@@ -28,31 +26,46 @@ async function seedStandardsDataset(): Promise<string> {
return result.id;
}
async function seedStandardsConfig(datasetId: string): Promise<void> {
async function seedStandardsConfig(datasetId: string): Promise<string> {
const existing = await db.selectFrom("standards_configs")
.select(["id"])
.where("name", "=", DEFAULT_NAME)
.executeTakeFirst();
if (existing) {
log.info({ id: existing.id }, "default standards config already seeded, skipping");
return;
return existing.id;
}
const hasSelected = await db.selectFrom("standards_configs")
.select(["id"])
.where("is_selected", "=", true)
.executeTakeFirst();
const result = await db.insertInto("standards_configs")
.values({
name: DEFAULT_NAME,
dataset_id: datasetId,
params: standardsConfig.params,
is_selected: !hasSelected,
})
.returning("id")
.executeTakeFirstOrThrow();
log.info({ id: result.id, selected: !hasSelected }, "seeded default standards config");
log.info({ id: result.id }, "seeded default standards config");
return result.id;
}
async function seedCalculator(standardsConfigId: string): Promise<void> {
const existing = await db.selectFrom("calculators")
.select(["id"])
.where("name", "=", DEFAULT_NAME)
.executeTakeFirst();
if (existing) {
log.info({ id: existing.id }, "default calculator already seeded, skipping");
return;
}
const result = await db.insertInto("calculators")
.values({
name: DEFAULT_NAME,
standards_config_id: standardsConfigId
})
.returning("id")
.executeTakeFirstOrThrow();
log.info({ id: result.id }, "seeded default calculator");
}
(async () => {
@@ -65,7 +78,8 @@ async function seedStandardsConfig(datasetId: string): Promise<void> {
try {
const datasetId = await seedStandardsDataset();
await seedStandardsConfig(datasetId);
const standardsConfigId = await seedStandardsConfig(datasetId);
await seedCalculator(standardsConfigId);
log.info("seed finished");
}
catch (err) {