使用 FaunaDB 的 API 服务器
FaunaDB 自称为“现代应用程序的数据 API”。它是一个具有 GraphQL 接口的数据库,使您可以使用 GraphQL 与其交互。由于我们使用 HTTP 请求与其通信,因此我们不需要管理连接,这非常适合无服务器应用程序。
本教程假设您拥有 FaunaDB 和 Deno Deploy 帐户,已安装 Deno Deploy CLI,并且具备一些 GraphQL 的基本知识。
概览 跳转到标题
在本教程中,让我们构建一个小型名言 API,其中包含插入和检索名言的端点。稍后使用 FaunaDB 持久化这些名言。
让我们从定义 API 端点开始。
# A POST request to the endpoint should insert the quote to the list.
POST /quotes/
# Body of the request.
{
"quote": "Don't judge each day by the harvest you reap but by the seeds that you plant.",
"author": "Robert Louis Stevenson"
}
# A GET request to the endpoint should return all the quotes from the database.
GET /quotes/
# Response of the request.
{
"quotes": [
{
"quote": "Don't judge each day by the harvest you reap but by the seeds that you plant.",
"author": "Robert Louis Stevenson"
}
]
}
现在我们了解了端点应该如何工作,让我们继续构建它。
构建 API 端点 跳转到标题
首先,创建一个名为 quotes.ts
的文件,并粘贴以下内容。
通读代码中的注释以了解正在发生的事情。
import {
json,
serve,
validateRequest,
} from "https://deno.land/x/[email protected]/mod.ts";
serve({
"/quotes": handleQuotes,
});
// To get started, let's just use a global array of quotes.
const quotes = [
{
quote: "Those who can imagine anything, can create the impossible.",
author: "Alan Turing",
},
{
quote: "Any sufficiently advanced technology is equivalent to magic.",
author: "Arthur C. Clarke",
},
];
async function handleQuotes(request: Request) {
// Make sure the request is a GET request.
const { error } = await validateRequest(request, {
GET: {},
});
// validateRequest populates the error if the request doesn't meet
// the schema we defined.
if (error) {
return json({ error: error.message }, { status: error.status });
}
// Return all the quotes.
return json({ quotes });
}
使用 Deno CLI 运行上述程序。
deno run --allow-net=:8000 ./path/to/quotes.ts
# Listening on http://0.0.0.0:8000/
并 curl 端点以查看一些名言。
curl http://127.0.0.1:8000/quotes
# {"quotes":[
# {"quote":"Those who can imagine anything, can create the impossible.", "author":"Alan Turing"},
# {"quote":"Any sufficiently advanced technology is equivalent to magic.","author":"Arthur C. Clarke"}
# ]}
让我们继续处理 POST 请求。
更新 validateRequest
函数以确保 POST 请求遵循提供的 body 模式。
- const { error } = await validateRequest(request, {
+ const { error, body } = await validateRequest(request, {
GET: {},
+ POST: {
+ body: ["quote", "author"]
+ }
});
通过使用以下代码更新 handleQuotes
函数来处理 POST 请求。
async function handleQuotes(request: Request) {
const { error, body } = await validateRequest(request, {
GET: {},
POST: {
body: ["quote", "author"],
},
});
if (error) {
return json({ error: error.message }, { status: error.status });
}
+ // Handle POST requests.
+ if (request.method === "POST") {
+ const { quote, author } = body as { quote: string; author: string };
+ quotes.push({ quote, author });
+ return json({ quote, author }, { status: 201 });
+ }
return json({ quotes });
}
让我们通过插入一些数据来测试它。
curl --dump-header - --request POST --data '{"quote": "A program that has not been tested does not work.", "author": "Bjarne Stroustrup"}' http://127.0.0.1:8000/quotes
输出可能如下所示。
HTTP/1.1 201 Created
transfer-encoding: chunked
content-type: application/json; charset=utf-8
{"quote":"A program that has not been tested does not work.","author":"Bjarne Stroustrup"}
太棒了!我们构建了 API 端点,并且它按预期工作。由于数据存储在内存中,因此重启后会丢失。让我们使用 FaunaDB 来持久化我们的名言。
使用 FaunaDB 进行持久化 跳转到标题
让我们使用 GraphQL Schema 定义我们的数据库模式。
# We're creating a new type named `Quote` to represent a quote and its author.
type Quote {
quote: String!
author: String!
}
type Query {
# A new field in the Query operation to retrieve all quotes.
allQuotes: [Quote!]
}
Fauna 为其数据库提供了一个 graphql 端点,它为模式中定义的数据类型生成必要的 mutation,例如 create、update、delete。例如,fauna 将生成一个名为 createQuote
的 mutation,以在数据库中为数据类型 Quote
创建一个新的名言。我们还额外定义了一个名为 allQuotes
的 query 字段,它返回数据库中的所有名言。
让我们开始编写代码,以便从 Deno Deploy 应用程序与 fauna 交互。
为了与 fauna 交互,我们需要向其 graphql 端点发出 POST 请求,并带有适当的 query 和参数以获取返回的数据。因此,让我们构建一个通用函数来处理这些事情。
async function queryFauna(
query: string,
variables: { [key: string]: unknown },
): Promise<{
data?: any;
error?: any;
}> {
// Grab the secret from the environment.
const token = Deno.env.get("FAUNA_SECRET");
if (!token) {
throw new Error("environment variable FAUNA_SECRET not set");
}
try {
// Make a POST request to fauna's graphql endpoint with body being
// the query and its variables.
const res = await fetch("https://graphql.fauna.com/graphql", {
method: "POST",
headers: {
authorization: `Bearer ${token}`,
"content-type": "application/json",
},
body: JSON.stringify({
query,
variables,
}),
});
const { data, errors } = await res.json();
if (errors) {
// Return the first error if there are any.
return { data, error: errors[0] };
}
return { data };
} catch (error) {
return { error };
}
}
将此代码添加到 quotes.ts
文件中。现在让我们继续更新端点以使用 fauna。
async function handleQuotes(request: Request) {
const { error, body } = await validateRequest(request, {
GET: {},
POST: {
body: ["quote", "author"],
},
});
if (error) {
return json({ error: error.message }, { status: error.status });
}
if (request.method === "POST") {
+ const { quote, author, error } = await createQuote(
+ body as { quote: string; author: string }
+ );
+ if (error) {
+ return json({ error: "couldn't create the quote" }, { status: 500 });
+ }
return json({ quote, author }, { status: 201 });
}
return json({ quotes });
}
+async function createQuote({
+ quote,
+ author,
+}: {
+ quote: string;
+ author: string;
+}): Promise<{ quote?: string; author?: string; error?: string }> {
+ const query = `
+ mutation($quote: String!, $author: String!) {
+ createQuote(data: { quote: $quote, author: $author }) {
+ quote
+ author
+ }
+ }
+ `;
+
+ const { data, error } = await queryFauna(query, { quote, author });
+ if (error) {
+ return { error };
+ }
+
+ return data;
+}
现在我们已经更新了代码以插入新的名言,让我们在继续测试代码之前设置一个 fauna 数据库。
创建一个新数据库
- 转到 https://dashboard.fauna.com(如果需要,请登录)并单击新建数据库
- 填写数据库名称字段,然后单击保存。
- 单击左侧边栏上可见的 GraphQL 部分。
- 创建一个以
.gql
结尾的文件,内容为我们上面定义的模式。
生成一个密钥以访问数据库
- 单击 安全 部分,然后单击 新建密钥。
- 选择 服务器 角色,然后单击 保存。复制密钥。
现在让我们使用密钥运行应用程序。
FAUNA_SECRET=<the_secret_you_just_obtained> deno run --allow-net=:8000 --watch quotes.ts
# Listening on http://0.0.0.0:8000
curl --dump-header - --request POST --data '{"quote": "A program that has not been tested does not work.", "author": "Bjarne Stroustrup"}' http://127.0.0.1:8000/quotes
请注意名言是如何添加到 FaunaDB 中的集合中的。
让我们编写一个新函数来获取所有名言。
async function getAllQuotes() {
const query = `
query {
allQuotes {
data {
quote
author
}
}
}
`;
const {
data: {
allQuotes: { data: quotes },
},
error,
} = await queryFauna(query, {});
if (error) {
return { error };
}
return { quotes };
}
并使用以下代码更新 handleQuotes
函数。
-// To get started, let's just use a global array of quotes.
-const quotes = [
- {
- quote: "Those who can imagine anything, can create the impossible.",
- author: "Alan Turing",
- },
- {
- quote: "Any sufficiently advanced technology is equivalent to magic.",
- author: "Arthur C. Clarke",
- },
-];
async function handleQuotes(request: Request) {
const { error, body } = await validateRequest(request, {
GET: {},
POST: {
body: ["quote", "author"],
},
});
if (error) {
return json({ error: error.message }, { status: error.status });
}
if (request.method === "POST") {
const { quote, author, error } = await createQuote(
body as { quote: string; author: string },
);
if (error) {
return json({ error: "couldn't create the quote" }, { status: 500 });
}
return json({ quote, author }, { status: 201 });
}
+ // It's assumed that the request method is "GET".
+ {
+ const { quotes, error } = await getAllQuotes();
+ if (error) {
+ return json({ error: "couldn't fetch the quotes" }, { status: 500 });
+ }
+
+ return json({ quotes });
+ }
}
curl http://127.0.0.1:8000/quotes
您应该看到我们已插入到数据库中的所有名言。API 的最终代码可在 https://deno.org.cn/examples/fauna.ts 获取。
部署 API 跳转到标题
现在我们已经准备好一切,让我们部署您的新 API!
- 在您的浏览器中,访问 Deno Deploy 并链接您的 GitHub 帐户。
- 选择包含您的新 API 的存储库。
- 您可以为您的项目命名,或允许 Deno 为您生成一个名称
- 在入口点下拉列表中选择
index.ts
- 单击部署项目
为了使您的应用程序正常工作,我们将需要配置其环境变量。
在您的项目的成功页面上,或在您的项目仪表板中,单击添加环境变量。在环境变量下,单击 + 添加变量。创建一个名为 FAUNA_SECRET
的新变量 - 值应该是我们之前创建的密钥。
单击以保存变量。
在您的项目概览中,单击查看以在浏览器中查看项目,在 url 的末尾添加 /quotes
以查看 FaunaDB 的内容。