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 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(
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue