feat: delete old candles on CSV upload before inserting new ones

This commit is contained in:
Marko Djordjevic 2026-02-12 18:47:11 +01:00
parent 974d9f5598
commit 011bea2350
2 changed files with 2600 additions and 18315 deletions

20891
EURUSD.csv

File diff suppressed because it is too large Load diff

View file

@ -2,6 +2,7 @@ import { NextRequest, NextResponse } from 'next/server';
import Papa from 'papaparse'; import Papa from 'papaparse';
import { db } from '@/lib/db'; import { db } from '@/lib/db';
import { candles } from '@/lib/db/schema'; import { candles } from '@/lib/db/schema';
import { sql } from 'drizzle-orm';
export async function POST(request: NextRequest): Promise<NextResponse> { export async function POST(request: NextRequest): Promise<NextResponse> {
try { try {
@ -82,22 +83,13 @@ export async function POST(request: NextRequest): Promise<NextResponse> {
}; };
}); });
// Insert with upsert behavior (replace on conflict) // Delete all old candles before inserting new ones
let count = 0; await db.delete(candles);
for (const candle of candleData) {
await db // Insert new candles
.insert(candles) const count = candleData.length;
.values(candle) if (count > 0) {
.onConflictDoUpdate({ await db.insert(candles).values(candleData);
target: candles.time,
set: {
open: candle.open,
high: candle.high,
low: candle.low,
close: candle.close,
},
});
count++;
} }
resolve( resolve(