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