mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-10-27 21:25:48 -04:00
switch db from fauna to prisma & planetscale (#971)
This commit is contained in:
@@ -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;
|
||||
};
|
||||
|
||||
|
||||
@@ -2,7 +2,6 @@ import { NextSeo } from "next-seo";
|
||||
import Content from "../components/Content";
|
||||
import PageTitle from "../components/PageTitle";
|
||||
import Link from "../components/Link";
|
||||
import Image from "../components/Image";
|
||||
import IFrame from "../components/IFrame";
|
||||
import Blockquote from "../components/Blockquote";
|
||||
import CodeInline from "../components/CodeInline";
|
||||
@@ -10,8 +9,6 @@ import { H2 } from "../components/Heading";
|
||||
import { UnorderedList, ListItem } from "../components/List";
|
||||
import { fathomSiteId, siteDomain } from "../lib/config";
|
||||
|
||||
import faunaImg from "../public/static/images/privacy/fauna_hits.png";
|
||||
|
||||
const Privacy = () => {
|
||||
return (
|
||||
<>
|
||||
@@ -53,13 +50,14 @@ const Privacy = () => {
|
||||
|
||||
<p>
|
||||
A very simple hit counter on each blog post tallies an aggregate number of pageviews (i.e.{" "}
|
||||
<CodeInline>hits = hits + 1</CodeInline>) in a <Link href="https://fauna.com/">Fauna</Link> database.
|
||||
Individual views and identifying (or non-identifying) details are <strong>never stored or logged</strong>.
|
||||
<CodeInline>hits = hits + 1</CodeInline>) in a <Link href="https://planetscale.com/">PlanetScale</Link> SQL
|
||||
database. Individual views and identifying (or non-identifying) details are{" "}
|
||||
<strong>never stored or logged</strong>.
|
||||
</p>
|
||||
|
||||
<p>
|
||||
The <Link href="https://github.com/jakejarvis/jarv.is/blob/main/pages/api/hits.ts">serverless function</Link>{" "}
|
||||
and{" "}
|
||||
The <Link href="https://github.com/jakejarvis/jarv.is/blob/main/prisma/schema.prisma">database schema</Link>,{" "}
|
||||
<Link href="https://github.com/jakejarvis/jarv.is/blob/main/pages/api/hits.ts">serverless function</Link> and{" "}
|
||||
<Link href="https://github.com/jakejarvis/jarv.is/blob/main/components/HitCounter/HitCounter.tsx">
|
||||
client script
|
||||
</Link>{" "}
|
||||
@@ -67,8 +65,6 @@ const Privacy = () => {
|
||||
are public.
|
||||
</p>
|
||||
|
||||
<Image src={faunaImg} alt="The entire database schema." />
|
||||
|
||||
<p>
|
||||
<Link href="https://usefathom.com/ref/ZEYG0O">
|
||||
<strong>Fathom Analytics</strong>
|
||||
|
||||
Reference in New Issue
Block a user