Zod validated config for security and ease of getting started

This commit is contained in:
Kalle
2026-06-15 14:12:15 +03:00
parent 215243e071
commit 75179e5077
46 changed files with 386 additions and 275 deletions

View File

@@ -8,23 +8,16 @@ import {
SqliteDialect,
} from "kysely";
import { format } from "sql-formatter";
import invariant from "~/utils/invariant";
import { Config } from "~/config";
import { ServerConfig } from "~/config.server";
import { logger } from "~/utils/logger";
import { roundToNDecimalPlaces } from "~/utils/number";
import type { DB } from "./tables";
const LOG_LEVEL = (["trunc", "full", "none"] as const).find(
(val) => val === process.env.SQL_LOG,
);
const SENTRY_ENABLED = import.meta.env.VITE_SENTRY_ENABLED === "true";
const migratedEmptyDb = new Database("db-test.sqlite3").serialize();
invariant(process.env.DB_PATH, "DB_PATH env variable must be set");
export const sql = new Database(
process.env.NODE_ENV === "test" ? migratedEmptyDb : process.env.DB_PATH,
ServerConfig.isTest ? migratedEmptyDb : ServerConfig.dbPath,
);
sql.pragma("journal_mode = WAL");
@@ -53,7 +46,7 @@ export const db = new Kysely<DB>({
});
function log(event: LogEvent) {
if (SENTRY_ENABLED && event.level === "query") {
if (Config.sentry.enabled && event.level === "query") {
// Backdated span so the query nests under the active loader/action span
// in Sentry's waterfall. `onlyIfParent: true` skips emission when there's
// no active trace (e.g. cron routines), avoiding orphan root spans.
@@ -65,7 +58,7 @@ function log(event: LogEvent) {
}).end();
}
if (LOG_LEVEL === "trunc" || LOG_LEVEL === "full") {
if (ServerConfig.sqlLog === "trunc" || ServerConfig.sqlLog === "full") {
logQuery(event);
} else {
logError(event);
@@ -129,7 +122,7 @@ function formatSql(sql: string, params: readonly unknown[]) {
const lines = formatted.split("\n");
if (LOG_LEVEL === "full" || lines.length <= 11) {
if (ServerConfig.sqlLog === "full" || lines.length <= 11) {
return addParams(formatted, params);
}