feat: delete old candles on CSV upload before inserting new ones
This commit is contained in:
parent
974d9f5598
commit
011bea2350
2 changed files with 2600 additions and 18315 deletions
20891
EURUSD.csv
20891
EURUSD.csv
File diff suppressed because it is too large
Load diff
|
|
@ -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(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue