1
mirror of https://github.com/jakejarvis/jarv.is.git synced 2025-07-19 14:25:31 -04:00

switch db from fauna to prisma & planetscale (#971)

This commit is contained in:
2022-06-23 17:26:13 -04:00
committed by GitHub
parent e782f1a8e8
commit d7eba0b39c
11 changed files with 103 additions and 247 deletions

View File

@@ -1,6 +1,5 @@
import faunadb from "faunadb";
import pRetry from "p-retry";
import { getAllNotes } from "../../lib/helpers/parse-notes";
import { prisma } from "../../lib/helpers/prisma";
import { logServerError } from "../../lib/helpers/sentry";
import type { NextApiRequest, NextApiResponse } from "next";
@@ -26,21 +25,10 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(405).end();
}
const client = new faunadb.Client({
secret: process.env.FAUNADB_SERVER_SECRET || "",
checkNewVersion: false, // https://github.com/fauna/faunadb-js/pull/504
});
const { slug } = req.query;
if (slug) {
// increment this page's hits. retry 3 times in case of Fauna "contended transaction" error:
// https://sentry.io/share/issue/9c60a58211954ed7a8dfbe289bd107b5/
const { hits } = await pRetry(() => incrementPageHits(slug, client), {
onFailedAttempt: (error) => {
console.warn(`Attempt ${error.attemptNumber} failed, trying again...`);
},
retries: 3,
});
const hits = await incrementPageHits(slug as string);
// disable caching on both ends
res.setHeader("Cache-Control", "private, no-cache, no-store, must-revalidate");
@@ -50,7 +38,7 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
return res.status(200).json({ hits });
} else {
// return overall site stats if slug not specified
const siteStats = await getSiteStats(client);
const siteStats = await getSiteStats();
// let Vercel edge cache results for 15 mins
res.setHeader("Cache-Control", "public, max-age=0, s-maxage=900, stale-while-revalidate");
@@ -66,54 +54,38 @@ const handler = async (req: NextApiRequest, res: NextApiResponse) => {
await logServerError(error);
// 500 Internal Server Error
return res.status(500).json({ success: false, message });
return res.status(500).json({ message });
}
};
const incrementPageHits = async (slug: string | string[], client: faunadb.Client): Promise<PageStats> => {
const q = faunadb.query;
const result: { data: PageStats } = await client.query(
q.Let(
{
match: q.Match(q.Index("hits_by_slug"), slug),
const incrementPageHits = async (slug: string): Promise<number> => {
const pageHits = await prisma.hits.upsert({
where: {
slug,
},
create: {
slug,
},
update: {
hits: {
increment: 1,
},
q.If(
q.Exists(q.Var("match")),
q.Let(
{
ref: q.Select("ref", q.Get(q.Var("match"))),
hits: q.ToInteger(q.Select("hits", q.Select("data", q.Get(q.Var("match"))))),
},
q.Update(q.Var("ref"), {
data: {
hits: q.Add(q.Var("hits"), 1),
},
})
),
q.Create(q.Collection("hits"), {
data: {
slug,
hits: 1,
},
})
)
)
);
},
});
// send client the *new* hit count
return result.data;
return pageHits.hits;
};
const getSiteStats = async (client: faunadb.Client): Promise<SiteStats> => {
const getSiteStats = async (): Promise<SiteStats> => {
const notes = await getAllNotes();
const q = faunadb.query;
const { data: pages }: { data: SiteStats["pages"] } = await client.query(
q.Map(
q.Paginate(q.Documents(q.Collection("hits")), { size: 99 }),
q.Lambda((x) => q.Select("data", q.Get(x)))
)
);
const pages: SiteStats["pages"] = await prisma.hits.findMany({
orderBy: [
{
hits: "desc",
},
],
});
const siteStats: SiteStats = {
total: { hits: 0 },
@@ -135,9 +107,6 @@ const getSiteStats = async (client: faunadb.Client): Promise<SiteStats> => {
return page;
});
// sort by hits (descending)
siteStats.pages.sort((a, b) => (a.hits > b.hits ? -1 : 1));
return siteStats;
};