mirror of
https://github.com/jakejarvis/jarv.is.git
synced 2025-04-27 18:30:30 -04:00
move long and messy Fauna query to a user-defined function in the cloud
https://docs.fauna.com/fauna/current/api/fql/user_defined_functions
This commit is contained in:
parent
1d76ff93ad
commit
c113bdce2e
@ -10,7 +10,7 @@ require("dotenv").config();
|
|||||||
// https://github.com/netlify/netlify-lambda/issues/201
|
// https://github.com/netlify/netlify-lambda/issues/201
|
||||||
require("encoding");
|
require("encoding");
|
||||||
|
|
||||||
exports.handler = async (event, context) => {
|
exports.handler = async (event) => {
|
||||||
// some rudimentary error handling
|
// some rudimentary error handling
|
||||||
const { slug } = event.queryStringParameters;
|
const { slug } = event.queryStringParameters;
|
||||||
if (!slug || slug === "/") {
|
if (!slug || slug === "/") {
|
||||||
@ -27,33 +27,8 @@ exports.handler = async (event, context) => {
|
|||||||
secret: process.env.FAUNADB_SERVER_SECRET,
|
secret: process.env.FAUNADB_SERVER_SECRET,
|
||||||
});
|
});
|
||||||
|
|
||||||
const result = await client.query(
|
// refer to snippet below for the `hit` function defined in the Fauna cloud
|
||||||
q.Let(
|
const result = await client.query(q.Call(q.Function("hit"), slug));
|
||||||
{
|
|
||||||
match: q.Match(q.Index("hits_by_slug"), slug),
|
|
||||||
},
|
|
||||||
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: slug,
|
|
||||||
hits: 1,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
)
|
|
||||||
)
|
|
||||||
);
|
|
||||||
|
|
||||||
// send client the new hit count
|
// send client the new hit count
|
||||||
return {
|
return {
|
||||||
@ -81,3 +56,35 @@ exports.handler = async (event, context) => {
|
|||||||
};
|
};
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
/**
|
||||||
|
|
||||||
|
This is the FaunaDB function named `hit` defined in the cloud:
|
||||||
|
https://dashboard.fauna.com/functions/hit/@db/global/jarv.is
|
||||||
|
https://docs.fauna.com/fauna/current/api/fql/user_defined_functions
|
||||||
|
|
||||||
|
{
|
||||||
|
name: "hit",
|
||||||
|
role: null,
|
||||||
|
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 } })
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
|
*/
|
||||||
|
Loading…
x
Reference in New Issue
Block a user