mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2026-01-14 10:42:56 -05:00
switch db from fauna to prisma & planetscale (#971)
This commit is contained in:
21
lib/helpers/prisma.ts
Normal file
21
lib/helpers/prisma.ts
Normal file
@@ -0,0 +1,21 @@
|
||||
import { PrismaClient } from "@prisma/client";
|
||||
|
||||
// PrismaClient is attached to the `global` object in development to prevent
|
||||
// exhausting your database connection limit.
|
||||
//
|
||||
// Learn more:
|
||||
// https://pris.ly/d/help/next-js-best-practices
|
||||
|
||||
declare global {
|
||||
// allow global `var` declarations
|
||||
// eslint-disable-next-line no-var
|
||||
var prisma: PrismaClient | undefined;
|
||||
}
|
||||
|
||||
export const prisma =
|
||||
global.prisma ||
|
||||
new PrismaClient({
|
||||
log: ["query"],
|
||||
});
|
||||
|
||||
if (process.env.NODE_ENV !== "production") global.prisma = prisma;
|
||||
@@ -1,66 +0,0 @@
|
||||
// Initialize a new pageview database. For use with Fauna Schema Migrate:
|
||||
// https://github.com/fauna-labs/fauna-schema-migrate#readme
|
||||
|
||||
import faunadb from "faunadb";
|
||||
const {
|
||||
CreateCollection,
|
||||
CreateIndex,
|
||||
CreateFunction,
|
||||
Query,
|
||||
Collection,
|
||||
Role,
|
||||
Var,
|
||||
Index,
|
||||
Let,
|
||||
Match,
|
||||
Lambda,
|
||||
Get,
|
||||
Create,
|
||||
Update,
|
||||
Add,
|
||||
Select,
|
||||
If,
|
||||
Exists,
|
||||
ToInteger,
|
||||
} = faunadb.query;
|
||||
|
||||
// initializes the empty database
|
||||
CreateCollection({ name: "hits" });
|
||||
|
||||
// this allows us to quickly pull a post's corresponding row
|
||||
CreateIndex({
|
||||
name: "hits_by_slug",
|
||||
source: Collection("hits"),
|
||||
terms: [
|
||||
{
|
||||
field: ["data", "slug"],
|
||||
},
|
||||
],
|
||||
unique: false,
|
||||
serialized: true,
|
||||
});
|
||||
|
||||
// a wrapper to get a post's row, add one to it, and return the new tally
|
||||
CreateFunction({
|
||||
name: "increment_hit",
|
||||
body: Query(
|
||||
Lambda(
|
||||
"slug",
|
||||
Let(
|
||||
{ match: Match(Index("hits_by_slug"), Var("slug")) },
|
||||
If(
|
||||
Exists(Var("match")),
|
||||
Let(
|
||||
{
|
||||
ref: Select("ref", Get(Var("match"))),
|
||||
hits: ToInteger(Select("hits", Select("data", Get(Var("match"))))),
|
||||
},
|
||||
Update(Var("ref"), { data: { hits: Add(Var("hits"), 1) } })
|
||||
),
|
||||
Create(Collection("hits"), { data: { slug: Var("slug"), hits: 1 } })
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
role: Role("server"),
|
||||
});
|
||||
Reference in New Issue
Block a user