fix(api): 修复 Drizzle 查询 where 子句的语法错误,采用 inArray 重构资产过滤条件

This commit is contained in:
kennethcheng 2026-05-01 15:11:00 +08:00
parent b63691e1c8
commit 371ac24c0e
2 changed files with 8 additions and 2 deletions

View File

@ -79,6 +79,12 @@
- 重构 `<SyncButton />` 并将其提升至 Dashboard 首页,实现总资产大盘的全局一键实盘刷新。 - 重构 `<SyncButton />` 并将其提升至 Dashboard 首页,实现总资产大盘的全局一键实盘刷新。
## 修复记录 ## 修复记录
## 修复 Drizzle ORM 的逻辑或语法错误,将错误的链式 .or() 改写为更具扩展性的 inArray() 语法
- 修复 `app/api/cron/fetch-prices/route.ts``.where(eq(assets.type, 'STOCK').or(eq(assets.type, 'CRYPTO')))` 的非法链式调用语法。
- 废弃错误的 `.where(eq(...).or(eq(...)))` 模式,改为使用 `inArray(assets.type, ['STOCK', 'CRYPTO'])`
- `inArray` 已从 `drizzle-orm` 引入,同时保留了 `eq` 的导入以兼容其他查询。
- `inArray` 写法在语义等價且更具扩展性,未来添加新资产类型(如 FUND, BOND只需在数组中追加枚举值即可。
- 解决了日期选择控件的时区偏移 Bug确保全球通用`src/libs/utils.ts` 中重写 `formatDateForDatetimeLocal()``parseDateTimeLocalToUTC_v2()` 函数,采用 `Intl.DateTimeFormat` 动态获取 `Asia/Shanghai` 时区偏移量,确保 UTC 时间到本地时间的双向转换精确无误,修复了用户选 10 点展示为 2 点的问题。修正了前端数据格式化逻辑,在 `src/lib/formatters.ts` 中增加空值/NaN 兜底处理,在 `src/app/dashboard/page.tsx` 中将平均成本与摊薄成本的显示条件从 `.gt(0)` 改为 `.ne(0)`,支持英特尔负成本等极端场景下的精确数字展示。 - 解决了日期选择控件的时区偏移 Bug确保全球通用`src/libs/utils.ts` 中重写 `formatDateForDatetimeLocal()``parseDateTimeLocalToUTC_v2()` 函数,采用 `Intl.DateTimeFormat` 动态获取 `Asia/Shanghai` 时区偏移量,确保 UTC 时间到本地时间的双向转换精确无误,修复了用户选 10 点展示为 2 点的问题。修正了前端数据格式化逻辑,在 `src/lib/formatters.ts` 中增加空值/NaN 兜底处理,在 `src/app/dashboard/page.tsx` 中将平均成本与摊薄成本的显示条件从 `.gt(0)` 改为 `.ne(0)`,支持英特尔负成本等极端场景下的精确数字展示。
## 资产分布图表按市场维度升级 (Task 32) ## 资产分布图表按市场维度升级 (Task 32)

View File

@ -1,7 +1,7 @@
import { NextResponse } from 'next/server'; import { NextResponse } from 'next/server';
import { db } from '@/db'; import { db } from '@/db';
import { assets, assetPricesHistory } from '@/db/schema'; import { assets, assetPricesHistory } from '@/db/schema';
import { eq } from 'drizzle-orm'; import { eq, inArray } from 'drizzle-orm';
import { ProxyAgent, setGlobalDispatcher } from 'undici'; import { ProxyAgent, setGlobalDispatcher } from 'undici';
export const dynamic = 'force-dynamic'; export const dynamic = 'force-dynamic';
@ -92,7 +92,7 @@ export async function GET(req: Request) {
const allAssets = await db const allAssets = await db
.select() .select()
.from(assets) .from(assets)
.where(eq(assets.type, 'STOCK').or(eq(assets.type, 'CRYPTO'))); .where(inArray(assets.type, ['STOCK', 'CRYPTO']));
if (allAssets.length === 0) { if (allAssets.length === 0) {
return NextResponse.json({ return NextResponse.json({