I have an API route that generates data in my DataBase, this API route is only called in my CRON job set in Vercel.
However, every time I build the project, new data show in the database. I do believe it is because of the Nature of NextJS to pre-execute an endpoint so it will have on cache, but how do I prevent adding data in my Database?
The code is simple: https://github.com/guifeliper/dart-throwing-monkey-crypto/blob/main/apps/monkey-crypto/app/api/generateTokens/route.ts
export const revalidate = 60 * 60 * 24 * 6; // every 6 days
export async function GET(request: Request) {
const url =
`${process.env.COINMARKETCAP_URL}/v1/cryptocurrency/listings/latest?limit=100` ??
"";
var options = {
headers: {
"X-CMC_PRO_API_KEY": process.env.COINMARKETCAP_API ?? "",
},
next: { revalidate: 60 * 60 * 24 * 6 },
};
const res = await fetch(url, options);
const data = await res.json();
const tokensList: Tokens[] =
data?.data?.map((token: any) => {
return {
name: token.name,
symbol: token.symbol,
priceAtDrawn: token.quote["USD"].price,
priceAtContest: 0,
category: "Top-100",
timeframe: getYearWeekString(),
};
}) ?? [];
const BTCBenchmark = tokensList
.filter((token) => token.symbol === "BTC")
.map((token) => ({ ...token, category: "BTC-benchmark" }));
if (tokensList?.length == 0) {
return NextResponse.json(
{ message: "Error on token list" },
{ status: 500 }
);
}
const selectedTokens = selectTokens(tokensList, 10);
return await addTokensDrawn([...selectedTokens, ...BTCBenchmark]);
}
async function addTokensDrawn(data: any) {
try {
await prisma.tokenDrawn.createMany({ data });
return NextResponse.json({ message: "Add Successfully" }, { status: 200 });
} catch (error) {
console.error("Request error", error);
return NextResponse.json({ message: "Error on add" }, { status: 500 });
}
}