31 lines
1.0 KiB
TypeScript
31 lines
1.0 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import fs from 'fs';
|
|
import path from 'path';
|
|
import serverConfig from '@lifetracker/shared/config';
|
|
import { getConnectionDetails } from '@lifetracker/db/drizzle';
|
|
import { pgDump } from 'pg-dump-restore';
|
|
|
|
|
|
const dbPath = path.join(serverConfig.dataDir, `lifetracker-${new Date().getTime()}.sql`);
|
|
|
|
const returnVal = (await pgDump(getConnectionDetails(), {
|
|
filePath: dbPath,
|
|
}));
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
// Read the production database file
|
|
const dbFile = fs.readFileSync(dbPath);
|
|
return new NextResponse(dbFile, {
|
|
headers: {
|
|
'Content-Type': 'application/octet-stream',
|
|
'Content-Disposition': `attachment; filename="lifetracker-${new Date().getTime()}.sql"`
|
|
|
|
}
|
|
});
|
|
} catch (error) {
|
|
console.error('Error downloading the database:', error);
|
|
return NextResponse.json({ message: 'Error processing download.' }, { status: 500 });
|
|
}
|
|
}
|