fix(db): 修复快照查询引擎的 ReferenceError,重构为标准 Drizzle ORM 查询语法

This commit is contained in:
2026-04-29 12:55:44 +08:00
parent 8f5ce4bc74
commit 838bb0ef95
2 changed files with 12 additions and 4 deletions
+7 -4
View File
@@ -3,7 +3,7 @@
import { db } from '@/db';
import { portfolioSnapshots } from '@/db/schema';
import { getPortfolioPositions } from './portfolio';
import { eq, sql } from 'drizzle-orm';
import { desc, eq, gte, sql } from 'drizzle-orm';
import Big from 'big.js';
function getTodayInShanghai(): string {
@@ -89,13 +89,16 @@ export async function getSnapshots(params?: {
let query = db
.select()
.from(portfolioSnapshots)
.orderBy(sql`"${date}" DESC`);
.orderBy(desc(portfolioSnapshots.date))
.$dynamic();
if (startDate) {
query = query.where(sql`"${date}" >= ${startDate}`);
query = query.where(gte(portfolioSnapshots.date, startDate));
}
if (endDate) {
query = query.where(sql`"${date}" <= ${endDate}`);
query = query.where(
portfolioSnapshots.date.lte(endDate)
);
}
const snapshots = await query.limit(limit);