Restructuring calculator data in database and improve api surface
This commit is contained in:
@@ -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) {
|
||||
|
||||
Reference in New Issue
Block a user