Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 13 additions & 7 deletions frontend/db/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,30 @@ dotenv.config();

type AppDatabase = PgDatabase<PgQueryResultHKT, typeof schema>;

const APP_ENV = process.env.APP_ENV ?? 'local';
const APP_ENV = process.env.APP_ENV?.trim().toLowerCase();
const IS_LOCAL_ENV = APP_ENV === 'local';
const STRICT_LOCAL_DB_GUARD = process.env.SHOP_STRICT_LOCAL_DB === '1';
const REQUIRED_LOCAL_DB_URL = process.env.SHOP_REQUIRED_DATABASE_URL_LOCAL;

if (STRICT_LOCAL_DB_GUARD) {
if (APP_ENV !== 'local') {
if (!IS_LOCAL_ENV) {
throw new Error(
`[db] SHOP_STRICT_LOCAL_DB=1 requires APP_ENV=local (got "${APP_ENV}")`
`[db] SHOP_STRICT_LOCAL_DB=1 requires APP_ENV=local (got "${APP_ENV ?? 'undefined'}")`
);
}

if (!process.env.DATABASE_URL_LOCAL?.trim()) {
throw new Error(
'[db] SHOP_STRICT_LOCAL_DB=1 requires DATABASE_URL_LOCAL to be set'
);
}

if (process.env.DATABASE_URL?.trim()) {
throw new Error(
'[db] SHOP_STRICT_LOCAL_DB=1 forbids DATABASE_URL during shop-local tests'
);
}

if (
REQUIRED_LOCAL_DB_URL &&
process.env.DATABASE_URL_LOCAL !== REQUIRED_LOCAL_DB_URL
Expand All @@ -43,8 +47,8 @@ if (STRICT_LOCAL_DB_GUARD) {

let db: AppDatabase;

if (APP_ENV === 'local') {
const url = process.env.DATABASE_URL_LOCAL;
if (IS_LOCAL_ENV) {
const url = process.env.DATABASE_URL_LOCAL?.trim();

if (!url) {
throw new Error('[db] APP_ENV=local requires DATABASE_URL_LOCAL to be set');
Expand All @@ -60,10 +64,12 @@ if (APP_ENV === 'local') {
console.log('[db] using local PostgreSQL (pg)');
}
} else {
const url = process.env.DATABASE_URL;
const url = process.env.DATABASE_URL?.trim();

if (!url) {
throw new Error(`[db] APP_ENV=${APP_ENV} requires DATABASE_URL to be set`);
throw new Error(
`[db] APP_ENV=${APP_ENV ?? 'undefined'} requires DATABASE_URL to be set`
);
}

const sql = neon(url);
Expand Down
Loading