Skip to content

Commit 97cdfa5

Browse files
authored
Sl/feat/db optimization (#423)
1 parent 75662f4 commit 97cdfa5

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

frontend/app/[locale]/layout.tsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ export default async function LocaleLayout({
3737
title: string;
3838
}> = [];
3939

40+
4041
const enableAdmin =
4142
(
4243
process.env.ENABLE_ADMIN_API ??

frontend/db/index.ts

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,29 @@ dotenv.config();
1212
type AppDatabase = PgDatabase<PgQueryResultHKT, typeof schema>;
1313

1414
const APP_ENV = process.env.APP_ENV?.trim().toLowerCase();
15+
16+
if (!APP_ENV) {
17+
throw new Error(
18+
'[db] APP_ENV is required. Set APP_ENV=local in .env for development, or APP_ENV=develop/production for deployment'
19+
);
20+
}
21+
1522
const IS_LOCAL_ENV = APP_ENV === 'local';
23+
24+
if (process.env.NODE_ENV !== 'test') {
25+
console.log('[db] runtime env check', {
26+
has_DATABASE_URL: Boolean(process.env.DATABASE_URL?.trim()),
27+
has_DATABASE_URL_LOCAL: Boolean(process.env.DATABASE_URL_LOCAL?.trim()),
28+
});
29+
}
30+
1631
const STRICT_LOCAL_DB_GUARD = process.env.SHOP_STRICT_LOCAL_DB === '1';
1732
const REQUIRED_LOCAL_DB_URL = process.env.SHOP_REQUIRED_DATABASE_URL_LOCAL;
1833

1934
if (STRICT_LOCAL_DB_GUARD) {
2035
if (!IS_LOCAL_ENV) {
2136
throw new Error(
22-
`[db] SHOP_STRICT_LOCAL_DB=1 requires APP_ENV=local (got "${APP_ENV ?? 'undefined'}")`
37+
`[db] SHOP_STRICT_LOCAL_DB=1 requires APP_ENV=local (got "${APP_ENV}")`
2338
);
2439
}
2540

@@ -68,7 +83,7 @@ if (IS_LOCAL_ENV) {
6883

6984
if (!url) {
7085
throw new Error(
71-
`[db] APP_ENV=${APP_ENV ?? 'undefined'} requires DATABASE_URL to be set`
86+
`[db] APP_ENV=${APP_ENV} requires DATABASE_URL to be set`
7287
);
7388
}
7489

frontend/db/queries/categories/admin-categories.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@ export async function getAdminCategoryList(): Promise<AdminCategoryItem[]> {
2020
title: categoryTranslations.title,
2121
})
2222
.from(categories)
23-
.innerJoin(
23+
.leftJoin(
2424
categoryTranslations,
2525
sql`${categoryTranslations.categoryId} = ${categories.id} AND ${categoryTranslations.locale} = ${ADMIN_LOCALE}`
2626
)
2727
.orderBy(categories.displayOrder);
2828

29-
return rows;
29+
return rows.map(row => ({
30+
id: row.id,
31+
slug: row.slug,
32+
title: row.title ?? row.slug,
33+
}));
3034
}
3135

3236
export async function getMaxQuizDisplayOrder(

0 commit comments

Comments
 (0)