Hi, I have an API route that I made using Planetscale's database-js library. Here's the code:
import { Client } from "@planetscale/database";
export const runtime = 'edge' // 'nodejs' (default) | 'edge'
const db = new Client({
url: process.env["DATABASE_URL"],
});
export async function GET() {
const startTime = new Date().getTime(); // Start timestamp
const conn = db.connection();
const subjectList = await conn.execute("SELECT name from subjects"); // Specify the type as Subject[]
const rows = subjectList.rows; // Get the "rows" property
const json = JSON.stringify(rows);
const endTime = new Date().getTime(); // End timestamp
const durationInMs = endTime - startTime; // Calculate the duration in milliseconds
console.log(`Execution time with edge: ${durationInMs}ms`); // Print the duration to the console
return new Response(json, {
headers: {
"content-type": "application/json;charset=UTF-8",
"access-control-allow-origin": "*",
},
});
}
I've timed this API call to try and see if this is any faster than using Prisma, however I noticed that I've been getting spikes in the response times and I'm assuming they're cold starts. (I've attached a screenshot).
I'm pretty sure I'm doing something wrong here or misunderstood because from what I know edge functions are suppose to get rid of cold starts. If anyone can help me figure out where I went wrong or if I'm completely misunderstanding edge functions I'd appreciate it.